traefik/pkg/middlewares/headers/headers_test.go

114 lines
2.8 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package headers
// Middleware tests based on https://github.com/unrolled/secure
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/tracing"
2018-11-14 09:18:03 +00:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew_withoutOptions(t *testing.T) {
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
2018-11-14 09:18:03 +00:00
mid, err := New(context.Background(), next, dynamic.Headers{}, "testing")
require.Errorf(t, err, "headers configuration not valid")
2018-11-14 09:18:03 +00:00
assert.Nil(t, mid)
2018-11-14 09:18:03 +00:00
}
func TestNew_allowedHosts(t *testing.T) {
2018-11-14 09:18:03 +00:00
testCases := []struct {
desc string
fromHost string
expected int
}{
{
desc: "Should accept the request when given a host that is in the list",
fromHost: "foo.com",
expected: http.StatusOK,
},
{
desc: "Should refuse the request when no host is given",
fromHost: "",
expected: http.StatusInternalServerError,
},
{
desc: "Should refuse the request when no matching host is given",
fromHost: "boo.com",
expected: http.StatusInternalServerError,
},
}
emptyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
cfg := dynamic.Headers{
2018-11-14 09:18:03 +00:00
AllowedHosts: []string{"foo.com", "bar.com"},
}
mid, err := New(context.Background(), emptyHandler, cfg, "foo")
2018-11-14 09:18:03 +00:00
require.NoError(t, err)
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
2018-11-14 09:18:03 +00:00
req.Host = test.fromHost
2018-11-19 15:40:03 +00:00
rw := httptest.NewRecorder()
mid.ServeHTTP(rw, req)
assert.Equal(t, test.expected, rw.Code)
2018-11-19 15:40:03 +00:00
})
}
}
2019-04-02 08:40:04 +00:00
func TestNew_customHeaders(t *testing.T) {
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
2019-04-02 08:40:04 +00:00
cfg := dynamic.Headers{
CustomRequestHeaders: map[string]string{
"X-Custom-Request-Header": "test_request",
2019-04-02 08:40:04 +00:00
},
CustomResponseHeaders: map[string]string{
"X-Custom-Response-Header": "test_response",
},
2019-04-02 08:40:04 +00:00
}
mid, err := New(context.Background(), next, cfg, "testing")
require.NoError(t, err)
2019-04-02 08:40:04 +00:00
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
2019-04-02 08:40:04 +00:00
rw := httptest.NewRecorder()
2019-04-02 08:40:04 +00:00
mid.ServeHTTP(rw, req)
2019-04-02 08:40:04 +00:00
assert.Equal(t, http.StatusOK, rw.Code)
2019-04-02 08:40:04 +00:00
assert.Equal(t, "test_request", req.Header.Get("X-Custom-Request-Header"))
assert.Equal(t, "test_response", rw.Header().Get("X-Custom-Response-Header"))
2019-04-02 08:40:04 +00:00
}
func Test_headers_getTracingInformation(t *testing.T) {
2019-04-02 08:40:04 +00:00
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
mid := &headers{
2019-04-02 08:40:04 +00:00
handler: next,
name: "testing",
}
name, trace := mid.GetTracingInformation()
2019-04-02 08:40:04 +00:00
assert.Equal(t, "testing", name)
assert.Equal(t, tracing.SpanKindNoneEnum, trace)
}