Test a mux that routes a trailing slash, see also middleware/strip_test.go for an example of using a middleware to handle trailing slashes.
(t *testing.T)
| 283 | // Test a mux that routes a trailing slash, see also middleware/strip_test.go |
| 284 | // for an example of using a middleware to handle trailing slashes. |
| 285 | func TestMuxTrailingSlash(t *testing.T) { |
| 286 | r := NewRouter() |
| 287 | r.NotFound(func(w http.ResponseWriter, r *http.Request) { |
| 288 | w.WriteHeader(404) |
| 289 | w.Write([]byte("nothing here")) |
| 290 | }) |
| 291 | |
| 292 | subRoutes := NewRouter() |
| 293 | indexHandler := func(w http.ResponseWriter, r *http.Request) { |
| 294 | accountID := URLParam(r, "accountID") |
| 295 | w.Write([]byte(accountID)) |
| 296 | } |
| 297 | subRoutes.Get("/", indexHandler) |
| 298 | |
| 299 | r.Mount("/accounts/{accountID}", subRoutes) |
| 300 | r.Get("/accounts/{accountID}/", indexHandler) |
| 301 | |
| 302 | ts := httptest.NewServer(r) |
| 303 | defer ts.Close() |
| 304 | |
| 305 | if _, body := testRequest(t, ts, "GET", "/accounts/admin", nil); body != "admin" { |
| 306 | t.Fatal(body) |
| 307 | } |
| 308 | if _, body := testRequest(t, ts, "GET", "/accounts/admin/", nil); body != "admin" { |
| 309 | t.Fatal(body) |
| 310 | } |
| 311 | if _, body := testRequest(t, ts, "GET", "/nothing-here", nil); body != "nothing here" { |
| 312 | t.Fatal(body) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | func TestMuxNestedNotFound(t *testing.T) { |
| 317 | r := NewRouter() |
nothing calls this directly
no test coverage detected