varnishtest "Test std, vtc & debug vmod"

server s1 {
	rxreq
	expect req.http.encrypted == "ROT52"
	txresp -hdr "foo: bAr" -hdr "bar: fOo" -bodylen 4
} -start

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

	sub vcl_init {
		new objx = debug.obj();
		debug.vsc_new();
	}

	sub vcl_synth {
		set req.http.overwrite = "the workspace " +
		    "to ensure we notice any unfinished privs";
	}

	sub priv_task {
		debug.test_priv_task("foo");
	}

	sub vcl_recv {
		if (req.url == "/priv-task-no-mem") {
			vtc.workspace_alloc(client, -4);
			call priv_task;
			return (fail);
		}
		if (req.url == "/fail") {
			debug.test_priv_task("foo");
			return (fail);
		}
		debug.rot52(req);
		debug.vsc_count();
	}

	sub vcl_deliver {
		set resp.http.foo = std.toupper(resp.http.foo);
		set resp.http.bar = std.tolower(resp.http.bar);
		set resp.http.who = debug.author(mithrandir);
		set resp.http.really = debug.author();
		set resp.http.what = vtc.typesize("dfijlopsz");
		set resp.http.not = vtc.typesize("*");
		debug.test_priv_call();
		debug.test_priv_vcl();
		objx.test_priv_call();
		objx.test_priv_vcl();
		std.log("VCL" + " initiated " + "log");
		std.syslog(8 + 7, "Somebody runs varnishtest");
		debug.rot52(resp);
	}
} -start

varnish v1 -expect DEBUG.count == 0

client c1 {
	txreq -url "/bar"
	rxresp
	expect resp.status == 200
	expect resp.bodylen == "4"
	expect resp.http.foo == "BAR"
	expect resp.http.bar == "foo"
	expect resp.http.who == "Tollef"
	expect resp.http.really == "Poul-Henning"
	expect resp.http.encrypted == "ROT52"
	expect resp.http.what >= 16
	expect resp.http.not == -1
	txreq -url "/fail"
	rxresp
	expect resp.status == 503
} -run

client c1 {
	txreq -url "/priv-task-no-mem"
	rxresp
	expect resp.status == 503
} -run

varnish v1 -expect DEBUG.count == 1

logexpect l1 -v v1 -g raw -d 1 {
	expect * 1001 VCL_call		{^DELIVER}
	expect 0 =    RespUnset	{^foo: bAr}
	expect 0 =    RespHeader	{^foo: BAR}
	expect 0 =    RespUnset	{^bar: fOo}
	expect 0 =    RespHeader	{^bar: foo}
	expect 0 =    RespHeader	{^who: Tollef}
	expect 0 =    RespHeader	{^really: Poul-Henning}
	expect 0 =    RespHeader	{^what: [1-9][0-9]}
	expect 0 =    RespHeader	{^not: -1}
	expect 0 =    VCL_Log		{^VCL initiated log}
	expect 0 =    RespHeader	{^Encrypted: ROT52}
	expect 0 =    VCL_return	{^deliver}
} -start

logexpect l1 -wait

varnish v1 -errvcl {Wrong enum value.  Expected one of:} {
	import debug;
	sub vcl_deliver {
		set resp.http.who = debug.author(ENUM);
	}
}

varnish v1 -errvcl {Wrong enum value.  Expected one of:} {
	import debug;
	sub vcl_deliver {
		set resp.http.who = debug.author(slink);
	}
}

varnish v1 -errvcl {Expression has type STRING, expected REAL} {
	import std;
	sub vcl_deliver {
		set resp.http.who = std.random("foo", "bar");
	}
}
