2018-01-26 20:22:03 +03:00
package integration
import (
2023-11-24 11:30:06 +03:00
"io"
2018-01-26 20:22:03 +03:00
"net/http"
2024-01-09 19:00:07 +03:00
"testing"
2018-01-26 20:22:03 +03:00
"time"
2018-08-29 12:58:03 +03:00
"github.com/gorilla/websocket"
2024-01-09 19:00:07 +03:00
"github.com/stretchr/testify/assert"
"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"
2018-01-26 20:22:03 +03:00
)
2021-11-25 13:10:06 +03:00
type RetrySuite struct {
BaseSuite
whoamiIP string
}
2018-01-26 20:22:03 +03:00
2024-01-09 19:00:07 +03:00
func TestRetrySuite ( t * testing . T ) {
suite . Run ( t , new ( RetrySuite ) )
}
func ( s * RetrySuite ) SetupSuite ( ) {
s . BaseSuite . SetupSuite ( )
s . createComposeProject ( "retry" )
s . composeUp ( )
s . whoamiIP = s . getComposeServiceIP ( "whoami" )
}
2021-11-25 13:10:06 +03:00
2024-01-09 19:00:07 +03:00
func ( s * RetrySuite ) TearDownSuite ( ) {
s . BaseSuite . TearDownSuite ( )
2018-01-26 20:22:03 +03:00
}
2024-01-09 19:00:07 +03:00
func ( s * RetrySuite ) TestRetry ( ) {
file := s . adaptFile ( "fixtures/retry/simple.toml" , struct { WhoamiIP string } { s . whoamiIP } )
2018-01-26 20:22:03 +03:00
2024-01-09 19:00:07 +03:00
s . traefikCmd ( withConfigFile ( file ) )
2018-01-26 20:22:03 +03:00
2024-01-09 19:00:07 +03:00
err := try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 60 * time . Second , try . BodyContains ( "PathPrefix(`/`)" ) )
require . NoError ( s . T ( ) , err )
2018-01-26 20:22:03 +03:00
response , err := http . Get ( "http://127.0.0.1:8000/" )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2022-09-14 15:42:08 +03:00
// The test only verifies that the retry middleware makes sure that the working service is eventually reached.
2024-01-09 19:00:07 +03:00
assert . Equal ( s . T ( ) , http . StatusOK , response . StatusCode )
2020-11-05 18:14:04 +03:00
}
2024-01-09 19:00:07 +03:00
func ( s * RetrySuite ) TestRetryBackoff ( ) {
file := s . adaptFile ( "fixtures/retry/backoff.toml" , struct { WhoamiIP string } { s . whoamiIP } )
2020-11-05 18:14:04 +03:00
2024-01-09 19:00:07 +03:00
s . traefikCmd ( withConfigFile ( file ) )
2020-11-05 18:14:04 +03:00
2024-01-09 19:00:07 +03:00
err := try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 60 * time . Second , try . BodyContains ( "PathPrefix(`/`)" ) )
require . NoError ( s . T ( ) , err )
2020-11-05 18:14:04 +03:00
response , err := http . Get ( "http://127.0.0.1:8000/" )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2022-09-14 15:42:08 +03:00
// The test only verifies that the retry middleware allows finally to reach the working service.
2024-01-09 19:00:07 +03:00
assert . Equal ( s . T ( ) , http . StatusOK , response . StatusCode )
2018-01-26 20:22:03 +03:00
}
2018-08-29 12:58:03 +03:00
2024-01-09 19:00:07 +03:00
func ( s * RetrySuite ) TestRetryWebsocket ( ) {
file := s . adaptFile ( "fixtures/retry/simple.toml" , struct { WhoamiIP string } { s . whoamiIP } )
2018-08-29 12:58:03 +03:00
2024-01-09 19:00:07 +03:00
s . traefikCmd ( withConfigFile ( file ) )
2018-08-29 12:58:03 +03:00
2024-01-09 19:00:07 +03:00
err := try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 60 * time . Second , try . BodyContains ( "PathPrefix(`/`)" ) )
require . NoError ( s . T ( ) , err )
2018-08-29 12:58:03 +03:00
2022-09-14 15:42:08 +03:00
// The test only verifies that the retry middleware makes sure that the working service is eventually reached.
2018-08-29 12:58:03 +03:00
_ , response , err := websocket . DefaultDialer . Dial ( "ws://127.0.0.1:8000/echo" , nil )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
assert . Equal ( s . T ( ) , http . StatusSwitchingProtocols , response . StatusCode )
2018-08-29 12:58:03 +03:00
2022-09-14 15:42:08 +03:00
// The test verifies a second time that the working service is eventually reached.
2018-08-29 12:58:03 +03:00
_ , response , err = websocket . DefaultDialer . Dial ( "ws://127.0.0.1:8000/echo" , nil )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
assert . Equal ( s . T ( ) , http . StatusSwitchingProtocols , response . StatusCode )
2018-08-29 12:58:03 +03:00
}
2023-11-24 11:30:06 +03:00
2024-01-09 19:00:07 +03:00
func ( s * RetrySuite ) TestRetryWithStripPrefix ( ) {
file := s . adaptFile ( "fixtures/retry/strip_prefix.toml" , struct { WhoamiIP string } { s . whoamiIP } )
2023-11-24 11:30:06 +03:00
2024-01-09 19:00:07 +03:00
s . traefikCmd ( withConfigFile ( file ) )
2023-11-24 11:30:06 +03:00
2024-01-09 19:00:07 +03:00
err := try . GetRequest ( "http://127.0.0.1:8080/api/rawdata" , 60 * time . Second , try . BodyContains ( "PathPrefix(`/`)" ) )
require . NoError ( s . T ( ) , err )
2023-11-24 11:30:06 +03:00
response , err := http . Get ( "http://127.0.0.1:8000/test" )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2023-11-24 11:30:06 +03:00
body , err := io . ReadAll ( response . Body )
2024-01-09 19:00:07 +03:00
require . NoError ( s . T ( ) , err )
2023-11-24 11:30:06 +03:00
2024-01-09 19:00:07 +03:00
assert . Contains ( s . T ( ) , string ( body ) , "GET / HTTP/1.1" )
assert . Contains ( s . T ( ) , string ( body ) , "X-Forwarded-Prefix: /test" )
2023-11-24 11:30:06 +03:00
}