vtest "new vinylncsa matching rules"

# Test things we send to the backend

server s1 {
	rxreq
	txresp
} -start

vinyl v1 -vcl+backend {

	sub vcl_backend_fetch {
		set bereq.http.bereqhdr = "vbf-modified";
		set bereq.method = "HEAD";
		set bereq.url = "/vbf-url?q=vbfQuerry";
		set bereq.http.Authorization = "basic dmJmOnBhc3M=";
		unset bereq.http.unset;
	}

	sub vcl_backend_response {
		set bereq.http.bereqhdr = "vbr-modified";
		set bereq.http.notsent = "notsent";
		set bereq.http.unset = "toolate";
		set bereq.method = "CONNECT";
		set bereq.url = "/vbr-url?q=vbrQuerry";
		set bereq.http.Authorization = "basic dmJyOnBhc3M=";
	}

} -start


client c1 {
	txreq -url "/client-url?q=clientQuerry" -hdr "bereqhdr: client-header" -hdr "unset: client" -hdr "Authorization:basic Y2xpZW50OnBhc3M="
	rxresp
} -run

shell {
	vinylncsa -n ${v1_name} -d -b -F '%H %{bereqhdr}i %{notsent}i %{unset}i %m %q %U %u' > ncsa_sb.txt

	cat >expected_sb.txt <<-EOF
	HTTP/1.1 vbf-modified - - HEAD ?q=vbfQuerry /vbf-url vbf
	EOF
	diff -u expected_sb.txt ncsa_sb.txt
}

shell {
	vinylncsa -n ${v1_name} -d -b -F '%{notsent:first}i %{notsent:last}i %{unset:first}i %{unset:last}i' > ncsa_sb.txt

	cat >expected_sb.txt <<-EOF
	notsent notsent client toolate
	EOF
	diff -u expected_sb.txt ncsa_sb.txt
}

vinyl v1 -stop

# Test things we receive from the backend

server s1 {
	rxreq
	txresp -status 202 -hdr "beresp: origin" -hdr "unset: origin"
} -start

vinyl v1 -vcl+backend {

	sub vcl_backend_response {
		set beresp.http.beresp = "vbr-updated";
		set beresp.status = 200;
		unset beresp.http.unset;
	}

} -start


client c1 {
	txreq
	rxresp
} -run


shell {
	vinylncsa -n ${v1_name} -d -b -F '%s %{beresp}o %{unset}o' > ncsa_rb.txt

	cat >expected_rb.txt <<-EOF
	202 origin origin
	EOF
	diff -u expected_rb.txt ncsa_rb.txt
}

shell {
	vinylncsa -n ${v1_name} -d -b -F '%{beresp:first}o %{beresp:last}o %{unset:first}o %{unset:last}o' > ncsa_rb.txt

	cat >expected_rb.txt <<-EOF
	origin vbr-updated origin origin
	EOF
	diff -u expected_rb.txt ncsa_rb.txt
}

vinyl v1 -stop

# Test things we send to the client

server s1 {
	rxreq
	txresp -status 202 -hdr "resp: origin" -hdr "unset: origin"
} -start

vinyl v1 -vcl+backend {

	sub vcl_backend_response {
		set beresp.http.resp = "vbr-updated";
		set beresp.status = 200;
	}

	sub vcl_deliver {
		set resp.http.resp = "deliver-updated";
		set resp.status = 201;
		set resp.http.added = "deliver";
		unset resp.http.unset;
	}

} -start


client c1 {
	txreq
	rxresp
} -run

shell {
	vinylncsa -n ${v1_name} -d -c -F '%s %{resp}o %{unset}o %{added}o' > ncsa_sc.txt

	cat >expected_sc.txt <<-EOF
	201 deliver-updated - deliver
	EOF
	diff -u expected_sc.txt ncsa_sc.txt
}

shell {
	vinylncsa -n ${v1_name} -d -c -F '%{resp:first}o %{resp:last}o %{unset:first}o %{unset:last}o' > ncsa_sc.txt

	cat >expected_sc.txt <<-EOF
	vbr-updated deliver-updated origin origin
	EOF
	diff -u expected_sc.txt ncsa_sc.txt
}

vinyl v1 -stop

# Test things we receive from the client

server s1 {
	rxreq
	txresp
} -start

vinyl v1 -vcl+backend {

	sub vcl_recv {
		set req.http.reqhdr = "recv-modified";
		set req.method = "HEAD";
		set req.url = "/recv-url?q=recvQuerry";
		set req.http.Authorization = "basic cmVjdjpwYXNz";
		set req.http.notreceived = "recv";
		unset req.http.unset;
	}

	sub vcl_hash {
		set req.http.reqhdr = "hash-modified";
		set req.method = "GET";
		set req.url = "/hash-url?q=hashQuerry";
		set req.http.Authorization = "basic aGFzaDpwYXNz";
		set req.http.notreceived = "hash";
		set req.http.unset = "hash";
	}

} -start


client c1 {
	txreq -req "POST" -url "/client-url?q=clientQuerry" \
	    -hdr "reqhdr: client-header" \
	    -hdr "Authorization:basic Y2xpZW50OnBhc3M=" \
	    -hdr "unset: client"
	rxresp
} -run


shell {
	vinylncsa -n ${v1_name} -d -c -F '%H %{reqhdr}i %{notreceived}i %{unset}i %m %q %U %u' > ncsa_rc.txt

	cat >expected_rc.txt <<-EOF
	HTTP/1.1 client-header - client POST ?q=clientQuerry /client-url client
	EOF
	diff -u expected_rc.txt ncsa_rc.txt
}

shell {
	vinylncsa -n ${v1_name} -d -c -F '%{notreceived:first}i %{notreceived:last}i %{unset:first}i %{unset:last}i' > ncsa_rc.txt

	cat >expected_rc.txt <<-EOF
	recv hash client hash
	EOF
	diff -u expected_rc.txt ncsa_rc.txt
}
