Skip to content
Myr

fix(context): verify URL is Non-nil in initQueryCache() (#3969)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Adriano Sela Aviles · committed by GitHub · parent 4f339e6 · Browse files · 2 files+44 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+44 added−1 removed

  • other:+44 added−1 removed

2 files+44 added−1 removed

modified:context_test.go+43 added
421 unmodified lines
422 assert.Empty(t, value)423 assert.Empty(t, c.PostForm("foo"))424}425426func TestContextInitQueryCache(t *testing.T) {427 validURL, err := url.Parse("https://github.com/gin-gonic/gin/pull/3969?key=value&otherkey=othervalue")428 assert.Nil(t, err)429430 tests := []struct {431 testName string432 testContext *Context433 expectedQueryCache url.Values434 }{435 {436 testName: "queryCache should remain unchanged if already not nil",437 testContext: &Context{438 queryCache: url.Values{"a": []string{"b"}},439 Request: &http.Request{URL: validURL}, // valid request for evidence that values weren't extracted440 },441 expectedQueryCache: url.Values{"a": []string{"b"}},442 },443 {444 testName: "queryCache should be empty when Request is nil",445 testContext: &Context{Request: nil}, // explicit nil for readability446 expectedQueryCache: url.Values{},447 },448 {449 testName: "queryCache should be empty when Request.URL is nil",450 testContext: &Context{Request: &http.Request{URL: nil}}, // explicit nil for readability451 expectedQueryCache: url.Values{},452 },453 {454 testName: "queryCache should be populated when it not yet populated and Request + Request.URL are non nil",455 testContext: &Context{Request: &http.Request{URL: validURL}}, // explicit nil for readability456 expectedQueryCache: url.Values{"key": []string{"value"}, "otherkey": []string{"othervalue"}},457 },458 }459460 for _, test := range tests {461 t.Run(test.testName, func(t *testing.T) {462 test.testContext.initQueryCache()463 assert.Equal(t, test.expectedQueryCache, test.testContext.queryCache)464 })465 }466467}468469func TestContextDefaultQueryOnEmptyRequest(t *testing.T) {470 c, _ := CreateTestContext(httptest.NewRecorder()) // here c.Request == nil471 assert.NotPanics(t, func() {472 value, ok := c.GetQuery("NoKey")
2420 unmodified lines
modified:context.go+1 added-1 removed
473 unmodified lines
474}475476func (c *Context) initQueryCache() {477 if c.queryCache == nil {478 if c.Request != nil {478 if c.Request != nil && c.Request.URL != nil {479 c.queryCache = c.Request.URL.Query()480 } else {481 c.queryCache = url.Values{}482 }
807 unmodified lines