Skip to content

Commit deb83b6

Browse files
authored
gin.Context.SetParam shortcut for e2e tests (#2848)
* Added SetParam shortcut for e2e tests, added SetParam test * Adjusted naming and formatting * fixed typo
1 parent 5c62979 commit deb83b6

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,15 @@ func (c *Context) Param(key string) string {
383383
return c.Params.ByName(key)
384384
}
385385

386+
// AddParam adds param to context and
387+
// replaces path param key with given value for e2e testing purposes
388+
// Example Route: "/user/:id"
389+
// AddParam("id", 1)
390+
// Result: "/user/1"
391+
func (c *Context) AddParam(key, value string) {
392+
c.Params = append(c.Params, Param{Key: key, Value: value})
393+
}
394+
386395
// Query returns the keyed url query value if it exists,
387396
// otherwise it returns an empty string `("")`.
388397
// It is shortcut for `c.Request.URL.Query().Get(key)`

context_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,3 +2137,14 @@ func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
21372137
})
21382138
}
21392139
}
2140+
2141+
func TestContextAddParam(t *testing.T) {
2142+
c := &Context{}
2143+
id := "id"
2144+
value := "1"
2145+
c.AddParam(id, value)
2146+
2147+
v, ok := c.Params.Get(id)
2148+
assert.Equal(t, ok, true)
2149+
assert.Equal(t, value, v)
2150+
}

0 commit comments

Comments
 (0)