|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// unknownLengthBody wraps a reader without exposing a Len method, so |
| 15 | +// httptest.NewRequest cannot infer Content-Length from it. This mirrors a |
| 16 | +// chunked-transfer-encoded request, where the body size is unknown upfront. |
| 17 | +func unknownLengthBody(s string) io.Reader { |
| 18 | + return io.NopCloser(strings.NewReader(s)) |
| 19 | +} |
| 20 | + |
| 21 | +func TestWithMaxBodySize(t *testing.T) { |
| 22 | + const limit = 16 |
| 23 | + |
| 24 | + t.Run("allowed request under the limit passes through", func(t *testing.T) { |
| 25 | + var nextCalled bool |
| 26 | + var readBody string |
| 27 | + var readErr error |
| 28 | + |
| 29 | + next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 30 | + nextCalled = true |
| 31 | + b, err := io.ReadAll(r.Body) |
| 32 | + readBody, readErr = string(b), err |
| 33 | + }) |
| 34 | + |
| 35 | + handler := WithMaxBodySize(limit)(next) |
| 36 | + |
| 37 | + req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader("short")) |
| 38 | + rr := httptest.NewRecorder() |
| 39 | + handler.ServeHTTP(rr, req) |
| 40 | + |
| 41 | + assert.True(t, nextCalled, "next handler should be called for an allowed request") |
| 42 | + require.NoError(t, readErr) |
| 43 | + assert.Equal(t, "short", readBody) |
| 44 | + assert.Equal(t, http.StatusOK, rr.Code) |
| 45 | + }) |
| 46 | + |
| 47 | + t.Run("boundary size exactly at the limit is allowed", func(t *testing.T) { |
| 48 | + body := strings.Repeat("a", limit) |
| 49 | + |
| 50 | + var nextCalled bool |
| 51 | + var readBody string |
| 52 | + var readErr error |
| 53 | + |
| 54 | + next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 55 | + nextCalled = true |
| 56 | + b, err := io.ReadAll(r.Body) |
| 57 | + readBody, readErr = string(b), err |
| 58 | + }) |
| 59 | + |
| 60 | + handler := WithMaxBodySize(limit)(next) |
| 61 | + |
| 62 | + req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(body)) |
| 63 | + rr := httptest.NewRecorder() |
| 64 | + handler.ServeHTTP(rr, req) |
| 65 | + |
| 66 | + assert.True(t, nextCalled, "next handler should be called when the body is exactly at the limit") |
| 67 | + require.NoError(t, readErr, "reading exactly maxBytes should not error") |
| 68 | + assert.Equal(t, body, readBody, "the full boundary-size body should be readable") |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("oversized request with known Content-Length is rejected before next runs", func(t *testing.T) { |
| 72 | + body := strings.Repeat("a", limit+1) |
| 73 | + |
| 74 | + var nextCalled bool |
| 75 | + next := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { |
| 76 | + nextCalled = true |
| 77 | + }) |
| 78 | + |
| 79 | + handler := WithMaxBodySize(limit)(next) |
| 80 | + |
| 81 | + req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(body)) |
| 82 | + require.Equal(t, int64(limit+1), req.ContentLength, "test setup: Content-Length should be known") |
| 83 | + |
| 84 | + rr := httptest.NewRecorder() |
| 85 | + handler.ServeHTTP(rr, req) |
| 86 | + |
| 87 | + assert.False(t, nextCalled, "next handler must not run for an oversized request") |
| 88 | + assert.Equal(t, http.StatusRequestEntityTooLarge, rr.Code) |
| 89 | + assert.Contains(t, rr.Body.String(), "request body too large") |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("oversized request with unknown length fails on downstream read", func(t *testing.T) { |
| 93 | + body := strings.Repeat("a", limit+1) |
| 94 | + |
| 95 | + var nextCalled bool |
| 96 | + var readErr error |
| 97 | + |
| 98 | + next := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 99 | + nextCalled = true |
| 100 | + _, readErr = io.ReadAll(r.Body) |
| 101 | + }) |
| 102 | + |
| 103 | + handler := WithMaxBodySize(limit)(next) |
| 104 | + |
| 105 | + req := httptest.NewRequest(http.MethodPost, "/mcp", unknownLengthBody(body)) |
| 106 | + require.Equal(t, int64(-1), req.ContentLength, "test setup: Content-Length should be unknown") |
| 107 | + |
| 108 | + rr := httptest.NewRecorder() |
| 109 | + handler.ServeHTTP(rr, req) |
| 110 | + |
| 111 | + assert.True(t, nextCalled, "next handler still runs; the limit is enforced on read") |
| 112 | + require.Error(t, readErr) |
| 113 | + assert.True(t, isMaxBytesError(readErr), "expected a *http.MaxBytesError, got %v", readErr) |
| 114 | + }) |
| 115 | +} |
0 commit comments