2017-07-06 14:28:13 +00:00
|
|
|
package integration
|
2017-06-30 23:04:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2023-01-02 16:00:05 +00:00
|
|
|
"net/http/httptest"
|
2024-01-09 16:00:07 +00:00
|
|
|
"testing"
|
2017-06-30 23:04:18 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
"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"
|
2017-06-30 23:04:18 +00:00
|
|
|
)
|
|
|
|
|
2021-11-25 10:10:06 +00:00
|
|
|
// ErrorPagesSuite test suites.
|
2017-07-10 12:58:31 +00:00
|
|
|
type ErrorPagesSuite struct {
|
|
|
|
BaseSuite
|
|
|
|
ErrorPageIP string
|
|
|
|
BackendIP string
|
2017-06-30 23:04:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func TestErrorPagesSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ErrorPagesSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ErrorPagesSuite) SetupSuite() {
|
|
|
|
s.BaseSuite.SetupSuite()
|
|
|
|
|
|
|
|
s.createComposeProject("error_pages")
|
|
|
|
s.composeUp()
|
|
|
|
|
|
|
|
s.ErrorPageIP = s.getComposeServiceIP("nginx2")
|
|
|
|
s.BackendIP = s.getComposeServiceIP("nginx1")
|
|
|
|
}
|
2017-07-10 12:58:31 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *ErrorPagesSuite) TearDownSuite() {
|
|
|
|
s.BaseSuite.TearDownSuite()
|
2017-07-10 12:58:31 +00:00
|
|
|
}
|
2017-06-30 23:04:18 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *ErrorPagesSuite) TestSimpleConfiguration() {
|
|
|
|
file := s.adaptFile("fixtures/error_pages/simple.toml", struct {
|
2017-06-30 23:04:18 +00:00
|
|
|
Server1 string
|
|
|
|
Server2 string
|
2023-01-02 16:00:05 +00:00
|
|
|
}{"http://" + s.BackendIP + ":80", s.ErrorPageIP})
|
2017-06-30 23:04:18 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file))
|
2017-06-30 23:04:18 +00:00
|
|
|
|
2018-02-23 09:52:03 +00:00
|
|
|
frontendReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8080", nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-06-30 23:04:18 +00:00
|
|
|
frontendReq.Host = "test.local"
|
|
|
|
|
|
|
|
err = try.Request(frontendReq, 2*time.Second, try.BodyContains("nginx"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-06-30 23:04:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *ErrorPagesSuite) TestErrorPage() {
|
2018-07-03 08:02:03 +00:00
|
|
|
// error.toml contains a mis-configuration of the backend host
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/error_pages/error.toml", struct {
|
2017-06-30 23:04:18 +00:00
|
|
|
Server1 string
|
|
|
|
Server2 string
|
2017-07-10 12:58:31 +00:00
|
|
|
}{s.BackendIP, s.ErrorPageIP})
|
2017-06-30 23:04:18 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file))
|
2017-06-30 23:04:18 +00:00
|
|
|
|
2018-02-23 09:52:03 +00:00
|
|
|
frontendReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8080", nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-06-30 23:04:18 +00:00
|
|
|
frontendReq.Host = "test.local"
|
|
|
|
|
|
|
|
err = try.Request(frontendReq, 2*time.Second, try.BodyContains("An error occurred."))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-06-30 23:04:18 +00:00
|
|
|
}
|
2023-01-02 16:00:05 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *ErrorPagesSuite) TestErrorPageFlush() {
|
2023-01-02 16:00:05 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.Header().Add("Transfer-Encoding", "chunked")
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = rw.Write([]byte("KO"))
|
|
|
|
}))
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/error_pages/simple.toml", struct {
|
2023-01-02 16:00:05 +00:00
|
|
|
Server1 string
|
|
|
|
Server2 string
|
|
|
|
}{srv.URL, s.ErrorPageIP})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file))
|
2023-01-02 16:00:05 +00:00
|
|
|
|
|
|
|
frontendReq, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8080", nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2023-01-02 16:00:05 +00:00
|
|
|
frontendReq.Host = "test.local"
|
|
|
|
|
|
|
|
err = try.Request(frontendReq, 2*time.Second,
|
|
|
|
try.BodyContains("An error occurred."),
|
|
|
|
try.HasHeaderValue("Content-Type", "text/html", true),
|
|
|
|
)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2023-01-02 16:00:05 +00:00
|
|
|
}
|