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) {
|
||||
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"))
|
||||
|
||||
assert.Equal(t, "value", c.Value(key))
|
||||
|
||||
c2 := &Context{}
|
||||
c2.Request, _ = http.NewRequest("POST", "/", nil)
|
||||
c2.Request = c2.Request.WithContext(context.WithValue(context.TODO(), "key", "value2"))
|
||||
assert.Equal(t, "value2", c2.Value("key"))
|
||||
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