Skip to content

Commit

Permalink
add comment and update test method
Browse files Browse the repository at this point in the history
gin_integration_test.go#L407
  • Loading branch information
qm012 committed Jun 29, 2021
1 parent b81f257 commit 4e8e2cb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
58 changes: 23 additions & 35 deletions gin_integration_test.go
Expand Up @@ -15,7 +15,6 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -389,39 +388,28 @@ func testGetRequestHandler(t *testing.T, h http.Handler, url string) {

func TestRunDynamicRouting(t *testing.T) {
router := New()
go func() {
router.GET("/aa/*xx", func(c *Context) { c.String(http.StatusOK, "/aa/*xx") })
router.GET("/ab/*xx", func(c *Context) { c.String(http.StatusOK, "/ab/*xx") })
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("/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/") })
}()
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err.Error())
}
go func() {
err := router.RunListener(listener)
if err != nil {
t.Fatal(err.Error())
}
}()
router.GET("/aa/*xx", func(c *Context) { c.String(http.StatusOK, "/aa/*xx") })
router.GET("/ab/*xx", func(c *Context) { c.String(http.StatusOK, "/ab/*xx") })
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("/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/") })

ts := httptest.NewServer(router)
defer ts.Close()

addr := listener.Addr().String()
addr = addr[strings.LastIndex(addr, ":"):]
testRequest(t, "http://localhost"+addr+"/aa/aa", "/aa/*xx")
testRequest(t, "http://localhost"+addr+"/ab/ab", "/ab/*xx")
testRequest(t, "http://localhost"+addr+"/all", "/:cc")
testRequest(t, "http://localhost"+addr+"/all/cc", "/:cc/cc")
testRequest(t, "http://localhost"+addr+"/a/cc", "/:cc/cc")
testRequest(t, "http://localhost"+addr+"/a", "/:cc")
testRequest(t, "http://localhost"+addr+"/get/test/abc/", "/get/test/abc/")
testRequest(t, "http://localhost"+addr+"/get/te/abc/", "/get/:param/abc/")
testRequest(t, "http://localhost"+addr+"/get/xx/abc/", "/get/:param/abc/")
testRequest(t, "http://localhost"+addr+"/get/tt/abc/", "/get/:param/abc/")
testRequest(t, "http://localhost"+addr+"/get/a/abc/", "/get/:param/abc/")
testRequest(t, "http://localhost"+addr+"/get/t/abc/", "/get/:param/abc/")
testRequest(t, "http://localhost"+addr+"/get/aa/abc/", "/get/:param/abc/")
testRequest(t, "http://localhost"+addr+"/get/abas/abc/", "/get/:param/abc/")
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/")
}
4 changes: 1 addition & 3 deletions tree.go
Expand Up @@ -429,19 +429,17 @@ walk: // Outer loop for walking the tree
for {
prefix := n.path

// match '/'
// match '/', If this condition is matched, the next route is found
if strings.HasSuffix(n.path, "/") || strings.Contains(n.fullPath, ":") || n.path == "" {
matchNum++
}

if len(path) > len(prefix) {

if path[:len(prefix)] == prefix {
path = path[len(prefix):]

// Try all the non-wildcard children first by matching the indices
idxc := path[0]

for i, c := range []byte(n.indices) {
if c == idxc {
if strings.HasPrefix(n.children[len(n.children)-1].path, ":") {
Expand Down

0 comments on commit 4e8e2cb

Please sign in to comment.