2017-09-09 11:36:03 +00:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-01-09 16:00:07 +00:00
|
|
|
"testing"
|
2017-09-09 11:36:03 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/integration/try"
|
2017-09-09 11:36:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RateLimitSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
ServerIP string
|
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
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 11:36:03 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *RateLimitSuite) TearDownSuite() {
|
|
|
|
s.BaseSuite.TearDownSuite()
|
2017-09-09 11:36:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *RateLimitSuite) TestSimpleConfiguration() {
|
|
|
|
file := s.adaptFile("fixtures/ratelimit/simple.toml", struct {
|
2017-09-09 11:36:03 +00:00
|
|
|
Server1 string
|
|
|
|
}{s.ServerIP})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file))
|
2017-09-09 11:36:03 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1*time.Second, try.BodyContains("ratelimit"))
|
|
|
|
require.NoError(s.T(), err)
|
2019-06-17 16:14:08 +00:00
|
|
|
|
2019-08-26 10:20:06 +00:00
|
|
|
start := time.Now()
|
|
|
|
count := 0
|
|
|
|
for {
|
|
|
|
err = try.GetRequest("http://127.0.0.1:8081/", 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2019-08-26 10:20:06 +00:00
|
|
|
count++
|
|
|
|
if count > 100 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stop := time.Now()
|
|
|
|
elapsed := stop.Sub(start)
|
|
|
|
if elapsed < time.Second*99/100 {
|
2024-01-09 16:00:07 +00:00
|
|
|
s.T().Fatalf("requests throughput was too fast wrt to rate limiting: 100 requests in %v", elapsed)
|
2019-08-26 10:20:06 +00:00
|
|
|
}
|
2017-09-09 11:36:03 +00:00
|
|
|
}
|