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"
|
|
|
|
)
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
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
|
2019-07-10 07:26:04 +00:00
|
|
|
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,
|
2019-07-10 07:26:04 +00:00
|
|
|
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,
|
2019-07-10 07:26:04 +00:00
|
|
|
conf: map[string]*dynamic.Middleware{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
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"},
|
2019-07-10 07:26:04 +00:00
|
|
|
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}
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
conf: map[string]*dynamic.Middleware{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Headers: &dynamic.Headers{
|
2018-11-14 09:18:03 +00:00
|
|
|
ReferrerPolicy: "no-referrer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
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,
|
2019-07-10 07:26:04 +00:00
|
|
|
conf: map[string]*dynamic.Middleware{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Headers: &dynamic.Headers{
|
2018-11-14 09:18:03 +00:00
|
|
|
CustomResponseHeaders: map[string]string{"X-Foo": "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
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,
|
2019-07-10 07:26:04 +00:00
|
|
|
conf: map[string]*dynamic.Middleware{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Headers: &dynamic.Headers{
|
2018-11-14 09:18:03 +00:00
|
|
|
CustomResponseHeaders: map[string]string{"X-Foo": "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
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,
|
2019-07-10 07:26:04 +00:00
|
|
|
conf: map[string]*dynamic.Middleware{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Headers: &dynamic.Headers{
|
2018-11-14 09:18:03 +00:00
|
|
|
CustomResponseHeaders: map[string]string{"X-Foo": "foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"bar": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Headers: &dynamic.Headers{
|
2018-11-14 09:18:03 +00:00
|
|
|
CustomResponseHeaders: map[string]string{"X-Foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"chain": {
|
2019-07-10 07:26:04 +00:00
|
|
|
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()
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
rtConf := runtime.NewConfig(dynamic.Configuration{
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
2019-05-16 08:58:06 +00:00
|
|
|
Middlewares: test.conf,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
builder := NewBuilder(rtConf.Middlewares)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
rm := builder.Build(context.Background(), test.middlewares)
|
2020-06-15 10:20:05 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|