Skip to content

Commit 5fffc45

Browse files
committed
fmt, add more tests
🏠 Remote-Dev: homespace
1 parent b856042 commit 5fffc45

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

internal/api/server_test.go

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
package api
1+
package api_test
22

33
import (
44
"net/http"
55
"net/http/httptest"
66
"testing"
7+
8+
"github.com/modelcontextprotocol/registry/internal/api"
79
)
810

911
func TestTrailingSlashMiddleware(t *testing.T) {
1012
// Create a simple handler that returns "OK"
11-
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
13+
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
1214
w.WriteHeader(http.StatusOK)
13-
w.Write([]byte("OK"))
15+
_, _ = w.Write([]byte("OK"))
1416
})
1517

1618
// Wrap with our middleware
17-
middleware := TrailingSlashMiddleware(handler)
19+
middleware := api.TrailingSlashMiddleware(handler)
1820

1921
tests := []struct {
20-
name string
21-
path string
22-
expectedStatus int
22+
name string
23+
path string
24+
expectedStatus int
2325
expectedLocation string
24-
expectRedirect bool
26+
expectRedirect bool
2527
}{
2628
{
2729
name: "root path should not redirect",
@@ -36,31 +38,44 @@ func TestTrailingSlashMiddleware(t *testing.T) {
3638
expectRedirect: false,
3739
},
3840
{
39-
name: "path with trailing slash should redirect",
40-
path: "/v0/servers/",
41-
expectedStatus: http.StatusPermanentRedirect,
41+
name: "path with trailing slash should redirect",
42+
path: "/v0/servers/",
43+
expectedStatus: http.StatusPermanentRedirect,
4244
expectedLocation: "/v0/servers",
43-
expectRedirect: true,
45+
expectRedirect: true,
4446
},
4547
{
46-
name: "nested path with trailing slash should redirect",
47-
path: "/v0/servers/123/",
48-
expectedStatus: http.StatusPermanentRedirect,
48+
name: "nested path with trailing slash should redirect",
49+
path: "/v0/servers/123/",
50+
expectedStatus: http.StatusPermanentRedirect,
4951
expectedLocation: "/v0/servers/123",
50-
expectRedirect: true,
52+
expectRedirect: true,
5153
},
5254
{
53-
name: "deep nested path with trailing slash should redirect",
54-
path: "/v0/auth/github/token/",
55-
expectedStatus: http.StatusPermanentRedirect,
55+
name: "deep nested path with trailing slash should redirect",
56+
path: "/v0/auth/github/token/",
57+
expectedStatus: http.StatusPermanentRedirect,
5658
expectedLocation: "/v0/auth/github/token",
57-
expectRedirect: true,
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,
5873
},
5974
}
6075

6176
for _, tt := range tests {
6277
t.Run(tt.name, func(t *testing.T) {
63-
req := httptest.NewRequest("GET", tt.path, nil)
78+
req := httptest.NewRequest(http.MethodGet, tt.path, nil)
6479
w := httptest.NewRecorder()
6580

6681
middleware.ServeHTTP(w, req)
@@ -77,4 +92,4 @@ func TestTrailingSlashMiddleware(t *testing.T) {
7792
}
7893
})
7994
}
80-
}
95+
}

0 commit comments

Comments
 (0)