1
0
mirror of https://github.com/containous/traefik.git synced 2024-12-22 13:34:03 +03:00
traefik/integration/ratelimit_test.go

61 lines
1.3 KiB
Go
Raw Normal View History

2017-09-09 14:36:03 +03:00
package integration
import (
"net/http"
"testing"
2017-09-09 14:36:03 +03:00
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
2023-02-03 17:24:05 +03:00
"github.com/traefik/traefik/v3/integration/try"
2017-09-09 14:36:03 +03:00
)
type RateLimitSuite struct {
BaseSuite
ServerIP string
}
func TestRateLimitSuite(t *testing.T) {
suite.Run(t, new(RateLimitSuite))
}
func (s *RateLimitSuite) SetupSuite() {
s.BaseSuite.SetupSuite()
s.createComposeProject("ratelimit")
s.composeUp()
s.ServerIP = s.getComposeServiceIP("whoami1")
}
2017-09-09 14:36:03 +03:00
func (s *RateLimitSuite) TearDownSuite() {
s.BaseSuite.TearDownSuite()
2017-09-09 14:36:03 +03:00
}
func (s *RateLimitSuite) TestSimpleConfiguration() {
file := s.adaptFile("fixtures/ratelimit/simple.toml", struct {
2017-09-09 14:36:03 +03:00
Server1 string
}{s.ServerIP})
s.traefikCmd(withConfigFile(file))
2017-09-09 14:36:03 +03:00
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1*time.Second, try.BodyContains("ratelimit"))
require.NoError(s.T(), err)
start := time.Now()
count := 0
for {
err = try.GetRequest("http://127.0.0.1:8081/", 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
require.NoError(s.T(), err)
count++
if count > 100 {
break
}
}
stop := time.Now()
elapsed := stop.Sub(start)
if elapsed < time.Second*99/100 {
s.T().Fatalf("requests throughput was too fast wrt to rate limiting: 100 requests in %v", elapsed)
}
2017-09-09 14:36:03 +03:00
}