mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 19:31:45 +03:00
fa33568fab
Merge in DNS/adguard-home from imp-errcheck to master Squashed commit of the following: commit ed046b8ef59a092a27c623cd14b3fc2ef305fc3d Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 15:00:04 2021 +0300 stats: imp cleanup more commit e53a9240d3e3eec2417c768b98c267a8cd54d992 Merge: da734317676f8c76
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:59:40 2021 +0300 Merge branch 'master' into imp-errcheck commit da734317035543b52e5a9030813084bdc92ba90a Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:37:26 2021 +0300 stats: imp cleanup commit 8b4ad150129111a09be6fa2944a21bd06ab8e5a1 Merge: 385c8a6c5081ead0
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Feb 4 14:34:26 2021 +0300 Merge branch 'master' into imp-errcheck commit 385c8a6c91e3bf07a457da370c8cc77820b91600 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Jan 29 20:41:57 2021 +0300 all: imp errcheck in tests
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"
|
|
)
|
|
|
|
func TestLimitRequestBody(t *testing.T) {
|
|
errReqLimitReached := &aghio.LimitReachedError{
|
|
Limit: RequestBodySizeLimit,
|
|
}
|
|
|
|
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, RequestBodySizeLimit+1)),
|
|
want: make([]byte, RequestBodySizeLimit),
|
|
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)
|
|
|
|
assert.Equal(t, tc.want, res.Body.Bytes())
|
|
assert.Equal(t, tc.wantErr, err)
|
|
})
|
|
}
|
|
}
|