vtest "vinylncsa handling"

server s1 -repeat 4 {
	rxreq
	txresp
} -start

vinyl v1 -vcl+backend {
	backend none none;

	sub vcl_backend_fetch {
		# XXX would like to do everything in v_b_e, but
		#     return(pass(x)) is not supported
		if (bereq.url == "/hitpass") {
			set bereq.backend = s1;
		} else {
			set bereq.backend = none;
		}
	}

	sub vcl_backend_response {
		set beresp.ttl = 1y;
		return (pass(1y));
	}

	sub vcl_backend_error {
		set beresp.status = 200;
		set beresp.ttl = 1y;
		set beresp.body = bereq.url;
		if (bereq.url ~ "^/hitmiss") {
			set beresp.uncacheable  = true;
		}
		return (deliver);
	}

	sub vcl_miss {
		if (req.url == "/hitmisspass" && req.is_hitmiss) {
			return (pass);
		}
	}

	sub vcl_recv {
		if (req.url == "/pass") {
			return (pass);
		}
		if (req.url == "/synth") {
			return (synth(204));
		}
	}
} -start

client c1 {
	# prime
	txreq -url "/hitmiss"
	rxresp
	txreq -url "/hitmisspass"
	rxresp
	expect resp.status == 200
	txreq -url "/hitpass"
	rxresp
	expect resp.status == 200
	txreq -url "/hit"
	rxresp
	expect resp.status == 200

	# re-check
	txreq -url "/hitmiss"
	rxresp
	expect resp.status == 200
	txreq -url "/hitmisspass"
	rxresp
	expect resp.status == 200
	txreq -url "/hitpass"
	rxresp
	expect resp.status == 200
	txreq -url "/hit"
	rxresp
	expect resp.status == 200

	# others
	txreq -url "/pass"
	rxresp
	expect resp.status == 200
	txreq -url "/synth"
	rxresp
	expect resp.status == 204
} -run

shell {
	cat <<EOF >expect
miss /hitmiss
miss /hitmisspass
miss /hitpass
miss /hit
hitmiss /hitmiss
pass /hitmisspass
hitpass /hitpass
hit /hit
pass /pass
synth /synth
EOF
	vinylncsa -d -n ${v1_name} -F "%{Vinyl:handling}x %U" >have
	diff -u expect have
}
