From 42e0c82bf4f243ea7342bd9e8baeb76882fc63d4 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 6 May 2015 19:16:27 +0200 Subject: [PATCH] catch-all without slash --- routes_test.go | 25 ++++++++++++++++++++++--- tree.go | 2 +- tree_test.go | 8 ++++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/routes_test.go b/routes_test.go index 2c34c92f..09a87590 100644 --- a/routes_test.go +++ b/routes_test.go @@ -132,7 +132,26 @@ func TestRouteParamsByName(t *testing.T) { assert.Equal(t, w.Code, 200) assert.Equal(t, name, "john") assert.Equal(t, lastName, "smith") - assert.Equal(t, wild, "/is/super/great") + assert.Equal(t, wild, "is/super/great") +} + +func TestRouteEmptyWildcardParam(t *testing.T) { + name := "A" + wild := "A" + router := New() + router.GET("/test/:name/*wild", func(c *Context) { + name = c.Params.ByName("name") + wild = c.Params.ByName("wild") + }) + + // RUN + w := performRequest(router, "GET", "/test/john/") + assert.Equal(t, w.Code, 200) + assert.Equal(t, name, "john") + assert.Empty(t, wild) + + w = performRequest(router, "GET", "/test/john") + assert.Equal(t, w.Code, 404) } // TestHandleStaticFile - ensure the static file handles properly @@ -162,7 +181,7 @@ func TestRouteStaticFile(t *testing.T) { } // TestHandleStaticDir - ensure the root/sub dir handles properly -func TestRouteStaticDir(t *testing.T) { +func TestRouteStaticGETToDir(t *testing.T) { // SETUP r := New() r.Static("/", "./") @@ -179,7 +198,7 @@ func TestRouteStaticDir(t *testing.T) { } // TestHandleHeadToDir - ensure the root/sub dir handles properly -func TestRouteHeadToDir(t *testing.T) { +func TestRouteStaticHeadToDir(t *testing.T) { // SETUP router := New() router.Static("/", "./") diff --git a/tree.go b/tree.go index 8cd67e70..fab74ff3 100644 --- a/tree.go +++ b/tree.go @@ -393,7 +393,7 @@ walk: // Outer loop for walking the tree i := len(p) p = p[:i+1] // expand slice within preallocated capacity p[i].Key = n.path[2:] - p[i].Value = path + p[i].Value = path[1:] handlers = n.handlers return diff --git a/tree_test.go b/tree_test.go index 800e7512..4098fcfb 100644 --- a/tree_test.go +++ b/tree_test.go @@ -181,14 +181,14 @@ func TestTreeWildcard(t *testing.T) { {"/cmd/test/", false, "/cmd/:tool/", Params{Param{"tool", "test"}}}, {"/cmd/test", true, "", Params{Param{"tool", "test"}}}, {"/cmd/test/3", false, "/cmd/:tool/:sub", Params{Param{"tool", "test"}, Param{"sub", "3"}}}, - {"/src/", false, "/src/*filepath", Params{Param{"filepath", "/"}}}, - {"/src/some/file.png", false, "/src/*filepath", Params{Param{"filepath", "/some/file.png"}}}, + {"/src/", false, "/src/*filepath", Params{Param{"filepath", ""}}}, + {"/src/some/file.png", false, "/src/*filepath", Params{Param{"filepath", "some/file.png"}}}, {"/search/", false, "/search/", nil}, {"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{"query", "someth!ng+in+ünìcodé"}}}, {"/search/someth!ng+in+ünìcodé/", true, "", Params{Param{"query", "someth!ng+in+ünìcodé"}}}, {"/user_gopher", false, "/user_:name", Params{Param{"name", "gopher"}}}, {"/user_gopher/about", false, "/user_:name/about", Params{Param{"name", "gopher"}}}, - {"/files/js/inc/framework.js", false, "/files/:dir/*filepath", Params{Param{"dir", "js"}, Param{"filepath", "/inc/framework.js"}}}, + {"/files/js/inc/framework.js", false, "/files/:dir/*filepath", Params{Param{"dir", "js"}, Param{"filepath", "inc/framework.js"}}}, {"/info/gordon/public", false, "/info/:user/public", Params{Param{"user", "gordon"}}}, {"/info/gordon/project/go", false, "/info/:user/project/:project", Params{Param{"user", "gordon"}, Param{"project", "go"}}}, }) @@ -300,7 +300,7 @@ func TestTreeDupliatePath(t *testing.T) { checkRequests(t, tree, testRequests{ {"/", false, "/", nil}, {"/doc/", false, "/doc/", nil}, - {"/src/some/file.png", false, "/src/*filepath", Params{Param{"filepath", "/some/file.png"}}}, + {"/src/some/file.png", false, "/src/*filepath", Params{Param{"filepath", "some/file.png"}}}, {"/search/someth!ng+in+ünìcodé", false, "/search/:query", Params{Param{"query", "someth!ng+in+ünìcodé"}}}, {"/user_gopher", false, "/user_:name", Params{Param{"name", "gopher"}}}, })