-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware_test.go
142 lines (140 loc) · 3 KB
/
middleware_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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package caddy_chrome
import (
"encoding/json"
"github.com/alecthomas/assert/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"regexp"
"testing"
)
func TestMiddleware_UnmarshalCaddyfile(t *testing.T) {
re := regexp.MustCompile(`\s+`)
for _, testCase := range []struct {
caddyfile string
json string
}{
{
caddyfile: `chrome`,
json: `{}`,
},
{
caddyfile: `chrome {
}`,
json: `{}`,
},
{
caddyfile: `chrome {
timeout 10s
}`,
json: `{"timeout":"10s"}`,
},
{
caddyfile: `chrome {
mime_types text/html
}`,
json: `{"mime_types":["text/html"]}`,
},
{
caddyfile: `chrome {
mime_types text/html application/xhtml+xml
}`,
json: `{"mime_types":["text/html","application/xhtml+xml"]}`,
},
{
caddyfile: `chrome {
exec
}`,
json: `{"exec_browser":{"default_flags":true}}`,
},
{
caddyfile: `chrome {
exec /usr/bin/chrome
}`,
json: `{"exec_browser":{"path":"/usr/bin/chrome","default_flags":true}}`,
},
{
caddyfile: `chrome {
exec /usr/bin/chrome --
}`,
json: `{"exec_browser":{"path":"/usr/bin/chrome","default_flags":true}}`,
},
{
caddyfile: `chrome {
exec /usr/bin/chrome --headless
}`,
json: `{"exec_browser":{"path":"/usr/bin/chrome","default_flags":true,"flags":["--headless"]}}`,
},
{
caddyfile: `chrome {
exec /usr/bin/chrome -- --headless
}`,
json: `{"exec_browser":{"path":"/usr/bin/chrome","default_flags":true,"flags":["--headless"]}}`,
},
{
caddyfile: `chrome {
exec --headless
}`,
json: `{"exec_browser":{"default_flags":true,"flags":["--headless"]}}`,
},
{
caddyfile: `chrome {
exec_no_default_flags /usr/bin/chrome
}`,
json: `{"exec_browser":{"path":"/usr/bin/chrome"}}`,
},
{
caddyfile: `chrome {
exec_no_default_flags
}`,
json: `{"exec_browser":{}}`,
},
{
caddyfile: `chrome {
url http://localhost:9222/
}`,
json: `{"remote_browser":{"url":"http://localhost:9222/"}}`,
},
{
caddyfile: `chrome {
fulfill_hosts localhost
}`,
json: `{"fulfill_hosts":["localhost"]}`,
},
{
caddyfile: `chrome {
fulfill_hosts my.domain api.my.domain cdn.my.domain
}`,
json: `{"fulfill_hosts":["my.domain","api.my.domain","cdn.my.domain"]}`,
},
{
caddyfile: `chrome {
continue_hosts external-cdn.example.com
}`,
json: `{"continue_hosts":["external-cdn.example.com"]}`,
},
{
caddyfile: `chrome {
continue_hosts external-cdn.example.com analytics.example.com
}`,
json: `{"continue_hosts":["external-cdn.example.com","analytics.example.com"]}`,
},
{
caddyfile: `chrome {
links
}`,
json: `{"links":true}`,
},
} {
t.Run(re.ReplaceAllString(testCase.caddyfile, " "), func(t *testing.T) {
m := new(Middleware)
err := m.UnmarshalCaddyfile(caddyfile.NewTestDispenser(testCase.caddyfile))
if err != nil {
t.Fatal(err)
}
j, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, testCase.json, string(j))
})
}
}