varnishtest "Check ban counters and duplicate ban elimination"

server s1 {
	rxreq
	txresp -hdr "foo: 0" -body "foo0"
	rxreq
	txresp -hdr "foo: 1" -body "foo1"
	rxreq
	txresp -hdr "foo: 2" -body "foo2"
	rxreq
	txresp -hdr "foo: 3" -body "foo3"
} -start

varnish v1 -vcl+backend {} -start

varnish v1 -cliok "ban req.url ~ FOO"

# There is one "magic" ban from boot
varnish v1 -expect bans_added == 2
varnish v1 -cliok "ban.list"

# Our fetch is not affected by the ban
# as the FOO-ban was preexisting
client c1 {
	txreq -url /BAR
	rxresp
	expect resp.http.foo == 0
	txreq -url /FOO
	rxresp
	expect resp.http.foo == 1
} -run

varnish v1 -cliok "ban.list"
varnish v1 -expect bans_tested == 0
varnish v1 -expect bans_tests_tested == 0

# Add another ban
varnish v1 -cliok "ban req.url ~ FOO"
varnish v1 -expect bans_added == 3
varnish v1 -cliok "ban.list"

# The cached object will be band, and a new
# fetched from the backend
client c1 {
	txreq -url /FOO
	rxresp
	expect resp.http.foo == 2
} -run

varnish v1 -expect bans_tested == 1
varnish v1 -expect bans_tests_tested == 1
varnish v1 -cliok "ban.list"

# Fetch the cached copy, just for grins
client c1 {
	txreq -url /FOO
	rxresp
	expect resp.http.foo == 2
} -run


# Now add another two bans, Kilroy should not be hit
varnish v1 -cliok "ban req.url ~ KILROY"
varnish v1 -cliok "ban req.url ~ FOO"
varnish v1 -expect bans_added == 5

# Enable dup removal of bans
varnish v1 -cliok "param.set ban_dups on"

# This should incapacitate the two previous FOO bans.
varnish v1 -cliok "ban req.url ~ FOO"
varnish v1 -expect bans_added == 6
varnish v1 -expect bans_dups == 3
varnish v1 -cliok "ban.list"

# And we should get a fresh object from backend
client c1 {
	txreq -url /FOO
	rxresp
	expect resp.http.foo == 3
} -run

# With only two objects having ever been compared
varnish v1 -expect bans_tested == 2
varnish v1 -expect bans_tests_tested == 2
varnish v1 -cliok "ban.list"
varnish v1 -clijson "ban.list -j"

# Test a bogus regexp
varnish v1 -clierr 106 "ban req.url ~ [[["

# Ban expression with quoting
varnish v1 -cliok {ban req.url ~ "BAR"}
shell {varnishadm -n ${tmpdir}/v1 ban 'obj.http.Host ~ \"Foo\"'}
varnish v1 -cliexpect {(?s)\d+\.\d+\s+\d+\s+-\s+\Qobj.http.Host ~ "Foo"\E} "ban.list"
varnish v1 -clijson "ban.list -j"
