varnishtest "Test vmod functions/contructor named arguments and defaults"

server s1 {
	rxreq
	txresp -bodylen 6
} -start

varnish v1 -vcl+backend {
	import debug;

	sub vcl_init {
		new obj0 = debug.obj();
		new obj1 = debug.obj("only_argument");
		new obj2 = debug.obj("string", one);
		new obj3 = debug.obj(string="s_two", number=two);
		new obj4 = debug.obj(number=three, string="s_three");
		new obj5 = debug.obj(number=three);

		new oo0 = debug.obj_opt();
		new oo1 = debug.obj_opt(s="string");
		new oo2 = debug.obj_opt(b=true);
	}

	sub vcl_deliver {
		set resp.http.foo1 = debug.argtest("1", 2.1, "3a");
		set resp.http.foo2 = debug.argtest("1", two=-2.2, three="3b");
		set resp.http.foo3 = debug.argtest("1", three="3c", two=2.3);
		set resp.http.foo4 = debug.argtest("1", 2.4, three="3d", four=-1);
		set resp.http.foo5 = debug.argtest("1", 2.5);
		set resp.http.foo6 = debug.argtest("1", four=6);
		set resp.http.foo7 = debug.argtest("1", opt="7");

		set resp.http.obj0 = obj0.string() + ", " + obj0.number();
		set resp.http.obj1 = obj1.string() + ", " + obj1.number();
		set resp.http.obj2 = obj2.string() + ", " + obj2.number();
		set resp.http.obj3 = obj3.string() + ", " + obj3.number();
		set resp.http.obj4 = obj4.string() + ", " + obj4.number();
		set resp.http.obj5 = obj5.string() + ", " + obj5.number();

		set resp.http.oo0 = oo0.meth_opt(s="s1");
		set resp.http.oo1 = oo1.meth_opt(b=false);
		set resp.http.oo2 = oo2.meth_opt();
	}
} -start

client c1 {
	txreq
	rxresp
	expect resp.bodylen == "6"
	expect resp.http.foo1 == "1 2.1 3a , 4 0 <undef>"
	expect resp.http.foo2 == "1 -2.2 3b , 4 0 <undef>"
	expect resp.http.foo3 == "1 2.3 3c , 4 0 <undef>"
	expect resp.http.foo4 == "1 2.4 3d , -1 0 <undef>"
	expect resp.http.foo5 == "1 2.5 3 , 4 0 <undef>"
	expect resp.http.foo6 == "1 2 3 , 6 0 <undef>"
	expect resp.http.foo7 == "1 2 3 , 4 1 7"

	expect resp.http.obj0 == "default, one"
	expect resp.http.obj1 == "only_argument, one"
	expect resp.http.obj2 == "string, one"
	expect resp.http.obj3 == "s_two, two"
	expect resp.http.obj4 == "s_three, three"
	expect resp.http.obj5 == "default, three"

	expect resp.http.oo0 == "obj oo0 obj_s *undef* obj_b *undef* met_s s1 met_b *undef*"
	expect resp.http.oo1 == "obj oo1 obj_s string obj_b *undef* met_s *undef* met_b false"
	expect resp.http.oo2 == "obj oo2 obj_s *undef* obj_b true met_s *undef* met_b *undef*"

} -run

delay .1

varnish v1 -errvcl {Argument 'one' already used} {
	import debug;
	backend b1 {.host = "${localhost}";}
	sub vcl_deliver {
		set resp.http.foo5 = debug.argtest("1", one="1");
	}
}

varnish v1 -errvcl {Argument 'one' missing} {
	import debug;
	backend b1 {.host = "${localhost}";}
	sub vcl_deliver {
		set resp.http.foo5 = debug.argtest(two=2.0, three="3");
	}
}

varnish v1 -errvcl {Unknown argument 'five'} {
	import debug;
	backend b1 {.host = "${localhost}";}
	sub vcl_deliver {
		set resp.http.foo5 = debug.argtest("1", two=2.0, five="3");
	}
}
