Skip to content

Commit

Permalink
fix: fix another corner case with incorrect param resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed May 24, 2022
1 parent c22c6d4 commit 7e3d720
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 31 deletions.
12 changes: 5 additions & 7 deletions request.go
Expand Up @@ -136,21 +136,20 @@ func (ps *Params) findParam(paramIndex int) (string, bool) {

// Wildcard can be only in the final node.
if ps.node.isWC {
pathLen -= int(ps.wildcardLen)
if currParamIndex == paramIndex {
pathLen -= int(ps.wildcardLen)
return path[pathLen:], true
}

currParamIndex--
currNode = currNode.parent
path = path[:pathLen-1]
pathLen -= int(ps.wildcardLen)
path = path[:pathLen]
}

for currNode != nil {
if currNode.part[0] != ':' {
if currNode.part[0] != ':' { // static node
pathLen -= len(currNode.part)
path = path[:pathLen]

currNode = currNode.parent
continue
}
Expand All @@ -165,9 +164,8 @@ func (ps *Params) findParam(paramIndex int) (string, bool) {
return path[pathLen:], true
}

path = path[:pathLen]

currParamIndex--
path = path[:pathLen]
currNode = currNode.parent
}

Expand Down
82 changes: 58 additions & 24 deletions router_test.go
Expand Up @@ -355,34 +355,68 @@ func TestWildcardAtSplitNode(t *testing.T) {
}

func TestRouteWithNamedAndWildcardParams(t *testing.T) {
router := New()
t.Run("without static nodes", func(t *testing.T) {
router := New()

var params map[string]string
router.GET("/:id/*path", func(w http.ResponseWriter, req Request) error {
params = req.Params().Map()
return nil
})
var params map[string]string
router.GET("/:id/*path", func(w http.ResponseWriter, req Request) error {
params = req.Params().Map()
return nil
})

t.Run("with path", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/123/hello/world", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, map[string]string{
"id": "123",
"path": "hello/world",
}, params)
t.Run("with path", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/123/hello/world", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, map[string]string{
"id": "123",
"path": "hello/world",
}, params)
})

t.Run("without path", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/123/", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, map[string]string{
"id": "123",
"path": "",
}, params)
})
})

t.Run("without path", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/123/", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, map[string]string{
"id": "123",
"path": "",
}, params)
t.Run("with static nodes", func(t *testing.T) {
router := New()

var params map[string]string
router.GET("/:id/foo/*path", func(w http.ResponseWriter, req Request) error {
params = req.Params().Map()
return nil
})

t.Run("with path", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/123/foo/hello/world", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, map[string]string{
"id": "123",
"path": "hello/world",
}, params)
})

t.Run("without path", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/123/foo/", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, map[string]string{
"id": "123",
"path": "",
}, params)
})
})
}

Expand Down

0 comments on commit 7e3d720

Please sign in to comment.