Compare commits

...

1 Commits

Author SHA1 Message Date
Manu Mtz-Almeida 42e0c82bf4 catch-all without slash 2015-05-06 19:16:27 +02:00
3 changed files with 27 additions and 8 deletions

View File

@ -132,7 +132,26 @@ func TestRouteParamsByName(t *testing.T) {
assert.Equal(t, w.Code, 200) assert.Equal(t, w.Code, 200)
assert.Equal(t, name, "john") assert.Equal(t, name, "john")
assert.Equal(t, lastName, "smith") 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 // TestHandleStaticFile - ensure the static file handles properly
@ -162,7 +181,7 @@ func TestRouteStaticFile(t *testing.T) {
} }
// TestHandleStaticDir - ensure the root/sub dir handles properly // TestHandleStaticDir - ensure the root/sub dir handles properly
func TestRouteStaticDir(t *testing.T) { func TestRouteStaticGETToDir(t *testing.T) {
// SETUP // SETUP
r := New() r := New()
r.Static("/", "./") r.Static("/", "./")
@ -179,7 +198,7 @@ func TestRouteStaticDir(t *testing.T) {
} }
// TestHandleHeadToDir - ensure the root/sub dir handles properly // TestHandleHeadToDir - ensure the root/sub dir handles properly
func TestRouteHeadToDir(t *testing.T) { func TestRouteStaticHeadToDir(t *testing.T) {
// SETUP // SETUP
router := New() router := New()
router.Static("/", "./") router.Static("/", "./")

View File

@ -393,7 +393,7 @@ walk: // Outer loop for walking the tree
i := len(p) i := len(p)
p = p[:i+1] // expand slice within preallocated capacity p = p[:i+1] // expand slice within preallocated capacity
p[i].Key = n.path[2:] p[i].Key = n.path[2:]
p[i].Value = path p[i].Value = path[1:]
handlers = n.handlers handlers = n.handlers
return return

View File

@ -181,14 +181,14 @@ func TestTreeWildcard(t *testing.T) {
{"/cmd/test/", false, "/cmd/:tool/", Params{Param{"tool", "test"}}}, {"/cmd/test/", false, "/cmd/:tool/", Params{Param{"tool", "test"}}},
{"/cmd/test", true, "", 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"}}}, {"/cmd/test/3", false, "/cmd/:tool/:sub", Params{Param{"tool", "test"}, Param{"sub", "3"}}},
{"/src/", false, "/src/*filepath", Params{Param{"filepath", "/"}}}, {"/src/", false, "/src/*filepath", Params{Param{"filepath", ""}}},
{"/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/", false, "/search/", nil}, {"/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é", 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é"}}}, {"/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", false, "/user_:name", Params{Param{"name", "gopher"}}},
{"/user_gopher/about", false, "/user_:name/about", 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/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"}}}, {"/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{ checkRequests(t, tree, testRequests{
{"/", false, "/", nil}, {"/", false, "/", nil},
{"/doc/", false, "/doc/", 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é"}}}, {"/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"}}}, {"/user_gopher", false, "/user_:name", Params{Param{"name", "gopher"}}},
}) })