|
| 1 | +package middleware |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestMCPRequestMetadataExtractsToolCall(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + const body = `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"pkgsite_search","arguments":{"query":"slices"}}}` |
| 16 | + handler := MCPRequestMetadata(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 17 | + if got, want := r.Header.Get(HeaderInternalMCPMethod), "tools/call"; got != want { |
| 18 | + t.Fatalf("method = %q, want %q", got, want) |
| 19 | + } |
| 20 | + if got, want := r.Header.Get(HeaderInternalMCPName), "pkgsite_search"; got != want { |
| 21 | + t.Fatalf("name = %q, want %q", got, want) |
| 22 | + } |
| 23 | + gotBody, err := io.ReadAll(r.Body) |
| 24 | + if err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + if string(gotBody) != body { |
| 28 | + t.Fatalf("body = %q, want %q", string(gotBody), body) |
| 29 | + } |
| 30 | + })) |
| 31 | + |
| 32 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/mcp", strings.NewReader(body)) |
| 33 | + handler.ServeHTTP(httptest.NewRecorder(), req) |
| 34 | +} |
| 35 | + |
| 36 | +func TestMCPRequestMetadataExtractsListMethodWithoutName(t *testing.T) { |
| 37 | + t.Parallel() |
| 38 | + |
| 39 | + handler := MCPRequestMetadata(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 40 | + if got, want := r.Header.Get(HeaderInternalMCPMethod), "tools/list"; got != want { |
| 41 | + t.Fatalf("method = %q, want %q", got, want) |
| 42 | + } |
| 43 | + if got := r.Header.Get(HeaderInternalMCPName); got != "" { |
| 44 | + t.Fatalf("name = %q, want empty", got) |
| 45 | + } |
| 46 | + })) |
| 47 | + |
| 48 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/mcp", strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/list"}`)) |
| 49 | + handler.ServeHTTP(httptest.NewRecorder(), req) |
| 50 | +} |
| 51 | + |
| 52 | +func TestMCPRequestMetadataSkipsNonMCPRequest(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + handler := MCPRequestMetadata(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 56 | + if got := r.Header.Get(HeaderInternalMCPMethod); got != "" { |
| 57 | + t.Fatalf("method = %q, want empty", got) |
| 58 | + } |
| 59 | + })) |
| 60 | + |
| 61 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/other", strings.NewReader(`{"method":"tools/list"}`)) |
| 62 | + handler.ServeHTTP(httptest.NewRecorder(), req) |
| 63 | +} |
| 64 | + |
| 65 | +func TestMCPRequestMetadataClearsCallerSuppliedInternalHeaders(t *testing.T) { |
| 66 | + t.Parallel() |
| 67 | + |
| 68 | + handler := MCPRequestMetadata(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 69 | + if got := r.Header.Get(HeaderInternalMCPMethod); got != "" { |
| 70 | + t.Fatalf("method = %q, want empty", got) |
| 71 | + } |
| 72 | + if got := r.Header.Get(HeaderInternalMCPName); got != "" { |
| 73 | + t.Fatalf("name = %q, want empty", got) |
| 74 | + } |
| 75 | + })) |
| 76 | + |
| 77 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/other", strings.NewReader(`{"method":"tools/list"}`)) |
| 78 | + req.Header.Set(HeaderInternalMCPMethod, "tools/call") |
| 79 | + req.Header.Set(HeaderInternalMCPName, "pkgsite_search") |
| 80 | + handler.ServeHTTP(httptest.NewRecorder(), req) |
| 81 | +} |
| 82 | + |
| 83 | +func TestMCPRequestMetadataSkipsUnknownLengthBodyWithoutTruncating(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + |
| 86 | + body := strings.Repeat("x", int(maxMCPMetadataBodyBytes)+1) |
| 87 | + handler := MCPRequestMetadata(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 88 | + if got := r.Header.Get(HeaderInternalMCPMethod); got != "" { |
| 89 | + t.Fatalf("method = %q, want empty", got) |
| 90 | + } |
| 91 | + gotBody, err := io.ReadAll(r.Body) |
| 92 | + if err != nil { |
| 93 | + t.Fatal(err) |
| 94 | + } |
| 95 | + if string(gotBody) != body { |
| 96 | + t.Fatalf("body length = %d, want %d", len(gotBody), len(body)) |
| 97 | + } |
| 98 | + })) |
| 99 | + |
| 100 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/mcp", strings.NewReader(body)) |
| 101 | + req.ContentLength = -1 |
| 102 | + req.Header.Set(HeaderInternalMCPMethod, "tools/list") |
| 103 | + handler.ServeHTTP(httptest.NewRecorder(), req) |
| 104 | +} |
| 105 | + |
| 106 | +func TestMCPRequestMetadataRestoresPartialBodyAfterReadError(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + const body = `{"jsonrpc":"2.0"` |
| 110 | + handler := MCPRequestMetadata(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { |
| 111 | + if got := r.Header.Get(HeaderInternalMCPMethod); got != "" { |
| 112 | + t.Fatalf("method = %q, want empty", got) |
| 113 | + } |
| 114 | + gotBody, err := io.ReadAll(r.Body) |
| 115 | + if err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + if string(gotBody) != body { |
| 119 | + t.Fatalf("body = %q, want %q", string(gotBody), body) |
| 120 | + } |
| 121 | + })) |
| 122 | + |
| 123 | + req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/mcp", &errorReader{data: body}) |
| 124 | + req.ContentLength = int64(len(body)) |
| 125 | + handler.ServeHTTP(httptest.NewRecorder(), req) |
| 126 | +} |
| 127 | + |
| 128 | +type errorReader struct { |
| 129 | + data string |
| 130 | + done bool |
| 131 | +} |
| 132 | + |
| 133 | +func (r *errorReader) Close() error { |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func (r *errorReader) Read(p []byte) (int, error) { |
| 138 | + if r.done { |
| 139 | + return 0, io.EOF |
| 140 | + } |
| 141 | + r.done = true |
| 142 | + return copy(p, r.data), errors.New("read failed") |
| 143 | +} |
0 commit comments