mirror of
https://github.com/labstack/echo.git
synced 2024-12-26 20:54:00 +02:00
e827c85dc5
* Added implementation #950 Signed-off-by: Vishal Rana <vr@labstack.com> * Added test for body-dump middleware Signed-off-by: Vishal Rana <vr@labstack.com>
41 lines
904 B
Go
41 lines
904 B
Go
package middleware
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestBodyDump(t *testing.T) {
|
|
e := echo.New()
|
|
hw := "Hello, World!"
|
|
req := httptest.NewRequest(echo.POST, "/", strings.NewReader(hw))
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
h := func(c echo.Context) error {
|
|
body, err := ioutil.ReadAll(c.Request().Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return c.String(http.StatusOK, string(body))
|
|
}
|
|
|
|
requestBody := ""
|
|
responseBody := ""
|
|
mw := BodyDump(func(c echo.Context, reqBody, resBody []byte) {
|
|
requestBody = string(reqBody)
|
|
responseBody = string(resBody)
|
|
})
|
|
if assert.NoError(t, mw(h)(c)) {
|
|
assert.Equal(t, requestBody, hw)
|
|
assert.Equal(t, responseBody, hw)
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Equal(t, hw, rec.Body.String())
|
|
}
|
|
}
|