Home / Function/ Test_HTTPMiddleware() — fiber Function Reference

Test_HTTPMiddleware() — fiber Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

middleware/adaptor/adaptor_test.go lines 325–409

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

	tests := []struct {
		name       string
		url        string
		method     string
		statusCode int
	}{
		{
			name:       "Should return 200",
			url:        "/",
			method:     "POST",
			statusCode: 200,
		},
		{
			name:       "Should return 405",
			url:        "/",
			method:     "GET",
			statusCode: 405,
		},
		{
			name:       "Should return 400",
			url:        "/unknown",
			method:     "POST",
			statusCode: 404,
		},
	}

	nethttpMW := func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if r.Method != http.MethodPost {
				w.WriteHeader(http.StatusMethodNotAllowed)
				return
			}

			r = r.WithContext(context.WithValue(r.Context(), TestContextKey, "okay"))
			r = r.WithContext(context.WithValue(r.Context(), TestContextSecondKey, "not_okay"))
			r = r.WithContext(context.WithValue(r.Context(), TestContextSecondKey, "okay"))

			next.ServeHTTP(w, r)
		})
	}

	app := fiber.New()
	app.Use(HTTPMiddleware(nethttpMW))
	app.Post("/", func(c fiber.Ctx) error {
		value := c.RequestCtx().Value(TestContextKey)
		val, ok := value.(string)
		if !ok {
			t.Error("unexpected error on type-assertion")
		}
		if value != nil {
			c.Set("context_okay", val)
		}
		value = c.RequestCtx().Value(TestContextSecondKey)
		if value != nil {
			val, ok := value.(string)
			if !ok {
				t.Error("unexpected error on type-assertion")
			}
			c.Set("context_second_okay", val)
		}
		return c.SendStatus(fiber.StatusOK)
	})

	for _, tt := range tests {
		req, err := http.NewRequestWithContext(context.Background(), tt.method, tt.url, http.NoBody)
		req.Host = expectedHost
		require.NoError(t, err)

		resp, err := app.Test(req)
		require.NoError(t, err)
		require.Equal(t, tt.statusCode, resp.StatusCode, "StatusCode")
	}

	req, err := http.NewRequestWithContext(context.Background(), fiber.MethodPost, "/", http.NoBody)
	req.Host = expectedHost
	require.NoError(t, err)

	resp, err := app.Test(req)

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free