Skip to content

Commit

Permalink
Prevent panic in Context.GetQuery() when there is no Request
Browse files Browse the repository at this point in the history
I have an endpoint that uses an optional query parameter via DefaultQuery(). 
In one unit test case I want to test the route with that parameter.
I write that test as
```go
	w := httptest.NewRecorder()
	c, _ := gin.CreateTestContext(w)
	routeHandler(c)
```
And this panics when routeHandler() calls c.DefaultQuery() because c.Request == nil.
  • Loading branch information
pofl committed Jun 16, 2020
1 parent 5e40c1d commit 8cd66f7
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion context.go
Expand Up @@ -416,7 +416,11 @@ func (c *Context) QueryArray(key string) []string {

func (c *Context) initQueryCache() {
if c.queryCache == nil {
c.queryCache = c.Request.URL.Query()
if c.Request != nil {
c.queryCache = c.Request.URL.Query()
} else {
c.queryCache = url.Values{}
}
}
}

Expand Down

0 comments on commit 8cd66f7

Please sign in to comment.