2017-09-09 14:36:03 +03:00
package integration
import (
"net/http"
2024-01-09 19:00:07 +03:00
"testing"
2017-09-09 14:36:03 +03:00
"time"
2024-01-09 19:00:07 +03:00
"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
}
2024-01-09 19:00:07 +03: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 14:36:03 +03:00
2024-01-09 19:00:07 +03:00
func ( s * RateLimitSuite ) TearDownSuite ( ) {
s . BaseSuite . TearDownSuite ( )
2017-09-09 14:36:03 +03:00
}
2024-01-09 19:00:07 +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 } )
2024-01-09 19:00:07 +03:00
s . traefikCmd ( withConfigFile ( file ) )
2017-09-09 14:36:03 +03:00
2024-01-09 19:00:07 +03: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 19:14:08 +03:00
2019-08-26 13:20:06 +03: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 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2019-08-26 13:20:06 +03:00
count ++
if count > 100 {
break
}
}
stop := time . Now ( )
elapsed := stop . Sub ( start )
if elapsed < time . Second * 99 / 100 {
2024-01-09 19:00:07 +03:00
s . T ( ) . Fatalf ( "requests throughput was too fast wrt to rate limiting: 100 requests in %v" , elapsed )
2019-08-26 13:20:06 +03:00
}
2017-09-09 14:36:03 +03:00
}