mirror of https://github.com/gin-gonic/gin.git
fix Request.Context() checks
This commit is contained in:
parent
d4a64265f2
commit
14247a51e5
16
context.go
16
context.go
|
@ -1181,15 +1181,17 @@ func (c *Context) SetAccepted(formats ...string) {
|
||||||
/************************************/
|
/************************************/
|
||||||
|
|
||||||
// hasRequestContext returns whether c.Request has Context and fallback.
|
// hasRequestContext returns whether c.Request has Context and fallback.
|
||||||
|
func (c *Context) hasFallback() bool {
|
||||||
|
return c.engine != nil && c.engine.ContextWithFallback
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Context) hasRequestContext() bool {
|
func (c *Context) hasRequestContext() bool {
|
||||||
hasFallback := c.engine != nil && c.engine.ContextWithFallback
|
return c.Request != nil && c.Request.Context() != nil
|
||||||
hasRequestContext := c.Request != nil && c.Request.Context() != nil
|
|
||||||
return hasFallback && hasRequestContext
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
|
// Deadline returns that there is no deadline (ok==false) when c.Request has no Context.
|
||||||
func (c *Context) Deadline() (deadline time.Time, ok bool) {
|
func (c *Context) Deadline() (deadline time.Time, ok bool) {
|
||||||
if !c.hasRequestContext() {
|
if !c.hasFallback() || !c.hasRequestContext() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return c.Request.Context().Deadline()
|
return c.Request.Context().Deadline()
|
||||||
|
@ -1197,7 +1199,7 @@ func (c *Context) Deadline() (deadline time.Time, ok bool) {
|
||||||
|
|
||||||
// Done returns nil (chan which will wait forever) when c.Request has no Context.
|
// Done returns nil (chan which will wait forever) when c.Request has no Context.
|
||||||
func (c *Context) Done() <-chan struct{} {
|
func (c *Context) Done() <-chan struct{} {
|
||||||
if !c.hasRequestContext() {
|
if !c.hasFallback() || !c.hasRequestContext() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return c.Request.Context().Done()
|
return c.Request.Context().Done()
|
||||||
|
@ -1205,7 +1207,7 @@ func (c *Context) Done() <-chan struct{} {
|
||||||
|
|
||||||
// Err returns nil when c.Request has no Context.
|
// Err returns nil when c.Request has no Context.
|
||||||
func (c *Context) Err() error {
|
func (c *Context) Err() error {
|
||||||
if !c.hasRequestContext() {
|
if !c.hasFallback() || !c.hasRequestContext() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return c.Request.Context().Err()
|
return c.Request.Context().Err()
|
||||||
|
@ -1226,7 +1228,7 @@ func (c *Context) Value(key any) any {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !c.hasRequestContext() {
|
if !c.hasFallback() || !c.hasRequestContext() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return c.Request.Context().Value(key)
|
return c.Request.Context().Value(key)
|
||||||
|
|
|
@ -2178,20 +2178,38 @@ func TestRemoteIPFail(t *testing.T) {
|
||||||
|
|
||||||
func TestHasRequestContext(t *testing.T) {
|
func TestHasRequestContext(t *testing.T) {
|
||||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
assert.False(t, c.hasRequestContext(), "no request, no fallback")
|
assert.False(t, c.hasRequestContext())
|
||||||
c.engine.ContextWithFallback = true
|
c.engine.ContextWithFallback = true
|
||||||
assert.False(t, c.hasRequestContext(), "no request, has fallback")
|
assert.False(t, c.hasRequestContext())
|
||||||
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
|
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
|
||||||
assert.True(t, c.hasRequestContext(), "has request, has fallback")
|
assert.True(t, c.hasRequestContext())
|
||||||
c.Request, _ = http.NewRequestWithContext(nil, "", "", nil) //nolint:staticcheck
|
c.Request, _ = http.NewRequestWithContext(nil, "", "", nil) //nolint:staticcheck
|
||||||
assert.False(t, c.hasRequestContext(), "has request with nil ctx, has fallback")
|
assert.False(t, c.hasRequestContext())
|
||||||
c.engine.ContextWithFallback = false
|
c.engine.ContextWithFallback = false
|
||||||
assert.False(t, c.hasRequestContext(), "has request, no fallback")
|
assert.False(t, c.hasRequestContext())
|
||||||
|
|
||||||
c = &Context{}
|
c = &Context{}
|
||||||
assert.False(t, c.hasRequestContext(), "no request, no engine")
|
assert.False(t, c.hasRequestContext())
|
||||||
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
|
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
|
||||||
assert.False(t, c.hasRequestContext(), "has request, no engine")
|
assert.True(t, c.hasRequestContext())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHasFallback(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
assert.False(t, c.hasFallback())
|
||||||
|
c.engine.ContextWithFallback = true
|
||||||
|
assert.True(t, c.hasFallback())
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
assert.True(t, c.hasFallback())
|
||||||
|
c.Request, _ = http.NewRequestWithContext(nil, "", "", nil) //nolint:staticcheck
|
||||||
|
assert.True(t, c.hasFallback())
|
||||||
|
c.engine.ContextWithFallback = false
|
||||||
|
assert.False(t, c.hasFallback())
|
||||||
|
|
||||||
|
c = &Context{}
|
||||||
|
assert.False(t, c.hasFallback())
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
assert.False(t, c.hasFallback())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextWithFallbackDeadlineFromRequestContext(t *testing.T) {
|
func TestContextWithFallbackDeadlineFromRequestContext(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue