varnishtest "ESI requests turned off and other filters with esi"

server s1 {
	rxreq
	expect req.url == /
	txresp -body {
		<html>
		Before include
		<esi:include src="/body"/>
		After include
	}
	rxreq
	txresp -body {
		<html>
		Before include
		<esi:include src="/body"/>
		After include
	}
	rxreq
	expect req.url == "/body"
	txresp -body {
		<pre>Included file</pre>
	}
} -start

varnish v1 -syntax 4.0 -vcl+backend {
	sub vcl_recv {
		if (req.url == "/") {
			set req.esi = false;
		}
	}
	sub vcl_backend_response {
		set beresp.do_esi = true;
	}
} -start

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 73
	expect resp.status == 200
	txreq -url "/esi"
	rxresp
	expect resp.bodylen == 76
	expect resp.status == 200
} -run

varnish v1 -vsl_catchup

varnish v1 -expect esi_errors == 0
varnish v1 -expect MAIN.s_resp_bodybytes == 149

server s1 {
	rxreq
	expect req.url == /top2
	txresp -gzipbody {
		<html>
		Before include
		<esi:include src="/body"/>
		After include
	}
	rxreq
	expect req.url == /recurse
	txresp -gzipbody {
		<html>
		Before include
		<esi:include src="/top2"/>
		After include
	}
} -start

varnish v1 -syntax 4.1 -vcl+backend {
	sub vcl_deliver {
		set resp.http.was = resp.do_esi;
		set resp.http.filter0 = resp.filters;
		if (req.url == "/top2") {
			set resp.do_esi = false;
		}
		set resp.http.filters = resp.filters;
		if (req.http.fiddle) {
			set resp.filters = resp.filters;
			set resp.do_esi = false;
		}
	}
	sub vcl_backend_response {
		set beresp.do_esi = true;
	}
}

client c1 {
	txreq -url /top2
	rxresp
	expect resp.bodylen == 73
	expect resp.status == 200
	expect resp.http.was == true
	expect resp.http.filter0 == "esi gunzip"
	expect resp.http.filters == "gunzip"

	txreq -url "/esi"
	rxresp
	expect resp.bodylen == 76
	expect resp.status == 200
	expect resp.http.was == true
	expect resp.http.filter0 == "esi"
	expect resp.http.filters == "esi"

	txreq -url "/esi" -hdr "Range: bytes=1-2"
	rxresp
	expect resp.bodylen == 2
	expect resp.status == 206
	expect resp.http.was == true
	expect resp.http.filters == "esi range"

	txreq -url "/recurse"
	rxresp
	expect resp.bodylen == 120
	expect resp.status == 200
	expect resp.http.was == true
	expect resp.http.filters == "esi gunzip"

	txreq -url "/recurse" -hdr "Range: bytes=1-2"
	rxresp
	expect resp.bodylen == 2
	expect resp.status == 206
	expect resp.http.was == true
	expect resp.http.filters == "esi gunzip range"

	txreq -url /top2 -hdr "fiddle: do_esi"
	rxresp
	expect resp.status == 503
	expect resp.bodylen == 251
} -run

varnish v1 -expect esi_errors == 0
varnish v1 -expect MAIN.s_resp_bodybytes == 673
