2020-10-06 22:27:36 +00:00
|
|
|
// Copyright 2020 the Pinniped contributors. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package securityheader
|
|
|
|
|
|
|
|
import (
|
2020-12-16 03:34:34 +00:00
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
2020-10-06 22:27:36 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2020-12-16 03:34:34 +00:00
|
|
|
"time"
|
2020-10-06 22:27:36 +00:00
|
|
|
|
2020-12-16 03:34:34 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-06 22:27:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrap(t *testing.T) {
|
2020-12-16 03:34:34 +00:00
|
|
|
testServer := httptest.NewServer(Wrap(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-Test-Header", "test value")
|
2020-10-06 22:27:36 +00:00
|
|
|
_, _ = w.Write([]byte("hello world"))
|
2020-12-16 03:34:34 +00:00
|
|
|
})))
|
|
|
|
t.Cleanup(testServer.Close)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, testServer.URL, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
require.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "hello world", string(respBody))
|
|
|
|
|
|
|
|
expected := http.Header{
|
|
|
|
"X-Test-Header": []string{"test value"},
|
2020-10-06 22:27:36 +00:00
|
|
|
"Content-Security-Policy": []string{"default-src 'none'; frame-ancestors 'none'"},
|
|
|
|
"Content-Type": []string{"text/plain; charset=utf-8"},
|
|
|
|
"Referrer-Policy": []string{"no-referrer"},
|
|
|
|
"X-Content-Type-Options": []string{"nosniff"},
|
|
|
|
"X-Frame-Options": []string{"DENY"},
|
|
|
|
"X-Xss-Protection": []string{"1; mode=block"},
|
2020-12-14 23:28:32 +00:00
|
|
|
"X-Dns-Prefetch-Control": []string{"off"},
|
|
|
|
"Cache-Control": []string{"no-cache", "no-store", "max-age=0", "must-revalidate"},
|
|
|
|
"Pragma": []string{"no-cache"},
|
|
|
|
"Expires": []string{"0"},
|
2020-12-16 03:34:34 +00:00
|
|
|
}
|
|
|
|
for key, values := range expected {
|
|
|
|
assert.Equalf(t, values, resp.Header.Values(key), "unexpected values for header %s", key)
|
|
|
|
}
|
2020-10-06 22:27:36 +00:00
|
|
|
}
|