vtest "Test VCL regsub()"

server s1 {
	rxreq
	txresp \
		-hdr "Foobar: _barf_" \
		-hdr "Connection: close" \
		-body "012345\n"
} -start

vinyl v1 -vcl+backend {
	sub vcl_backend_response {
		set beresp.http.Snafu1 = regsub(beresp.http.Foobar, "ar", "\0\0");
		set beresp.http.Snafu2 =
		    regsub(beresp.http.Foobar, "(b)(a)(r)(f)", "\4\3\2p");
		set beresp.http.Snafu3 =
		    regsub(beresp.http.Foobar, "(b)(a)(r)(f)", "\4\\\3\2p");
		set beresp.http.Snafu4 =
		    regsub(beresp.http.Foobar, "(b)(a)(r)(f)", "\4\&\3\2p");
		set beresp.http.Snafu5 =
		    regsub(beresp.http.Foobar, "(b)(a)(r)(f)", "\0\4\3\2\\p");
		set beresp.http.Snafu6 =
		    regsub(beresp.http.Foobar, "(b)(a)(r)(f)", "\4\&\3\2p\");
		set beresp.http.Snafu7 =
		    regsub(beresp.http.Foobar, "ar", bereq.http.nosuchheader);
		set beresp.http.inval =
		    regsub(beresp.http.Foobar, "(b)(a)(r)f", "\9\8\7\6\5\4\3\2p");
	}
} -start

client c1 {
	txreq -url "/"
	rxresp
	expect resp.status == 200
	expect resp.http.X-Vinyl == "1001"
	expect resp.http.foobar == "_barf_"
	expect resp.http.snafu1 == "_bararf_"
	expect resp.http.snafu2 == "_frap_"
	expect resp.http.snafu3 == "_f\\rap_"
	expect resp.http.snafu4 == "_f&rap_"
	# NB: have to escape the \\ in the next two lines
	expect resp.http.snafu5 == "_barffra\\p_"
	expect resp.http.snafu6 == "_f&rap\\_"
	expect resp.http.snafu7 == "_bf_"
	expect resp.http.inval == "_rap_"
} -run
