Browse Source

catch-all without slash

Manu Mtz-Almeida 10 years ago
parent
commit
42e0c82bf4
3 changed files with 27 additions and 8 deletions
  1. 22 3
      routes_test.go
  2. 1 1
      tree.go
  3. 4 4
      tree_test.go

+ 22 - 3
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("/", "./")

+ 1 - 1
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

+ 4 - 4
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"}}},
 	})