MCPcopy
hub / github.com/gin-gonic/gin / TestRouteParamsByName

Function TestRouteParamsByName

routes_test.go:277–306  ·  view source on GitHub ↗

TestContextParamsGet tests that a parameter can be parsed from the URL.

(t *testing.T)

Source from the content-addressed store, hash-verified

275
276// TestContextParamsGet tests that a parameter can be parsed from the URL.
277func TestRouteParamsByName(t *testing.T) {
278 name := ""
279 lastName := ""
280 wild := ""
281 router := New()
282 router.GET("/test/:name/:last_name/*wild", func(c *Context) {
283 name = c.Params.ByName("name")
284 lastName = c.Params.ByName("last_name")
285 var ok bool
286 wild, ok = c.Params.Get("wild")
287
288 assert.True(t, ok)
289 assert.Equal(t, name, c.Param("name"))
290 assert.Equal(t, lastName, c.Param("last_name"))
291
292 assert.Empty(t, c.Param("wtf"))
293 assert.Empty(t, c.Params.ByName("wtf"))
294
295 wtf, ok := c.Params.Get("wtf")
296 assert.Empty(t, wtf)
297 assert.False(t, ok)
298 })
299
300 w := PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")
301
302 assert.Equal(t, http.StatusOK, w.Code)
303 assert.Equal(t, "john", name)
304 assert.Equal(t, "smith", lastName)
305 assert.Equal(t, "/is/super/great", wild)
306}
307
308// TestContextParamsGet tests that a parameter can be parsed from the URL even with extra slashes.
309func TestRouteParamsByNameWithExtraSlash(t *testing.T) {

Callers

nothing calls this directly

Calls 6

NewFunction · 0.85
PerformRequestFunction · 0.85
ByNameMethod · 0.80
ParamMethod · 0.80
GETMethod · 0.65
GetMethod · 0.45

Tested by

no test coverage detected