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

allow escaping of colon in route path #1988

Merged
merged 1 commit into from Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions router.go
Expand Up @@ -98,6 +98,9 @@ func (r *Router) Add(method, path string, h HandlerFunc) {

for i, lcpIndex := 0, len(path); i < lcpIndex; i++ {
if path[i] == ':' {
if i > 0 && path[i-1] == '\\' {
continue
}
j := i + 1

r.insert(method, path[:i], nil, staticKind, "", nil)
Expand Down
52 changes: 52 additions & 0 deletions router_test.go
Expand Up @@ -1118,6 +1118,58 @@ func TestRouterParamStaticConflict(t *testing.T) {
}
}

func TestRouterParam_escapeColon(t *testing.T) {
// to allow Google cloud API like route paths with colon in them
// i.e. https://service.name/v1/some/resource/name:customVerb <- that `:customVerb` is not path param. It is just a string
e := New()

e.POST("/files/a/long/file\\:undelete", handlerFunc)
e.POST("/v1/some/resource/name:customVerb", handlerFunc)

var testCases = []struct {
whenURL string
expectRoute interface{}
expectParam map[string]string
expectError string
}{
{
whenURL: "/files/a/long/file\\:undelete",
expectRoute: "/files/a/long/file\\:undelete",
expectParam: map[string]string{},
},
{
whenURL: "/files/a/long/file\\:notMatching",
expectRoute: nil,
expectError: "code=404, message=Not Found",
expectParam: nil,
},
{
whenURL: "/v1/some/resource/name:PATCH",
expectRoute: "/v1/some/resource/name:customVerb",
expectParam: map[string]string{"customVerb": ":PATCH"},
},
}
for _, tc := range testCases {
t.Run(tc.whenURL, func(t *testing.T) {
c := e.NewContext(nil, nil).(*context)

e.router.Find(http.MethodPost, tc.whenURL, c)
err := c.handler(c)

assert.Equal(t, tc.expectRoute, c.Get("path"))
if tc.expectError != "" {
assert.EqualError(t, err, tc.expectError)
} else {
assert.NoError(t, err)
}
for param, expectedValue := range tc.expectParam {
assert.Equal(t, expectedValue, c.Param(param))
}
checkUnusedParamValues(t, c, tc.expectParam)
})
}
}

func TestRouterMatchAny(t *testing.T) {
e := New()
r := e.router
Expand Down