mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-25 08:08:18 +03:00
edfa8c147f
Squashed commit of the following: commit 8d83eebba851e8e09bb08b1c94a247cb049a1b75 Merge:c6574a33c
b6ed76965
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Aug 5 16:59:50 2024 +0300 Merge branch 'master' into AGDNS-2280-upd-golibs commitc6574a33c6
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Jul 31 19:56:58 2024 +0300 all: upd proxy, golibs
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package home
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/AdguardTeam/golibs/ioutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestLimitRequestBody(t *testing.T) {
|
|
errReqLimitReached := &ioutil.LimitError{
|
|
Limit: defaultReqBodySzLim.Bytes(),
|
|
}
|
|
|
|
testCases := []struct {
|
|
wantErr error
|
|
name string
|
|
body string
|
|
want []byte
|
|
}{{
|
|
wantErr: nil,
|
|
name: "not_so_big",
|
|
body: "somestr",
|
|
want: []byte("somestr"),
|
|
}, {
|
|
wantErr: errReqLimitReached,
|
|
name: "so_big",
|
|
body: string(make([]byte, defaultReqBodySzLim+1)),
|
|
want: make([]byte, defaultReqBodySzLim),
|
|
}, {
|
|
wantErr: nil,
|
|
name: "empty",
|
|
body: "",
|
|
want: []byte(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())
|
|
})
|
|
}
|
|
}
|