varnishtest "test if our default parameters make sense ..."

feature 64bit
feature !sanitizer
feature !coverage

# ... for our definition of a standard use case:
# - 2019 header madness
# - 5 ESI levels down
# - 10 VCL subs down
# - PCRE2 regsub

server s1 {
	rxreq
	expect req.http.esi0 == "foo"
	txresp \
	    -hdr "Content-Type: text/html;charset=utf-8" \
	    -hdr "Content-Language: en-US" \
	    -hdr "X-UA-Compatible: IE=Edge" \
	    -hdr "X-Content-Type-Options: nosniff" \
	    -hdr "Content-Security-Policy-Report-Only: script-src 'unsafe-inline' 'unsafe-eval' 'self' blob: data: https:; style-src 'self' 'unsafe-inline' blob: data: https:; default-src 'self' https:; img-src https: blob: data: android-webview-video-poster:; frame-src blob: data: https:; worker-src blob: data: https:; child-src blob: data: https:; object-src 'self'; font-src 'self' https: blob: data: safari-extension://*; media-src 'self' blob: data: https:; connect-src wss: blob: data: https:; report-uri /csp_ep" \
	    -hdr "Content-Security-Policy: upgrade-insecure-requests" \
	    -hdr "Server: MySecretServerSauce" \
	    -hdr "Cache-Control: public, max-age=90" \
	    -hdr "Connection: keep-alive" \
	    -hdr "Vary: Accept-Encoding, Origin" \
	    -gzipbody {
		<html>
		Before include
		<esi:include src="/a" sr="foo"/>
		After include
		</html>
	}

	rxreq
	expect req.url == "/a1"
	expect req.http.esi0 != "foo"
	txresp \
	    -hdr "Content-Type: text/html;charset=utf-8" \
	    -hdr "Content-Language: en-US" \
	    -hdr "X-UA-Compatible: IE=Edge" \
	    -hdr "X-Content-Type-Options: nosniff" \
	    -hdr "Content-Security-Policy-Report-Only: script-src 'unsafe-inline' 'unsafe-eval' 'self' blob: data: https:; style-src 'self' 'unsafe-inline' blob: data: https:; default-src 'self' https:; img-src https: blob: data: android-webview-video-poster:; frame-src blob: data: https:; worker-src blob: data: https:; child-src blob: data: https:; object-src 'self'; font-src 'self' https: blob: data: safari-extension://*; media-src 'self' blob: data: https:; connect-src wss: blob: data: https:; report-uri /csp_ep" \
	    -hdr "Content-Security-Policy: upgrade-insecure-requests" \
	    -hdr "Server: MySecretServerSauce" \
	    -hdr "Cache-Control: public, max-age=90" \
	    -hdr "Connection: keep-alive" \
	    -hdr "Vary: Accept-Encoding, Origin" \
	    -gzipbody {
		<html>
		Before include
		<esi:include src="/b" sr="foo"/>
		After include
		</html>
	}

	rxreq
	expect req.url == "/b2"
	expect req.http.esi0 != "foo"
	txresp \
	    -hdr "Content-Type: text/html;charset=utf-8" \
	    -hdr "Content-Language: en-US" \
	    -hdr "X-UA-Compatible: IE=Edge" \
	    -hdr "X-Content-Type-Options: nosniff" \
	    -hdr "Content-Security-Policy-Report-Only: script-src 'unsafe-inline' 'unsafe-eval' 'self' blob: data: https:; style-src 'self' 'unsafe-inline' blob: data: https:; default-src 'self' https:; img-src https: blob: data: android-webview-video-poster:; frame-src blob: data: https:; worker-src blob: data: https:; child-src blob: data: https:; object-src 'self'; font-src 'self' https: blob: data: safari-extension://*; media-src 'self' blob: data: https:; connect-src wss: blob: data: https:; report-uri /csp_ep" \
	    -hdr "Content-Security-Policy: upgrade-insecure-requests" \
	    -hdr "Server: MySecretServerSauce" \
	    -hdr "Cache-Control: public, max-age=90" \
	    -hdr "Connection: keep-alive" \
	    -hdr "Vary: Accept-Encoding, Origin" \
	    -gzipbody {
		<html>
		Before include
		<esi:include src="/c" sr="foo"/>
		After include
		</html>
	}

	rxreq
	expect req.url == "/c3"
	expect req.http.esi0 != "foo"
	txresp \
	    -hdr "Content-Type: text/html;charset=utf-8" \
	    -hdr "Content-Language: en-US" \
	    -hdr "X-UA-Compatible: IE=Edge" \
	    -hdr "X-Content-Type-Options: nosniff" \
	    -hdr "Content-Security-Policy-Report-Only: script-src 'unsafe-inline' 'unsafe-eval' 'self' blob: data: https:; style-src 'self' 'unsafe-inline' blob: data: https:; default-src 'self' https:; img-src https: blob: data: android-webview-video-poster:; frame-src blob: data: https:; worker-src blob: data: https:; child-src blob: data: https:; object-src 'self'; font-src 'self' https: blob: data: safari-extension://*; media-src 'self' blob: data: https:; connect-src wss: blob: data: https:; report-uri /csp_ep" \
	    -hdr "Content-Security-Policy: upgrade-insecure-requests" \
	    -hdr "Server: MySecretServerSauce" \
	    -hdr "Cache-Control: public, max-age=90" \
	    -hdr "Connection: keep-alive" \
	    -hdr "Vary: Accept-Encoding, Origin" \
	    -gzipbody {
		<html>
		Before include
		<esi:include src="/d" sr="foo"/>
		After include
		</html>
	}

	rxreq
	expect req.url == "/d4"
	expect req.http.esi0 != "foo"
	txresp \
	    -hdr "Content-Type: text/html;charset=utf-8" \
	    -hdr "Content-Language: en-US" \
	    -hdr "X-UA-Compatible: IE=Edge" \
	    -hdr "X-Content-Type-Options: nosniff" \
	    -hdr "Content-Security-Policy-Report-Only: script-src 'unsafe-inline' 'unsafe-eval' 'self' blob: data: https:; style-src 'self' 'unsafe-inline' blob: data: https:; default-src 'self' https:; img-src https: blob: data: android-webview-video-poster:; frame-src blob: data: https:; worker-src blob: data: https:; child-src blob: data: https:; object-src 'self'; font-src 'self' https: blob: data: safari-extension://*; media-src 'self' blob: data: https:; connect-src wss: blob: data: https:; report-uri /csp_ep" \
	    -hdr "Content-Security-Policy: upgrade-insecure-requests" \
	    -hdr "Server: MySecretServerSauce" \
	    -hdr "Cache-Control: public, max-age=90" \
	    -hdr "Connection: keep-alive" \
	    -hdr "Vary: Accept-Encoding, Origin" \
	    -gzipbody {
		<html>
		Before include
		<esi:include src="/e" sr="foo"/>
		After include
		</html>
	}

	rxreq
	expect req.url == "/e5"
	expect req.http.esi0 != "foo"
	txresp \
	    -hdr "Content-Type: text/html;charset=utf-8" \
	    -hdr "Content-Language: en-US" \
	    -hdr "X-UA-Compatible: IE=Edge" \
	    -hdr "X-Content-Type-Options: nosniff" \
	    -hdr "Content-Security-Policy-Report-Only: script-src 'unsafe-inline' 'unsafe-eval' 'self' blob: data: https:; style-src 'self' 'unsafe-inline' blob: data: https:; default-src 'self' https:; img-src https: blob: data: android-webview-video-poster:; frame-src blob: data: https:; worker-src blob: data: https:; child-src blob: data: https:; object-src 'self'; font-src 'self' https: blob: data: safari-extension://*; media-src 'self' blob: data: https:; connect-src wss: blob: data: https:; report-uri /csp_ep" \
	    -hdr "Content-Security-Policy: upgrade-insecure-requests" \
	    -hdr "Server: MySecretServerSauce" \
	    -hdr "Cache-Control: public, max-age=90" \
	    -hdr "Connection: keep-alive" \
	    -hdr "Vary: Accept-Encoding, Origin" \
	    -gzipbody {
		<html>
		LAST
		</html>
	}
} -start

