fix Request.Context() checks (#3512)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Bence Vidosits · committed by GitHub · parent 2d4bbec · Browse files · 2 files·+29 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Bence Vidosits <bence.vidosits1@ibm.com>
Diff stats
2 files+29 added−4 removed
- other:+29 added−4 removed
Provenance
- Commit
bb1fc2e- Tree
7161688- Author
Bence Vidosits <38434845+bvidosits@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
2d4bbec
2 files changed+29 added−4 removed
modified:context_test.go+18 added
2174 unmodified lines2175 assert.Nil(t, ip)2176 assert.False(t, trust)2177 }2178 2179+func TestHasRequestContext(t *testing.T) {2180+ c, _ := CreateTestContext(httptest.NewRecorder())2181+ assert.False(t, c.hasRequestContext(), "no request, no fallback")2182+ c.engine.ContextWithFallback = true2183+ assert.False(t, c.hasRequestContext(), "no request, has fallback")2184+ c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)2185+ assert.True(t, c.hasRequestContext(), "has request, has fallback")2186+ c.Request, _ = http.NewRequestWithContext(nil, "", "", nil) //nolint:staticcheck2187+ assert.False(t, c.hasRequestContext(), "has request with nil ctx, has fallback")2188+ c.engine.ContextWithFallback = false2189+ assert.False(t, c.hasRequestContext(), "has request, no fallback")2190+2191+ c = &Context{}2192+ assert.False(t, c.hasRequestContext(), "no request, no engine")2193+ c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)2194+ assert.False(t, c.hasRequestContext(), "has request, no engine")2195+}2196+2197 func TestContextWithFallbackDeadlineFromRequestContext(t *testing.T) {2198 c, _ := CreateTestContext(httptest.NewRecorder())2199 // enable ContextWithFallback feature flag2200 c.engine.ContextWithFallback = true247 unmodified linesmodified:context.go+11 added-4 removed
1178 unmodified lines1179 /************************************/1180 /***** GOLANG.ORG/X/NET/CONTEXT *****/1181 /************************************/1182 1183+// hasRequestContext returns whether c.Request has Context and fallback.1184+func (c *Context) hasRequestContext() bool {1185+ hasFallback := c.engine != nil && c.engine.ContextWithFallback1186+ hasRequestContext := c.Request != nil && c.Request.Context() != nil1187+ return hasFallback && hasRequestContext1188+}1189+1190 // Deadline returns that there is no deadline (ok==false) when c.Request has no Context.1191 func (c *Context) Deadline() (deadline time.Time, ok bool) {1185- if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {1192+ if !c.hasRequestContext() {1193 return1194 }1195 return c.Request.Context().Deadline()1196 }1197 1198 // Done returns nil (chan which will wait forever) when c.Request has no Context.1199 func (c *Context) Done() <-chan struct{} {1193- if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {1200+ if !c.hasRequestContext() {1201 return nil1202 }1203 return c.Request.Context().Done()1204 }1205 1206 // Err returns nil when c.Request has no Context.1207 func (c *Context) Err() error {1201- if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {1208+ if !c.hasRequestContext() {1209 return nil1210 }1211 return c.Request.Context().Err()1212 }12 unmodified lines1225 if val, exists := c.Get(keyAsString); exists {1226 return val1227 }1228 }1222- if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {1229+ if !c.hasRequestContext() {1230 return nil1231 }1232 return c.Request.Context().Value(key)1233 }