blob: 64cedc292ed5da0d090f72ee7a46aa3341a82ffb [file] [log] [blame]
Jarno Huuskonen3759fe72019-01-09 15:40:27 +02001varnishtest "haproxy host header: map / redirect tests"
2feature ignore_unknown_macro
3
4#REQUIRE_VERSION=1.6
5
6server s1 {
7 rxreq
8 expect req.method == "GET"
9 expect req.http.host == "test1.example.com"
10 txresp -body "test1 ok"
11} -start
12
13server s2 {
14 rxreq
15 expect req.method == "GET"
16 expect req.http.host == "test2.example.com"
17 txresp -body "test2 ok"
18} -start
19
20server s3 {
21 rxreq
22 expect req.method == "GET"
23 expect req.http.host == "test3.example.com"
24 txresp -body "test3 ok"
25} -start
26
27server s4 {
28 rxreq
29 expect req.method == "GET"
30 expect req.http.host == "test1.example.invalid"
31 txresp -body "test1 after del map ok"
32} -start
33
34haproxy h1 -conf {
35 defaults
36 mode http
37 ${no-htx} option http-use-htx
38 log global
39 option httplog
40 timeout connect 15ms
41 timeout client 20ms
42 timeout server 20ms
43
44 frontend fe1
45 bind "fd@${fe1}"
46
47 # redirect Host: example.org / subdomain.example.org
48 http-request redirect prefix %[req.hdr(Host),lower,regsub(:\d+$,,),map_str(${testdir}/h00003.map)] code 301 if { hdr(Host),lower,regsub(:\d+$,,),map_str(${testdir}/h00003.map) -m found }
49
50 # set var and redirect in be1
51 http-request set-var(txn.testvar) req.hdr(Testvar),lower,regsub(:\d+$,,),map_str(${testdir}/h00003.map) if { hdr(Testvar),lower,regsub(:\d+$,,),map_str(${testdir}/h00003.map) -m found }
52
53 # use map to select backend (no default map value)
54 use_backend %[req.hdr(Host),lower,map_dom(${testdir}/h00003-be.map)] if { hdr_dom(Host) -i test1.example.com || hdr_dom(Host) -i test2.example.com }
55
56 # use map to select backend with default value(test3_be)
57 use_backend %[req.hdr(Host),lower,map_dom(${testdir}/h00003-be.map,test3_be)] if { hdr_dom(Host) -m end -i example.com }
58
59 # use map(after del map test1.example.com) default value(test4_be)
60 use_backend %[req.hdr(Host),lower,map_dom(${testdir}/h00003-be.map,test4_be)] if { hdr_dom(Host) -m end -i example.invalid }
61
62 default_backend be1
63
64 backend be1
65 http-request redirect prefix %[var(txn.testvar)] code 301 if { var(txn.testvar) -m found }
66 http-request deny
67
68 backend test1_be
69 server s1 ${s1_addr}:${s1_port}
70
71 backend test2_be
72 server s2 ${s2_addr}:${s2_port}
73
74 backend test3_be
75 server s3 ${s3_addr}:${s3_port}
76
77 backend test4_be
78 server s4 ${s4_addr}:${s4_port}
79} -start
80
81# Check map redirects
82client c1 -connect ${h1_fe1_sock} {
83 txreq -hdr "Host: example.org:8443"
84 rxresp
85 expect resp.status == 301
86 expect resp.http.location ~ "https://www.example.org"
Christopher Faulet70578982019-04-01 11:21:57 +020087 # Closes connection
88} -run
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020089
Christopher Faulet70578982019-04-01 11:21:57 +020090client c2 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020091 txreq -hdr "Host: subdomain.example.org"
92 rxresp
93 expect resp.status == 301
94 expect resp.http.location ~ "https://www.subdomain.example.org"
Christopher Faulet70578982019-04-01 11:21:57 +020095 # Closes connection
96} -run
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020097
Christopher Faulet70578982019-04-01 11:21:57 +020098client c3 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020099 # redirect on Testvar header
100 txreq -hdr "Testvar: subdomain.example.org"
101 rxresp
102 expect resp.status == 301
103 expect resp.http.location ~ "https://www.subdomain.example.org"
Christopher Faulet70578982019-04-01 11:21:57 +0200104 # Closes connection
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200105} -run
106
Christopher Faulet70578982019-04-01 11:21:57 +0200107client c4 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200108 txreq -hdr "Host: www.subdomain.example.org"
109 rxresp
110 expect resp.status == 403
111 # Closes connection
112} -run
113
Christopher Faulet70578982019-04-01 11:21:57 +0200114client c5 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200115 txreq -hdr "Testvar: www.subdomain.example.org"
116 rxresp
117 expect resp.status == 403
118 # Closes connection
119} -run
120
Christopher Faulet70578982019-04-01 11:21:57 +0200121client c6 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200122 txreq -hdr "Host: :8443example.org"
123 rxresp
124 expect resp.status == 403
125 # Closes connection
126} -run
127
128# Check map backend selection
Christopher Faulet70578982019-04-01 11:21:57 +0200129client c7 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200130 txreq -hdr "Host: test1.example.com"
131 rxresp
132 expect resp.status == 200
133 expect resp.body == "test1 ok"
134
135 txreq -hdr "Host: test2.example.com"
136 rxresp
137 expect resp.status == 200
138 expect resp.body == "test2 ok"
139
140 txreq -hdr "Host: test3.example.com"
141 rxresp
142 expect resp.status == 200
143 expect resp.body == "test3 ok"
144} -run
145
146# cli show maps
147haproxy h1 -cli {
148 send "show map ${testdir}/h00003.map"
149 expect ~ "^0x[a-f0-9]+ example\\.org https://www\\.example\\.org\\n0x[a-f0-9]+ subdomain\\.example\\.org https://www\\.subdomain\\.example\\.org\\n$"
150
151 send "show map ${testdir}/h00003-be.map"
152 expect ~ "^0x[a-f0-9]+ test1\\.example\\.com test1_be\\n0x[a-f0-9]+ test1\\.example\\.invalid test1_be\\n0x[a-f0-9]+ test2\\.example\\.com test2_be\\n$"
153}
154
155haproxy h1 -cli {
156 # clear map ${testdir}/h00003.map
157 send "clear map ${testdir}/h00003.map"
158 expect ~ "^\\n"
159
160 send "show map ${testdir}/h00003.map"
161 expect ~ "^\\n"
162
163 # del map ${testdir}/h00003-be.map test1.example.{com,invalid}
164 send "del map ${testdir}/h00003-be.map test1.example.com"
165 expect ~ "^\\n"
166
167 send "del map ${testdir}/h00003-be.map test1.example.invalid"
168 expect ~ "^\\n"
169
170 send "show map ${testdir}/h00003-be.map"
171 expect ~ "^0x[a-f0-9]+ test2\\.example\\.com test2_be\\n$"
172}
173
174# Check map backend after del map
175client c6 -connect ${h1_fe1_sock} {
176 # test1.example.invalid should go to test4_be after del map
177 txreq -hdr "Host: test1.example.invalid"
178 rxresp
179 expect resp.status == 200
180 expect resp.body == "test1 after del map ok"
181} -run