From 5186e18b8009877e2307c60652fa1865636dbd14 Mon Sep 17 00:00:00 2001 From: Florian Polster Date: Mon, 15 Jun 2020 21:40:02 +0200 Subject: [PATCH] Add unit tests to prevent panicing on missing Request --- context_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/context_test.go b/context_test.go index ce077bc6..bf63bab3 100644 --- a/context_test.go +++ b/context_test.go @@ -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")