varnish v1 -vcl+backend {
	import std;
	import debug;
	import vtc;

	sub recv0 { call recv1; std.log("STK recv0 " + debug.stk()); }
	sub recv1 { call recv2; std.log("STK recv1 " + debug.stk()); }
	sub recv2 { call recv3; std.log("STK recv2 " + debug.stk()); }
	sub recv3 { call recv4; std.log("STK recv3 " + debug.stk()); }
	sub recv4 { call recv5; std.log("STK recv4 " + debug.stk()); }
	sub recv5 { call recv6; std.log("STK recv5 " + debug.stk()); }
	sub recv6 { call recv7; std.log("STK recv6 " + debug.stk()); }
	sub recv7 { call recv8; std.log("STK recv7 " + debug.stk()); }
	sub recv8 { call recv9; std.log("STK recv8 " + debug.stk()); }
	sub recv9 {
		std.log("STK recv9 " + debug.stk() + " WS " +
		    vtc.workspace_free(client));
		set req.http.regex = regsub(req.http.cookie, "(.*)", "\1\1\1\1\1\1\1\1");
		std.log("WS " + vtc.workspace_free(client));
		# hey geoff, this is deliberate
		set req.http.regex = regsub(req.http.regex,
		  "(.*)(.{5})(.{6})(.{7})(.{8})", "/\5\4\3\2\1");
		std.log("WS " + vtc.workspace_free(client));
		std.log("REGEX recv9 " + req.http.regex);
	}

	sub vcl_recv {
		if (req.esi_level > 0) {
			set req.url = req.url + req.esi_level;
		} else {
			set req.http.esi0 = "foo";
		}
		std.log("STK recv " + debug.stk());
		call recv0;
	}
	sub vcl_backend_response {
		set beresp.do_esi = true;
	}
	sub vcl_deliver {
		std.log("STK deliver " + debug.stk());
	}
} -start

