mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-16 02:18:28 -07:00
ea8950a80d
Squashed commit of the following: commit 5345a14b3565f358c56a37500cafb35b7e397951 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 21 21:13:06 2021 +0300 all: fix windows tests commit 8b9cdbe3e78f43339d21277f04e686bb154f6968 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 21 20:23:55 2021 +0300 all: imp code commit 271fdbe74c29d8ea4b53d7f56d2a36612dfed7b3 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 21 19:43:32 2021 +0300 all: imp testing commit e340f9d48679c57fc8eb579b8b78d4957be111c4 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 21 18:53:51 2021 +0300 all: use testutil
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package home
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLimitRequestBody(t *testing.T) {
|
|
errReqLimitReached := &aghio.LimitReachedError{
|
|
Limit: defaultReqBodySzLim,
|
|
}
|
|
|
|
testCases := []struct {
|
|
name string
|
|
body string
|
|
want []byte
|
|
wantErr error
|
|
}{{
|
|
name: "not_so_big",
|
|
body: "somestr",
|
|
want: []byte("somestr"),
|
|
wantErr: nil,
|
|
}, {
|
|
name: "so_big",
|
|
body: string(make([]byte, defaultReqBodySzLim+1)),
|
|
want: make([]byte, defaultReqBodySzLim),
|
|
wantErr: errReqLimitReached,
|
|
}, {
|
|
name: "empty",
|
|
body: "",
|
|
want: []byte(nil),
|
|
wantErr: nil,
|
|
}}
|
|
|
|
makeHandler := func(t *testing.T, err *error) http.HandlerFunc {
|
|
t.Helper()
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var b []byte
|
|
b, *err = io.ReadAll(r.Body)
|
|
_, werr := w.Write(b)
|
|
require.NoError(t, werr)
|
|
})
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var err error
|
|
handler := makeHandler(t, &err)
|
|
lim := limitRequestBody(handler)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "https://www.example.com", strings.NewReader(tc.body))
|
|
res := httptest.NewRecorder()
|
|
|
|
lim.ServeHTTP(res, req)
|
|
|
|
assert.Equal(t, tc.wantErr, err)
|
|
assert.Equal(t, tc.want, res.Body.Bytes())
|
|
})
|
|
}
|
|
}
|