Skip to content

Commit

Permalink
gin.Context.SetParam shortcut for e2e tests (gin-gonic#2848)
Browse files Browse the repository at this point in the history
* Added SetParam shortcut for e2e tests, added SetParam test

* Adjusted naming and formatting

* fixed typo
  • Loading branch information
filikos authored and daheige committed Apr 18, 2022
1 parent 2be1f9d commit 78c3bc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions context.go
Expand Up @@ -383,6 +383,15 @@ func (c *Context) Param(key string) string {
return c.Params.ByName(key)
}

// AddParam adds param to context and
// replaces path param key with given value for e2e testing purposes
// Example Route: "/user/:id"
// AddParam("id", 1)
// Result: "/user/1"
func (c *Context) AddParam(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)`
Expand Down
11 changes: 11 additions & 0 deletions context_test.go
Expand Up @@ -2137,3 +2137,14 @@ func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
})
}
}

func TestContextAddParam(t *testing.T) {
c := &Context{}
id := "id"
value := "1"
c.AddParam(id, value)

v, ok := c.Params.Get(id)
assert.Equal(t, ok, true)
assert.Equal(t, value, v)
}

0 comments on commit 78c3bc4

Please sign in to comment.