mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 19:31:45 +03:00
0d44822c43
Merge in DNS/adguard-home from 2639-testify-require-3 to master Updates #2639. Squashed commit of the following: commit 83d7afcbb7e5393db5a0242f3eaca063710d36b7 Merge: ef154b6de83b919d
Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Feb 12 13:07:58 2021 +0300 Merge branch 'master' into 2639-testify-require-3 commit ef154b6d3c89f975ce28369372757a1205baa655 Merge: 5b46073a2eb21ef4
Author: Eugene Burkov <e.burkov@adguard.com> Date: Fri Feb 12 12:40:43 2021 +0300 Merge branch 'master' into 2639-testify-require-3 commit 5b46073a09badef44c86a5f48c6bb874c8df2674 Merge: 7dd7b6e0890f0322
Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Feb 10 21:20:51 2021 +0300 Merge branch 'master' into 2639-testify-require-3 commit 7dd7b6e00ead2bf507af541c801a9ac770106440 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Feb 10 21:19:36 2021 +0300 dhcpd: fix comment commit 9e74adbcf21dad58409c3dfc8e08b6470bfedc22 Author: Eugene Burkov <e.burkov@adguard.com> Date: Wed Feb 10 15:13:40 2021 +0300 all: imp tests drastically
69 lines
1.4 KiB
Go
69 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: 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)
|
|
|
|
require.Equal(t, tc.wantErr, err)
|
|
assert.Equal(t, tc.want, res.Body.Bytes())
|
|
})
|
|
}
|
|
}
|