From dfbc051df93c4aee8ebc1acf63d8349aeefbed04 Mon Sep 17 00:00:00 2001 From: qm012 Date: Sun, 4 Jul 2021 15:12:24 +0800 Subject: [PATCH] add more test and fix multiple params error --- gin_integration_test.go | 72 +++++++++++++++++++++++++++++------------ tree.go | 6 ++-- tree_test.go | 18 +++++++++-- 3 files changed, 71 insertions(+), 25 deletions(-) diff --git a/gin_integration_test.go b/gin_integration_test.go index b8ba6418..9bcc19d4 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -23,7 +23,8 @@ import ( ) // 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) { if len(params) == 0 { @@ -44,12 +45,20 @@ func testRequest(t *testing.T, params ...string) { body, ioerr := ioutil.ReadAll(resp.Body) assert.NoError(t, ioerr) - var expected = "it worked" - if len(params) > 1 { - expected = params[1] + var responseStatus = "200 OK" + if len(params) > 1 && 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) { @@ -393,25 +402,46 @@ func TestRunDynamicRouting(t *testing.T) { router.GET("/", func(c *Context) { c.String(http.StatusOK, "home") }) 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/: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/: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) defer ts.Close() - testRequest(t, ts.URL+"/", "home") - testRequest(t, ts.URL+"/aa/aa", "/aa/*xx") - testRequest(t, ts.URL+"/ab/ab", "/ab/*xx") - testRequest(t, ts.URL+"/all", "/:cc") - testRequest(t, ts.URL+"/all/cc", "/:cc/cc") - testRequest(t, ts.URL+"/a/cc", "/:cc/cc") - testRequest(t, ts.URL+"/a", "/:cc") - testRequest(t, ts.URL+"/get/test/abc/", "/get/test/abc/") - testRequest(t, ts.URL+"/get/te/abc/", "/get/:param/abc/") - testRequest(t, ts.URL+"/get/xx/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+"/", "", "home") + testRequest(t, ts.URL+"/aa/aa", "", "/aa/*xx") + testRequest(t, ts.URL+"/ab/ab", "", "/ab/*xx") + testRequest(t, ts.URL+"/all", "", "/:cc") + testRequest(t, ts.URL+"/all/cc", "", "/:cc/cc") + testRequest(t, ts.URL+"/a/cc", "", "/:cc/cc") + testRequest(t, ts.URL+"/c/d/ee", "", "/:cc/:dd/ee") + testRequest(t, ts.URL+"/c/d/e/ff", "", "/:cc/:dd/:ee/ff") + testRequest(t, ts.URL+"/c/d/e/f/gg", "", "/:cc/:dd/:ee/:ff/gg") + testRequest(t, ts.URL+"/c/d/e/f/g/hh", "", "/:cc/:dd/:ee/:ff/:gg/hh") + testRequest(t, ts.URL+"/a", "", "/:cc") + testRequest(t, ts.URL+"/get/test/abc/", "", "/get/test/abc/") + testRequest(t, ts.URL+"/get/te/abc/", "", "/get/:param/abc/") + testRequest(t, ts.URL+"/get/xx/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") } diff --git a/tree.go b/tree.go index 42c843d5..ad232aef 100644 --- a/tree.go +++ b/tree.go @@ -512,7 +512,9 @@ walk: // Outer loop for walking the tree // call /a/dd 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 + // matchNum: not match,need add matchNum skippedPath = "" + matchNum++ continue walk } @@ -598,7 +600,7 @@ walk: // Outer loop for walking the tree return } - if path != "/" { + if path != "/" && skippedPath != "" { path = skippedPath n = latestNode 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 // extra trailing slash if a leaf exists for that path - value.tsr = true + // tree.go line:569 handle leaf nodes return } } diff --git a/tree_test.go b/tree_test.go index ad8f2146..91213eee 100644 --- a/tree_test.go +++ b/tree_test.go @@ -158,8 +158,14 @@ func TestTreeWildcard(t *testing.T) { "/ab/*xx", "/: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/:param/abc/", + "/something/:paramname/thirdthing", + "/something/secondthing/test", } for _, route := range routes { 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/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"}}}, + {"/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) @@ -587,8 +601,8 @@ func TestTreeFindCaseInsensitivePath(t *testing.T) { "/u/öpfêl", "/v/Äpfêl/", "/v/Öpfêl", - "/w/♬", // 3 byte - "/w/♭/", // 3 byte, last byte differs + "/w/♬", // 3 byte + "/w/♭/", // 3 byte, last byte differs "/w/𠜎", // 4 byte "/w/𠜏/", // 4 byte longPath,