mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-18 23:12:17 +08:00
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:
parent
5e40c1d49c
commit
8cd66f7fa8
@ -416,7 +416,11 @@ func (c *Context) QueryArray(key string) []string {
|
|||||||
|
|
||||||
func (c *Context) initQueryCache() {
|
func (c *Context) initQueryCache() {
|
||||||
if c.queryCache == nil {
|
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{}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user