vtest "Test hash director"

server s1 {
	rxreq
	txresp -hdr "Foo: 2" -body "2"
	rxreq
	txresp -hdr "Foo: 3" -body "3"
	rxreq
	txresp -hdr "Foo: 6" -body "6"
	rxreq
	txresp -hdr "Foo: 8" -body "8"
} -start

server s2 -repeat 2 -keepalive {
	rxreq
	txresp -hdr "Foo: 1" -body "1"
	rxreq
	txresp -hdr "Foo: 4" -body "4"
	rxreq
	txresp -hdr "Foo: 9" -body "9"
} -start

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

	sub vcl_init {
		new h1 = directors.hash();
		h1.add_backend(s1);
		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 -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "1"

	txreq -url /3 -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "2"

	txreq -url /13 -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "3"

	txreq -url /8 -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "4"

	txreq -url "/emptystring" -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "6"

	txreq -url "/nohdr" -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "8"

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

client c1 {
	txreq -req "DELETE" -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.status == 204

	txreq -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "1"

	txreq -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "4"

	txreq -hdr "Host: 127.0.0.1"
	rxresp
	expect resp.http.foo == "9"
} -run
