| 28 | ) |
| 29 | |
| 30 | func 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 | } |