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

vinyl 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
vinyl v1 -clierr 104 "ban"
vinyl v1 -clierr 104 "ban foo"
vinyl v1 -clierr 104 "ban foo bar"
vinyl v1 -clierr 106 "ban a b c && a"
vinyl v1 -clierr 106 "ban a b c && a b"
vinyl v1 -clierr 106 "ban a b c || a b c"
vinyl v1 -cliok "ban.list"

# exact match, not matching
vinyl v1 -cliok "ban req.url == foo"
vinyl 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
vinyl v1 -cliok "ban req.url == /foo"
vinyl 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
vinyl v1 -cliok "ban req.url ~ bar"
vinyl 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
vinyl v1 -cliok "ban req.url ~ foo"
vinyl 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
vinyl v1 -cliok "ban obj.http.foo != bar7"
vinyl 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
vinyl v1 -cliok "ban req.http.foo == barcheck"
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 check, no header
vinyl v1 -cliok "ban req.url ~ foo && obj.http.bar == barcheck"
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
