MCPcopy Index your code
hub / github.com/labstack/echo / TestRateLimiter

Function TestRateLimiter

middleware/rate_limiter_test.go:21–60  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestRateLimiter(t *testing.T) {
22 e := echo.New()
23
24 handler := func(c *echo.Context) error {
25 return c.String(http.StatusOK, "test")
26 }
27
28 var inMemoryStore = NewRateLimiterMemoryStoreWithConfig(RateLimiterMemoryStoreConfig{Rate: 1, Burst: 3})
29
30 mw := RateLimiterWithConfig(RateLimiterConfig{Store: inMemoryStore})
31
32 testCases := []struct {
33 id string
34 expectErr string
35 }{
36 {id: "127.0.0.1"},
37 {id: "127.0.0.1"},
38 {id: "127.0.0.1"},
39 {id: "127.0.0.1", expectErr: "code=429, message=rate limit exceeded"},
40 {id: "127.0.0.1", expectErr: "code=429, message=rate limit exceeded"},
41 {id: "127.0.0.1", expectErr: "code=429, message=rate limit exceeded"},
42 {id: "127.0.0.1", expectErr: "code=429, message=rate limit exceeded"},
43 }
44
45 for _, tc := range testCases {
46 req := httptest.NewRequest(http.MethodGet, "/", nil)
47 req.Header.Add(echo.HeaderXRealIP, tc.id)
48
49 rec := httptest.NewRecorder()
50 c := e.NewContext(req, rec)
51
52 err := mw(handler)(c)
53 if tc.expectErr != "" {
54 assert.EqualError(t, err, tc.expectErr)
55 } else {
56 assert.NoError(t, err)
57 }
58 assert.Equal(t, http.StatusOK, rec.Code)
59 }
60}
61
62func TestMustRateLimiterWithConfig_panicBehaviour(t *testing.T) {
63 var inMemoryStore = NewRateLimiterMemoryStoreWithConfig(RateLimiterMemoryStoreConfig{Rate: 1, Burst: 3})

Callers

nothing calls this directly

Calls 5

StringMethod · 0.95
RateLimiterWithConfigFunction · 0.85
NewContextMethod · 0.80
AddMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…