MCPcopy Create free account
hub / github.com/TomDoesTech/GOTTH / TestCSPMiddleware

Function TestCSPMiddleware

internal/middleware/middleware_test.go:11–60  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

9)
10
11func TestCSPMiddleware(t *testing.T) {
12
13 testCases := []struct {
14 name string
15 }{
16 {
17 name: "success",
18 },
19 }
20
21 for _, tc := range testCases {
22
23 t.Run(tc.name, func(t *testing.T) {
24
25 assert := assert.New(t)
26
27 next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
28 ctx := r.Context()
29
30 nonces := GetNonces(ctx)
31 twNonce := GetTwNonce(ctx)
32 htmxNonce := GetHtmxNonce(ctx)
33 responseTargetsNonce := GetResponseTargetsNonce(ctx)
34
35 assert.Equal(nonces.Tw, twNonce)
36 assert.Len(twNonce, 32)
37
38 assert.Equal(nonces.Htmx, htmxNonce)
39 assert.Len(htmxNonce, 32)
40
41 assert.Equal(nonces.ResponseTargets, responseTargetsNonce)
42 assert.Len(responseTargetsNonce, 32)
43
44 })
45
46 middleware := CSPMiddleware(next)
47
48 recorder := httptest.NewRecorder()
49 request := httptest.NewRequest("GET", "/", nil)
50
51 middleware.ServeHTTP(recorder, request)
52
53 csp := recorder.Header().Get("Content-Security-Policy")
54
55 assert.NotEmpty(csp)
56
57 })
58 }
59
60}

Callers

nothing calls this directly

Calls 6

GetNoncesFunction · 0.85
GetTwNonceFunction · 0.85
GetHtmxNonceFunction · 0.85
GetResponseTargetsNonceFunction · 0.85
CSPMiddlewareFunction · 0.85
ServeHTTPMethod · 0.45

Tested by

no test coverage detected