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

Function TestMuxComplicatedNotFound

mux_test.go:497–587  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

495}
496
497func TestMuxComplicatedNotFound(t *testing.T) {
498 decorateRouter := func(r *Mux) {
499 // Root router with groups
500 r.Get("/auth", func(w http.ResponseWriter, r *http.Request) {
501 w.Write([]byte("auth get"))
502 })
503 r.Route("/public", func(r Router) {
504 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
505 w.Write([]byte("public get"))
506 })
507 })
508
509 // sub router with groups
510 sub0 := NewRouter()
511 sub0.Route("/resource", func(r Router) {
512 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
513 w.Write([]byte("private get"))
514 })
515 })
516 r.Mount("/private", sub0)
517
518 // sub router with groups
519 sub1 := NewRouter()
520 sub1.Route("/resource", func(r Router) {
521 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
522 w.Write([]byte("private get"))
523 })
524 })
525 r.With(func(next http.Handler) http.Handler { return next }).Mount("/private_mw", sub1)
526 }
527
528 testNotFound := func(t *testing.T, r *Mux) {
529 ts := httptest.NewServer(r)
530 defer ts.Close()
531
532 // check that we didn't break correct routes
533 if _, body := testRequest(t, ts, "GET", "/auth", nil); body != "auth get" {
534 t.Fatal(body)
535 }
536 if _, body := testRequest(t, ts, "GET", "/public", nil); body != "public get" {
537 t.Fatal(body)
538 }
539 if _, body := testRequest(t, ts, "GET", "/public/", nil); body != "public get" {
540 t.Fatal(body)
541 }
542 if _, body := testRequest(t, ts, "GET", "/private/resource", nil); body != "private get" {
543 t.Fatal(body)
544 }
545 // check custom not-found on all levels
546 if _, body := testRequest(t, ts, "GET", "/nope", nil); body != "custom not-found" {
547 t.Fatal(body)
548 }
549 if _, body := testRequest(t, ts, "GET", "/public/nope", nil); body != "custom not-found" {
550 t.Fatal(body)
551 }
552 if _, body := testRequest(t, ts, "GET", "/private/nope", nil); body != "custom not-found" {
553 t.Fatal(body)
554 }

Callers

nothing calls this directly

Calls 9

GetMethod · 0.95
RouteMethod · 0.95
MountMethod · 0.95
WithMethod · 0.95
NotFoundMethod · 0.95
NewRouterFunction · 0.85
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65

Tested by

no test coverage detected