Home / Function/ TestLimiterSlidingPropagatesRequestContextToStorage() — fiber Function Reference

TestLimiterSlidingPropagatesRequestContextToStorage() — fiber Function Reference

Architecture documentation for the TestLimiterSlidingPropagatesRequestContextToStorage() function in limiter_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  bb671d74_1b55_c7e1_5447_a449c0f633f1["TestLimiterSlidingPropagatesRequestContextToStorage()"]
  0509da96_221e_0d0d_fe2e_f2e2e3695b6f["limiter_test.go"]
  bb671d74_1b55_c7e1_5447_a449c0f633f1 -->|defined in| 0509da96_221e_0d0d_fe2e_f2e2e3695b6f
  ceee159b_ff25_aa39_7380_65ce5ea23643["newContextRecorderLimiterStorage()"]
  bb671d74_1b55_c7e1_5447_a449c0f633f1 -->|calls| ceee159b_ff25_aa39_7380_65ce5ea23643
  7f532e95_c67c_e1fb_201a_4523be5937f5["contextWithMarker()"]
  bb671d74_1b55_c7e1_5447_a449c0f633f1 -->|calls| 7f532e95_c67c_e1fb_201a_4523be5937f5
  418b866d_b436_722f_b08d_9990bcc9fd7d["canceledContextWithMarker()"]
  bb671d74_1b55_c7e1_5447_a449c0f633f1 -->|calls| 418b866d_b436_722f_b08d_9990bcc9fd7d
  style bb671d74_1b55_c7e1_5447_a449c0f633f1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/limiter/limiter_test.go lines 405–469

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

	storage := newContextRecorderLimiterStorage()

	app := fiber.New()

	app.Use(func(c fiber.Ctx) error {
		path := c.Path()
		if path == "/normal" {
			c.SetContext(contextWithMarker("sliding-normal"))
		}
		if path == "/rollback" {
			c.SetContext(canceledContextWithMarker("sliding-rollback"))
		}
		return c.Next()
	})

	app.Use(New(Config{
		Storage:                storage,
		Max:                    1,
		Expiration:             time.Minute,
		SkipSuccessfulRequests: true,
		KeyGenerator: func(c fiber.Ctx) string {
			return c.Path()
		},
		LimiterMiddleware: SlidingWindow{},
	}))

	app.Get("/:mode", func(c fiber.Ctx) error {
		return c.SendString("ok")
	})

	for _, path := range []string{"/normal", "/rollback"} {
		resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, path, http.NoBody))
		require.NoError(t, err)
		require.Equal(t, fiber.StatusOK, resp.StatusCode)
	}

	gets := storage.recordedGets()
	require.Len(t, gets, 4)

	sets := storage.recordedSets()
	require.Len(t, sets, 4)

	verifyRecords := func(t *testing.T, records []contextRecord, key, wantValue string, wantCanceled bool) {
		t.Helper()
		var matched []contextRecord
		for _, rec := range records {
			if rec.key == key {
				matched = append(matched, rec)
			}
		}
		require.Len(t, matched, 2)
		for _, rec := range matched {
			require.Equal(t, wantValue, rec.value)
			require.Equal(t, wantCanceled, rec.canceled)
		}
	}

	verifyRecords(t, gets, "/normal", "sliding-normal", false)
	verifyRecords(t, gets, "/rollback", "sliding-rollback", true)
	verifyRecords(t, sets, "/normal", "sliding-normal", false)
	verifyRecords(t, sets, "/rollback", "sliding-rollback", true)
}

Subdomains

Frequently Asked Questions

What does TestLimiterSlidingPropagatesRequestContextToStorage() do?
TestLimiterSlidingPropagatesRequestContextToStorage() is a function in the fiber codebase, defined in middleware/limiter/limiter_test.go.
Where is TestLimiterSlidingPropagatesRequestContextToStorage() defined?
TestLimiterSlidingPropagatesRequestContextToStorage() is defined in middleware/limiter/limiter_test.go at line 405.
What does TestLimiterSlidingPropagatesRequestContextToStorage() call?
TestLimiterSlidingPropagatesRequestContextToStorage() calls 3 function(s): canceledContextWithMarker, contextWithMarker, newContextRecorderLimiterStorage.

Analyze Your Own Codebase

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

Try Supermodel Free