2017-06-15 08:03:38 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2018-02-21 20:44:17 +02:00
|
|
|
"errors"
|
2022-11-21 14:29:43 +02:00
|
|
|
"io"
|
2017-06-15 08:03:38 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2017-06-15 08:03:38 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBodyDump(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
hw := "Hello, World!"
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(hw))
|
2017-06-15 08:03:38 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
h := func(c echo.Context) error {
|
2022-11-21 14:29:43 +02:00
|
|
|
body, err := io.ReadAll(c.Request().Body)
|
2017-06-15 08:03:38 +02:00
|
|
|
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)
|
|
|
|
})
|
2018-02-21 20:44:17 +02:00
|
|
|
|
2022-10-12 20:47:21 +02:00
|
|
|
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())
|
2017-06-15 08:03:38 +02:00
|
|
|
}
|
2018-02-21 20:44:17 +02:00
|
|
|
|
|
|
|
// Must set default skipper
|
|
|
|
BodyDumpWithConfig(BodyDumpConfig{
|
|
|
|
Skipper: nil,
|
|
|
|
Handler: func(c echo.Context, reqBody, resBody []byte) {
|
|
|
|
requestBody = string(reqBody)
|
|
|
|
responseBody = string(resBody)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBodyDumpFails(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
hw := "Hello, World!"
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(hw))
|
2018-02-21 20:44:17 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
h := func(c echo.Context) error {
|
|
|
|
return errors.New("some error")
|
|
|
|
}
|
|
|
|
|
2018-08-29 05:49:22 +02:00
|
|
|
mw := BodyDump(func(c echo.Context, reqBody, resBody []byte) {})
|
2018-02-21 20:44:17 +02:00
|
|
|
|
|
|
|
if !assert.Error(t, mw(h)(c)) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
mw = BodyDumpWithConfig(BodyDumpConfig{
|
|
|
|
Skipper: nil,
|
|
|
|
Handler: nil,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
mw = BodyDumpWithConfig(BodyDumpConfig{
|
|
|
|
Skipper: func(c echo.Context) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
Handler: func(c echo.Context, reqBody, resBody []byte) {
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if !assert.Error(t, mw(h)(c)) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
})
|
2017-06-15 08:03:38 +02:00
|
|
|
}
|