(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestBodyLimitMiddleware(t *testing.T) { |
| 15 | app, _ := tests.NewTestApp() |
| 16 | defer app.Cleanup() |
| 17 | |
| 18 | pbRouter, err := apis.NewRouter(app) |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | pbRouter.POST("/a", func(e *core.RequestEvent) error { |
| 23 | return e.String(200, "a") |
| 24 | }) // default global BodyLimit check |
| 25 | |
| 26 | pbRouter.POST("/b", func(e *core.RequestEvent) error { |
| 27 | return e.String(200, "b") |
| 28 | }).Bind(apis.BodyLimit(20)) |
| 29 | |
| 30 | mux, err := pbRouter.BuildMux() |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | |
| 35 | scenarios := []struct { |
| 36 | url string |
| 37 | size int64 |
| 38 | expectedStatus int |
| 39 | }{ |
| 40 | {"/a", 21, 200}, |
| 41 | {"/a", apis.DefaultMaxBodySize + 1, 413}, |
| 42 | {"/b", 20, 200}, |
| 43 | {"/b", 21, 413}, |
| 44 | } |
| 45 | |
| 46 | for _, s := range scenarios { |
| 47 | t.Run(fmt.Sprintf("%s_%d", s.url, s.size), func(t *testing.T) { |
| 48 | rec := httptest.NewRecorder() |
| 49 | req := httptest.NewRequest("POST", s.url, bytes.NewReader(make([]byte, s.size))) |
| 50 | mux.ServeHTTP(rec, req) |
| 51 | |
| 52 | result := rec.Result() |
| 53 | defer result.Body.Close() |
| 54 | |
| 55 | if result.StatusCode != s.expectedStatus { |
| 56 | t.Fatalf("Expected response status %d, got %d", s.expectedStatus, result.StatusCode) |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…