Skip to content

Commit 5186e18

Browse files
committed
Add unit tests to prevent panicing on missing Request
1 parent 8cd66f7 commit 5186e18

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

context_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,21 @@ func TestContextQuery(t *testing.T) {
410410
assert.Empty(t, c.PostForm("foo"))
411411
}
412412

413+
func TestContextDefaultQueryOnEmptyRequest(t *testing.T) {
414+
c, _ := CreateTestContext(httptest.NewRecorder()) // here c.Request == nil
415+
assert.NotPanics(t, func() {
416+
value, ok := c.GetQuery("NoKey")
417+
assert.False(t, ok)
418+
assert.Empty(t, value)
419+
})
420+
assert.NotPanics(t, func() {
421+
assert.Equal(t, "nada", c.DefaultQuery("NoKey", "nada"))
422+
})
423+
assert.NotPanics(t, func() {
424+
assert.Empty(t, c.Query("NoKey"))
425+
})
426+
}
427+
413428
func TestContextQueryAndPostForm(t *testing.T) {
414429
c, _ := CreateTestContext(httptest.NewRecorder())
415430
body := bytes.NewBufferString("foo=bar&page=11&both=&foo=second")

0 commit comments

Comments
 (0)