MCPcopy
hub / github.com/gin-gonic/gin / TestContextWithFallbackValueFromRequestContext

Function TestContextWithFallbackValueFromRequestContext

context_test.go:3188–3251  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

3186}
3187
3188func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
3189 type contextKey string
3190
3191 tests := []struct {
3192 name string
3193 getContextAndKey func() (*Context, any)
3194 value any
3195 }{
3196 {
3197 name: "c with struct context key",
3198 getContextAndKey: func() (*Context, any) {
3199 type KeyStruct struct{} // https://staticcheck.dev/docs/checks/#SA1029
3200 var key KeyStruct
3201 c, _ := CreateTestContext(httptest.NewRecorder())
3202 // enable ContextWithFallback feature flag
3203 c.engine.ContextWithFallback = true
3204 c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)
3205 c.Request = c.Request.WithContext(context.WithValue(context.TODO(), key, "value"))
3206 return c, key
3207 },
3208 value: "value",
3209 },
3210 {
3211 name: "c with string context key",
3212 getContextAndKey: func() (*Context, any) {
3213 c, _ := CreateTestContext(httptest.NewRecorder())
3214 // enable ContextWithFallback feature flag
3215 c.engine.ContextWithFallback = true
3216 c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)
3217 c.Request = c.Request.WithContext(context.WithValue(context.TODO(), contextKey("key"), "value"))
3218 return c, contextKey("key")
3219 },
3220 value: "value",
3221 },
3222 {
3223 name: "c with nil http.Request",
3224 getContextAndKey: func() (*Context, any) {
3225 c, _ := CreateTestContext(httptest.NewRecorder())
3226 // enable ContextWithFallback feature flag
3227 c.engine.ContextWithFallback = true
3228 c.Request = nil
3229 return c, "key"
3230 },
3231 value: nil,
3232 },
3233 {
3234 name: "c with nil http.Request.Context()",
3235 getContextAndKey: func() (*Context, any) {
3236 c, _ := CreateTestContext(httptest.NewRecorder())
3237 // enable ContextWithFallback feature flag
3238 c.engine.ContextWithFallback = true
3239 c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)
3240 return c, "key"
3241 },
3242 value: nil,
3243 },
3244 }
3245 for _, tt := range tests {

Callers

nothing calls this directly

Calls 4

CreateTestContextFunction · 0.85
contextKeyTypeAlias · 0.85
RunMethod · 0.80
ValueMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…