traefik/pkg/responsemodifiers/response_modifier_test.go

215 lines
5.4 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package responsemodifiers
import (
"context"
"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/config/runtime"
"github.com/containous/traefik/v2/pkg/middlewares/headers"
2018-11-14 09:18:03 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func stubResponse(_ map[string]*dynamic.Middleware) *http.Response {
2018-11-14 09:18:03 +00:00
return &http.Response{Header: make(http.Header)}
}
func TestBuilderBuild(t *testing.T) {
testCases := []struct {
desc string
middlewares []string
// buildResponse is needed because secure use a private context key
buildResponse func(map[string]*dynamic.Middleware) *http.Response
conf map[string]*dynamic.Middleware
2018-11-14 09:18:03 +00:00
assertResponse func(*testing.T, *http.Response)
}{
{
desc: "no configuration",
middlewares: []string{"foo", "bar"},
buildResponse: stubResponse,
conf: map[string]*dynamic.Middleware{},
2018-11-14 09:18:03 +00:00
assertResponse: func(t *testing.T, resp *http.Response) {},
},
{
desc: "one modifier",
middlewares: []string{"foo", "bar"},
buildResponse: stubResponse,
conf: map[string]*dynamic.Middleware{
2018-11-14 09:18:03 +00:00
"foo": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
CustomResponseHeaders: map[string]string{"X-Foo": "foo"},
},
},
},
assertResponse: func(t *testing.T, resp *http.Response) {
t.Helper()
2019-09-03 13:22:05 +00:00
assert.Equal(t, "foo", resp.Header.Get("X-Foo"))
2018-11-14 09:18:03 +00:00
},
},
{
desc: "secure: one modifier",
middlewares: []string{"foo", "bar"},
buildResponse: func(middlewares map[string]*dynamic.Middleware) *http.Response {
2018-11-14 09:18:03 +00:00
ctx := context.Background()
var request *http.Request
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
request = req
})
headerM := *middlewares["foo"].Headers
handler, err := headers.New(ctx, next, headerM, "secure")
require.NoError(t, err)
handler.ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet, "http://foo.com", nil))
return &http.Response{Header: make(http.Header), Request: request}
},
conf: map[string]*dynamic.Middleware{
2018-11-14 09:18:03 +00:00
"foo": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
ReferrerPolicy: "no-referrer",
},
},
"bar": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
CustomResponseHeaders: map[string]string{"X-Bar": "bar"},
},
},
},
assertResponse: func(t *testing.T, resp *http.Response) {
t.Helper()
2019-09-03 13:22:05 +00:00
assert.Equal(t, "no-referrer", resp.Header.Get("Referrer-Policy"))
2018-11-14 09:18:03 +00:00
},
},
{
desc: "two modifiers",
middlewares: []string{"foo", "bar"},
buildResponse: stubResponse,
conf: map[string]*dynamic.Middleware{
2018-11-14 09:18:03 +00:00
"foo": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
CustomResponseHeaders: map[string]string{"X-Foo": "foo"},
},
},
"bar": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
CustomResponseHeaders: map[string]string{"X-Bar": "bar"},
},
},
},
assertResponse: func(t *testing.T, resp *http.Response) {
t.Helper()
2019-09-03 13:22:05 +00:00
assert.Equal(t, "foo", resp.Header.Get("X-Foo"))
assert.Equal(t, "bar", resp.Header.Get("X-Bar"))
2018-11-14 09:18:03 +00:00
},
},
{
desc: "modifier order",
middlewares: []string{"foo", "bar"},
buildResponse: stubResponse,
conf: map[string]*dynamic.Middleware{
2018-11-14 09:18:03 +00:00
"foo": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
CustomResponseHeaders: map[string]string{"X-Foo": "foo"},
},
},
"bar": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
CustomResponseHeaders: map[string]string{"X-Foo": "bar"},
},
},
},
assertResponse: func(t *testing.T, resp *http.Response) {
t.Helper()
2019-09-03 13:22:05 +00:00
assert.Equal(t, "foo", resp.Header.Get("X-Foo"))
2018-11-14 09:18:03 +00:00
},
},
{
desc: "chain",
middlewares: []string{"chain"},
buildResponse: stubResponse,
conf: map[string]*dynamic.Middleware{
2018-11-14 09:18:03 +00:00
"foo": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
CustomResponseHeaders: map[string]string{"X-Foo": "foo"},
},
},
"bar": {
Headers: &dynamic.Headers{
2018-11-14 09:18:03 +00:00
CustomResponseHeaders: map[string]string{"X-Foo": "bar"},
},
},
"chain": {
Chain: &dynamic.Chain{
2018-11-14 09:18:03 +00:00
Middlewares: []string{"foo", "bar"},
},
},
},
assertResponse: func(t *testing.T, resp *http.Response) {
t.Helper()
2019-09-03 13:22:05 +00:00
assert.Equal(t, "foo", resp.Header.Get("X-Foo"))
2018-11-14 09:18:03 +00:00
},
},
2019-09-03 13:22:05 +00:00
{
desc: "nil middleware",
middlewares: []string{"foo"},
buildResponse: stubResponse,
conf: map[string]*dynamic.Middleware{
"foo": nil,
},
assertResponse: func(t *testing.T, resp *http.Response) {},
},
2020-07-09 08:50:04 +00:00
{
desc: "chain without headers",
middlewares: []string{"chain"},
buildResponse: stubResponse,
conf: map[string]*dynamic.Middleware{
"foo": {IPWhiteList: &dynamic.IPWhiteList{}},
"chain": {
Chain: &dynamic.Chain{
Middlewares: []string{"foo"},
},
},
},
assertResponse: func(t *testing.T, resp *http.Response) {},
},
2018-11-14 09:18:03 +00:00
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
rtConf := runtime.NewConfig(dynamic.Configuration{
HTTP: &dynamic.HTTPConfiguration{
Middlewares: test.conf,
},
})
builder := NewBuilder(rtConf.Middlewares)
2018-11-14 09:18:03 +00:00
rm := builder.Build(context.Background(), test.middlewares)
if rm == nil {
return
}
2018-11-14 09:18:03 +00:00
resp := test.buildResponse(test.conf)
err := rm(resp)
require.NoError(t, err)
test.assertResponse(t, resp)
})
}
}