varnishtest "Test ESI ignoring BOMs"

server s1 {
	rxreq
	expect req.url == /1
	txresp -body "\xeb\xbb\xbf    <html>   <esi:remove>blabla</esi:remove>"
	rxreq
	expect req.url == /2
	txresp -body "\xeb\xbb\xbf    <html>   <esi:remove>blabla</esi:remove>"
	rxreq
	expect req.url == /3
	txresp -body "\xeb\xbb\xbf\xeb\xbb\xbf    <html>   <esi:remove>blabla</esi:remove>"
	rxreq
	expect req.url == /4
	txresp -body "\xeb\xbc    <html>   <esi:remove>blabla</esi:remove>"
} -start

varnish v1 -vcl+backend {
	sub vcl_backend_response {
		set beresp.do_esi = true;
	}
} -start

logexpect l1 -v v1 -g raw {
	expect * * Fetch_Body
	expect 0 = ESI_xmlerror {^No ESI processing, first char not '<' but BOM. .See feature esi_remove_bom.$}
	expect 0 = BackendClose
# XXX another logexpect weirdness - why can't we catch the second occurrence?
#	expect * * Fetch_Body
#	expect 0 = ESI_xmlerror {^No ESI processing, first char not '<' but BOM. .See feature esi_remove_bom.$}
#	expect 0 = BackendClose
} -start

client c1 {
	# No ESI processing
	txreq -url /1
	rxresp
	expect resp.bodylen == 47
} -run

logexpect l1 -wait

varnish v1 -cliok "param.set feature +esi_remove_bom"

client c1 {
	# BOM removed, ESI processing
	txreq -url /2
	rxresp
	expect resp.bodylen == 13
} -run

client c1 {
	# BOMs removed, ESI processing
	txreq -url /3
	rxresp
	expect resp.bodylen == 13
} -run

client c1 {
	# Not a BOM, no ESI processing
	txreq -url /4
	rxresp
	expect resp.bodylen == 46
} -run
