From 8cd66f7fa8939e4a47eb99132374e610e29cd338 Mon Sep 17 00:00:00 2001 From: Florian Polster Date: Mon, 15 Jun 2020 13:35:25 +0200 Subject: [PATCH] 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. --- context.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index a9458833ba..95b1807d72 100644 --- a/context.go +++ b/context.go @@ -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{} + } } }