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

vinyl v1 -vcl+backend {} -start

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

# There is one "magic" ban from boot
vinyl v1 -expect bans_added == 2
vinyl 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

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

# Add another ban
vinyl v1 -cliok "ban req.url ~ FOO"
vinyl v1 -expect bans_added == 3
vinyl 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

vinyl v1 -expect bans_tested == 1
vinyl v1 -expect bans_tests_tested == 1
vinyl 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
vinyl v1 -cliok "ban req.url ~ KILROY"
vinyl v1 -cliok "ban req.url ~ FOO"
vinyl v1 -expect bans_added == 5

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

# This should incapacitate the two previous FOO bans.
vinyl v1 -cliok "ban req.url ~ FOO"
vinyl v1 -expect bans_added == 6
vinyl v1 -expect bans_dups == 3
vinyl 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
vinyl v1 -expect bans_tested == 2
vinyl v1 -expect bans_tests_tested == 2
vinyl v1 -cliok "ban.list"
vinyl v1 -clijson "ban.list -j"

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

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