2018-11-14 09:18:03 +00:00
|
|
|
package responsemodifiers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
"github.com/containous/traefik/pkg/config/dynamic"
|
2019-07-15 15:04:04 +00:00
|
|
|
"github.com/containous/traefik/pkg/config/runtime"
|
2019-03-15 08:42:03 +00:00
|
|
|
"github.com/containous/traefik/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()
|
|
|
|
|
|
|
|
assert.Equal(t, resp.Header.Get("X-Foo"), "foo")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
|
|
|
|
assert.Equal(t, resp.Header.Get("Referrer-Policy"), "no-referrer")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
|
|
|
|
assert.Equal(t, resp.Header.Get("X-Foo"), "foo")
|
|
|
|
assert.Equal(t, resp.Header.Get("X-Bar"), "bar")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
|
|
|
|
assert.Equal(t, resp.Header.Get("X-Foo"), "foo")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
|
|
|
|
assert.Equal(t, resp.Header.Get("X-Foo"), "foo")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
resp := test.buildResponse(test.conf)
|
|
|
|
|
|
|
|
err := rm(resp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
test.assertResponse(t, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|