mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-12-06 16:22:15 +00:00
test(api): cover chunked body-limit ingress (#1748)
Co-authored-by: marcel299 <29774059+marcel299@users.noreply.github.com>
This commit is contained in:
@@ -403,6 +403,74 @@ func newMultipartServer(t *testing.T, hardTimeout time.Duration, handler echo.Ha
|
||||
return ts
|
||||
}
|
||||
|
||||
func TestContextMiddleware_BoundsUnknownLengthMultipartIngress(t *testing.T) {
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
part, err := writer.CreateFormFile("files", "large.bin")
|
||||
if err != nil {
|
||||
t.Fatalf("create multipart file: %v", err)
|
||||
}
|
||||
if _, err := part.Write(bytes.Repeat([]byte("A"), 2<<20)); err != nil {
|
||||
t.Fatalf("write multipart file: %v", err)
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatalf("close multipart writer: %v", err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
limit int64
|
||||
wantStatus int
|
||||
wantReadMax int64
|
||||
wantHandler bool
|
||||
}{
|
||||
{name: "bounded", limit: 1024, wantStatus: http.StatusRequestEntityTooLarge, wantReadMax: 1025},
|
||||
{name: "unlimited control", wantStatus: http.StatusOK, wantReadMax: int64(body.Len()), wantHandler: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
counted := &countingReadCloser{r: bytes.NewReader(body.Bytes())}
|
||||
req := httptest.NewRequest(http.MethodPost, "/forms/test", nil)
|
||||
req.Body = counted
|
||||
req.ContentLength = -1
|
||||
req.TransferEncoding = []string{"chunked"}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
|
||||
logger := slog.New(slog.DiscardHandler)
|
||||
server := newEchoServer()
|
||||
server.Pre(func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) error {
|
||||
c.Set("logger", logger)
|
||||
return next(c)
|
||||
}
|
||||
})
|
||||
handlerCalled := false
|
||||
server.POST("/forms/test", func(c *echo.Context) error {
|
||||
handlerCalled = true
|
||||
c.Get("context").(*Context).SetAnswer(map[string]string{"result": "ok"})
|
||||
return nil
|
||||
}, contextMiddleware(gotenberg.NewFileSystem(new(gotenberg.OsMkdirAll)), time.Minute, tc.limit, downloadFromConfig{disable: true}, archiveConfig{}))
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
server.ServeHTTP(rec, req)
|
||||
if rec.Code != tc.wantStatus {
|
||||
t.Fatalf("status = %d, want %d; body = %q", rec.Code, tc.wantStatus, rec.Body.String())
|
||||
}
|
||||
if handlerCalled != tc.wantHandler {
|
||||
t.Fatalf("handler called = %t, want %t", handlerCalled, tc.wantHandler)
|
||||
}
|
||||
if counted.n > tc.wantReadMax {
|
||||
t.Fatalf("read %d bytes, want at most %d", counted.n, tc.wantReadMax)
|
||||
}
|
||||
if tc.wantHandler && counted.n != int64(body.Len()) {
|
||||
t.Fatalf("unlimited request read %d/%d bytes", counted.n, body.Len())
|
||||
}
|
||||
if !tc.wantHandler && !strings.Contains(rec.Body.String(), "The request body exceeds the configured size limit") {
|
||||
t.Fatalf("limit error = %q", rec.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// postMultipart posts a multipart form with one field to url.
|
||||
func postMultipart(t *testing.T, url string) *http.Response {
|
||||
t.Helper()
|
||||
|
||||
Reference in New Issue
Block a user