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