2017-06-15 08:03:38 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2018-02-21 20:44:17 +02:00
|
|
|
"errors"
|
2022-12-04 22:17:48 +02:00
|
|
|
"io"
|
2017-06-15 08:03:38 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
"github.com/labstack/echo/v5"
|
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-12-04 22:17:48 +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 := ""
|
2021-07-15 22:34:01 +02:00
|
|
|
mw, err := BodyDumpConfig{Handler: func(c echo.Context, reqBody, resBody []byte) {
|
2017-06-15 08:03:38 +02:00
|
|
|
requestBody = string(reqBody)
|
|
|
|
responseBody = string(resBody)
|
2021-07-15 22:34:01 +02:00
|
|
|
}}.ToMiddleware()
|
|
|
|
assert.NoError(t, err)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2021-07-15 22:34:01 +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
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBodyDump_skipper(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
isCalled := false
|
|
|
|
mw, err := BodyDumpConfig{
|
|
|
|
Skipper: func(c echo.Context) bool {
|
|
|
|
return true
|
|
|
|
},
|
2018-02-21 20:44:17 +02:00
|
|
|
Handler: func(c echo.Context, reqBody, resBody []byte) {
|
2021-07-15 22:34:01 +02:00
|
|
|
isCalled = true
|
2018-02-21 20:44:17 +02:00
|
|
|
},
|
2021-07-15 22:34:01 +02:00
|
|
|
}.ToMiddleware()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("{}"))
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
h := func(c echo.Context) error {
|
|
|
|
return errors.New("some error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mw(h)(c)
|
|
|
|
assert.EqualError(t, err, "some error")
|
|
|
|
assert.False(t, isCalled)
|
2018-02-21 20:44:17 +02:00
|
|
|
}
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
func TestBodyDump_fails(t *testing.T) {
|
2018-02-21 20:44:17 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
mw, err := BodyDumpConfig{Handler: func(c echo.Context, reqBody, resBody []byte) {}}.ToMiddleware()
|
|
|
|
assert.NoError(t, err)
|
2018-02-21 20:44:17 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
err = mw(h)(c)
|
|
|
|
assert.EqualError(t, err, "some error")
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2018-02-21 20:44:17 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBodyDumpWithConfig_panic(t *testing.T) {
|
2018-02-21 20:44:17 +02:00
|
|
|
assert.Panics(t, func() {
|
2021-07-15 22:34:01 +02:00
|
|
|
mw := BodyDumpWithConfig(BodyDumpConfig{
|
2018-02-21 20:44:17 +02:00
|
|
|
Skipper: nil,
|
|
|
|
Handler: nil,
|
|
|
|
})
|
2021-07-15 22:34:01 +02:00
|
|
|
assert.NotNil(t, mw)
|
2018-02-21 20:44:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotPanics(t, func() {
|
2021-07-15 22:34:01 +02:00
|
|
|
mw := BodyDumpWithConfig(BodyDumpConfig{Handler: func(c echo.Context, reqBody, resBody []byte) {}})
|
|
|
|
assert.NotNil(t, mw)
|
|
|
|
})
|
|
|
|
}
|
2018-02-21 20:44:17 +02:00
|
|
|
|
2021-07-15 22:34:01 +02:00
|
|
|
func TestBodyDump_panic(t *testing.T) {
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
mw := BodyDump(nil)
|
|
|
|
assert.NotNil(t, mw)
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotPanics(t, func() {
|
|
|
|
BodyDump(func(c echo.Context, reqBody, resBody []byte) {})
|
2018-02-21 20:44:17 +02:00
|
|
|
})
|
2017-06-15 08:03:38 +02:00
|
|
|
}
|