@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 268 lines 7.63 KB
Raw
1 package httpcaddyfile
2
3 import (
4 "strings"
5 "testing"
6 )
7
8 func TestSystemdListenPlaceholderSurvivesAdaptation(t *testing.T) {
9 adapted := adaptCaddyfile(t, `{
10 auto_https off
11 }
12
13 http://localhost {
14 bind fd/{systemd.listen.web}
15 respond ok
16 }`)
17 if !strings.Contains(adapted, `"listen":["fd/{systemd.listen.web}"]`) {
18 t.Fatalf("adapted listener lost systemd placeholder: %s", adapted)
19 }
20 }
21
22 func TestParseAddress(t *testing.T) {
23 for i, test := range []struct {
24 input string
25 scheme, host, port, path string
26 shouldErr bool
27 }{
28 {``, "", "", "", "", false},
29 {`localhost`, "", "localhost", "", "", false},
30 {`localhost:1234`, "", "localhost", "1234", "", false},
31 {`localhost:`, "", "localhost", "", "", false},
32 {`0.0.0.0`, "", "0.0.0.0", "", "", false},
33 {`127.0.0.1:1234`, "", "127.0.0.1", "1234", "", false},
34 {`:1234`, "", "", "1234", "", false},
35 {`[::1]`, "", "::1", "", "", false},
36 {`[::1]:1234`, "", "::1", "1234", "", false},
37 {`:`, "", "", "", "", false},
38 {`:http`, "", "", "", "", true},
39 {`:https`, "", "", "", "", true},
40 {`localhost:http`, "", "", "", "", true}, // using service name in port is verboten, as of Go 1.12.8
41 {`localhost:https`, "", "", "", "", true},
42 {`http://localhost:https`, "", "", "", "", true}, // conflict
43 {`http://localhost:http`, "", "", "", "", true}, // repeated scheme
44 {`host:https/path`, "", "", "", "", true},
45 {`http://localhost:443`, "http", "localhost", "443", "", false}, // NOTE: not conventional
46 {`https://localhost:80`, "https", "localhost", "80", "", false}, // NOTE: not conventional
47 {`http://localhost`, "http", "localhost", "", "", false},
48 {`https://localhost`, "https", "localhost", "", "", false},
49 {`http://{env.APP_DOMAIN}`, "http", "{env.APP_DOMAIN}", "", "", false},
50 {`{env.APP_DOMAIN}:80`, "", "{env.APP_DOMAIN}", "80", "", false},
51 {`{env.APP_DOMAIN}/path`, "", "{env.APP_DOMAIN}", "", "/path", false},
52 {`example.com/{env.APP_PATH}`, "", "example.com", "", "/{env.APP_PATH}", false},
53 {`http://127.0.0.1`, "http", "127.0.0.1", "", "", false},
54 {`https://127.0.0.1`, "https", "127.0.0.1", "", "", false},
55 {`http://[::1]`, "http", "::1", "", "", false},
56 {`http://localhost:1234`, "http", "localhost", "1234", "", false},
57 {`https://127.0.0.1:1234`, "https", "127.0.0.1", "1234", "", false},
58 {`http://[::1]:1234`, "http", "::1", "1234", "", false},
59 {``, "", "", "", "", false},
60 {`::1`, "", "::1", "", "", false},
61 {`localhost::`, "", "localhost::", "", "", false},
62 {`#$%@`, "", "#$%@", "", "", false}, // don't want to presume what the hostname could be
63 {`host/path`, "", "host", "", "/path", false},
64 {`http://host/`, "http", "host", "", "/", false},
65 {`//asdf`, "", "", "", "//asdf", false},
66 {`:1234/asdf`, "", "", "1234", "/asdf", false},
67 {`http://host/path`, "http", "host", "", "/path", false},
68 {`https://host:443/path/foo`, "https", "host", "443", "/path/foo", false},
69 {`host:80/path`, "", "host", "80", "/path", false},
70 {`/path`, "", "", "", "/path", false},
71 } {
72 actual, err := ParseAddress(test.input)
73
74 if err != nil && !test.shouldErr {
75 t.Errorf("Test %d (%s): Expected no error, but had error: %v", i, test.input, err)
76 }
77 if err == nil && test.shouldErr {
78 t.Errorf("Test %d (%s): Expected error, but had none (%#v)", i, test.input, actual)
79 }
80
81 if !test.shouldErr && actual.Original != test.input {
82 t.Errorf("Test %d (%s): Expected original '%s', got '%s'", i, test.input, test.input, actual.Original)
83 }
84 if actual.Scheme != test.scheme {
85 t.Errorf("Test %d (%s): Expected scheme '%s', got '%s'", i, test.input, test.scheme, actual.Scheme)
86 }
87 if actual.Host != test.host {
88 t.Errorf("Test %d (%s): Expected host '%s', got '%s'", i, test.input, test.host, actual.Host)
89 }
90 if actual.Port != test.port {
91 t.Errorf("Test %d (%s): Expected port '%s', got '%s'", i, test.input, test.port, actual.Port)
92 }
93 if actual.Path != test.path {
94 t.Errorf("Test %d (%s): Expected path '%s', got '%s'", i, test.input, test.path, actual.Path)
95 }
96 }
97 }
98
99 func TestAddressString(t *testing.T) {
100 for i, test := range []struct {
101 addr Address
102 expected string
103 }{
104 {Address{Scheme: "http", Host: "host", Port: "1234", Path: "/path"}, "http://host:1234/path"},
105 {Address{Scheme: "", Host: "host", Port: "", Path: ""}, "http://host"},
106 {Address{Scheme: "", Host: "host", Port: "80", Path: ""}, "http://host"},
107 {Address{Scheme: "", Host: "host", Port: "443", Path: ""}, "https://host"},
108 {Address{Scheme: "https", Host: "host", Port: "443", Path: ""}, "https://host"},
109 {Address{Scheme: "https", Host: "host", Port: "", Path: ""}, "https://host"},
110 {Address{Scheme: "", Host: "host", Port: "80", Path: "/path"}, "http://host/path"},
111 {Address{Scheme: "http", Host: "", Port: "1234", Path: ""}, "http://:1234"},
112 {Address{Scheme: "", Host: "", Port: "", Path: ""}, ""},
113 } {
114 actual := test.addr.String()
115 if actual != test.expected {
116 t.Errorf("Test %d: expected '%s' but got '%s'", i, test.expected, actual)
117 }
118 }
119 }
120
121 func TestKeyNormalization(t *testing.T) {
122 testCases := []struct {
123 input string
124 expect Address
125 }{
126 {
127 input: "example.com",
128 expect: Address{
129 Host: "example.com",
130 },
131 },
132 {
133 input: "http://host:1234/path",
134 expect: Address{
135 Scheme: "http",
136 Host: "host",
137 Port: "1234",
138 Path: "/path",
139 },
140 },
141 {
142 input: "HTTP://A/ABCDEF",
143 expect: Address{
144 Scheme: "http",
145 Host: "a",
146 Path: "/ABCDEF",
147 },
148 },
149 {
150 input: "A/ABCDEF",
151 expect: Address{
152 Host: "a",
153 Path: "/ABCDEF",
154 },
155 },
156 {
157 input: "A:2015/Path",
158 expect: Address{
159 Host: "a",
160 Port: "2015",
161 Path: "/Path",
162 },
163 },
164 {
165 input: "sub.{env.MY_DOMAIN}",
166 expect: Address{
167 Host: "sub.{env.MY_DOMAIN}",
168 },
169 },
170 {
171 input: "sub.ExAmPle",
172 expect: Address{
173 Host: "sub.example",
174 },
175 },
176 {
177 input: "sub.\\{env.MY_DOMAIN\\}",
178 expect: Address{
179 Host: "sub.\\{env.my_domain\\}",
180 },
181 },
182 {
183 input: "sub.{env.MY_DOMAIN}.com",
184 expect: Address{
185 Host: "sub.{env.MY_DOMAIN}.com",
186 },
187 },
188 {
189 input: ":80",
190 expect: Address{
191 Port: "80",
192 },
193 },
194 {
195 input: ":443",
196 expect: Address{
197 Port: "443",
198 },
199 },
200 {
201 input: ":1234",
202 expect: Address{
203 Port: "1234",
204 },
205 },
206 {
207 input: "",
208 expect: Address{},
209 },
210 {
211 input: ":",
212 expect: Address{},
213 },
214 {
215 input: "[::]",
216 expect: Address{
217 Host: "::",
218 },
219 },
220 {
221 input: "127.0.0.1",
222 expect: Address{
223 Host: "127.0.0.1",
224 },
225 },
226 {
227 input: "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:1234",
228 expect: Address{
229 Host: "2001:db8:85a3:8d3:1319:8a2e:370:7348",
230 Port: "1234",
231 },
232 },
233 {
234 // IPv4 address in IPv6 form (#4381)
235 input: "[::ffff:cff4:e77d]:1234",
236 expect: Address{
237 Host: "::ffff:cff4:e77d",
238 Port: "1234",
239 },
240 },
241 {
242 input: "::ffff:cff4:e77d",
243 expect: Address{
244 Host: "::ffff:cff4:e77d",
245 },
246 },
247 }
248 for i, tc := range testCases {
249 addr, err := ParseAddress(tc.input)
250 if err != nil {
251 t.Errorf("Test %d: Parsing address '%s': %v", i, tc.input, err)
252 continue
253 }
254 actual := addr.Normalize()
255 if actual.Scheme != tc.expect.Scheme {
256 t.Errorf("Test %d: Input '%s': Expected Scheme='%s' but got Scheme='%s'", i, tc.input, tc.expect.Scheme, actual.Scheme)
257 }
258 if actual.Host != tc.expect.Host {
259 t.Errorf("Test %d: Input '%s': Expected Host='%s' but got Host='%s'", i, tc.input, tc.expect.Host, actual.Host)
260 }
261 if actual.Port != tc.expect.Port {
262 t.Errorf("Test %d: Input '%s': Expected Port='%s' but got Port='%s'", i, tc.input, tc.expect.Port, actual.Port)
263 }
264 if actual.Path != tc.expect.Path {
265 t.Errorf("Test %d: Input '%s': Expected Path='%s' but got Path='%s'", i, tc.input, tc.expect.Path, actual.Path)
266 }
267 }
268 }