2021-09-15 08:36:14 +00:00
|
|
|
package dashboard
|
2020-07-28 08:08:03 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-15 08:36:14 +00:00
|
|
|
"errors"
|
|
|
|
"io/fs"
|
2020-07-28 08:08:03 +00:00
|
|
|
"net/http"
|
2021-02-18 13:54:03 +00:00
|
|
|
"net/http/httptest"
|
2020-07-28 08:08:03 +00:00
|
|
|
"testing"
|
2021-09-15 08:36:14 +00:00
|
|
|
"testing/fstest"
|
|
|
|
"time"
|
2020-07-28 08:08:03 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-02-18 13:54:03 +00:00
|
|
|
func Test_ContentSecurityPolicy(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2021-09-15 08:36:14 +00:00
|
|
|
handler Handler
|
2021-02-18 13:54:03 +00:00
|
|
|
expected int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "OK",
|
2021-09-15 08:36:14 +00:00
|
|
|
handler: Handler{
|
|
|
|
assets: fstest.MapFS{"foobar.html": &fstest.MapFile{
|
2021-09-23 13:30:01 +00:00
|
|
|
Mode: 0o755,
|
2021-09-15 08:36:14 +00:00
|
|
|
ModTime: time.Now(),
|
|
|
|
}},
|
2021-02-18 13:54:03 +00:00
|
|
|
},
|
|
|
|
expected: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Not found",
|
2021-09-15 08:36:14 +00:00
|
|
|
handler: Handler{
|
|
|
|
assets: fstest.MapFS{},
|
2021-02-18 13:54:03 +00:00
|
|
|
},
|
|
|
|
expected: http.StatusNotFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Internal server error",
|
2021-09-15 08:36:14 +00:00
|
|
|
handler: Handler{
|
|
|
|
assets: errorFS{},
|
2021-02-18 13:54:03 +00:00
|
|
|
},
|
|
|
|
expected: http.StatusInternalServerError,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/foobar.html", nil)
|
|
|
|
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
|
|
|
|
test.handler.ServeHTTP(rw, req)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, rw.Code)
|
|
|
|
assert.Equal(t, "frame-src 'self' https://traefik.io https://*.traefik.io;", rw.Result().Header.Get("Content-Security-Policy"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-09-15 08:36:14 +00:00
|
|
|
|
|
|
|
type errorFS struct{}
|
|
|
|
|
|
|
|
func (e errorFS) Open(name string) (fs.File, error) {
|
|
|
|
return nil, errors.New("oops")
|
|
|
|
}
|