vtest "Test request byte counters"

server s1 {
	rxreq
	expect req.url == "/1"
	txresp -hdr "Accept-ranges: bytes" -bodylen 1000

	rxreq
	expect req.url == "/2"
	txresp -hdr "Accept-ranges: bytes" -bodylen 2000
} -start

vinyl v1 -vcl+backend {
	sub vcl_backend_response {
		set beresp.do_stream = false;
	}
	sub vcl_deliver {
		unset resp.http.date;
		unset resp.http.age;
		unset resp.http.via;
		unset resp.http.X-Vinyl;
	}
} -start

# Request (1001):
# POST /1 HTTP/1.1\r\n		 18 bytes
# Host: foo\r\n			 11 bytes
# Content-Length: 4\r\n		 19 bytes
# User-Agent: c1\r\n		 16 bytes
# \r\n				  2 bytes
# Total:			 66 bytes
# Response:
# HTTP/1.1 200 OK\r\n		 17 bytes
# Content-Length: 1000\r\n	 22 bytes
# Connection: keep-alive\r\n	 24 bytes
# Accept-Ranges: bytes\r\n	 22 bytes
# Server: s1\r\n		 12 bytes
# \r\n				  2 bytes
# Total:			 99 bytes

# Request (1003):
# GET /2 HTTP/1.1\r\n		 17 bytes
# Host: foo\r\n			 11 bytes
# \r\n				  2 bytes
# Total:			 30 bytes
# Response:
# HTTP/1.1 200 OK\r\n		 17 bytes
# Content-Length: 2000\r\n	 22 bytes
# Connection: keep-alive\r\n	 24 bytes
# Accept-Ranges: bytes\r\n	 22 bytes
# Server: s1\r\n		 12 bytes
# \r\n				  2 bytes
# Total:			 99 bytes

# Request (1005):
# GET /2 HTTP/1.1\r\n		 17 bytes
# Host: foo\r\n			 11 bytes
# \r\n				  2 bytes
# Total:			 30 bytes
# Response:
# HTTP/1.1 200 OK\r\n		 17 bytes
# Content-Length: 2000\r\n	 22 bytes
# Connection: keep-alive\r\n	 24 bytes
# Accept-Ranges: bytes\r\n	 22 bytes
# Server: s1\r\n		 12 bytes
# \r\n				  2 bytes
# Total:			 99 bytes

# Request (1006):
# GET\r\n			  5 bytes
# \r\n				  2 bytes
# Total:			  7 bytes
# Response:
# HTTP/1.1 400 Bad Request\r\n	 26 bytes
# \r\n				  2 bytes
# Total:			 28 bytes
logexpect l1 -v v1 -g session {
	expect * 1001	Begin	"^req .* rxreq"
	expect * =	ReqAcct	"^66 4 70 99 1000 1099$"
	expect 0 =	End
	expect * 1003	Begin	"^req .* rxreq"
	expect * =	ReqAcct "^30 0 30 99 2000 2099$"
	expect 0 =	End
	expect * 1005	Begin	"^req .* rxreq"
	expect * =	ReqAcct "^30 0 30 99 2000 2099$"
	expect 0 =	End
	expect * 1006	Begin	"^req .* rxreq"
	expect * =	ReqAcct	"^7 0 7 28 0 28$"
	expect 0 =	End
} -start

# Request 1001
client c1 {
	txreq -method POST -url "/1" -hdr "Host: foo" -body "asdf"
	rxresp
	expect resp.http.accept-ranges == "bytes"
	expect resp.status == 200

	send "GET /2 HTTP/1.1\r\nHost: foo\r\n\r\nGET /2 HTTP/1.1\r\nHost: foo\r\n\r\n"
	rxresp
	expect resp.http.accept-ranges == "bytes"
	expect resp.status == 200
	rxresp
	expect resp.http.accept-ranges == "bytes"
	expect resp.status == 200

	send "GET\r\n\r\n"
	rxresp
	expect resp.http.accept-ranges == "resp.http.accept-ranges"
	expect resp.status == 400
} -run

logexpect l1 -wait

vinyl v1 -expect s_req_hdrbytes == 133
vinyl v1 -expect s_req_bodybytes == 4
vinyl v1 -expect s_resp_hdrbytes == 325
vinyl v1 -expect s_resp_bodybytes == 5000
