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

Function TestMuxNestedMethodNotAllowed

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

Source from the content-addressed store, hash-verified

429}
430
431func TestMuxNestedMethodNotAllowed(t *testing.T) {
432 r := NewRouter()
433 r.Get("/root", func(w http.ResponseWriter, r *http.Request) {
434 w.Write([]byte("root"))
435 })
436 r.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
437 w.WriteHeader(405)
438 w.Write([]byte("root 405"))
439 })
440
441 sr1 := NewRouter()
442 sr1.Get("/sub1", func(w http.ResponseWriter, r *http.Request) {
443 w.Write([]byte("sub1"))
444 })
445 sr1.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
446 w.WriteHeader(405)
447 w.Write([]byte("sub1 405"))
448 })
449
450 sr2 := NewRouter()
451 sr2.Get("/sub2", func(w http.ResponseWriter, r *http.Request) {
452 w.Write([]byte("sub2"))
453 })
454
455 pathVar := NewRouter()
456 pathVar.Get("/{var}", func(w http.ResponseWriter, r *http.Request) {
457 w.Write([]byte("pv"))
458 })
459 pathVar.MethodNotAllowed(func(w http.ResponseWriter, r *http.Request) {
460 w.WriteHeader(405)
461 w.Write([]byte("pv 405"))
462 })
463
464 r.Mount("/prefix1", sr1)
465 r.Mount("/prefix2", sr2)
466 r.Mount("/pathVar", pathVar)
467
468 ts := httptest.NewServer(r)
469 defer ts.Close()
470
471 if _, body := testRequest(t, ts, "GET", "/root", nil); body != "root" {
472 t.Fatal(body)
473 }
474 if _, body := testRequest(t, ts, "PUT", "/root", nil); body != "root 405" {
475 t.Fatal(body)
476 }
477 if _, body := testRequest(t, ts, "GET", "/prefix1/sub1", nil); body != "sub1" {
478 t.Fatal(body)
479 }
480 if _, body := testRequest(t, ts, "PUT", "/prefix1/sub1", nil); body != "sub1 405" {
481 t.Fatal(body)
482 }
483 if _, body := testRequest(t, ts, "GET", "/prefix2/sub2", nil); body != "sub2" {
484 t.Fatal(body)
485 }
486 if _, body := testRequest(t, ts, "PUT", "/prefix2/sub2", nil); body != "root 405" {
487 t.Fatal(body)
488 }

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected