varnishtest "Test Vary functionality"

barrier b1 cond 3

server s1 {
	rxreq
	expect req.http.foobar == "1"
	txresp -hdr "Vary: Foobar" -hdr "Snafu: 1" -body "1111\n"

	rxreq
	expect req.http.foobar == "2"
	txresp -hdr "Vary: Foobar" -hdr "Snafu: 2" -body "2222\n"

	rxreq
	expect req.http.foobar == "3"
	txresp -hdr "Vary: Foobar" -hdr "Snafu: 3" -body "3333\n"

	rxreq
	expect req.http.foobar == <undef>
	txresp -hdr "Vary: Foobar" -hdr "Snafu: 4" -body "4444\n"

	rxreq
	expect req.http.foobar == ""
	txresp -hdr "Vary: Foobar" -hdr "Snafu: 5" -body "5555\n"

	# #3858 test vary prediction turning out wrong
	# no Vary, HFM and waitinglist
	rxreq
	expect req.http.foobar == "x"
	barrier b1 sync
	txresp -hdr "Cache-Control: no-cache"

	rxreq
	expect req.http.foobar == "x"
	txresp -hdr "Cache-Control: no-cache"
} -start

varnish v1 -vcl+backend {} -start

client c1 {
	txreq -hdr "Foobar: 1"
	rxresp
	expect resp.status == 200
	expect resp.http.X-Varnish == "1001"
	expect resp.http.snafu == "1"

	txreq -hdr "Foobar: 2"
	rxresp
	expect resp.status == 200
	expect resp.http.X-Varnish == "1003"
	expect resp.http.snafu == "2"

	txreq -hdr "Foobar: 3"
	rxresp
	expect resp.status == 200
	expect resp.http.X-Varnish == "1005"
	expect resp.http.snafu == "3"

	txreq
	rxresp
	expect resp.status == 200
	expect resp.http.X-Varnish == "1007"
	expect resp.http.snafu == "4"

	txreq -hdr "Foobar:  1 "
	rxresp
	expect resp.status == 200
	expect resp.http.X-Varnish == "1009 1002"
	expect resp.http.snafu == "1"

	txreq -hdr "Foobar:  1 "
	rxresp
	expect resp.status == 200
	expect resp.http.X-Varnish == "1010 1002"
	expect resp.http.snafu == "1"

	# empty header != no header
	txreq -hdr "Foobar:  "
	rxresp
	expect resp.status == 200
	expect resp.http.X-Varnish == "1011"
	expect resp.http.snafu == "5"
} -run

client c1 {
	txreq -hdr "Foobar: x"
	barrier b1 sync
	rxresp
	expect resp.status == 200
	expect resp.http.Vary == <undef>
} -start

client c2 {
	txreq -hdr "Foobar: x"
	barrier b1 sync
	rxresp
	expect resp.status == 200
	expect resp.http.Vary == <undef>
} -start


client c1 -wait
client c2 -wait
