Home / Function/ TestContextWithFallbackValueFromRequestContext() — gin Function Reference

TestContextWithFallbackValueFromRequestContext() — gin Function Reference

Architecture documentation for the TestContextWithFallbackValueFromRequestContext() function in context_test.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  dd134ff6_c092_ed90_0d4e_ad8ec890844a["TestContextWithFallbackValueFromRequestContext()"]
  ebe0ae48_a62b_a38f_5bac_5bbbd96fc508["context_test.go"]
  dd134ff6_c092_ed90_0d4e_ad8ec890844a -->|defined in| ebe0ae48_a62b_a38f_5bac_5bbbd96fc508
  style dd134ff6_c092_ed90_0d4e_ad8ec890844a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

context_test.go lines 3177–3240

func TestContextWithFallbackValueFromRequestContext(t *testing.T) {
	type contextKey string

	tests := []struct {
		name             string
		getContextAndKey func() (*Context, any)
		value            any
	}{
		{
			name: "c with struct context key",
			getContextAndKey: func() (*Context, any) {
				type KeyStruct struct{} // https://staticcheck.dev/docs/checks/#SA1029
				var key KeyStruct
				c, _ := CreateTestContext(httptest.NewRecorder())
				// enable ContextWithFallback feature flag
				c.engine.ContextWithFallback = true
				c.Request, _ = http.NewRequest(http.MethodPost, "/", 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, any) {
				c, _ := CreateTestContext(httptest.NewRecorder())
				// enable ContextWithFallback feature flag
				c.engine.ContextWithFallback = true
				c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)
				c.Request = c.Request.WithContext(context.WithValue(context.TODO(), contextKey("key"), "value"))
				return c, contextKey("key")
			},
			value: "value",
		},
		{
			name: "c with nil http.Request",
			getContextAndKey: func() (*Context, any) {
				c, _ := CreateTestContext(httptest.NewRecorder())
				// enable ContextWithFallback feature flag
				c.engine.ContextWithFallback = true
				c.Request = nil
				return c, "key"
			},
			value: nil,
		},
		{
			name: "c with nil http.Request.Context()",
			getContextAndKey: func() (*Context, any) {
				c, _ := CreateTestContext(httptest.NewRecorder())
				// enable ContextWithFallback feature flag
				c.engine.ContextWithFallback = true
				c.Request, _ = http.NewRequest(http.MethodPost, "/", 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))
		})
	}
}

Domain

Subdomains

Defined In

Frequently Asked Questions

What does TestContextWithFallbackValueFromRequestContext() do?
TestContextWithFallbackValueFromRequestContext() is a function in the gin codebase, defined in context_test.go.
Where is TestContextWithFallbackValueFromRequestContext() defined?
TestContextWithFallbackValueFromRequestContext() is defined in context_test.go at line 3177.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free