vtest "unset bereq.body tests"

server s1 {
	rxreq
	expect req.method == "GET"
	expect req.url == "/cached"
	expect req.http.Content-Length == <undef>
	expect req.http.Transfer-Encoding == <undef>
	txresp

	rxreq
	expect req.method == "GET"
	expect req.url == "/cachednobody"
	expect req.http.Content-Length == <undef>
	expect req.http.Transfer-Encoding == <undef>
	txresp

	rxreq
	expect req.method == "HEAD"
	expect req.url == "/head-restart-get"
	expect req.http.Content-Length == <undef>
	expect req.http.Transfer-Encoding == <undef>
	txresp

	rxreq
	expect req.method == "GET"
	expect req.url == "/head-restart-get"
	expect req.http.Content-Length == <undef>
	expect req.http.Transfer-Encoding == <undef>
	txresp

	rxreq
	expect req.method == "HEAD"
	expect req.url == "/head-restart-get-chunked"
	expect req.http.Content-Length == <undef>
	expect req.http.Transfer-Encoding == <undef>
	txresp

	rxreq
	expect req.method == "GET"
	expect req.url == "/head-restart-get-chunked"
	expect req.http.Content-Length == <undef>
	expect req.http.Transfer-Encoding == <undef>
	txresp
} -start

vinyl v1 -vcl+backend {
	import std;

	sub vcl_recv {
		if (req.url == "/cached") {
			std.cache_req_body(2KB);
		}
		set req.http.restarts = req.restarts;
	}

	sub vcl_hash {
		hash_data(req.restarts);
	}

	sub vcl_deliver {
		if (req.url ~ "^/head-restart-get" && req.restarts == 0) {
			return (restart);
		}
	}

	sub vcl_backend_fetch {
		if (bereq.url ~ "^/head-restart-get" && bereq.http.restarts == "0") {
			set bereq.method = "HEAD";
		}

		if (bereq.http.restarts == "0") {
			unset bereq.body;
		}

		if (bereq.http.Content-Length) {
			return (abandon);
		}

		return (fetch);
	}
} -start

client c1 {
	txreq -url "/cached" -body "fine"
	rxresp
	expect resp.status == 200

	txreq -url "/cachednobody"
	rxresp
	expect resp.status == 200

	txreq -url "/head-restart-get" -body "fine"
	rxresp
	expect resp.status == 200

	txreq -url "/head-restart-get-chunked" -hdr "Transfer-encoding: chunked"
	chunkedlen 10
	chunkedlen 0
	rxresp
	expect resp.status == 200
} -run
