varnishtest "Test std.time() and std.strftime()"

server s1 {
	rxreq
	txresp
} -start

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

	sub vcl_deliver {
		set resp.http.x-date = std.time(
		    regsub(req.http.x-date, "z", " "), now);
		if (req.http.over) {
			vtc.workspace_alloc(client, -1);
		}
		set resp.http.utc = std.strftime(
		    std.time(req.http.x-date, now),
		    "%Y%m%dT%H%M%SZ");
		if (std.time(req.http.x-date, now) < now - 1y) {
			set resp.http.x-past = 1;
		}
		if (std.time(req.http.x-date, now) > now + 1y) {
			set resp.http.x-future = 1;
		}
		if (req.http.real) {
			if (resp.http.x-date != "" +
			    std.time(real=std.real(req.http.real))) {
				return(synth(503));
			}
		}
		if (req.http.integer) {
			if (resp.http.x-date != "" +
			    std.time(integer=std.integer(req.http.integer))) {
				return(synth(503));
			}
		}
	}
} -start

client c1 {
	txreq -hdr "X-Date: Mon, 20 Dec 2010 00:00:00 GMT" \
	      -hdr "real: 1292803200.0" \
	      -hdr "integer: 1292803200"
	rxresp
	expect resp.status == 200
	expect resp.http.x-past == 1
	expect resp.http.x-date == "Mon, 20 Dec 2010 00:00:00 GMT"
	expect resp.http.utc == "20101220T000000Z"

	# invalid date
	txreq -hdr "X-Date: Monday, 20-Dec-30 00:00:00 GMT" \
	      -hdr "real: 1923955200.0" \
	      -hdr "integer: 1923955200"
	rxresp
	expect resp.status == 503
	expect resp.http.x-past == <undef>
	expect resp.http.x-future == <undef>

	txreq -hdr "X-Date: Monday, 30-Feb-15 00:00:00 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-past == <undef>
	expect resp.http.x-future == <undef>

	txreq -hdr "X-Date: Friday, 20-Dec-30 00:00:00 GMT" \
	      -hdr "real: 1923955200.0" \
	      -hdr "integer: 1923955200"
	rxresp
	expect resp.status == 200
	expect resp.http.x-future == 1
	expect resp.http.x-date == "Fri, 20 Dec 2030 00:00:00 GMT"
	expect resp.http.utc == "20301220T000000Z"

	txreq -hdr "X-Date: Mon Dec 20 00:00:00 2010"
	rxresp
	expect resp.status == 200
	expect resp.http.x-past == 1
	expect resp.http.x-date == "Mon, 20 Dec 2010 00:00:00 GMT"
	expect resp.http.utc == "20101220T000000Z"

	txreq -hdr "X-Date: 2030-12-20T00:00:00"
	rxresp
	expect resp.status == 200
	expect resp.http.x-future == 1
	expect resp.http.x-date == "Fri, 20 Dec 2030 00:00:00 GMT"

	txreq -hdr "X-Date: 1055455200.00"
	rxresp
	expect resp.status == 200
	expect resp.http.x-past == 1
	expect resp.http.x-date == "Thu, 12 Jun 2003 22:00:00 GMT"
	expect resp.http.utc == "20030612T220000Z"

	txreq -hdr "X-Date: 1923955200"
	rxresp
	expect resp.status == 200
	expect resp.http.x-future == 1
	expect resp.http.x-date == "Fri, 20 Dec 2030 00:00:00 GMT"

	# overflow
	txreq -hdr "X-Date: 1923955200" -hdr "Over: 1"
	rxresp
	expect resp.status == 500
} -run

client c1 {
	delay .2

	# Coverage tests of vtim.c

	# leapsecond
	txreq -hdr "X-Date: Mon, 20 Dec 2010 00:00:60 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date == "Mon, 20 Dec 2010 00:00:59 GMT"
	delay .1

	# Range tests
	txreq -hdr "X-Date: Mon, 20 Dec 2010 00:00:61 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != "Mon, 20 Dec 2010 00:00:61 GMT"
	delay .1

	txreq -hdr "X-Date: Mon, 20 Dec 2010 00:60:00 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != "Mon, 20 Dec 2010 00:60:00 GMT"
	delay .1

	txreq -hdr "X-Date: Mon, 20 Dec 2010 24:00:00 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != "Mon, 20 Dec 2010 24:00:00 GMT"
	delay .1

	txreq -hdr "X-Date: Tue, 20 Dec 2010 00:00:00 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != "Tue, 20 Dec 2010 00:00:00 GMT"
	delay .1

	txreq -hdr "X-Date: Mon, 29 Feb 2010 00:00:00 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != "Mon, 29 Feb 2010 00:00:00 GMT"
	delay .1

	txreq -hdr "X-Date: Wed, 29 Feb 2012 00:00:00 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date == "Wed, 29 Feb 2012 00:00:00 GMT"
	delay .1

	txreq -hdr "X-Date: 2010-13-20T00:00:00"
	rxresp
	expect resp.status == 200
	delay .1

	txreq -hdr "X-Date: Sun 31 Dec 1899 23:59:59 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != "Sun 31 Dec 1899 23:59:59 GMT"
	delay .1

	# White space etc.
	txreq -hdr "X-Date: z Wed, 29 Feb 2012 00:00:00 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date == "Wed, 29 Feb 2012 00:00:00 GMT"
	delay .1

	txreq -hdr "X-Date: Wedx 29 Feb 2012 00:00:00 GMT"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != "Wed, 29 Feb 2012 00:00:00 GMT"
	delay .1

	txreq -hdr "X-Date: Wed, 29 Feb 2012 00:00:00 GMT x"
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != "Wed, 29 Feb 2012 00:00:00 GMT"
	delay .1

	txreq
	rxresp
	expect resp.status == 200
	expect resp.http.x-date != <undef>
} -run
