varnishtest "Check naming of backends"

server s1 -repeat 2 -keepalive {
	rxreq
	txresp
} -start

varnish v1 -vcl+backend {
	import directors;

	sub vcl_recv {
		return (pass);
	}

	sub vcl_backend_response {
		set beresp.http.X-Backend-Name = bereq.backend;
	}
} -start

client c1 {
	txreq -url "/"
	rxresp
	expect resp.http.X-Backend-Name == "s1"
} -run

varnish v1 -vcl+backend {
	import directors;

	sub vcl_init {
		new bar = directors.random();
		bar.add_backend(directors.lookup("s1"), 1);
	}

	sub vcl_recv {
		set req.backend_hint = bar.backend();
		return (pass);
	}

	sub vcl_backend_response {
		set beresp.http.X-Director-Name = bereq.backend;
		set beresp.http.X-Backend-Name = beresp.backend;
	}
}

client c1 {
	txreq -url "/"
	rxresp
	expect resp.http.X-Director-Name == "bar"
	expect resp.http.X-Backend-Name == "s1"
} -run

varnish v1 -errvcl "Not available in subroutine 'vcl_recv'" {
	import directors;

	backend dummy None;

	sub vcl_recv {
		if (req.url == "/lookup") {
			set req.http.foo = directors.lookup("s1");
		}
		return (pass);
	}

}
