vtest "Test banning a url with VCL ban"

server s1 {
	rxreq
	expect req.url == "/foo"
	txresp -hdr "foo: bar5" -body "1111\n"

	rxreq
	expect req.url == "/foo"
	txresp -hdr "foo: bar6" -body "11111\n"

	rxreq
	expect req.url == "/foo"
	txresp -hdr "foo: bar7" -body "111111\n"

	rxreq
	expect req.url == "/foo"
	txresp -hdr "foo: bar8" -body "1111111\n"
} -start

# code from purging.rst
vinyl v1 -vcl+backend {
	import std;

	sub vcl_recv {
		if (req.method == "BAN") {
			if (std.ban("req.http.host == " + req.http.host +
				    " && req.url == " + req.url)) {
				return(synth(204, "Ban added"));
			} else {
				# return ban error in 400 response
				return(synth(400, std.ban_error()));
			}
		}
		if (req.method == "BANSTR") {
			if (std.ban(req.http.ban)) {
				return(synth(204, "Ban added"));
			} else {
				# return ban error in 400 response
				return(synth(400, std.ban_error()));
			}
		}
	}
} -start

# Fetch into cache
client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar5
	expect resp.bodylen == 5
} -run

# Ban: something else
client c1 {
	txreq -req BAN -url /foox
	rxresp
	expect resp.status == 204
} -run
vinyl v1 -cliok "ban.list"

# Still in cache
client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar5
	expect resp.bodylen == 5
} -run

# Ban: it
client c1 {
	txreq -req BAN -url /foo
	rxresp
	expect resp.status == 204
} -run
vinyl v1 -cliok "ban.list"

# New obj
client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar6
	expect resp.bodylen == 6
} -run

# Ban: everything else
client c1 {
	txreq -req BANSTR -hdr "ban: req.url != /foo"
	rxresp
	expect resp.status == 204
} -run
vinyl v1 -cliok "ban.list"

# still there
client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar6
	expect resp.bodylen == 6
} -run

# Ban: it
client c1 {
	txreq -req BANSTR -hdr "Ban: obj.http.foo == bar6"
	rxresp
	expect resp.status == 204
} -run
vinyl v1 -cliok "ban.list"

# New one
client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar7
	expect resp.bodylen == 7
} -run

# Ban: something else
client c1 {
	txreq -req BANSTR -hdr "Ban: obj.http.foo == bar6"
	rxresp
	expect resp.status == 204
} -run

vinyl v1 -cliok "ban.list"

# Still there
client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar7
	expect resp.bodylen == 7
} -run

# Header match
client c1 {
	txreq -req BANSTR -hdr "Ban: req.http.foo == barcheck"
	rxresp
	expect resp.status == 204
} -run
vinyl v1 -cliok "ban.list"

client c1 {
	txreq -url "/foo" -hdr "foo: barcheck"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar8
	expect resp.bodylen == 8
} -run

# Header match
client c1 {
	txreq -req BANSTR -hdr "Ban: obj.http.foo == barcheck"
	rxresp
	expect resp.status == 204
} -run
vinyl v1 -cliok "ban.list"
vinyl v1 -clijson "ban.list -j"

client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar8
	expect resp.bodylen == 8
} -run

# Error
client c1 {
	txreq -req BANSTR -hdr "Ban: xobj.http.foo == barcheck"
	rxresp
	expect resp.status == 400
	expect resp.reason == {Unknown or unsupported field "xobj.http.foo"}
} -run
