Prevent panic in Context.GetQuery() when there is no Request

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.
This commit is contained in:
Florian Polster 2020-06-15 13:35:25 +02:00 committed by Florian Polster
parent 5e40c1d49c
commit 8cd66f7fa8

View File

@ -416,7 +416,11 @@ func (c *Context) QueryArray(key string) []string {
func (c *Context) initQueryCache() {
if c.queryCache == nil {
if c.Request != nil {
c.queryCache = c.Request.URL.Query()
} else {
c.queryCache = url.Values{}
}
}
}