varnish v1 -cliok "param.set debug +syncvsl"

client c1 {
	txreq \
	    -hdr "Host: foo" \
	    -hdr "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" \
	    -hdr "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" \
	    -hdr "Accept-Language: en-US,en;q=0.5" \
	    -hdr "Accept-Encoding: gzip, deflate, br" \
	    -hdr "Cookie: logged_in=yes; sess=vXgKJaOHR1I78w1WeTyH51EZSu1rhrC1aAINn+a4sVk/IMouksSSP0Mg4jzqhTMtdLilDo3t04fxRJP1ywB/loN9674CLOu2yzT996hUbzM8oza68yNzhSkkL4afQYOwLMJbtFvtY+lLHk3TJRHSS243HcYluLoo7qjmpiiUfx6JyIbRtl5xPPgVGkLgSA1Fu/yCXwfVCNhnLWHMSm1zd15CoroUCFDkuO0OponjseGPBzJ7NdFk2Fi5SJFZmhzHcBH/Ri/Uu5UeJwVAcJe9oPNuaWUR/Oy/D3nU81lOels8ypYJRmAAzO5r7RJ7KmIvjZhqxLG7cMViH/roegSgqxHsjXb/kSec2dmq1wQqSPYjxN/pIp8PefyM/IAho2h3WVKRDhYmAokhDIA8/UgMxaIyrWh1Ep6D16IU1uRMgx5Gjr6VJJ42GV23+OhfvlpdYoZxy7b9bwf7T3ABniF+VJOdMO5PTWfuG2Xt515FZ/byNpMYnMvWNGh4Ior8QyV2W0Nz4p0NJ5RWsnHYAoD3ySRC5E/cpu9RQsXdE1sVNDa7uMzgt0Bbnpk1ALeNN9JJ/l6zLATCKcvixty0Aonyi1nyG9LNL6+rtzsDOh7S5uDul67P2lXFUta1eY2Ma0e/JAHJcKgTqgFGCZJvsoFydnyu23AanhaPT4c3w3ZpGs0; evil_tracker=JcDDfXw14Efx4iLycPEDQaF8+Csci+cRHz0pwTm1JW9kvXyKlUcGVlpCw7qYZtORuNnVb3m6HOwJneFhAdDlw5FQbQh1YmX8ZBgKD51Fo8T0R/0a8W0suJ/mJrQ6H6MFjgZc8YE7vx8zt+nUPT0qfZ9TCSndA0EXLerIc6Cdu06wBPF0m2ydkMKIPn/R6pU+mVrn58RZrLdcbsrwm5mhSCM9RjDYqEMye9n7jhTbdyna+X+7S8XubJRXqWa9Zft2UuprU0wnUVUA6eFdqvaiAGoepQFjJjh13g0fp6+GJiNwfSJbjTi3GK2o9E9t8qfLr0Avzjj9rqPG2G5MBxZMjg" \
	    -hdr "DNT: 1" \
	    -hdr "Connection: keep-alive" \
	    -hdr "Upgrade-Insecure-Requests: 1" \
	    -hdr {If-None-Match: W/"9060a5e7924af13779c0437265ad2f1c"}
	rxresp
	expect resp.status == 200

}

client c1 -run
varnish v1 -expect esi_errors == 0
