varnishtest "Check synthetic error page caching"

# First test that an internally generated error is not cached

varnish v1 -vcl {
	backend foo {
		.host = "${bad_backend}";
	}
} -start

client c1 {
	txreq -url "/"
	rxresp
	expect resp.status == 503
	expect resp.http.X-varnish == "1001"
} -run

delay .1

client c1 {
	txreq -url "/"
	rxresp
	expect resp.status == 503
	expect resp.http.X-varnish == "1004"
} -run

delay .1

# Then check that a cacheable error from the backend is

varnish v1 -cliok "ban req.url ~ .*"

server s1 {
	rxreq
	txresp -status 301
} -start

varnish v1 -vcl+backend {
	sub vcl_backend_response {
		set beresp.http.ttl = beresp.ttl;
		set beresp.http.uncacheable = beresp.uncacheable;
	}
}

client c1 {
	txreq -url "/"
	rxresp
	expect resp.status == 301
	expect resp.http.X-varnish == "1007"
} -run

delay .1

client c1 {
	txreq -url "/"
	rxresp
	expect resp.status == 301
	expect resp.http.X-varnish == "1010 1008"
} -run

delay .1

# Then check that a non-cacheable error from the backend can be

server s1 {
	rxreq
	txresp -status 502
} -start

varnish v1 -vcl+backend {
	sub vcl_backend_response {
		if (beresp.status == 502) {
			set beresp.ttl = 10m;
		}
	}
}

client c1 {
	txreq -url "/2"
	rxresp
	expect resp.status == 502
	expect resp.http.X-varnish == "1012"
} -run

delay .1

client c1 {
	txreq -url "/2"
	rxresp
	expect resp.status == 502
	expect resp.http.X-varnish == "1015 1013"
} -run

delay .1

varnish v1 -expect VBE.vcl1.foo.fail_econnrefused > 0
varnish v1 -expect VBE.vcl1.foo.helddown > 0
