haproxy/reg-tests/http-rules/map_redirect.vtc
Christopher Faulet d09d7676d0 REG-TESTS: map_redirect: Don't use hdr_dom in ACLs with "-m end" matching method
hdr_dom() is a alias of "hdr() -m dom". So using it with another explicit
matching method does not work because the matching on the domain will never
be performed. Only the last matching method is used. The scripts was working
by chance because no port was set on host header values.

The script was fixed by using "host_only" converter. In addition, host
header values were changed to have a port now.
2025-09-01 15:45:05 +02:00

204 lines
6.2 KiB
Text

varnishtest "haproxy host header: map / redirect tests"
feature cmd "$HAPROXY_PROGRAM -cc 'version_atleast(2.5-dev5) && (feature(PCRE) || feature(PCRE2))'"
feature ignore_unknown_macro
server s1 {
rxreq
expect req.method == "GET"
expect req.http.host == "test1.example.com:1234"
txresp -body "test1 ok"
} -start
server s2 {
rxreq
expect req.method == "GET"
expect req.http.host == "test2.example.com:1234"
txresp -body "test2 ok"
} -start
server s3 {
rxreq
expect req.method == "GET"
expect req.http.host == "test3.example.com:1234"
txresp -body "test3 ok"
} -start
server s4 {
rxreq
expect req.method == "GET"
expect req.http.host == "test1.example.invalid"
txresp -body "test1 after del map ok"
} -start
haproxy h1 -conf {
global
.if feature(THREAD)
thread-groups 1
.endif
defaults
mode http
log global
timeout connect "${HAPROXY_TEST_TIMEOUT-5s}"
timeout client "${HAPROXY_TEST_TIMEOUT-5s}"
timeout server "${HAPROXY_TEST_TIMEOUT-5s}"
frontend fe1
bind "fd@${fe1}"
# automatically redirect matching paths from maps but skip rule on no-match
http-request redirect code 301 location %[path,map_str(${testdir}/map_redirect.map)] ignore-empty
# redirect Host: example.org / subdomain.example.org
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 }
# set var and redirect in be1
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 }
# use map to select backend (no default map value)
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 }
# use map to select backend with default value(test3_be)
use_backend %[req.hdr(Host),lower,map_dom(${testdir}/map_redirect-be.map,test3_be)] if { hdr(Host),host_only -m end -i example.com }
# use map(after del map test1.example.com) default value(test4_be)
use_backend %[req.hdr(Host),lower,map_dom(${testdir}/map_redirect-be.map,test4_be)] if { hdr(Host),host_only -m end -i example.invalid }
default_backend be1
backend be1
http-request redirect prefix %[var(txn.testvar)] code 301 if { var(txn.testvar) -m found }
http-request deny
backend test1_be
server s1 ${s1_addr}:${s1_port}
backend test2_be
server s2 ${s2_addr}:${s2_port}
backend test3_be
server s3 ${s3_addr}:${s3_port}
backend test4_be
server s4 ${s4_addr}:${s4_port}
} -start
# Check map redirects
client c1 -connect ${h1_fe1_sock} {
txreq -hdr "Host: example.org:8443"
rxresp
expect resp.status == 301
expect resp.http.location ~ "https://www.example.org"
txreq -url /path/to/old/file
rxresp
expect resp.status == 301
expect resp.http.location ~ "/path/to/new/file"
# Closes connection
} -run
client c2 -connect ${h1_fe1_sock} {
txreq -hdr "Host: subdomain.example.org"
rxresp
expect resp.status == 301
expect resp.http.location ~ "https://www.subdomain.example.org"
# Closes connection
} -run
client c3 -connect ${h1_fe1_sock} {
# redirect on Testvar header
txreq -hdr "Testvar: subdomain.example.org"
rxresp
expect resp.status == 301
expect resp.http.location ~ "https://www.subdomain.example.org"
# Closes connection
} -run
client c4 -connect ${h1_fe1_sock} {
txreq -hdr "Host: www.subdomain.example.org"
rxresp
expect resp.status == 403
# Closes connection
} -run
client c5 -connect ${h1_fe1_sock} {
txreq -hdr "Testvar: www.subdomain.example.org"
rxresp
expect resp.status == 403
# Closes connection
} -run
client c6 -connect ${h1_fe1_sock} {
txreq -hdr "Host: :8443example.org"
rxresp
expect resp.status == 403
# Closes connection
} -run
# Check map backend selection
client c7 -connect ${h1_fe1_sock} {
txreq -hdr "Host: test1.example.com:1234"
rxresp
expect resp.status == 200
expect resp.body == "test1 ok"
txreq -hdr "Host: test2.example.com:1234"
rxresp
expect resp.status == 200
expect resp.body == "test2 ok"
txreq -hdr "Host: test3.example.com:1234"
rxresp
expect resp.status == 200
expect resp.body == "test3 ok"
} -run
# cli show maps
haproxy h1 -cli {
send "show map ${testdir}/map_redirect.map"
expect ~ "^0x[a-f0-9]+ example\\.org https://www\\.example\\.org\\n0x[a-f0-9]+ subdomain\\.example\\.org https://www\\.subdomain\\.example\\.org\\n0x[a-f0-9]+ /path/to/old/file /path/to/new/file\n$"
send "show map ${testdir}/map_redirect-be.map"
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$"
}
haproxy h1 -cli {
# clear map ${testdir}/map_redirect.map
send "clear map ${testdir}/map_redirect.map"
expect ~ "^\\n"
send "show map ${testdir}/map_redirect.map"
expect ~ "^\\n"
# add map ${testdir}/map_redirect.map
send "add map ${testdir}/map_redirect.map site1_key site1_value"
expect ~ "^\\n"
# add 2 more entries as payload
send "add map ${testdir}/map_redirect.map <<\nsite2_key site2_value\nsite3_key site3_value\n"
expect ~ "^\\n"
send "show map ${testdir}/map_redirect.map"
expect ~ "^0x[a-f0-9]+ site1_key site1_value\\n0x[a-f0-9]+ site2_key site2_value\\n0x[a-f0-9]+ site3_key site3_value\\n$"
# del map ${testdir}/map_redirect-be.map test1.example.{com,invalid}
send "del map ${testdir}/map_redirect-be.map test1.example.com"
expect ~ "^\\n"
send "del map ${testdir}/map_redirect-be.map test1.example.invalid"
expect ~ "^\\n"
send "show map ${testdir}/map_redirect-be.map"
expect ~ "^0x[a-f0-9]+ test2\\.example\\.com test2_be\\n$"
}
# Check map backend after del map
client c6 -connect ${h1_fe1_sock} {
# test1.example.invalid should go to test4_be after del map
txreq -hdr "Host: test1.example.invalid"
rxresp
expect resp.status == 200
expect resp.body == "test1 after del map ok"
} -run