(t *testing.T)
| 3186 | } |
| 3187 | |
| 3188 | func 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 { |
nothing calls this directly
no test coverage detected
searching dependent graphs…