varnishtest "Test banning a url with cli: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 { } -start

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

# syntax checks
varnish v1 -clierr 104 "ban"
varnish v1 -clierr 104 "ban foo"
varnish v1 -clierr 104 "ban foo bar"
varnish v1 -clierr 106 "ban a b c && a"
varnish v1 -clierr 106 "ban a b c && a b"
varnish v1 -clierr 106 "ban a b c || a b c"
varnish v1 -cliok "ban.list"

# exact match, not matching
varnish v1 -cliok "ban req.url == foo"
varnish v1 -cliok "ban.list"

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

# exact match, matching
varnish v1 -cliok "ban req.url == /foo"
varnish v1 -cliok "ban.list"
client c1 {
	txreq -url "/foo"
	rxresp
	expect resp.status == 200
	expect resp.http.foo == bar6
	expect resp.bodylen == 6
} -run

# regexp nonmatch
varnish v1 -cliok "ban req.url ~ bar"
varnish v1 -cliok "ban.list"

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


# regexp match
varnish v1 -cliok "ban req.url ~ foo"
varnish v1 -cliok "ban.list"

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

# header check, nonmatch
varnish v1 -cliok "ban obj.http.foo != bar7"
varnish v1 -cliok "ban.list"

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

# header check, match
varnish v1 -cliok "ban req.http.foo == barcheck"
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 check, no header
varnish v1 -cliok "ban req.url ~ foo && obj.http.bar == barcheck"
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
