mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
add test case
This commit is contained in:
parent
ea979c8663
commit
e62a35745b
@ -2059,15 +2059,54 @@ func TestRemoteIPFail(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
|
func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
|
||||||
var key struct{}
|
tests := []struct {
|
||||||
c := &Context{}
|
name string
|
||||||
c.Request, _ = http.NewRequest("POST", "/", nil)
|
getContextAndKey func() (*Context, interface{})
|
||||||
c.Request = c.Request.WithContext(context.WithValue(context.TODO(), key, "value"))
|
value interface{}
|
||||||
|
}{
|
||||||
assert.Equal(t, "value", c.Value(key))
|
{
|
||||||
|
name: "c with struct context key",
|
||||||
c2 := &Context{}
|
getContextAndKey: func() (*Context, interface{}) {
|
||||||
c2.Request, _ = http.NewRequest("POST", "/", nil)
|
var key struct{}
|
||||||
c2.Request = c2.Request.WithContext(context.WithValue(context.TODO(), "key", "value2"))
|
c := &Context{}
|
||||||
assert.Equal(t, "value2", c2.Value("key"))
|
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…
x
Reference in New Issue
Block a user