-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecure_headers_test.go
46 lines (44 loc) · 1011 Bytes
/
secure_headers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package midfabs
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestSecureHeaders(t *testing.T) {
base := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
cases := map[string]struct {
handler http.Handler
want []string
}{
"no csp": {
handler: SecureHeaders("")(base),
want: []string{
"Referrer-Policy",
"X-Content-Type-Options",
"X-Frame-Options",
"Strict-Transport-Security",
},
},
"simple csp": {
handler: SecureHeaders("defaut-src 'self';")(base),
want: []string{
"Content-Security-Policy",
"Referrer-Policy",
"X-Content-Type-Options",
"X-Frame-Options",
"Strict-Transport-Security",
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
w := httptest.NewRecorder()
c.handler.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/", nil))
for i, key := range c.want {
if got := w.Header().Get(key); got == "" {
t.Fatalf(errorString, got, c.want[i])
}
}
})
}
}