varnishtest "Test hash director"

server s1 {
	rxreq
	txresp -hdr "Foo: 1" -body "1"
	rxreq
	txresp -hdr "Foo: 3" -body "3"
	rxreq
	txresp -hdr "Foo: 9" -body "9"
} -start

server s2 {
	rxreq
	txresp -hdr "Foo: 2" -body "2"
	rxreq
	txresp -hdr "Foo: 4" -body "4"
	rxreq
	txresp -hdr "Foo: 6" -body "6"
	rxreq
	txresp -hdr "Foo: 8" -body "8"
} -start

varnish v1 -vcl+backend {
	import std;
	import directors;

	sub vcl_init {
		new h1 = directors.hash();
		h1.add_backend(s1, 1);
		h1.add_backend(s2, 1);
	}

	sub vcl_recv {
		if (req.method == "DELETE") {
			h1.remove_backend(s1);
			return(synth(204));
		}
		if (req.url == "/nohdr") {
			set req.backend_hint = h1.backend(req.http.Void);
		} else if (req.url == "/emptystring") {
			set req.backend_hint = h1.backend("");
		} else if (req.url == "/13") {
			set req.backend_hint = h1.backend(req.http.Void + "" + req.url);
		} else if (req.url == "/ip") {
			set req.backend_hint = h1.backend(std.ip("127.0.0.1", client.ip));
		} else {
			set req.backend_hint = h1.backend(req.url);
		}
		return (pass);
	}
} -start

client c1 {
	txreq -url /1
	rxresp
	expect resp.http.foo == "1"

	txreq -url /3
	rxresp
	expect resp.http.foo == "2"

	txreq -url /13
	rxresp
	expect resp.http.foo == "3"

	txreq -url /8
	rxresp
	expect resp.http.foo == "4"

	txreq -url /emptystring
	rxresp
	expect resp.http.foo == "6"

	txreq -url /nohdr
	rxresp
	expect resp.http.foo == "8"

	txreq -url /ip
	rxresp
	expect resp.http.foo == "9"
} -run

server s2 -start

client c1 {
	txreq -req "DELETE"
	rxresp
	expect resp.status == 204

	txreq
	rxresp
	expect resp.http.foo == "2"

	txreq
	rxresp
	expect resp.http.foo == "4"

	txreq
	rxresp
	expect resp.http.foo == "6"

	txreq
	rxresp
	expect resp.http.foo == "8"
} -run
