|
| 1 | +package api_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/modelcontextprotocol/registry/internal/api" |
| 9 | +) |
| 10 | + |
| 11 | +func TestTrailingSlashMiddleware(t *testing.T) { |
| 12 | + // Create a simple handler that returns "OK" |
| 13 | + handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 14 | + w.WriteHeader(http.StatusOK) |
| 15 | + _, _ = w.Write([]byte("OK")) |
| 16 | + }) |
| 17 | + |
| 18 | + // Wrap with our middleware |
| 19 | + middleware := api.TrailingSlashMiddleware(handler) |
| 20 | + |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + path string |
| 24 | + expectedStatus int |
| 25 | + expectedLocation string |
| 26 | + expectRedirect bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "root path should not redirect", |
| 30 | + path: "/", |
| 31 | + expectedStatus: http.StatusOK, |
| 32 | + expectRedirect: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "path without trailing slash should pass through", |
| 36 | + path: "/v0/servers", |
| 37 | + expectedStatus: http.StatusOK, |
| 38 | + expectRedirect: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "path with trailing slash should redirect", |
| 42 | + path: "/v0/servers/", |
| 43 | + expectedStatus: http.StatusPermanentRedirect, |
| 44 | + expectedLocation: "/v0/servers", |
| 45 | + expectRedirect: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "nested path with trailing slash should redirect", |
| 49 | + path: "/v0/servers/123/", |
| 50 | + expectedStatus: http.StatusPermanentRedirect, |
| 51 | + expectedLocation: "/v0/servers/123", |
| 52 | + expectRedirect: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "deep nested path with trailing slash should redirect", |
| 56 | + path: "/v0/auth/github/token/", |
| 57 | + expectedStatus: http.StatusPermanentRedirect, |
| 58 | + expectedLocation: "/v0/auth/github/token", |
| 59 | + expectRedirect: true, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "path with query params and no trailing slash should pass through", |
| 63 | + path: "/v0/servers?limit=10", |
| 64 | + expectedStatus: http.StatusOK, |
| 65 | + expectRedirect: false, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "path with query params and trailing slash should redirect preserving query params", |
| 69 | + path: "/v0/servers/?limit=10", |
| 70 | + expectedStatus: http.StatusPermanentRedirect, |
| 71 | + expectedLocation: "/v0/servers?limit=10", |
| 72 | + expectRedirect: true, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + req := httptest.NewRequest(http.MethodGet, tt.path, nil) |
| 79 | + w := httptest.NewRecorder() |
| 80 | + |
| 81 | + middleware.ServeHTTP(w, req) |
| 82 | + |
| 83 | + if w.Code != tt.expectedStatus { |
| 84 | + t.Errorf("expected status %d, got %d", tt.expectedStatus, w.Code) |
| 85 | + } |
| 86 | + |
| 87 | + if tt.expectRedirect { |
| 88 | + location := w.Header().Get("Location") |
| 89 | + if location != tt.expectedLocation { |
| 90 | + t.Errorf("expected Location header %q, got %q", tt.expectedLocation, location) |
| 91 | + } |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
0 commit comments