MCPcopy
hub / github.com/go-chi/chi / TestMuxTrailingSlash

Function TestMuxTrailingSlash

mux_test.go:285–314  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
285func 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
316func TestMuxNestedNotFound(t *testing.T) {
317 r := NewRouter()

Callers

nothing calls this directly

Calls 9

NotFoundMethod · 0.95
GetMethod · 0.95
MountMethod · 0.95
NewRouterFunction · 0.85
URLParamFunction · 0.85
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65
WriteHeaderMethod · 0.45

Tested by

no test coverage detected