2024-04-05 15:18:03 +00:00
|
|
|
package headermodifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/testhelpers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestHeaderModifier(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2024-08-12 09:34:04 +00:00
|
|
|
config dynamic.HeaderModifier
|
2024-04-05 15:18:03 +00:00
|
|
|
requestHeaders http.Header
|
|
|
|
expectedHeaders http.Header
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no config",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{},
|
2024-04-05 15:18:03 +00:00
|
|
|
expectedHeaders: map[string][]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "set header",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Set: map[string]string{"Foo": "Bar"},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string][]string{"Foo": {"Bar"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "set header with existing headers",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Set: map[string]string{"Foo": "Bar"},
|
|
|
|
},
|
|
|
|
requestHeaders: map[string][]string{"Foo": {"Baz"}, "Bar": {"Foo"}},
|
|
|
|
expectedHeaders: map[string][]string{"Foo": {"Bar"}, "Bar": {"Foo"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "set multiple headers with existing headers",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Set: map[string]string{"Foo": "Bar", "Bar": "Foo"},
|
|
|
|
},
|
|
|
|
requestHeaders: map[string][]string{"Foo": {"Baz"}, "Bar": {"Foobar"}},
|
|
|
|
expectedHeaders: map[string][]string{"Foo": {"Bar"}, "Bar": {"Foo"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "add header",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Add: map[string]string{"Foo": "Bar"},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string][]string{"Foo": {"Bar"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "add header with existing headers",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Add: map[string]string{"Foo": "Bar"},
|
|
|
|
},
|
|
|
|
requestHeaders: map[string][]string{"Foo": {"Baz"}, "Bar": {"Foo"}},
|
|
|
|
expectedHeaders: map[string][]string{"Foo": {"Baz", "Bar"}, "Bar": {"Foo"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "add multiple headers with existing headers",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Add: map[string]string{"Foo": "Bar", "Bar": "Foo"},
|
|
|
|
},
|
|
|
|
requestHeaders: map[string][]string{"Foo": {"Baz"}, "Bar": {"Foobar"}},
|
|
|
|
expectedHeaders: map[string][]string{"Foo": {"Baz", "Bar"}, "Bar": {"Foobar", "Foo"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove header",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Remove: []string{"Foo"},
|
|
|
|
},
|
|
|
|
expectedHeaders: map[string][]string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove header with existing headers",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Remove: []string{"Foo"},
|
|
|
|
},
|
|
|
|
requestHeaders: map[string][]string{"Foo": {"Baz"}, "Bar": {"Foo"}},
|
|
|
|
expectedHeaders: map[string][]string{"Bar": {"Foo"}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "remove multiple headers with existing headers",
|
2024-08-12 09:34:04 +00:00
|
|
|
config: dynamic.HeaderModifier{
|
2024-04-05 15:18:03 +00:00
|
|
|
Remove: []string{"Foo", "Bar"},
|
|
|
|
},
|
|
|
|
requestHeaders: map[string][]string{"Foo": {"Bar"}, "Bar": {"Foo"}, "Baz": {"Bar"}},
|
|
|
|
expectedHeaders: map[string][]string{"Baz": {"Bar"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var gotHeaders http.Header
|
|
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
gotHeaders = r.Header
|
|
|
|
})
|
|
|
|
|
2024-06-13 15:06:04 +00:00
|
|
|
handler := NewRequestHeaderModifier(context.Background(), next, test.config, "foo-request-header-modifier")
|
2024-04-05 15:18:03 +00:00
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost", nil)
|
2024-08-12 09:34:04 +00:00
|
|
|
for h, v := range test.requestHeaders {
|
|
|
|
req.Header[h] = v
|
2024-04-05 15:18:03 +00:00
|
|
|
}
|
|
|
|
resp := httptest.NewRecorder()
|
2024-08-12 09:34:04 +00:00
|
|
|
|
2024-04-05 15:18:03 +00:00
|
|
|
handler.ServeHTTP(resp, req)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expectedHeaders, gotHeaders)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|