(t *testing.T)
| 170 | } |
| 171 | |
| 172 | func TestTrailingSlashMiddleware(t *testing.T) { |
| 173 | // Create a simple handler that returns "OK" |
| 174 | handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 175 | w.WriteHeader(http.StatusOK) |
| 176 | _, _ = w.Write([]byte("OK")) |
| 177 | }) |
| 178 | |
| 179 | // Wrap with our middleware |
| 180 | middleware := api.TrailingSlashMiddleware(handler) |
| 181 | |
| 182 | tests := []struct { |
| 183 | name string |
| 184 | path string |
| 185 | expectedStatus int |
| 186 | expectedLocation string |
| 187 | expectRedirect bool |
| 188 | }{ |
| 189 | { |
| 190 | name: "root path should not redirect", |
| 191 | path: "/", |
| 192 | expectedStatus: http.StatusOK, |
| 193 | expectRedirect: false, |
| 194 | }, |
| 195 | { |
| 196 | name: "path without trailing slash should pass through", |
| 197 | path: "/v0/servers", |
| 198 | expectedStatus: http.StatusOK, |
| 199 | expectRedirect: false, |
| 200 | }, |
| 201 | { |
| 202 | name: "path with trailing slash should redirect", |
| 203 | path: "/v0/servers/", |
| 204 | expectedStatus: http.StatusPermanentRedirect, |
| 205 | expectedLocation: "/v0/servers", |
| 206 | expectRedirect: true, |
| 207 | }, |
| 208 | { |
| 209 | name: "nested path with trailing slash should redirect", |
| 210 | path: "/v0/servers/123/", |
| 211 | expectedStatus: http.StatusPermanentRedirect, |
| 212 | expectedLocation: "/v0/servers/123", |
| 213 | expectRedirect: true, |
| 214 | }, |
| 215 | { |
| 216 | name: "deep nested path with trailing slash should redirect", |
| 217 | path: "/v0/auth/github/token/", |
| 218 | expectedStatus: http.StatusPermanentRedirect, |
| 219 | expectedLocation: "/v0/auth/github/token", |
| 220 | expectRedirect: true, |
| 221 | }, |
| 222 | { |
| 223 | name: "path with query params and no trailing slash should pass through", |
| 224 | path: "/v0/servers?limit=10", |
| 225 | expectedStatus: http.StatusOK, |
| 226 | expectRedirect: false, |
| 227 | }, |
| 228 | { |
| 229 | name: "path with query params and trailing slash should redirect preserving query params", |
nothing calls this directly
no test coverage detected
searching dependent graphs…