Skip to content

Commit

Permalink
home: cover middleware with test
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Nov 20, 2020
1 parent bedf436 commit 60dc706
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ and this project adheres to
### Changed

- Improved tests output ([#2273]).
- Various internal improvements ([#2271], [#2297]).

[#2271]: https://github.com/AdguardTeam/AdGuardHome/issues/2271
[#2297]: https://github.com/AdguardTeam/AdGuardHome/issues/2297

### Fixed

Expand Down
9 changes: 4 additions & 5 deletions internal/aghio/limitedreadcloser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (

// LimitReachedError records the limit and the operation that caused it.
type LimitReachedError struct {
Op string
Limit int64
}

// Error implements error interface for LimitReachedError.
// TODO(a.garipov): Think about error string format.
func (lre *LimitReachedError) Error() string {
b := &strings.Builder{}

b.WriteString(lre.Op)
b.WriteString(": limit reached with ")
b.WriteString("attempted to read more than ")
b.WriteString(strconv.FormatInt(lre.Limit, 10))
b.WriteString(" bytes read")
b.WriteString(" bytes")

return b.String()
}
Expand All @@ -36,7 +36,6 @@ type limitedReadCloser struct {
func (lrc *limitedReadCloser) Read(p []byte) (n int, err error) {
if lrc.n <= 0 {
return 0, &LimitReachedError{
Op: "Read",
Limit: lrc.limit,
}
}
Expand Down
4 changes: 1 addition & 3 deletions internal/aghio/limitedreadcloser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func TestLimitedReadCloser_Read(t *testing.T) {
rStr: "abc",
want: 0,
err: &LimitReachedError{
Op: "Read",
Limit: 0,
},
}, {
Expand Down Expand Up @@ -66,9 +65,8 @@ func TestLimitedReadCloser_LimitReachedError(t *testing.T) {
err error
}{{
name: "simplest",
want: "test: limit reached with 0 bytes read",
want: "attempted to read more than 0 bytes",
err: &LimitReachedError{
Op: "test",
Limit: 0,
},
}}
Expand Down
3 changes: 2 additions & 1 deletion internal/home/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func withMiddlewares(h http.Handler, middlewares ...middleware) (wrapped http.Ha
// RequestBodySizeLimit is maximum request body length in bytes.
const RequestBodySizeLimit = 64 * 1024

// LimitRequestBody substitutes body of the request with LimitedReadCloser.
// LimitRequestBody wraps underlying handler h, making it's request's body Read
// method limited.
func limitRequestBody(h http.Handler) (limited http.Handler) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Body = aghio.LimitReadCloser(r.Body, RequestBodySizeLimit)
Expand Down
64 changes: 64 additions & 0 deletions internal/home/middlewares_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package home

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/AdguardTeam/AdGuardHome/internal/aghio"
"github.com/stretchr/testify/assert"
)

func TestMiddlewares_LimitRequestBody(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)
w.Write(b)
})
}

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)
})
}
}

0 comments on commit 60dc706

Please sign in to comment.