1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-18 16:20:53 +02:00
echo/middleware/body_dump_test.go
Vishal Rana e827c85dc5 Issue 950 (#955)
* 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>
2017-06-14 23:03:38 -07:00

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())
}
}