maxbyteshandler_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package handler
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestMaxBytesHandler(t *testing.T) {
  10. maxb := MaxBytesHandler(10)
  11. handler := maxb(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
  12. req := httptest.NewRequest(http.MethodPost, "http://localhost",
  13. bytes.NewBufferString("123456789012345"))
  14. resp := httptest.NewRecorder()
  15. handler.ServeHTTP(resp, req)
  16. assert.Equal(t, http.StatusRequestEntityTooLarge, resp.Code)
  17. req = httptest.NewRequest(http.MethodPost, "http://localhost", bytes.NewBufferString("12345"))
  18. resp = httptest.NewRecorder()
  19. handler.ServeHTTP(resp, req)
  20. assert.Equal(t, http.StatusOK, resp.Code)
  21. }
  22. func TestMaxBytesHandlerNoLimit(t *testing.T) {
  23. maxb := MaxBytesHandler(-1)
  24. handler := maxb(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
  25. req := httptest.NewRequest(http.MethodPost, "http://localhost",
  26. bytes.NewBufferString("123456789012345"))
  27. resp := httptest.NewRecorder()
  28. handler.ServeHTTP(resp, req)
  29. assert.Equal(t, http.StatusOK, resp.Code)
  30. }