MCPcopy
hub / github.com/pocketbase/pocketbase / TestBodyLimitMiddleware

Function TestBodyLimitMiddleware

apis/middlewares_body_limit_test.go:14–60  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func 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}

Callers

nothing calls this directly

Calls 11

CleanupMethod · 0.95
NewTestAppFunction · 0.92
NewRouterFunction · 0.92
BodyLimitFunction · 0.92
POSTMethod · 0.80
BuildMuxMethod · 0.80
NewReaderMethod · 0.80
CloseMethod · 0.65
StringMethod · 0.45
BindMethod · 0.45
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…