Skip to content

Commit

Permalink
router: extend matched route path test for static routes
Browse files Browse the repository at this point in the history
  • Loading branch information
julienschmidt committed Jan 13, 2020
1 parent 0a8b88a commit 5a92924
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,40 +611,60 @@ func TestRouterParamsFromContext(t *testing.T) {
}

func TestRouterMatchedRoutePath(t *testing.T) {
route1 := "/user/:name"
routed1 := false
handle1 := func(_ http.ResponseWriter, req *http.Request, ps Params) {
route := ps.MatchedRoutePath()
if route != "/user/:name" {
t.Fatalf("Wrong matched route: want /user/:name, got %q", route)
if route != route1 {
t.Fatalf("Wrong matched route: want %s, got %s", route1, route)
}
routed1 = true
}

route2 := "/user/:name/details"
routed2 := false
handle2 := func(_ http.ResponseWriter, req *http.Request, ps Params) {
route := ps.MatchedRoutePath()
if route != "/user/:name/details" {
t.Fatalf("Wrong matched route: want /user/:name/details, got %q", route)
if route != route2 {
t.Fatalf("Wrong matched route: want %s, got %s", route2, route)
}
routed2 = true
}

route3 := "/"
routed3 := false
handle3 := func(_ http.ResponseWriter, req *http.Request, ps Params) {
route := ps.MatchedRoutePath()
if route != route3 {
t.Fatalf("Wrong matched route: want %s, got %s", route3, route)
}
routed3 = true
}

router := New()
router.SaveMatchedRoutePath = true
router.Handle(http.MethodGet, "/user/:name", handle1)
router.Handle(http.MethodGet, "/user/:name/details", handle2)
router.Handle(http.MethodGet, route1, handle1)
router.Handle(http.MethodGet, route2, handle2)
router.Handle(http.MethodGet, route3, handle3)

w := new(mockResponseWriter)
r, _ := http.NewRequest(http.MethodGet, "/user/gopher", nil)
router.ServeHTTP(w, r)
if !routed1 || routed2 {
if !routed1 || routed2 || routed3 {
t.Fatal("Routing failed!")
}

w = new(mockResponseWriter)
r, _ = http.NewRequest(http.MethodGet, "/user/gopher/details", nil)
router.ServeHTTP(w, r)
if !routed2 {
if !routed2 || routed3 {
t.Fatal("Routing failed!")
}

w = new(mockResponseWriter)
r, _ = http.NewRequest(http.MethodGet, "/", nil)
router.ServeHTTP(w, r)
if !routed3 {
t.Fatal("Routing failed!")
}
}
Expand Down

0 comments on commit 5a92924

Please sign in to comment.