MCPcopy
hub / github.com/labstack/echo / TestDefaultRouter_UseEscapedPathForRouting

Function TestDefaultRouter_UseEscapedPathForRouting

router_test.go:3211–3278  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3209}
3210
3211func TestDefaultRouter_UseEscapedPathForRouting(t *testing.T) {
3212 var testCases = []struct {
3213 name string
3214 whenRawPath string
3215 expectBody string
3216 expectStatus int
3217 givenDoNotUseEscapedPathForRouting bool
3218 }{
3219 {
3220 name: "ok, static route",
3221 whenRawPath: "/what's%20up",
3222 expectBody: "/what's up",
3223 expectStatus: http.StatusTeapot,
3224 },
3225 {
3226 name: "nok, rawPath does not match with route path",
3227 givenDoNotUseEscapedPathForRouting: true,
3228 whenRawPath: "/what's%20up", // route is "/what's up"
3229 expectBody: "{\"message\":\"Not Found\"}\n",
3230 expectStatus: http.StatusNotFound,
3231 },
3232 {
3233 name: "ok, match path param",
3234 whenRawPath: "/test/what's%20up",
3235 expectBody: "/test/:param|what's up",
3236 expectStatus: http.StatusTeapot,
3237 },
3238 {
3239 name: "ok, path param is unescaped as it is in rawPath",
3240 givenDoNotUseEscapedPathForRouting: true,
3241 whenRawPath: "/test/what's%20up",
3242 expectBody: "/test/:param|what's%20up",
3243 expectStatus: http.StatusTeapot,
3244 },
3245 }
3246
3247 for _, tc := range testCases {
3248 t.Run(tc.name, func(t *testing.T) {
3249 e := New()
3250 router := NewRouter(RouterConfig{UseEscapedPathForMatching: !tc.givenDoNotUseEscapedPathForRouting})
3251 e.router = router
3252 e.contextPathParamAllocSize.Store(1)
3253
3254 ri, err := router.Add(Route{
3255 Method: http.MethodGet,
3256 Path: "/what's up",
3257 Handler: func(c *Context) error {
3258 return c.String(http.StatusTeapot, c.RouteInfo().Path)
3259 },
3260 })
3261 assert.NoError(t, err)
3262 assert.NotNil(t, ri)
3263 ri, err = router.Add(Route{
3264 Method: http.MethodGet,
3265 Path: "/test/:param",
3266 Handler: func(c *Context) error {
3267 return c.String(http.StatusTeapot, c.RouteInfo().Path+"|"+c.Param("param"))
3268 },

Callers

nothing calls this directly

Calls 7

AddMethod · 0.95
NewFunction · 0.85
NewRouterFunction · 0.85
requestFunction · 0.85
RouteInfoMethod · 0.80
ParamMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…