vtest "backend connection close"

server s1 {
	rxreq
	expect req.http.connection ~ close
	expect req.http.beresp-connection !~ close
	txresp
	expect_close

	accept
	rxreq
	expect req.http.connection !~ close
	expect req.http.beresp-connection ~ close
	txresp
	expect_close

	accept
	rxreq
	expect req.http.connection !~ close
	expect req.http.beresp-connection !~ close
	txresp -hdr "connection: close"
	expect_close

	accept
	rxreq
	expect req.http.connection ~ close
	expect req.http.unset-connection == true
	txresp
	expect_close
} -start

vinyl v1 -vcl+backend {
	sub vcl_recv {
		return (pass);
	}
	sub vcl_backend_fetch {
		set bereq.http.connection = bereq.http.bereq-connection;
	}
	sub vcl_backend_response {
		if (bereq.http.unset-connection) {
			unset bereq.http.connection;
		}
		# NB: this overrides unconditionally on purpose
		set beresp.http.connection = bereq.http.beresp-connection;
	}
} -start

client c1 {
	txreq -hdr "bereq-connection: close, X-Vinyl"
	rxresp
	expect resp.status == 200

	txreq -hdr "beresp-connection: close, X-Vinyl"
	rxresp
	expect resp.status == 200

	txreq
	rxresp
	expect resp.status == 200

	txreq -hdr "bereq-connection: close" -hdr "unset-connection: true"
	rxresp
	expect resp.status == 200
} -run

server s1 -wait

vinyl v1 -expect MAIN.backend_recycle == 0
vinyl v1 -expect VBE.vcl1.s1.conn == 0
