varnishtest "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

varnish v1 -vcl+backend {
	sub vcl_recv {
		if (req.method == "PURGE") {
			ban ("req.url == " + req.url);
			return (synth(410));
		}
		if (req.method == "PURGESTR") {
			ban ("" + req.http.ban);
			return (synth(410));
		}
	}
} -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 PURGE -url /foox
	rxresp
	expect resp.status == 410
} -run
varnish 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 PURGE -url /foo
	rxresp
	expect resp.status == 410
} -run
varnish 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 PURGESTR -hdr "ban: req.url != /foo"
	rxresp
	expect resp.status == 410
} -run
varnish 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 PURGESTR -hdr "Ban: obj.http.foo == bar6"
	rxresp
	expect resp.status == 410
} -run
varnish 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 PURGESTR -hdr "Ban: obj.http.foo == bar6"
	rxresp
	expect resp.status == 410
} -run

varnish 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 PURGESTR -hdr "Ban: req.http.foo == barcheck"
	rxresp
	expect resp.status == 410
} -run
varnish 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 PURGESTR -hdr "Ban: obj.http.foo == barcheck"
	rxresp
	expect resp.status == 410
} -run
varnish v1 -cliok "ban.list"
varnish 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
