1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-25 13:31:21 +00:00
go_helpers/http/csp_test.go
Knut Ahlers fe36f52937
Add CSP helper
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2023-06-17 14:37:18 +02:00

33 lines
846 B
Go

package http
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCSPClone(t *testing.T) {
c1 := CSP{"default-src": []CSPSourceValue{CSPSrcNone}}
c2 := c1.Clone()
c2.Add("default-src", CSPSrcSelf) // Makes no sense in real world!
assert.NotEqual(t, c1["default-src"], c2["default-src"], "expect c1 not to have changed")
}
func TestCSPToHeaderValue(t *testing.T) {
c := CSP{}
c.Set("default-src", CSPSrcNone)
c.Set("connect-src", CSPSrcSelf)
c.Set("font-src", CSPSrcSelf)
c.Set("img-src", CSPSrcSelf)
c.Add("img-src", CSPSrcSchemeData)
c.Set("script-src", CSPSrcSelf)
c.Add("script-src", CSPSrcUnsafeInline)
c.Set("style-src", CSPSrcSelf)
assert.Equal(t,
"connect-src 'self';default-src 'none';font-src 'self';img-src 'self' data:;script-src 'self' 'unsafe-inline';style-src 'self'",
c.ToHeaderValue())
}