vtest "test ban obj.* except obj.http.*"

# see c00021.vtc for obj.http.* tests

server s1 {
	rxreq
	expect req.url == "/old"
	txresp -status 204
	rxreq
	txresp -bodylen 1
	rxreq
	txresp -bodylen 2
	rxreq
	txresp -bodylen 3
	rxreq
	txresp -bodylen 4
	rxreq
	txresp -bodylen 5
	rxreq
	txresp -bodylen 6
	rxreq
	txresp -bodylen 7
} -start

vinyl v1 -vcl+backend {
	sub vcl_deliver {
		set resp.http.hits = obj.hits;
	}
} -start

client c1 {
	txreq -url "/old"
	rxresp
	expect resp.status == 204

	txreq -url "/old"
	rxresp
	expect resp.status == 204

	txreq
	rxresp
	expect resp.bodylen == 1
} -run

vinyl v1 -cliok "ban obj.status == 201"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 1
} -run

vinyl v1 -cliok "ban obj.status == 200"
vinyl v1 -cliok "ban.list"
vinyl v1 -clijson "ban.list -j"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 2
} -run

vinyl v1 -cliok "ban obj.keep == 0s && obj.ttl > 1d"
vinyl v1 -cliok "ban obj.keep == 0s && obj.ttl > 1d"

# BANS_FLAG_NODEDUP
vinyl v1 -cliexpect {(?s) obj\.ttl > 1d\b.*obj\.ttl > 1d\b} "ban.list"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 2
} -run

vinyl v1 -cliok "ban obj.status == 200 && obj.ttl <= 2m"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 3
} -run

vinyl v1 -cliok "ban obj.age > 1d"
vinyl v1 -cliok "ban obj.age > 1d"

# BANS_FLAG_NODEDUP
vinyl v1 -cliexpect {(?s) obj\.age > 1d\b.*obj\.age > 1d\b} "ban.list"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 3
} -run

vinyl v1 -cliok "ban obj.status == 200 && obj.age < 1m"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 4
} -run

vinyl v1 -cliok "ban obj.grace != 10s"
vinyl v1 -cliok "ban obj.grace != 10s"

# ! BANS_FLAG_NODEDUP
vinyl v1 -cliexpect {(?s) obj\.grace != 10s\b.* \d C\b} "ban.list"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 4
} -run

vinyl v1 -cliok "ban obj.status == 200 && obj.grace == 10s"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 5
} -run

vinyl v1 -cliok "ban obj.keep != 0s"
vinyl v1 -cliok "ban obj.keep != 0s"

# ! BANS_FLAG_NODEDUP
vinyl v1 -cliexpect {(?s) obj\.keep != 0s\b.* \d C\b} "ban.list"

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 5
} -run

vinyl v1 -cli "param.set ban_lurker_age 0.1"
vinyl v1 -cliok "ban obj.status == 200 && obj.keep == 0s"
delay 1

client c1 {
	txreq
	rxresp
	expect resp.bodylen == 6

	txreq
	rxresp
	expect resp.bodylen == 6
	expect resp.http.hits == 1
} -run

# now we should have two objects, /old from the beginning and the len==6 object
vinyl v1 -cliexpect { 2 C} "ban.list"
vinyl v1 -cli "param.set ban_lurker_age 600"
vinyl v1 -cliok "ban obj.last_hit < 1s"

# /old survives, but len==6 gets removed
client c1 {
	txreq -url "/old"
	rxresp
	expect resp.http.age > 0
	expect resp.http.hits == 2

	txreq
	rxresp
	expect resp.bodylen == 7
	expect resp.http.hits == 0
} -run

# duration formatting - 0s is being tested above
vinyl v1 -cliok "ban obj.keep == 123ms"
vinyl v1 -cliexpect {(?s) obj\.keep == 12[1-3]ms\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 0.456s"
vinyl v1 -cliexpect {(?s) obj\.keep == 45[3-6]ms\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 6.789s"
vinyl v1 -cliexpect {(?s) obj\.keep == 6.78[5-9]s\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 42y"
vinyl v1 -cliexpect {(?s) obj\.keep == 42y\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 365d"
vinyl v1 -cliexpect {(?s) obj\.keep == 1y\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 9w"
vinyl v1 -cliexpect {(?s) obj\.keep == 9w\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 7d"
vinyl v1 -cliexpect {(?s) obj\.keep == 1w\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 3d"
vinyl v1 -cliexpect {(?s) obj\.keep == 3d\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 24h"
vinyl v1 -cliexpect {(?s) obj\.keep == 1d\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 18h"
vinyl v1 -cliexpect {(?s) obj\.keep == 18h\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 1.5h"
vinyl v1 -cliexpect {(?s) obj\.keep == 90m\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 10m"
vinyl v1 -cliexpect {(?s) obj\.keep == 10m\b} "ban.list"
vinyl v1 -cliok "ban obj.keep == 0.5m"
vinyl v1 -cliexpect {(?s) obj\.keep == 30s\b} "ban.list"
