Skip to content

Commit 4f79fb6

Browse files
committed
Add small csp test
1 parent ba048b0 commit 4f79fb6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

coderd/httpmw/csp_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
11
package httpmw_test
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/coder/coder/coderd/httpmw"
12+
)
13+
14+
func TestCSPConnect(t *testing.T) {
15+
t.Parallel()
16+
17+
expected := []string{"example.com", "coder.com"}
18+
19+
r := httptest.NewRequest(http.MethodGet, "/", nil)
20+
rw := httptest.NewRecorder()
21+
22+
httpmw.CSPHeaders(func() []string {
23+
return expected
24+
})(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
25+
rw.WriteHeader(http.StatusOK)
26+
})).ServeHTTP(rw, r)
27+
28+
require.NotEmpty(t, rw.Header().Get("Content-Security-Policy"), "Content-Security-Policy header should not be empty")
29+
for _, e := range expected {
30+
require.Containsf(t, rw.Header().Get("Content-Security-Policy"), fmt.Sprintf("ws://%s", e), "Content-Security-Policy header should contain ws://%s", e)
31+
require.Containsf(t, rw.Header().Get("Content-Security-Policy"), fmt.Sprintf("wss://%s", e), "Content-Security-Policy header should contain wss://%s", e)
32+
}
33+
}

0 commit comments

Comments
 (0)