@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 337 lines 5.72 KB
Raw
1 package integration
2
3 import (
4 "testing"
5
6 "github.com/caddyserver/caddy/v2/caddytest"
7 )
8
9 func TestDefaultSNI(t *testing.T) {
10 // arrange
11 tester := caddytest.NewTester(t)
12 tester.InitServer(`{
13 "admin": {
14 "listen": "localhost:2999"
15 },
16 "apps": {
17 "http": {
18 "http_port": 9080,
19 "https_port": 9443,
20 "grace_period": 1,
21 "servers": {
22 "srv0": {
23 "listen": [
24 ":9443"
25 ],
26 "routes": [
27 {
28 "handle": [
29 {
30 "handler": "subroute",
31 "routes": [
32 {
33 "handle": [
34 {
35 "body": "hello from a.caddy.localhost",
36 "handler": "static_response",
37 "status_code": 200
38 }
39 ],
40 "match": [
41 {
42 "path": [
43 "/version"
44 ]
45 }
46 ]
47 }
48 ]
49 }
50 ],
51 "match": [
52 {
53 "host": [
54 "127.0.0.1"
55 ]
56 }
57 ],
58 "terminal": true
59 }
60 ],
61 "tls_connection_policies": [
62 {
63 "certificate_selection": {
64 "any_tag": ["cert0"]
65 },
66 "match": {
67 "sni": [
68 "127.0.0.1"
69 ]
70 }
71 },
72 {
73 "default_sni": "*.caddy.localhost"
74 }
75 ]
76 }
77 }
78 },
79 "tls": {
80 "certificates": {
81 "load_files": [
82 {
83 "certificate": "/caddy.localhost.crt",
84 "key": "/caddy.localhost.key",
85 "tags": [
86 "cert0"
87 ]
88 }
89 ]
90 }
91 },
92 "pki": {
93 "certificate_authorities" : {
94 "local" : {
95 "install_trust": false
96 }
97 }
98 }
99 }
100 }
101 `, "json")
102
103 // act and assert
104 // makes a request with no sni
105 tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a.caddy.localhost")
106 }
107
108 func TestDefaultSNIWithNamedHostAndExplicitIP(t *testing.T) {
109 // arrange
110 tester := caddytest.NewTester(t)
111 tester.InitServer(`
112 {
113 "admin": {
114 "listen": "localhost:2999"
115 },
116 "apps": {
117 "http": {
118 "http_port": 9080,
119 "https_port": 9443,
120 "grace_period": 1,
121 "servers": {
122 "srv0": {
123 "listen": [
124 ":9443"
125 ],
126 "routes": [
127 {
128 "handle": [
129 {
130 "handler": "subroute",
131 "routes": [
132 {
133 "handle": [
134 {
135 "body": "hello from a",
136 "handler": "static_response",
137 "status_code": 200
138 }
139 ],
140 "match": [
141 {
142 "path": [
143 "/version"
144 ]
145 }
146 ]
147 }
148 ]
149 }
150 ],
151 "match": [
152 {
153 "host": [
154 "a.caddy.localhost",
155 "127.0.0.1"
156 ]
157 }
158 ],
159 "terminal": true
160 }
161 ],
162 "tls_connection_policies": [
163 {
164 "certificate_selection": {
165 "any_tag": ["cert0"]
166 },
167 "default_sni": "a.caddy.localhost",
168 "match": {
169 "sni": [
170 "a.caddy.localhost",
171 "127.0.0.1",
172 ""
173 ]
174 }
175 },
176 {
177 "default_sni": "a.caddy.localhost"
178 }
179 ]
180 }
181 }
182 },
183 "tls": {
184 "certificates": {
185 "load_files": [
186 {
187 "certificate": "/a.caddy.localhost.crt",
188 "key": "/a.caddy.localhost.key",
189 "tags": [
190 "cert0"
191 ]
192 }
193 ]
194 }
195 },
196 "pki": {
197 "certificate_authorities" : {
198 "local" : {
199 "install_trust": false
200 }
201 }
202 }
203 }
204 }
205 `, "json")
206
207 // act and assert
208 // makes a request with no sni
209 tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a")
210 }
211
212 func TestDefaultSNIWithPortMappingOnly(t *testing.T) {
213 // arrange
214 tester := caddytest.NewTester(t)
215 tester.InitServer(`
216 {
217 "admin": {
218 "listen": "localhost:2999"
219 },
220 "apps": {
221 "http": {
222 "http_port": 9080,
223 "https_port": 9443,
224 "grace_period": 1,
225 "servers": {
226 "srv0": {
227 "listen": [
228 ":9443"
229 ],
230 "routes": [
231 {
232 "handle": [
233 {
234 "body": "hello from a.caddy.localhost",
235 "handler": "static_response",
236 "status_code": 200
237 }
238 ],
239 "match": [
240 {
241 "path": [
242 "/version"
243 ]
244 }
245 ]
246 }
247 ],
248 "tls_connection_policies": [
249 {
250 "certificate_selection": {
251 "any_tag": ["cert0"]
252 },
253 "default_sni": "a.caddy.localhost"
254 }
255 ]
256 }
257 }
258 },
259 "tls": {
260 "certificates": {
261 "load_files": [
262 {
263 "certificate": "/a.caddy.localhost.crt",
264 "key": "/a.caddy.localhost.key",
265 "tags": [
266 "cert0"
267 ]
268 }
269 ]
270 }
271 },
272 "pki": {
273 "certificate_authorities" : {
274 "local" : {
275 "install_trust": false
276 }
277 }
278 }
279 }
280 }
281 `, "json")
282
283 // act and assert
284 // makes a request with no sni
285 tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a.caddy.localhost")
286 }
287
288 func TestHttpOnlyOnDomainWithSNI(t *testing.T) {
289 caddytest.AssertAdapt(t, `
290 {
291 skip_install_trust
292 default_sni a.caddy.localhost
293 }
294 :80 {
295 respond /version 200 {
296 body "hello from localhost"
297 }
298 }
299 `, "caddyfile", `{
300 "apps": {
301 "http": {
302 "servers": {
303 "srv0": {
304 "listen": [
305 ":80"
306 ],
307 "routes": [
308 {
309 "match": [
310 {
311 "path": [
312 "/version"
313 ]
314 }
315 ],
316 "handle": [
317 {
318 "body": "hello from localhost",
319 "handler": "static_response",
320 "status_code": 200
321 }
322 ]
323 }
324 ]
325 }
326 }
327 },
328 "pki": {
329 "certificate_authorities": {
330 "local": {
331 "install_trust": false
332 }
333 }
334 }
335 }
336 }`)
337 }