mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 05:42:09 +08:00
add more test and fix multiple params error
This commit is contained in:
parent
9ec1d25617
commit
dfbc051df9
@ -23,7 +23,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// params[0]=url example:http://127.0.0.1:8080/index (cannot be empty)
|
// params[0]=url example:http://127.0.0.1:8080/index (cannot be empty)
|
||||||
// params[1]=response body (custom compare content)
|
// params[1]=response status (custom compare status) default:"200 OK"
|
||||||
|
// params[2]=response body (custom compare content) default:"it worked"
|
||||||
func testRequest(t *testing.T, params ...string) {
|
func testRequest(t *testing.T, params ...string) {
|
||||||
|
|
||||||
if len(params) == 0 {
|
if len(params) == 0 {
|
||||||
@ -44,12 +45,20 @@ func testRequest(t *testing.T, params ...string) {
|
|||||||
body, ioerr := ioutil.ReadAll(resp.Body)
|
body, ioerr := ioutil.ReadAll(resp.Body)
|
||||||
assert.NoError(t, ioerr)
|
assert.NoError(t, ioerr)
|
||||||
|
|
||||||
var expected = "it worked"
|
var responseStatus = "200 OK"
|
||||||
if len(params) > 1 {
|
if len(params) > 1 && params[1] != "" {
|
||||||
expected = params[1]
|
responseStatus = params[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
var responseBody = "it worked"
|
||||||
|
if len(params) > 2 && params[2] != "" {
|
||||||
|
responseBody = params[2]
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, responseStatus, resp.Status, "should get a "+responseStatus)
|
||||||
|
if responseStatus == "200 OK" {
|
||||||
|
assert.Equal(t, responseBody, string(body), "resp body should match")
|
||||||
}
|
}
|
||||||
assert.Equal(t, expected, string(body), "resp body should match")
|
|
||||||
assert.Equal(t, "200 OK", resp.Status, "should get a 200")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRunEmpty(t *testing.T) {
|
func TestRunEmpty(t *testing.T) {
|
||||||
@ -393,25 +402,46 @@ func TestRunDynamicRouting(t *testing.T) {
|
|||||||
router.GET("/", func(c *Context) { c.String(http.StatusOK, "home") })
|
router.GET("/", func(c *Context) { c.String(http.StatusOK, "home") })
|
||||||
router.GET("/:cc", func(c *Context) { c.String(http.StatusOK, "/:cc") })
|
router.GET("/:cc", func(c *Context) { c.String(http.StatusOK, "/:cc") })
|
||||||
router.GET("/:cc/cc", func(c *Context) { c.String(http.StatusOK, "/:cc/cc") })
|
router.GET("/:cc/cc", func(c *Context) { c.String(http.StatusOK, "/:cc/cc") })
|
||||||
|
|
||||||
|
router.GET("/:cc/:dd/ee", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/ee") })
|
||||||
|
router.GET("/:cc/:dd/:ee/ff", func(c *Context) { c.String(http.StatusOK, "/:cc/:dd/:ee/ff") })
|
||||||
|
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/test/abc/", func(c *Context) { c.String(http.StatusOK, "/get/test/abc/") })
|
||||||
router.GET("/get/:param/abc/", func(c *Context) { c.String(http.StatusOK, "/get/:param/abc/") })
|
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") })
|
||||||
|
|
||||||
ts := httptest.NewServer(router)
|
ts := httptest.NewServer(router)
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
testRequest(t, ts.URL+"/", "home")
|
testRequest(t, ts.URL+"/", "", "home")
|
||||||
testRequest(t, ts.URL+"/aa/aa", "/aa/*xx")
|
testRequest(t, ts.URL+"/aa/aa", "", "/aa/*xx")
|
||||||
testRequest(t, ts.URL+"/ab/ab", "/ab/*xx")
|
testRequest(t, ts.URL+"/ab/ab", "", "/ab/*xx")
|
||||||
testRequest(t, ts.URL+"/all", "/:cc")
|
testRequest(t, ts.URL+"/all", "", "/:cc")
|
||||||
testRequest(t, ts.URL+"/all/cc", "/:cc/cc")
|
testRequest(t, ts.URL+"/all/cc", "", "/:cc/cc")
|
||||||
testRequest(t, ts.URL+"/a/cc", "/:cc/cc")
|
testRequest(t, ts.URL+"/a/cc", "", "/:cc/cc")
|
||||||
testRequest(t, ts.URL+"/a", "/:cc")
|
testRequest(t, ts.URL+"/c/d/ee", "", "/:cc/:dd/ee")
|
||||||
testRequest(t, ts.URL+"/get/test/abc/", "/get/test/abc/")
|
testRequest(t, ts.URL+"/c/d/e/ff", "", "/:cc/:dd/:ee/ff")
|
||||||
testRequest(t, ts.URL+"/get/te/abc/", "/get/:param/abc/")
|
testRequest(t, ts.URL+"/c/d/e/f/gg", "", "/:cc/:dd/:ee/:ff/gg")
|
||||||
testRequest(t, ts.URL+"/get/xx/abc/", "/get/:param/abc/")
|
testRequest(t, ts.URL+"/c/d/e/f/g/hh", "", "/:cc/:dd/:ee/:ff/:gg/hh")
|
||||||
testRequest(t, ts.URL+"/get/tt/abc/", "/get/:param/abc/")
|
testRequest(t, ts.URL+"/a", "", "/:cc")
|
||||||
testRequest(t, ts.URL+"/get/a/abc/", "/get/:param/abc/")
|
testRequest(t, ts.URL+"/get/test/abc/", "", "/get/test/abc/")
|
||||||
testRequest(t, ts.URL+"/get/t/abc/", "/get/:param/abc/")
|
testRequest(t, ts.URL+"/get/te/abc/", "", "/get/:param/abc/")
|
||||||
testRequest(t, ts.URL+"/get/aa/abc/", "/get/:param/abc/")
|
testRequest(t, ts.URL+"/get/xx/abc/", "", "/get/:param/abc/")
|
||||||
testRequest(t, ts.URL+"/get/abas/abc/", "/get/:param/abc/")
|
testRequest(t, ts.URL+"/get/tt/abc/", "", "/get/:param/abc/")
|
||||||
|
testRequest(t, ts.URL+"/get/a/abc/", "", "/get/:param/abc/")
|
||||||
|
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+"/something/secondthing/test", "", "/something/secondthing/test")
|
||||||
|
testRequest(t, ts.URL+"/something/abcdad/thirdthing", "", "/something/:paramname/thirdthing")
|
||||||
|
testRequest(t, ts.URL+"/something/se/thirdthing", "", "/something/:paramname/thirdthing")
|
||||||
|
testRequest(t, ts.URL+"/something/s/thirdthing", "", "/something/:paramname/thirdthing")
|
||||||
|
testRequest(t, ts.URL+"/something/secondthing/thirdthing", "", "/something/:paramname/thirdthing")
|
||||||
|
// 404 not found
|
||||||
|
testRequest(t, ts.URL+"/a/dd", "404 Not Found")
|
||||||
|
testRequest(t, ts.URL+"/addr/dd/aa", "404 Not Found")
|
||||||
|
testRequest(t, ts.URL+"/something/secondthing/121", "404 Not Found")
|
||||||
}
|
}
|
||||||
|
6
tree.go
6
tree.go
@ -512,7 +512,9 @@ walk: // Outer loop for walking the tree
|
|||||||
// call /a/dd expectations:unmatch/404 Actual: panic
|
// call /a/dd expectations:unmatch/404 Actual: panic
|
||||||
// call /addr/dd/aa expectations:unmatch/404 Actual: panic
|
// call /addr/dd/aa expectations:unmatch/404 Actual: panic
|
||||||
// skippedPath: It can only be executed if the secondary route is not found
|
// skippedPath: It can only be executed if the secondary route is not found
|
||||||
|
// matchNum: not match,need add matchNum
|
||||||
skippedPath = ""
|
skippedPath = ""
|
||||||
|
matchNum++
|
||||||
continue walk
|
continue walk
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -598,7 +600,7 @@ walk: // Outer loop for walking the tree
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if path != "/" {
|
if path != "/" && skippedPath != "" {
|
||||||
path = skippedPath
|
path = skippedPath
|
||||||
n = latestNode
|
n = latestNode
|
||||||
skippedPath = ""
|
skippedPath = ""
|
||||||
@ -607,7 +609,7 @@ walk: // Outer loop for walking the tree
|
|||||||
|
|
||||||
// Nothing found. We can recommend to redirect to the same URL with an
|
// Nothing found. We can recommend to redirect to the same URL with an
|
||||||
// extra trailing slash if a leaf exists for that path
|
// extra trailing slash if a leaf exists for that path
|
||||||
value.tsr = true
|
// tree.go line:569 handle leaf nodes
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
18
tree_test.go
18
tree_test.go
@ -158,8 +158,14 @@ func TestTreeWildcard(t *testing.T) {
|
|||||||
"/ab/*xx",
|
"/ab/*xx",
|
||||||
"/:cc",
|
"/:cc",
|
||||||
"/:cc/cc",
|
"/:cc/cc",
|
||||||
|
"/:cc/:dd/ee",
|
||||||
|
"/:cc/:dd/:ee/ff",
|
||||||
|
"/:cc/:dd/:ee/:ff/gg",
|
||||||
|
"/:cc/:dd/:ee/:ff/:gg/hh",
|
||||||
"/get/test/abc/",
|
"/get/test/abc/",
|
||||||
"/get/:param/abc/",
|
"/get/:param/abc/",
|
||||||
|
"/something/:paramname/thirdthing",
|
||||||
|
"/something/secondthing/test",
|
||||||
}
|
}
|
||||||
for _, route := range routes {
|
for _, route := range routes {
|
||||||
tree.addRoute(route, fakeHandler(route))
|
tree.addRoute(route, fakeHandler(route))
|
||||||
@ -208,6 +214,14 @@ func TestTreeWildcard(t *testing.T) {
|
|||||||
{"/get/t/abc/", false, "/get/:param/abc/", Params{Param{Key: "param", Value: "t"}}},
|
{"/get/t/abc/", false, "/get/:param/abc/", Params{Param{Key: "param", Value: "t"}}},
|
||||||
{"/get/aa/abc/", false, "/get/:param/abc/", Params{Param{Key: "param", Value: "aa"}}},
|
{"/get/aa/abc/", false, "/get/:param/abc/", Params{Param{Key: "param", Value: "aa"}}},
|
||||||
{"/get/abas/abc/", false, "/get/:param/abc/", Params{Param{Key: "param", Value: "abas"}}},
|
{"/get/abas/abc/", false, "/get/:param/abc/", Params{Param{Key: "param", Value: "abas"}}},
|
||||||
|
{"/something/secondthing/test", false, "/something/secondthing/test", nil},
|
||||||
|
{"/something/abcdad/thirdthing", false, "/something/:paramname/thirdthing", Params{Param{Key: "paramname", Value: "abcdad"}}},
|
||||||
|
{"/something/se/thirdthing", false, "/something/:paramname/thirdthing", Params{Param{Key: "paramname", Value: "se"}}},
|
||||||
|
{"/something/s/thirdthing", false, "/something/:paramname/thirdthing", Params{Param{Key: "paramname", Value: "s"}}},
|
||||||
|
{"/c/d/ee", false, "/:cc/:dd/ee", Params{Param{Key: "cc", Value: "c"}, Param{Key: "dd", Value: "d"}}},
|
||||||
|
{"/c/d/e/ff", false, "/:cc/:dd/:ee/ff", Params{Param{Key: "cc", Value: "c"}, Param{Key: "dd", Value: "d"}, Param{Key: "ee", Value: "e"}}},
|
||||||
|
{"/c/d/e/f/gg", false, "/:cc/:dd/:ee/:ff/gg", Params{Param{Key: "cc", Value: "c"}, Param{Key: "dd", Value: "d"}, Param{Key: "ee", Value: "e"}, Param{Key: "ff", Value: "f"}}},
|
||||||
|
{"/c/d/e/f/g/hh", false, "/:cc/:dd/:ee/:ff/:gg/hh", Params{Param{Key: "cc", Value: "c"}, Param{Key: "dd", Value: "d"}, Param{Key: "ee", Value: "e"}, Param{Key: "ff", Value: "f"}, Param{Key: "gg", Value: "g"}}},
|
||||||
})
|
})
|
||||||
|
|
||||||
checkPriorities(t, tree)
|
checkPriorities(t, tree)
|
||||||
@ -587,8 +601,8 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) {
|
|||||||
"/u/öpfêl",
|
"/u/öpfêl",
|
||||||
"/v/Äpfêl/",
|
"/v/Äpfêl/",
|
||||||
"/v/Öpfêl",
|
"/v/Öpfêl",
|
||||||
"/w/♬", // 3 byte
|
"/w/♬", // 3 byte
|
||||||
"/w/♭/", // 3 byte, last byte differs
|
"/w/♭/", // 3 byte, last byte differs
|
||||||
"/w/𠜎", // 4 byte
|
"/w/𠜎", // 4 byte
|
||||||
"/w/𠜏/", // 4 byte
|
"/w/𠜏/", // 4 byte
|
||||||
longPath,
|
longPath,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user