-
Notifications
You must be signed in to change notification settings - Fork 0
/
cspolicy_test.go
55 lines (48 loc) · 1.37 KB
/
cspolicy_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
47
48
49
50
51
52
53
54
55
package cspolicy
import (
"testing"
"github.com/tiptophelmet/cspolicy/directives"
"github.com/tiptophelmet/cspolicy/src"
)
func TestEmptyBuild(t *testing.T) {
gotVal := Build()
wantVal := ""
if gotVal != wantVal {
t.Errorf("got: %s, want: %s", gotVal, wantVal)
}
}
func TestBuild(t *testing.T) {
gotVal := Build(
directives.DefaultSrc(src.None()),
directives.BaseURI(src.Self(), src.Host("*.example.com")),
directives.ChildSrc(
src.Host("cdn.example.com/assets"),
src.Host("resources.example.com/artifacts"),
),
directives.ConnectSrc(
src.Host("uploads.example.com"),
src.Host("status.example.com"),
src.Host("api.example.com"),
),
directives.FrameSrc(
src.Host("notes.example.com"),
src.Host("viewbox.example.com"),
),
directives.ImgSrc(
src.Self(),
src.Scheme("data:"),
src.Host("media.example.com"),
src.Host("avatars.example.com"),
),
directives.UpgradeInsecureRequests(),
)
wantVal := "default-src 'none'; base-uri 'self' *.example.com; " +
"child-src cdn.example.com/assets resources.example.com/artifacts; " +
"connect-src uploads.example.com status.example.com api.example.com; " +
"frame-src notes.example.com viewbox.example.com; " +
"img-src 'self' data: media.example.com avatars.example.com; " +
"upgrade-insecure-requests"
if gotVal != wantVal {
t.Errorf("got: %s, want: %s", gotVal, wantVal)
}
}