-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaderrules_test.go
More file actions
106 lines (100 loc) · 2.26 KB
/
Copy pathheaderrules_test.go
File metadata and controls
106 lines (100 loc) · 2.26 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package zeaburcaddyextension_test
import (
"reflect"
"testing"
zeaburcaddyextension "github.com/zeabur/caddy-extension"
)
// TestParseConfig tests the ParseConfig function
func TestParseConfig(t *testing.T) {
tests := []struct {
name string
input string
expected []zeaburcaddyextension.HeaderConfig
wantErr bool
}{
{
name: "Basic test",
input: `
/templates/index.html
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
/templates/index2.html
X-Frame-Options: SAMEORIGIN`,
expected: []zeaburcaddyextension.HeaderConfig{
{
Path: "/templates/index.html",
Headers: map[string]string{
"X-Frame-Options": "DENY",
"X-XSS-Protection": "1; mode=block",
},
},
{
Path: "/templates/index2.html",
Headers: map[string]string{
"X-Frame-Options": "SAMEORIGIN",
},
},
},
wantErr: false,
},
{
name: "Test with comments and empty lines",
input: `
# a path:
/templates/index.html
# headers for that path:
X-Frame-Options: DENY
# another path:
/templates/index2.html
X-Frame-Options: SAMEORIGIN
# This is a comment
/secure/page
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Referrer-Policy: no-referrer`,
expected: []zeaburcaddyextension.HeaderConfig{
{
Path: "/templates/index.html",
Headers: map[string]string{
"X-Frame-Options": "DENY",
},
},
{
Path: "/templates/index2.html",
Headers: map[string]string{
"X-Frame-Options": "SAMEORIGIN",
},
},
{
Path: "/secure/page",
Headers: map[string]string{
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "no-referrer",
},
},
},
wantErr: false,
},
{
name: "Invalid header line",
input: `
/templates/index.html
X-Frame-Options DENY`, // Missing colon
expected: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := zeaburcaddyextension.ParseHeaderConfig(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.expected) {
t.Errorf("ParseConfig() = %v, expected %v", got, tt.expected)
}
})
}
}