|
4 | 4 | "bytes" |
5 | 5 | "errors" |
6 | 6 | "io" |
| 7 | + "mime/multipart" |
7 | 8 | "net" |
8 | 9 | "net/http" |
9 | 10 | "net/http/httptest" |
@@ -578,6 +579,45 @@ func Test_Integration_App_ServerErrorHandler_PreservesHelmetHeadersOnBodyLimit(t |
578 | 579 | require.Equal(t, "require-corp", string(resp.Header.Peek("Cross-Origin-Embedder-Policy"))) |
579 | 580 | } |
580 | 581 |
|
| 582 | +func Test_Integration_App_ServerErrorHandler_CustomMultipartBodyLimit(t *testing.T) { |
| 583 | + t.Parallel() |
| 584 | + |
| 585 | + app := fiber.New(fiber.Config{ |
| 586 | + BodyLimit: 128, |
| 587 | + ErrorHandler: func(c fiber.Ctx, err error) error { |
| 588 | + code := fiber.StatusInternalServerError |
| 589 | + var fiberErr *fiber.Error |
| 590 | + if errors.As(err, &fiberErr) { |
| 591 | + code = fiberErr.Code |
| 592 | + } |
| 593 | + return c.Status(code).JSON(fiber.Map{ |
| 594 | + "code": code, |
| 595 | + "message": err.Error(), |
| 596 | + }) |
| 597 | + }, |
| 598 | + }) |
| 599 | + app.Post("/", func(c fiber.Ctx) error { |
| 600 | + return c.SendString("ok") |
| 601 | + }) |
| 602 | + |
| 603 | + body := &bytes.Buffer{} |
| 604 | + writer := multipart.NewWriter(body) |
| 605 | + fileWriter, err := writer.CreateFormFile("file", "large.txt") |
| 606 | + require.NoError(t, err) |
| 607 | + _, err = fileWriter.Write(bytes.Repeat([]byte{'a'}, 256)) |
| 608 | + require.NoError(t, err) |
| 609 | + require.NoError(t, writer.Close()) |
| 610 | + |
| 611 | + resp := performOversizedRequest(t, app, func(req *fasthttp.Request) { |
| 612 | + req.Header.Set(fiber.HeaderContentType, writer.FormDataContentType()) |
| 613 | + req.SetBody(body.Bytes()) |
| 614 | + }) |
| 615 | + |
| 616 | + require.Equal(t, fiber.StatusRequestEntityTooLarge, resp.StatusCode()) |
| 617 | + require.Contains(t, string(resp.Header.ContentType()), fiber.MIMEApplicationJSON) |
| 618 | + require.JSONEq(t, `{"code":413,"message":"Request Entity Too Large"}`, string(resp.Body())) |
| 619 | +} |
| 620 | + |
581 | 621 | func Test_Integration_App_ServerErrorHandler_PreservesRequestID(t *testing.T) { |
582 | 622 | const expectedRequestID = "integration-request-id" |
583 | 623 |
|
|
0 commit comments