varnishtest "Test the proper interaction between http-reuse and backend in transparent mode"
feature cmd "$HAPROXY_PROGRAM -cc 'feature(TPROXY)'"

# Verify that connections made to a backend in transparent mode with
# "http-reuse never" are only reused with that client, and those with
# "http-reuse always" are properly shared across subsequent clients.

feature ignore_unknown_macro

haproxy h1 -conf {
	global
    .if feature(THREAD)
        thread-groups 1
    .endif

		nbthread 1

	defaults
		timeout client 30s
		timeout server 30s
		timeout connect 30s
		mode http

	listen never
		bind "fd@${feN}"
		http-reuse never

		http-request set-dst hdr(dst)
		http-request set-dst-port hdr(dst-port)

		http-after-response set-header http_reuse %[bc_reused]
		server transparent 0.0.0.0

	listen always
		bind "fd@${feA}"
		http-reuse always

		http-request set-dst hdr(dst)
		http-request set-dst-port hdr(dst-port)

		http-after-response set-header http_reuse %[bc_reused]
		server transparent 0.0.0.0 pool-max-conn 10

	listen srv1
		bind "fd@${fes1}"
		http-request add-header srv-id s1
		http-request return status 200 hdr srv-id s1

	listen srv2
		bind "fd@${fes2}"
		http-request add-header srv-id s2
		http-request return status 200 hdr srv-id s2
} -start

### now the test in http-reuse never mode
# 1,1,1
client c1 -connect ${h1_feN_sock} {
	txreq -url /c1-1 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s1"

	txreq -url /c1-2 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s1"

	txreq -url /c1-3 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s1"
} -run

# 2,1,2
client c2 -connect ${h1_feN_sock} {
	txreq -url /c2-1 \
	  -hdr "dst: ${h1_fes2_addr}" \
	  -hdr "dst-port: ${h1_fes2_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s2"

	txreq -url /c2-2 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s1"

	txreq -url /c2-3 \
	  -hdr "dst: ${h1_fes2_addr}" \
	  -hdr "dst-port: ${h1_fes2_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s2"
} -run

# 1,1,2
client c3 -connect ${h1_feN_sock} {
	txreq -url /c3-1 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s1"

	txreq -url /c3-2 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s1"

	txreq -url /c3-3 \
	  -hdr "dst: ${h1_fes2_addr}" \
	  -hdr "dst-port: ${h1_fes2_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s2"
} -run

### now the test in http-reuse safe mode

# 1,1,1
client c4 -connect ${h1_feA_sock} {
	txreq -url /c4-1 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s1"

	txreq -url /c4-2 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s1"

	txreq -url /c4-3 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s1"
} -run

# 2,1,2
client c5 -connect ${h1_feA_sock} {
	txreq -url /c5-1 \
	  -hdr "dst: ${h1_fes2_addr}" \
	  -hdr "dst-port: ${h1_fes2_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s2"

	txreq -url /c5-2 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s1"

	txreq -url /c5-3 \
	  -hdr "dst: ${h1_fes2_addr}" \
	  -hdr "dst-port: ${h1_fes2_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s2"
} -run

# 1,1,2
client c6 -connect ${h1_feA_sock} {
	txreq -url /c6-1 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s1"

	txreq -url /c6-2 \
	  -hdr "dst: ${h1_fes1_addr}" \
	  -hdr "dst-port: ${h1_fes1_port}"
	rxresp
	expect resp.http.http_reuse == "1"
	expect resp.http.srv-id == "s1"

	txreq -url /c6-3 \
	  -hdr "dst: ${h1_fes2_addr}" \
	  -hdr "dst-port: ${h1_fes2_port}"
	rxresp
	expect resp.http.http_reuse == "0"
	expect resp.http.srv-id == "s2"
} -run
