Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: allow "/*catch-all" and "/normal" routes coexist #3270

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ func main() {
router.GET("/user/groups", func(c *gin.Context) {
c.String(http.StatusOK, "The available groups are [...]")
})

// This handler will match when none of the above routes match
router.GET("/*action", func(c *gin.Context) {
c.String(http.StatusOK, "Unknown Action")
})

router.Run(":8080")
}
Expand Down Expand Up @@ -1271,11 +1276,12 @@ func main() {
```go
func main() {
router := gin.Default()
router.StaticFS("/", http.Dir("dist"))
router.Static("/assets", "./assets")
router.StaticFS("/more_static", http.Dir("my_file_system"))
router.StaticFile("/favicon.ico", "./resources/favicon.ico")
router.StaticFileFS("/more_favicon.ico", "more_favicon.ico", http.Dir("my_file_system"))

// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
}
Expand Down
21 changes: 20 additions & 1 deletion gin_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,14 @@ func testGetRequestHandler(t *testing.T, h http.Handler, url string) {

func TestTreeRunDynamicRouting(t *testing.T) {
router := New()
router.GET("/", func(c *Context) { c.String(http.StatusOK, "home") })
router.GET("/aa/*xx", func(c *Context) { c.String(http.StatusOK, "/aa/*xx") })
router.GET("/ab/aa", func(c *Context) { c.String(http.StatusOK, "/ab/aa") })
router.GET("/ab/*xx", func(c *Context) { c.String(http.StatusOK, "/ab/*xx") })
router.GET("/", func(c *Context) { c.String(http.StatusOK, "home") })
router.GET("/ab/zz", func(c *Context) { c.String(http.StatusOK, "/ab/zz") })
router.GET("/abc/def", func(c *Context) { c.String(http.StatusOK, "/abc/def") })
router.GET("/abc/d/:ee/*all", func(c *Context) { c.String(http.StatusOK, "/abc/d/:ee/*all") })
router.GET("/abc/de/*all", func(c *Context) { c.String(http.StatusOK, "/abc/de/*all") })
router.GET("/:cc", func(c *Context) { c.String(http.StatusOK, "/:cc") })
router.GET("/c1/:dd/e", func(c *Context) { c.String(http.StatusOK, "/c1/:dd/e") })
router.GET("/c1/:dd/e1", func(c *Context) { c.String(http.StatusOK, "/c1/:dd/e1") })
Expand All @@ -429,6 +434,7 @@ func TestTreeRunDynamicRouting(t *testing.T) {
router.GET("/:cc/:dd/:ee/:ff/gg", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/:ee/:ff/gg") })
router.GET("/:cc/:dd/:ee/:ff/:gg/hh", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/:ee/:ff/:gg/hh") })
router.GET("/get/test/abc/", func(c *Context) { c.String(http.StatusOK, "/get/test/abc/") })
router.GET("/get/:param/*all", func(c *Context) { c.String(http.StatusOK, "/get/:param/*all") })
router.GET("/get/:param/abc/", func(c *Context) { c.String(http.StatusOK, "/get/:param/abc/") })
router.GET("/something/:paramname/thirdthing", func(c *Context) { c.String(http.StatusOK, "/something/:paramname/thirdthing") })
router.GET("/something/secondthing/test", func(c *Context) { c.String(http.StatusOK, "/something/secondthing/test") })
Expand Down Expand Up @@ -457,7 +463,13 @@ func TestTreeRunDynamicRouting(t *testing.T) {

testRequest(t, ts.URL+"/", "", "home")
testRequest(t, ts.URL+"/aa/aa", "", "/aa/*xx")
testRequest(t, ts.URL+"/ab/aa", "", "/ab/aa")
testRequest(t, ts.URL+"/ab/zz", "", "/ab/zz")
testRequest(t, ts.URL+"/ab/ab", "", "/ab/*xx")
testRequest(t, ts.URL+"/ab/aab", "", "/ab/*xx")
testRequest(t, ts.URL+"/ab/", "", "/ab/*xx")
testRequest(t, ts.URL+"/abc/d/e", "", "/abc/d/:ee/*all")
testRequest(t, ts.URL+"/abc/d//", "", "/abc/d/:ee/*all")
testRequest(t, ts.URL+"/all", "", "/:cc")
testRequest(t, ts.URL+"/all/cc", "", "/:cc/cc")
testRequest(t, ts.URL+"/a/cc", "", "/:cc/cc")
Expand Down Expand Up @@ -496,6 +508,10 @@ func TestTreeRunDynamicRouting(t *testing.T) {
testRequest(t, ts.URL+"/get/t/abc/", "", "/get/:param/abc/")
testRequest(t, ts.URL+"/get/aa/abc/", "", "/get/:param/abc/")
testRequest(t, ts.URL+"/get/abas/abc/", "", "/get/:param/abc/")
testRequest(t, ts.URL+"/get/testt/abc", "", "/get/:param/abc/")
testRequest(t, ts.URL+"/get/test/", "", "/get/:param")
testRequest(t, ts.URL+"/get/test/abcde/", "", "/get/:param/*all")
testRequest(t, ts.URL+"/get/test//abc", "", "/get/:param/*all")
testRequest(t, ts.URL+"/something/secondthing/test", "", "/something/secondthing/test")
testRequest(t, ts.URL+"/something/secondthingaaaa/thirdthing", "", "/something/:paramname/thirdthing")
testRequest(t, ts.URL+"/something/abcdad/thirdthing", "", "/something/:paramname/thirdthing")
Expand Down Expand Up @@ -547,6 +563,9 @@ func TestTreeRunDynamicRouting(t *testing.T) {
testRequest(t, ts.URL+"/get/abc/123abf/testss", "", "/get/abc/123abf/:param")
testRequest(t, ts.URL+"/get/abc/123abfff/te", "", "/get/abc/123abfff/:param")
// 404 not found
testRequest(t, ts.URL+"/abc/deX", "404 Not Found")
testRequest(t, ts.URL+"/abc/defX", "404 Not Found")
testRequest(t, ts.URL+"/abc/d/", "404 Not Found")
testRequest(t, ts.URL+"/c/d/e", "404 Not Found")
testRequest(t, ts.URL+"/c/d/e1", "404 Not Found")
testRequest(t, ts.URL+"/c/d/eee", "404 Not Found")
Expand Down
Loading