Home / Function/ Test_HTTPHandlerWithContext_local_context() — fiber Function Reference

Test_HTTPHandlerWithContext_local_context() — fiber Function Reference

Architecture documentation for the Test_HTTPHandlerWithContext_local_context() function in adaptor_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  5780514e_32e6_84b6_47da_05f2fb412724["Test_HTTPHandlerWithContext_local_context()"]
  8ec96b38_44b4_af66_6f6f_dd60f87b680c["adaptor_test.go"]
  5780514e_32e6_84b6_47da_05f2fb412724 -->|defined in| 8ec96b38_44b4_af66_6f6f_dd60f87b680c
  style 5780514e_32e6_84b6_47da_05f2fb412724 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/adaptor/adaptor_test.go lines 262–312

func Test_HTTPHandlerWithContext_local_context(t *testing.T) {
	t.Parallel()

	app := fiber.New()

	// unique type for avoiding collisions in context
	type key struct{}
	var testKey key

	const testVal string = "test-value"

	// a middleware to add a value to the local context
	app.Use(func(c fiber.Ctx) error {
		ctx := context.WithValue(c.Context(), testKey, testVal)
		c.SetContext(ctx)
		return c.Next()
	})

	// a handler that checks if the value has been appended to the local context
	app.Get("/", HTTPHandlerWithContext(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx, ok := LocalContextFromHTTPRequest(r)
		if !ok {
			http.Error(w, "local context not found", http.StatusInternalServerError)
			return
		}
		val, ok := ctx.Value(testKey).(string)
		if !ok {
			http.Error(w, "invalid context value", http.StatusInternalServerError)
			return
		}
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		w.WriteHeader(http.StatusOK)
		if _, err := w.Write([]byte(val)); err != nil {
			t.Logf("write failed: %v", err)
		}
	})))

	resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", http.NoBody), fiber.TestConfig{
		Timeout:       200 * time.Millisecond,
		FailOnTimeout: false,
	})
	require.NoError(t, err)

	defer resp.Body.Close() //nolint:errcheck // no need

	body, err := io.ReadAll(resp.Body)
	require.NoError(t, err)

	require.Equal(t, testVal, string(body))
	require.Equal(t, fiber.StatusOK, resp.StatusCode)
}

Domain

Subdomains

Frequently Asked Questions

What does Test_HTTPHandlerWithContext_local_context() do?
Test_HTTPHandlerWithContext_local_context() is a function in the fiber codebase, defined in middleware/adaptor/adaptor_test.go.
Where is Test_HTTPHandlerWithContext_local_context() defined?
Test_HTTPHandlerWithContext_local_context() is defined in middleware/adaptor/adaptor_test.go at line 262.

Analyze Your Own Codebase

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

Try Supermodel Free