vtest "catflap"

vinyl v1 -vcl {
	import debug;
	import std;
	import vtc;

	backend dummy { .host = "${bad_backend}"; }

	sub vcl_recv {
		if (req.http.id) {
			debug.catflap(miss);
		} else if (req.http.get == "first") {
			debug.catflap(first);
		} else if (req.http.get == "last") {
			debug.catflap(last);
		} else if (req.http.novcfpass) {
			vtc.workspace_reserve(client, -1);
			return (pass);
		} else if (req.http.novcfmiss) {
			vtc.workspace_reserve(client, -1);
		} else if (req.http.vcfpass) {
			debug.catflap(first);
			return (pass);
		} else if (req.http.rollback) {
			debug.catflap(first);
			std.rollback(req);
			vtc.workspace_reserve(client, -1);
		} else {
			return (fail);
		}
		return (hash);
	}

	sub vcl_backend_error {
		if (bereq.http.novcfpass || bereq.http.vcfpass) {
			set beresp.status = 206;
			return (deliver);
		}
		if (! bereq.http.id) {
			return (deliver);
		}
		set beresp.status = 200;
		set beresp.ttl = 1s;
		set beresp.grace = 1m;
		set beresp.http.id = bereq.http.id;
	}

	sub vcl_deliver {
		if (req.http.restart) {
			unset req.http.restart;
			unset req.http.id;
			set req.http.novcfpass = "yes";
			return (restart);
		}
	}
} -start

client c1 {
	txreq -hdr "id: 1"
	rxresp
	expect resp.status == 200
	txreq -hdr "novcfpass: yes"
	rxresp
	expect resp.status == 206
	txreq -hdr "id: 2"
	rxresp
	expect resp.status == 200
	txreq -hdr "id: 2" -hdr "restart: yes"
	rxresp
	expect resp.status == 206
	txreq -hdr "id: 3"
	rxresp
	expect resp.status == 200
	txreq -hdr "novcfpass: yes"
	rxresp
	expect resp.status == 206

	# the first object is the one which went into cache last

	txreq -hdr "get: first"
	rxresp
	expect resp.status == 200
	expect resp.http.id == "3"
	txreq -hdr "rollback: yes"
	rxresp
	expect resp.status == 200
	expect resp.http.id == "3"
	txreq -hdr "novcfpass: yes"
	rxresp
	expect resp.status == 206

	txreq -hdr "get: last"
	rxresp
	expect resp.status == 200
	expect resp.http.id == "1"
	txreq -hdr "novcfpass: yes"
	rxresp
	expect resp.status == 206

	# VCF with return(pass) must not leave the VCF dangling on the
	# workspace for a subsequent plain miss

	txreq -hdr "vcfpass: yes"
	rxresp
	expect resp.status == 206

	txreq -hdr "novcfmiss: yes"
	rxresp
	expect resp.status == 200
	expect resp.http.id == "3"
} -run
