MCPcopy
hub / github.com/gofiber/fiber / Test_Integration_Domain_WithLimiter

Function Test_Integration_Domain_WithLimiter

app_integration_test.go:1008–1042  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1006}
1007
1008func 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
1044func Test_Integration_Domain_WithCSRF(t *testing.T) {
1045 t.Parallel()

Callers

nothing calls this directly

Calls 7

NewFunction · 0.92
TestMethod · 0.80
NewMethod · 0.65
UseMethod · 0.65
GetMethod · 0.65
DomainMethod · 0.65
SendStringMethod · 0.65

Tested by

no test coverage detected