| 1006 | } |
| 1007 | |
| 1008 | func Test_Integration_Domain_WithLimiter(t *testing.T) { |
| 1009 | t.Parallel() |
| 1010 | |
| 1011 | const maxRequests = 3 |
| 1012 | |
| 1013 | app := fiber.New() |
| 1014 | app.Use(limiter.New(limiter.Config{ |
| 1015 | Max: maxRequests, |
| 1016 | Expiration: 1 * time.Minute, |
| 1017 | KeyGenerator: func(_ fiber.Ctx) string { |
| 1018 | return "domain-limiter" |
| 1019 | }, |
| 1020 | })) |
| 1021 | |
| 1022 | app.Domain("api.example.com").Get("/limited", func(c fiber.Ctx) error { |
| 1023 | return c.SendString("ok") |
| 1024 | }) |
| 1025 | |
| 1026 | // Make requests up to the limit |
| 1027 | for range maxRequests { |
| 1028 | req := httptest.NewRequest(http.MethodGet, "/limited", http.NoBody) |
| 1029 | req.Host = "api.example.com" |
| 1030 | resp, err := app.Test(req) |
| 1031 | require.NoError(t, err) |
| 1032 | require.Equal(t, fiber.StatusOK, resp.StatusCode) |
| 1033 | require.Equal(t, strconv.Itoa(maxRequests), resp.Header.Get("X-RateLimit-Limit")) |
| 1034 | } |
| 1035 | |
| 1036 | // Next request should be rate limited |
| 1037 | req := httptest.NewRequest(http.MethodGet, "/limited", http.NoBody) |
| 1038 | req.Host = "api.example.com" |
| 1039 | resp, err := app.Test(req) |
| 1040 | require.NoError(t, err) |
| 1041 | require.Equal(t, fiber.StatusTooManyRequests, resp.StatusCode) |
| 1042 | } |
| 1043 | |
| 1044 | func Test_Integration_Domain_WithCSRF(t *testing.T) { |
| 1045 | t.Parallel() |