2018-11-14 09:18:03 +00:00
|
|
|
package customerrors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/testhelpers"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandler(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2019-07-10 07:26:04 +00:00
|
|
|
errorPage *dynamic.ErrorPage
|
2018-11-14 09:18:03 +00:00
|
|
|
backendCode int
|
|
|
|
backendErrorHandler http.HandlerFunc
|
|
|
|
validate func(t *testing.T, recorder *httptest.ResponseRecorder)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no error",
|
2019-07-10 07:26:04 +00:00
|
|
|
errorPage: &dynamic.ErrorPage{Service: "error", Query: "/test", Status: []string{"500-501", "503-599"}},
|
2018-11-14 09:18:03 +00:00
|
|
|
backendCode: http.StatusOK,
|
|
|
|
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "My error page.")
|
|
|
|
}),
|
|
|
|
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
|
|
|
assert.Equal(t, http.StatusOK, recorder.Code, "HTTP status")
|
|
|
|
assert.Contains(t, recorder.Body.String(), http.StatusText(http.StatusOK))
|
|
|
|
},
|
|
|
|
},
|
2019-09-12 14:20:05 +00:00
|
|
|
{
|
|
|
|
desc: "no error, but not a 200",
|
|
|
|
errorPage: &dynamic.ErrorPage{Service: "error", Query: "/test", Status: []string{"500-501", "503-599"}},
|
|
|
|
backendCode: http.StatusPartialContent,
|
|
|
|
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "My error page.")
|
|
|
|
}),
|
|
|
|
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
|
|
|
assert.Equal(t, http.StatusPartialContent, recorder.Code, "HTTP status")
|
|
|
|
assert.Contains(t, recorder.Body.String(), http.StatusText(http.StatusPartialContent))
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "a 304, so no Write called",
|
|
|
|
errorPage: &dynamic.ErrorPage{Service: "error", Query: "/test", Status: []string{"500-501", "503-599"}},
|
|
|
|
backendCode: http.StatusNotModified,
|
|
|
|
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "whatever, should not be called")
|
|
|
|
}),
|
|
|
|
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
|
|
|
assert.Equal(t, http.StatusNotModified, recorder.Code, "HTTP status")
|
|
|
|
assert.Contains(t, recorder.Body.String(), "")
|
|
|
|
},
|
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
|
|
|
desc: "in the range",
|
2019-07-10 07:26:04 +00:00
|
|
|
errorPage: &dynamic.ErrorPage{Service: "error", Query: "/test", Status: []string{"500-501", "503-599"}},
|
2018-11-14 09:18:03 +00:00
|
|
|
backendCode: http.StatusInternalServerError,
|
|
|
|
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "My error page.")
|
|
|
|
}),
|
|
|
|
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, recorder.Code, "HTTP status")
|
|
|
|
assert.Contains(t, recorder.Body.String(), "My error page.")
|
|
|
|
assert.NotContains(t, recorder.Body.String(), "oops", "Should not return the oops page")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "not in the range",
|
2019-07-10 07:26:04 +00:00
|
|
|
errorPage: &dynamic.ErrorPage{Service: "error", Query: "/test", Status: []string{"500-501", "503-599"}},
|
2018-11-14 09:18:03 +00:00
|
|
|
backendCode: http.StatusBadGateway,
|
|
|
|
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, "My error page.")
|
|
|
|
}),
|
|
|
|
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
|
|
|
assert.Equal(t, http.StatusBadGateway, recorder.Code, "HTTP status")
|
|
|
|
assert.Contains(t, recorder.Body.String(), http.StatusText(http.StatusBadGateway))
|
|
|
|
assert.NotContains(t, recorder.Body.String(), "Test Server", "Should return the oops page since we have not configured the 502 code")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "query replacement",
|
2019-07-10 07:26:04 +00:00
|
|
|
errorPage: &dynamic.ErrorPage{Service: "error", Query: "/{status}", Status: []string{"503-503"}},
|
2018-11-14 09:18:03 +00:00
|
|
|
backendCode: http.StatusServiceUnavailable,
|
|
|
|
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.RequestURI == "/503" {
|
|
|
|
fmt.Fprintln(w, "My 503 page.")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, "Failed")
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
|
|
|
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code, "HTTP status")
|
|
|
|
assert.Contains(t, recorder.Body.String(), "My 503 page.")
|
|
|
|
assert.NotContains(t, recorder.Body.String(), "oops", "Should not return the oops page")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Single code",
|
2019-07-10 07:26:04 +00:00
|
|
|
errorPage: &dynamic.ErrorPage{Service: "error", Query: "/{status}", Status: []string{"503"}},
|
2018-11-14 09:18:03 +00:00
|
|
|
backendCode: http.StatusServiceUnavailable,
|
|
|
|
backendErrorHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.RequestURI == "/503" {
|
|
|
|
fmt.Fprintln(w, "My 503 page.")
|
|
|
|
} else {
|
|
|
|
fmt.Fprintln(w, "Failed")
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
validate: func(t *testing.T, recorder *httptest.ResponseRecorder) {
|
|
|
|
assert.Equal(t, http.StatusServiceUnavailable, recorder.Code, "HTTP status")
|
|
|
|
assert.Contains(t, recorder.Body.String(), "My 503 page.")
|
|
|
|
assert.NotContains(t, recorder.Body.String(), "oops", "Should not return the oops page")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
serviceBuilderMock := &mockServiceBuilder{handler: test.backendErrorHandler}
|
|
|
|
|
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(test.backendCode)
|
2019-09-12 14:20:05 +00:00
|
|
|
if test.backendCode == http.StatusNotModified {
|
|
|
|
return
|
|
|
|
}
|
2018-11-14 09:18:03 +00:00
|
|
|
fmt.Fprintln(w, http.StatusText(test.backendCode))
|
|
|
|
})
|
|
|
|
errorPageHandler, err := New(context.Background(), handler, *test.errorPage, serviceBuilderMock, "test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost/test", nil)
|
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
errorPageHandler.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
test.validate(t, recorder)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockServiceBuilder struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2020-09-01 16:16:04 +00:00
|
|
|
func (m *mockServiceBuilder) BuildHTTP(ctx context.Context, serviceName string) (http.Handler, error) {
|
2018-11-14 09:18:03 +00:00
|
|
|
return m.handler, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewResponseRecorder(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
rw http.ResponseWriter
|
|
|
|
expected http.ResponseWriter
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Without Close Notify",
|
|
|
|
rw: httptest.NewRecorder(),
|
|
|
|
expected: &responseRecorderWithoutCloseNotify{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "With Close Notify",
|
|
|
|
rw: &mockRWCloseNotify{},
|
|
|
|
expected: &responseRecorderWithCloseNotify{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-09-13 17:28:04 +00:00
|
|
|
rec := newResponseRecorder(context.Background(), test.rw)
|
2018-11-14 09:18:03 +00:00
|
|
|
assert.IsType(t, rec, test.expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockRWCloseNotify struct{}
|
|
|
|
|
|
|
|
func (m *mockRWCloseNotify) CloseNotify() <-chan bool {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRWCloseNotify) Header() http.Header {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRWCloseNotify) Write([]byte) (int, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockRWCloseNotify) WriteHeader(int) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|