mirror of https://github.com/gin-gonic/gin.git
gin.Context with fallback value from gin.Context.Request.Context() (#2751)
* Update tree.go (#2659) delete more "()" * updated comments for Get function for params (#2756) * ci: add github action workflows (#2596) * ci: add github action workflows * test: fixed the TestUnixSocket test on windows (#20) * ci: add github action workflows (#18) * Remove .travis.yml * ci: replace GITTER_ROOM_ID and upload coverage every time you go test * ci: update coverage using codecov/codecov-action@v1 * Merge branch 'master' into github-actions * repo: replace travis ci to github actions * ci: add go version 1.16 * fix: go install requires a specific version * chore(ci): remove go 1.12 support * chore(ci): remove os windows-latest Co-authored-by: thinkerou <thinkerou@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> * Setting trusted platform using an enum-like (#2739) * gin.Context with fallback value from c.Request.Context() * add test case Co-authored-by: youzeliang <youzel@126.com> Co-authored-by: Ashwani <ashwanisharma686@gmail.com> Co-authored-by: Jeff <laojianzi1994@gmail.com> Co-authored-by: thinkerou <thinkerou@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-authored-by: Alessandro (Ale) Segala <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
parent
09f6cff92a
commit
7834a03e84
|
@ -1186,8 +1186,12 @@ func (c *Context) Value(key interface{}) interface{} {
|
||||||
return c.Request
|
return c.Request
|
||||||
}
|
}
|
||||||
if keyAsString, ok := key.(string); ok {
|
if keyAsString, ok := key.(string); ok {
|
||||||
val, _ := c.Get(keyAsString)
|
if val, exists := c.Get(keyAsString); exists {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if c.Request == nil || c.Request.Context() == nil {
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
return c.Request.Context().Value(key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2057,3 +2057,56 @@ func TestRemoteIPFail(t *testing.T) {
|
||||||
assert.Nil(t, ip)
|
assert.Nil(t, ip)
|
||||||
assert.False(t, trust)
|
assert.False(t, trust)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
getContextAndKey func() (*Context, interface{})
|
||||||
|
value interface{}
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "c with struct context key",
|
||||||
|
getContextAndKey: func() (*Context, interface{}) {
|
||||||
|
var key struct{}
|
||||||
|
c := &Context{}
|
||||||
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||||
|
c.Request = c.Request.WithContext(context.WithValue(context.TODO(), key, "value"))
|
||||||
|
return c, key
|
||||||
|
},
|
||||||
|
value: "value",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "c with string context key",
|
||||||
|
getContextAndKey: func() (*Context, interface{}) {
|
||||||
|
c := &Context{}
|
||||||
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||||
|
c.Request = c.Request.WithContext(context.WithValue(context.TODO(), "key", "value"))
|
||||||
|
return c, "key"
|
||||||
|
},
|
||||||
|
value: "value",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "c with nil http.Request",
|
||||||
|
getContextAndKey: func() (*Context, interface{}) {
|
||||||
|
c := &Context{}
|
||||||
|
return c, "key"
|
||||||
|
},
|
||||||
|
value: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "c with nil http.Request.Context()",
|
||||||
|
getContextAndKey: func() (*Context, interface{}) {
|
||||||
|
c := &Context{}
|
||||||
|
c.Request, _ = http.NewRequest("POST", "/", nil)
|
||||||
|
return c, "key"
|
||||||
|
},
|
||||||
|
value: nil,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
c, key := tt.getContextAndKey()
|
||||||
|
assert.Equal(t, tt.value, c.Value(key))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue