MCPcopy
hub / github.com/julienschmidt/httprouter / TestRouterLookup

Function TestRouterLookup

router_test.go:478–528  ·  router_test.go::TestRouterLookup
(t *testing.T)

Source from the content-addressed store, hash-verified

476}
477
478func TestRouterLookup(t *testing.T) {
479 routed := false
480 wantHandle := func(_ http.ResponseWriter, _ *http.Request, _ Params) {
481 routed = true
482 }
483 wantParams := Params{Param{"name", "gopher"}}
484
485 router := New()
486
487 // try empty router first
488 handle, _, tsr := router.Lookup(http.MethodGet, "/nope")
489 if handle != nil {
490 t.Fatalf("Got handle for unregistered pattern: %v", handle)
491 }
492 if tsr {
493 t.Error("Got wrong TSR recommendation!")
494 }
495
496 // insert route and try again
497 router.GET("/user/:name", wantHandle)
498
499 handle, params, _ := router.Lookup(http.MethodGet, "/user/gopher")
500 if handle == nil {
501 t.Fatal("Got no handle!")
502 } else {
503 handle(nil, nil, nil)
504 if !routed {
505 t.Fatal("Routing failed!")
506 }
507 }
508
509 if !reflect.DeepEqual(params, wantParams) {
510 t.Fatalf("Wrong parameter values: want %v, got %v", wantParams, params)
511 }
512
513 handle, _, tsr = router.Lookup(http.MethodGet, "/user/gopher/")
514 if handle != nil {
515 t.Fatalf("Got handle for unregistered pattern: %v", handle)
516 }
517 if !tsr {
518 t.Error("Got no TSR recommendation!")
519 }
520
521 handle, _, tsr = router.Lookup(http.MethodGet, "/nope")
522 if handle != nil {
523 t.Fatalf("Got handle for unregistered pattern: %v", handle)
524 }
525 if tsr {
526 t.Error("Got wrong TSR recommendation!")
527 }
528}
529
530func TestRouterParamsFromContext(t *testing.T) {
531 routed := false

Callers

nothing calls this directly

Calls 3

NewFunction · 0.85
LookupMethod · 0.80
GETMethod · 0.80

Tested by

no test coverage detected