MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / TestMaxBody

Function TestMaxBody

pkg/webmiddleware/max_body_test.go:30–61  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

28)
29
30func TestMaxBody(t *testing.T) {
31 m := MaxBody(16)
32
33 t.Run("Normal Request", func(t *testing.T) {
34 a := assertions.New(t)
35 r := httptest.NewRequest(http.MethodGet, "/", nil)
36 rec := httptest.NewRecorder()
37 m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38 })).ServeHTTP(rec, r)
39 res := rec.Result()
40 a.So(res.StatusCode, should.Equal, http.StatusOK)
41 })
42
43 t.Run("Request Too Big", func(t *testing.T) {
44 a := assertions.New(t)
45 r := httptest.NewRequest(
46 http.MethodPost, "/",
47 bytes.NewBuffer([]byte("this is a little to much")),
48 )
49 rec := httptest.NewRecorder()
50 m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
51 _, err := io.ReadAll(r.Body)
52 a.So(err, should.HaveSameErrorDefinitionAs, ErrRequestBodyTooLarge)
53 webhandlers.Error(w, r, err)
54 })).ServeHTTP(rec, r)
55 res := rec.Result()
56 a.So(res.StatusCode, should.Equal, http.StatusBadRequest)
57
58 body, _ := io.ReadAll(res.Body)
59 a.So(string(body), should.ContainSubstring, "request_body_too_large")
60 })
61}

Callers

nothing calls this directly

Calls 6

ErrorFunction · 0.92
MaxBodyFunction · 0.85
RunMethod · 0.65
NewMethod · 0.65
ResultMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected