vtest "Test conversion functions in std"

server s1 {
	rxreq
	txresp
} -start

vinyl v1 -vcl+backend {
	import std;

	sub vcl_deliver {
		set resp.http.x-foo = std.integer(req.http.foo, 0);
		set resp.http.x-bar = std.integer(time=std.time(
		    real=std.real(resp.http.x-foo, 0.0), fallback=now),
		    fallback=1);

		set resp.http.x-baz = std.real(time=std.time(
		    real=std.real(resp.http.x-foo, 0.0), fallback=now),
		    fallback=1.0);

		set resp.http.x-qux = std.integer(
		    real=std.real(req.http.foo, 2.0), fallback=2);

		set resp.http.x-xyzzy1 = std.integer(
		    s=std.real(req.http.foo, 2.0));

		set resp.http.x-xyzzy2 = std.integer(
		    real=std.real(req.http.foo, 2.0));

		set resp.http.x-int-fallback = std.integer(
		    real=123456789012.345 * 1000.0 * 10,
		    fallback=2);
	}
} -start

client c1 {
	txreq -hdr "foo: 1140618699"
	rxresp
	expect resp.http.x-foo == resp.http.x-bar
	expect resp.http.x-baz == 1140618699.000
	expect resp.http.x-qux == 1140618699
	expect resp.http.x-xyzzy1 == resp.http.x-qux
	expect resp.http.x-xyzzy2 == resp.http.x-qux
	expect resp.http.x-int-fallback == 2
} -run

# check we get the fallback if the conversion fails
client c2 {
	txreq -hdr "foo: -9e99+1"
	rxresp
	expect resp.http.x-bar == 0
	expect resp.http.x-baz == 0.000
	expect resp.http.x-qux == 2
} -run
