Jarno Huuskonen | 3759fe7 | 2019-01-09 15:40:27 +0200 | [diff] [blame] | 1 | varnishtest "haproxy host header: map / redirect tests" |
| 2 | feature ignore_unknown_macro |
| 3 | |
| 4 | #REQUIRE_VERSION=1.6 |
| 5 | |
| 6 | server s1 { |
| 7 | rxreq |
| 8 | expect req.method == "GET" |
| 9 | expect req.http.host == "test1.example.com" |
| 10 | txresp -body "test1 ok" |
| 11 | } -start |
| 12 | |
| 13 | server s2 { |
| 14 | rxreq |
| 15 | expect req.method == "GET" |
| 16 | expect req.http.host == "test2.example.com" |
| 17 | txresp -body "test2 ok" |
| 18 | } -start |
| 19 | |
| 20 | server s3 { |
| 21 | rxreq |
| 22 | expect req.method == "GET" |
| 23 | expect req.http.host == "test3.example.com" |
| 24 | txresp -body "test3 ok" |
| 25 | } -start |
| 26 | |
| 27 | server 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 | |
| 34 | haproxy 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 |
| 82 | client 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" |
| 87 | |
| 88 | txreq -hdr "Host: subdomain.example.org" |
| 89 | rxresp |
| 90 | expect resp.status == 301 |
| 91 | expect resp.http.location ~ "https://www.subdomain.example.org" |
| 92 | |
| 93 | # redirect on Testvar header |
| 94 | txreq -hdr "Testvar: subdomain.example.org" |
| 95 | rxresp |
| 96 | expect resp.status == 301 |
| 97 | expect resp.http.location ~ "https://www.subdomain.example.org" |
| 98 | } -run |
| 99 | |
| 100 | client c2 -connect ${h1_fe1_sock} { |
| 101 | txreq -hdr "Host: www.subdomain.example.org" |
| 102 | rxresp |
| 103 | expect resp.status == 403 |
| 104 | # Closes connection |
| 105 | } -run |
| 106 | |
| 107 | client c3 -connect ${h1_fe1_sock} { |
| 108 | txreq -hdr "Testvar: www.subdomain.example.org" |
| 109 | rxresp |
| 110 | expect resp.status == 403 |
| 111 | # Closes connection |
| 112 | } -run |
| 113 | |
| 114 | client c4 -connect ${h1_fe1_sock} { |
| 115 | txreq -hdr "Host: :8443example.org" |
| 116 | rxresp |
| 117 | expect resp.status == 403 |
| 118 | # Closes connection |
| 119 | } -run |
| 120 | |
| 121 | # Check map backend selection |
| 122 | client c5 -connect ${h1_fe1_sock} { |
| 123 | txreq -hdr "Host: test1.example.com" |
| 124 | rxresp |
| 125 | expect resp.status == 200 |
| 126 | expect resp.body == "test1 ok" |
| 127 | |
| 128 | txreq -hdr "Host: test2.example.com" |
| 129 | rxresp |
| 130 | expect resp.status == 200 |
| 131 | expect resp.body == "test2 ok" |
| 132 | |
| 133 | txreq -hdr "Host: test3.example.com" |
| 134 | rxresp |
| 135 | expect resp.status == 200 |
| 136 | expect resp.body == "test3 ok" |
| 137 | } -run |
| 138 | |
| 139 | # cli show maps |
| 140 | haproxy h1 -cli { |
| 141 | send "show map ${testdir}/h00003.map" |
| 142 | expect ~ "^0x[a-f0-9]+ example\\.org https://www\\.example\\.org\\n0x[a-f0-9]+ subdomain\\.example\\.org https://www\\.subdomain\\.example\\.org\\n$" |
| 143 | |
| 144 | send "show map ${testdir}/h00003-be.map" |
| 145 | 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$" |
| 146 | } |
| 147 | |
| 148 | haproxy h1 -cli { |
| 149 | # clear map ${testdir}/h00003.map |
| 150 | send "clear map ${testdir}/h00003.map" |
| 151 | expect ~ "^\\n" |
| 152 | |
| 153 | send "show map ${testdir}/h00003.map" |
| 154 | expect ~ "^\\n" |
| 155 | |
| 156 | # del map ${testdir}/h00003-be.map test1.example.{com,invalid} |
| 157 | send "del map ${testdir}/h00003-be.map test1.example.com" |
| 158 | expect ~ "^\\n" |
| 159 | |
| 160 | send "del map ${testdir}/h00003-be.map test1.example.invalid" |
| 161 | expect ~ "^\\n" |
| 162 | |
| 163 | send "show map ${testdir}/h00003-be.map" |
| 164 | expect ~ "^0x[a-f0-9]+ test2\\.example\\.com test2_be\\n$" |
| 165 | } |
| 166 | |
| 167 | # Check map backend after del map |
| 168 | client c6 -connect ${h1_fe1_sock} { |
| 169 | # test1.example.invalid should go to test4_be after del map |
| 170 | txreq -hdr "Host: test1.example.invalid" |
| 171 | rxresp |
| 172 | expect resp.status == 200 |
| 173 | expect resp.body == "test1 after del map ok" |
| 174 | } -run |