Skip to content
Myr

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

2 files+29 added−4 removed

modified:context_test.go+18 added
2174 unmodified lines
2175 assert.Nil(t, ip)2176 assert.False(t, trust)2177}21782179func 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")21902191 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}21962197func TestContextWithFallbackDeadlineFromRequestContext(t *testing.T) {2198 c, _ := CreateTestContext(httptest.NewRecorder())2199 // enable ContextWithFallback feature flag2200 c.engine.ContextWithFallback = true
247 unmodified lines
modified:context.go+11 added-4 removed
1178 unmodified lines
1179/************************************/1180/***** GOLANG.ORG/X/NET/CONTEXT *****/1181/************************************/11821183// hasRequestContext returns whether c.Request has Context and fallback.1184func (c *Context) hasRequestContext() bool {1185 hasFallback := c.engine != nil && c.engine.ContextWithFallback1186 hasRequestContext := c.Request != nil && c.Request.Context() != nil1187 return hasFallback && hasRequestContext1188}11891190// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.1191func (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}11971198// Done returns nil (chan which will wait forever) when c.Request has no Context.1199func (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}12051206// Err returns nil when c.Request has no Context.1207func (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 lines
1225 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}