From a48bbac8fa89ecd52b802773e1721690d85a0155 Mon Sep 17 00:00:00 2001 From: toimtoimtoim Date: Fri, 25 Nov 2022 13:00:27 +0200 Subject: [PATCH 1/3] Add testcases for some BodyLimit middleware configuration options --- middleware/body_limit_test.go | 90 +++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/middleware/body_limit_test.go b/middleware/body_limit_test.go index 8ffed55a4..5044b38d6 100644 --- a/middleware/body_limit_test.go +++ b/middleware/body_limit_test.go @@ -83,3 +83,93 @@ func TestBodyLimitReader(t *testing.T) { assert.Equal(t, 2, n) assert.Equal(t, nil, err) } + +func TestBodyLimit_skipper(t *testing.T) { + e := echo.New() + h := func(c echo.Context) error { + body, err := io.ReadAll(c.Request().Body) + if err != nil { + return err + } + return c.String(http.StatusOK, string(body)) + } + mw := BodyLimitWithConfig(BodyLimitConfig{ + Skipper: func(c echo.Context) bool { + return true + }, + Limit: "2B", // if not skipped this limit would make request to fail limit check + }) + + hw := []byte("Hello, World!") + req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw)) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + err := mw(h)(c) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, rec.Code) + assert.Equal(t, hw, rec.Body.Bytes()) +} + +func TestBodyLimitWithConfig(t *testing.T) { + var testCases = []struct { + name string + givenLimit string + whenBody []byte + expectBody []byte + expectStatus int + expectError string + }{ + { + name: "ok, body is less than limit", + givenLimit: "10B", + whenBody: []byte("123456789"), + expectBody: []byte("123456789"), + expectError: "", + }, + { + name: "nok, body is more than limit", + givenLimit: "9B", + whenBody: []byte("1234567890"), + expectBody: []byte(nil), + expectError: "code=413, message=Request Entity Too Large", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + e := echo.New() + h := func(c echo.Context) error { + body, err := io.ReadAll(c.Request().Body) + if err != nil { + return err + } + return c.String(http.StatusOK, string(body)) + } + mw := BodyLimitWithConfig(BodyLimitConfig{ + Limit: tc.givenLimit, + }) + + req := httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(tc.whenBody)) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + err := mw(h)(c) + if tc.expectError != "" { + assert.EqualError(t, err, tc.expectError) + } else { + assert.NoError(t, err) + } + // not testing status as middlewares return error instead of committing it and OK cases are anyway 200 + assert.Equal(t, tc.expectBody, rec.Body.Bytes()) + }) + } +} + +func TestBodyLimit_panicOnInvalidLimit(t *testing.T) { + assert.PanicsWithError( + t, + "echo: invalid body-limit=", + func() { BodyLimit("") }, + ) +} From 3dfca996852d5b9196e122fce91a90a595eefb93 Mon Sep 17 00:00:00 2001 From: toimtoimtoim Date: Fri, 25 Nov 2022 13:02:42 +0200 Subject: [PATCH 2/3] Add testcases for some BodyLimit middleware configuration options --- middleware/body_limit_test.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/middleware/body_limit_test.go b/middleware/body_limit_test.go index 5044b38d6..23e4941ef 100644 --- a/middleware/body_limit_test.go +++ b/middleware/body_limit_test.go @@ -113,12 +113,11 @@ func TestBodyLimit_skipper(t *testing.T) { func TestBodyLimitWithConfig(t *testing.T) { var testCases = []struct { - name string - givenLimit string - whenBody []byte - expectBody []byte - expectStatus int - expectError string + name string + givenLimit string + whenBody []byte + expectBody []byte + expectError string }{ { name: "ok, body is less than limit", From f72f61b0621adad1e05e406d3736f50977a5d2e0 Mon Sep 17 00:00:00 2001 From: toimtoimtoim Date: Fri, 25 Nov 2022 13:07:24 +0200 Subject: [PATCH 3/3] Add testcases for some BodyLimit middleware configuration options --- middleware/body_limit_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/body_limit_test.go b/middleware/body_limit_test.go index 23e4941ef..2bfce372a 100644 --- a/middleware/body_limit_test.go +++ b/middleware/body_limit_test.go @@ -84,7 +84,7 @@ func TestBodyLimitReader(t *testing.T) { assert.Equal(t, nil, err) } -func TestBodyLimit_skipper(t *testing.T) { +func TestBodyLimitWithConfig_Skipper(t *testing.T) { e := echo.New() h := func(c echo.Context) error { body, err := io.ReadAll(c.Request().Body)