mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-26 15:05:50 +03:00
7d1ca48ae4
Merge in DNS/adguard-home from 2675-larger-requests to master Updates #2675. Squashed commit of the following: commit 2b45c9bfdc817980204b11de768b425fb72a6488 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Feb 15 13:38:44 2021 +0300 home: imp names commit dad39ae7ee35346ea91f15665acc93ba0b5653df Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Feb 15 13:31:53 2021 +0300 home: imp large req handling
68 lines
1.4 KiB
Go
68 lines
1.4 KiB
Go
package home
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"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(err *error) http.HandlerFunc {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var b []byte
|
|
b, *err = ioutil.ReadAll(r.Body)
|
|
_, werr := w.Write(b)
|
|
if werr != nil {
|
|
panic(werr)
|
|
}
|
|
})
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var err error
|
|
handler := makeHandler(&err)
|
|
lim := limitRequestBody(handler)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "https://www.example.com", strings.NewReader(tc.body))
|
|
res := httptest.NewRecorder()
|
|
|
|
lim.ServeHTTP(res, req)
|
|
|
|
require.Equal(t, tc.wantErr, err)
|
|
assert.Equal(t, tc.want, res.Body.Bytes())
|
|
})
|
|
}
|
|
}
|