forked from mirror/gin
Prevent panic in Context.GetQuery() when there is no Request (#2412)
Co-authored-by: thinkerou <thinkerou@gmail.com>
This commit is contained in:
parent
30b5f7e2d7
commit
815e1ce281
|
@ -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{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -410,6 +410,21 @@ func TestContextQuery(t *testing.T) {
|
|||
assert.Empty(t, c.PostForm("foo"))
|
||||
}
|
||||
|
||||
func TestContextDefaultQueryOnEmptyRequest(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder()) // here c.Request == nil
|
||||
assert.NotPanics(t, func() {
|
||||
value, ok := c.GetQuery("NoKey")
|
||||
assert.False(t, ok)
|
||||
assert.Empty(t, value)
|
||||
})
|
||||
assert.NotPanics(t, func() {
|
||||
assert.Equal(t, "nada", c.DefaultQuery("NoKey", "nada"))
|
||||
})
|
||||
assert.NotPanics(t, func() {
|
||||
assert.Empty(t, c.Query("NoKey"))
|
||||
})
|
||||
}
|
||||
|
||||
func TestContextQueryAndPostForm(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
body := bytes.NewBufferString("foo=bar&page=11&both=&foo=second")
|
||||
|
|
Loading…
Reference in New Issue