mirror of
https://github.com/caddyserver/caddy.git
synced 2025-06-23 00:21:23 +08:00
Introduce limits
middleware
1. Replace original `maxrequestbody` directive. 2. Add request header limit. fix issue #1587 Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
35
caddyhttp/limits/handler_test.go
Normal file
35
caddyhttp/limits/handler_test.go
Normal file
@ -0,0 +1,35 @@
|
||||
package limits
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mholt/caddy/caddyhttp/httpserver"
|
||||
)
|
||||
|
||||
func TestBodySizeLimit(t *testing.T) {
|
||||
var (
|
||||
gotContent []byte
|
||||
gotError error
|
||||
expectContent = "hello"
|
||||
)
|
||||
l := Limit{
|
||||
Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
gotContent, gotError = ioutil.ReadAll(r.Body)
|
||||
return 0, nil
|
||||
}),
|
||||
BodyLimits: []httpserver.PathLimit{{Path: "/", Limit: int64(len(expectContent))}},
|
||||
}
|
||||
|
||||
r := httptest.NewRequest("GET", "/", strings.NewReader(expectContent+expectContent))
|
||||
l.ServeHTTP(httptest.NewRecorder(), r)
|
||||
if got := string(gotContent); got != expectContent {
|
||||
t.Errorf("expected content[%s], got[%s]", expectContent, got)
|
||||
}
|
||||
if gotError != httpserver.MaxBytesExceededErr {
|
||||
t.Errorf("expect error %v, got %v", httpserver.MaxBytesExceededErr, gotError)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user