Fix - 🐛 The escape of special characters in route path does not work properly #1454 (#1462)

* Fix - 🐛 The escape of special characters in route path does not work properly #1454
pull/1464/head
RW 2021-07-30 20:39:10 +02:00 committed by GitHub
parent d777d889bb
commit 34520e75f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View File

@ -179,7 +179,7 @@ func (app *App) addPrefixToRoute(prefix string, route *Route) *Route {
}
route.Path = prefixedPath
route.path = prettyPath
route.path = RemoveEscapeChar(prettyPath)
route.routeParser = parseRoute(prettyPath)
route.root = false
route.star = false
@ -253,7 +253,7 @@ func (app *App) register(method, pathRaw string, handlers ...Handler) Router {
root: isRoot,
// Path data
path: pathPretty,
path: RemoveEscapeChar(pathPretty),
routeParser: parsedPretty,
Params: parsedRaw.params,

View File

@ -195,6 +195,50 @@ func Test_Route_Match_UnescapedPath(t *testing.T) {
utils.AssertEqual(t, StatusNotFound, resp.StatusCode, "Status code")
}
func Test_Route_Match_WithEscapeChar(t *testing.T) {
app := New()
// static route and escaped part
app.Get("/v1/some/resource/name\\:customVerb", func(c *Ctx) error {
return c.SendString("static")
})
// group route
group := app.Group("/v2/\\:firstVerb")
group.Get("/\\:customVerb", func(c *Ctx) error {
return c.SendString("group")
})
// route with resource param and escaped part
app.Get("/v3/:resource/name\\:customVerb", func(c *Ctx) error {
return c.SendString(c.Params("resource"))
})
// check static route
resp, err := app.Test(httptest.NewRequest(MethodGet, "/v1/some/resource/name:customVerb", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, "static", app.getString(body))
// check group route
resp, err = app.Test(httptest.NewRequest(MethodGet, "/v2/:firstVerb/:customVerb", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, "group", app.getString(body))
// check param route
resp, err = app.Test(httptest.NewRequest(MethodGet, "/v3/awesome/name:customVerb", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, StatusOK, resp.StatusCode, "Status code")
body, err = ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, "awesome", app.getString(body))
}
func Test_Route_Match_Middleware_HasPrefix(t *testing.T) {
app := New()