mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-17 05:42:09 +08:00
Added SetParam shortcut for e2e tests, added SetParam test
This commit is contained in:
parent
4e7584175d
commit
100d5889b6
@ -383,6 +383,15 @@ func (c *Context) Param(key string) string {
|
||||
return c.Params.ByName(key)
|
||||
}
|
||||
|
||||
// SetParam adds param to context an
|
||||
// replaces path param key with given value for e2e testing purposes
|
||||
// Example Route: "/user/:id"
|
||||
// SetParam("id", 1)
|
||||
// Result: "/user/1"
|
||||
func (c *Context) SetParam(key, value string) {
|
||||
c.Params = append(c.Params, Param{Key: key,Value: value})
|
||||
}
|
||||
|
||||
// Query returns the keyed url query value if it exists,
|
||||
// otherwise it returns an empty string `("")`.
|
||||
// It is shortcut for `c.Request.URL.Query().Get(key)`
|
||||
|
@ -2150,3 +2150,14 @@ func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestContextSetParam(t *testing.T) {
|
||||
c := &Context{}
|
||||
id := "id"
|
||||
value := "1"
|
||||
c.SetParam(id, value)
|
||||
|
||||
v, ok := c.Params.Get(id)
|
||||
assert.Equal(t, ok, true)
|
||||
assert.Equal(t, value, v)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user