blob: 57f83dc4fc71d2e4431ed6220633408850de47c2 [file] [log] [blame]
Jarno Huuskonen3759fe72019-01-09 15:40:27 +02001varnishtest "haproxy host header: map / redirect tests"
Ilya Shipitsin9ab31382019-04-17 12:19:56 +05002#REQUIRE_OPTIONS=PCRE
Jarno Huuskonen3759fe72019-01-09 15:40:27 +02003feature ignore_unknown_macro
4
5#REQUIRE_VERSION=1.6
6
7server s1 {
8 rxreq
9 expect req.method == "GET"
10 expect req.http.host == "test1.example.com"
11 txresp -body "test1 ok"
12} -start
13
14server s2 {
15 rxreq
16 expect req.method == "GET"
17 expect req.http.host == "test2.example.com"
18 txresp -body "test2 ok"
19} -start
20
21server s3 {
22 rxreq
23 expect req.method == "GET"
24 expect req.http.host == "test3.example.com"
25 txresp -body "test3 ok"
26} -start
27
28server s4 {
29 rxreq
30 expect req.method == "GET"
31 expect req.http.host == "test1.example.invalid"
32 txresp -body "test1 after del map ok"
33} -start
34
35haproxy h1 -conf {
36 defaults
37 mode http
38 ${no-htx} option http-use-htx
39 log global
40 option httplog
41 timeout connect 15ms
42 timeout client 20ms
43 timeout server 20ms
44
45 frontend fe1
46 bind "fd@${fe1}"
47
48 # redirect Host: example.org / subdomain.example.org
Frédéric Lécailleb894f922019-03-29 16:13:48 +010049 http-request redirect prefix %[req.hdr(Host),lower,regsub(:\d+$,,),map_str(${testdir}/map_redirect.map)] code 301 if { hdr(Host),lower,regsub(:\d+$,,),map_str(${testdir}/map_redirect.map) -m found }
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020050
51 # set var and redirect in be1
Frédéric Lécailleb894f922019-03-29 16:13:48 +010052 http-request set-var(txn.testvar) req.hdr(Testvar),lower,regsub(:\d+$,,),map_str(${testdir}/map_redirect.map) if { hdr(Testvar),lower,regsub(:\d+$,,),map_str(${testdir}/map_redirect.map) -m found }
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020053
54 # use map to select backend (no default map value)
Frédéric Lécailleb894f922019-03-29 16:13:48 +010055 use_backend %[req.hdr(Host),lower,map_dom(${testdir}/map_redirect-be.map)] if { hdr_dom(Host) -i test1.example.com || hdr_dom(Host) -i test2.example.com }
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020056
57 # use map to select backend with default value(test3_be)
Frédéric Lécailleb894f922019-03-29 16:13:48 +010058 use_backend %[req.hdr(Host),lower,map_dom(${testdir}/map_redirect-be.map,test3_be)] if { hdr_dom(Host) -m end -i example.com }
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020059
60 # use map(after del map test1.example.com) default value(test4_be)
Frédéric Lécailleb894f922019-03-29 16:13:48 +010061 use_backend %[req.hdr(Host),lower,map_dom(${testdir}/map_redirect-be.map,test4_be)] if { hdr_dom(Host) -m end -i example.invalid }
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020062
63 default_backend be1
64
65 backend be1
66 http-request redirect prefix %[var(txn.testvar)] code 301 if { var(txn.testvar) -m found }
67 http-request deny
68
69 backend test1_be
70 server s1 ${s1_addr}:${s1_port}
71
72 backend test2_be
73 server s2 ${s2_addr}:${s2_port}
74
75 backend test3_be
76 server s3 ${s3_addr}:${s3_port}
77
78 backend test4_be
79 server s4 ${s4_addr}:${s4_port}
80} -start
81
82# Check map redirects
83client c1 -connect ${h1_fe1_sock} {
84 txreq -hdr "Host: example.org:8443"
85 rxresp
86 expect resp.status == 301
87 expect resp.http.location ~ "https://www.example.org"
Christopher Faulet70578982019-04-01 11:21:57 +020088 # Closes connection
89} -run
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020090
Christopher Faulet70578982019-04-01 11:21:57 +020091client c2 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020092 txreq -hdr "Host: subdomain.example.org"
93 rxresp
94 expect resp.status == 301
95 expect resp.http.location ~ "https://www.subdomain.example.org"
Christopher Faulet70578982019-04-01 11:21:57 +020096 # Closes connection
97} -run
Jarno Huuskonen3759fe72019-01-09 15:40:27 +020098
Christopher Faulet70578982019-04-01 11:21:57 +020099client c3 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200100 # redirect on Testvar header
101 txreq -hdr "Testvar: subdomain.example.org"
102 rxresp
103 expect resp.status == 301
104 expect resp.http.location ~ "https://www.subdomain.example.org"
Christopher Faulet70578982019-04-01 11:21:57 +0200105 # Closes connection
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200106} -run
107
Christopher Faulet70578982019-04-01 11:21:57 +0200108client c4 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200109 txreq -hdr "Host: www.subdomain.example.org"
110 rxresp
111 expect resp.status == 403
112 # Closes connection
113} -run
114
Christopher Faulet70578982019-04-01 11:21:57 +0200115client c5 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200116 txreq -hdr "Testvar: www.subdomain.example.org"
117 rxresp
118 expect resp.status == 403
119 # Closes connection
120} -run
121
Christopher Faulet70578982019-04-01 11:21:57 +0200122client c6 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200123 txreq -hdr "Host: :8443example.org"
124 rxresp
125 expect resp.status == 403
126 # Closes connection
127} -run
128
129# Check map backend selection
Christopher Faulet70578982019-04-01 11:21:57 +0200130client c7 -connect ${h1_fe1_sock} {
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200131 txreq -hdr "Host: test1.example.com"
132 rxresp
133 expect resp.status == 200
134 expect resp.body == "test1 ok"
135
136 txreq -hdr "Host: test2.example.com"
137 rxresp
138 expect resp.status == 200
139 expect resp.body == "test2 ok"
140
141 txreq -hdr "Host: test3.example.com"
142 rxresp
143 expect resp.status == 200
144 expect resp.body == "test3 ok"
145} -run
146
147# cli show maps
148haproxy h1 -cli {
Frédéric Lécailleb894f922019-03-29 16:13:48 +0100149 send "show map ${testdir}/map_redirect.map"
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200150 expect ~ "^0x[a-f0-9]+ example\\.org https://www\\.example\\.org\\n0x[a-f0-9]+ subdomain\\.example\\.org https://www\\.subdomain\\.example\\.org\\n$"
151
Frédéric Lécailleb894f922019-03-29 16:13:48 +0100152 send "show map ${testdir}/map_redirect-be.map"
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200153 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$"
154}
155
156haproxy h1 -cli {
Frédéric Lécailleb894f922019-03-29 16:13:48 +0100157 # clear map ${testdir}/map_redirect.map
158 send "clear map ${testdir}/map_redirect.map"
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200159 expect ~ "^\\n"
160
Frédéric Lécailleb894f922019-03-29 16:13:48 +0100161 send "show map ${testdir}/map_redirect.map"
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200162 expect ~ "^\\n"
163
Frédéric Lécailleb894f922019-03-29 16:13:48 +0100164 # del map ${testdir}/map_redirect-be.map test1.example.{com,invalid}
165 send "del map ${testdir}/map_redirect-be.map test1.example.com"
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200166 expect ~ "^\\n"
167
Frédéric Lécailleb894f922019-03-29 16:13:48 +0100168 send "del map ${testdir}/map_redirect-be.map test1.example.invalid"
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200169 expect ~ "^\\n"
170
Frédéric Lécailleb894f922019-03-29 16:13:48 +0100171 send "show map ${testdir}/map_redirect-be.map"
Jarno Huuskonen3759fe72019-01-09 15:40:27 +0200172 expect ~ "^0x[a-f0-9]+ test2\\.example\\.com test2_be\\n$"
173}
174
175# Check map backend after del map
176client c6 -connect ${h1_fe1_sock} {
177 # test1.example.invalid should go to test4_be after del map
178 txreq -hdr "Host: test1.example.invalid"
179 rxresp
180 expect resp.status == 200
181 expect resp.body == "test1 after del map ok"
182} -run