vtest	"H1->H2 Upgrade"

barrier b1 cond 2 -cyclic

server s1 {
	rxreq
	expect req.url == /noupgrade
	expect req.http.host == foo.bar
	txresp -status 400 -bodylen 10
} -start

vinyl v1 -vcl+backend {} -start

vinyl v1 -cliok "param.set feature -http2"
vinyl v1 -cliok "param.set debug +syncvsl"

client c1 {
	send "GET /noupgrade HTTP/1.1\r\n"
	send "Host: foo.bar\r\n"
	send "Upgrade: h2c\r\n"
	send "HTTP2-Settings: AAMAAABkAAQAAP__\r\n"
	send "\r\n"
	rxresp
	expect resp.status == 400
	expect resp.bodylen == 10
} -run

server s1 {
	rxreq
	expect req.url == /upgrade1
	expect req.http.host == foo.bar
	expect req.bodylen == 4
	txresp -status 401 -bodylen 8

	rxreq
	expect req.url == /upgrade2
	expect req.http.host == foo.bar
	barrier b1 sync
	txresp -status 402 -bodylen 11

	rxreq
	expect req.url == /upgrade3
	barrier b1 sync
	txresp -status 200
} -start

vinyl v1 -vsl_catchup

vinyl v1 -expect MEMPOOL.req0.live == 0
vinyl v1 -expect MEMPOOL.req1.live == 0
vinyl v1 -expect MEMPOOL.sess0.live == 0
vinyl v1 -expect MEMPOOL.sess1.live == 0

vinyl v1 -cliok "param.set feature +http2"

# We don't support upgrades with body

client c1 {
	send "POST /upgrade1 HTTP/1.1\r\n"
	send "Host: foo.bar\r\n"
	send "Upgrade: h2c\r\n"
	send "HTTP2-Settings: AAMAAABkAAQAAP__\r\n"
	send "Content-Length: 4\r\n"
	send "\r\n"
	send "FOO\n"
	rxresp
	expect resp.status == 401
	expect resp.bodylen == 8
} -run

vinyl v1 -vsl_catchup

vinyl v1 -expect MEMPOOL.req0.live == 0
vinyl v1 -expect MEMPOOL.req1.live == 0
vinyl v1 -expect MEMPOOL.sess0.live == 0
vinyl v1 -expect MEMPOOL.sess1.live == 0

client c1 {
	send "GET /upgrade2 HTTP/1.1\r\n"
	send "Host: foo.bar\r\n"
	send "Upgrade: h2c\r\n"
	send "HTTP2-Settings: AAMAAABkAAQAAP__\r\n"
	send "\r\n"
	rxresp
	expect resp.status == 101
	expect resp.http.upgrade == h2c
	expect resp.http.connection == Upgrade
	txpri
	stream 0 {
		rxsettings
		txsettings
		txsettings -ack
		rxsettings
		expect settings.ack == true
	} -run
	barrier b1 sync
	stream 1 {
		rxresp
		expect resp.status == 402
		expect resp.bodylen == 11
	} -run
} -run

vinyl v1 -vsl_catchup

vinyl v1 -expect MEMPOOL.req0.live == 0
vinyl v1 -expect MEMPOOL.req1.live == 0
vinyl v1 -expect MEMPOOL.sess0.live == 0
vinyl v1 -expect MEMPOOL.sess1.live == 0

client c1 {
	# Illegal HTTP2-Settings
	send "GET /noupgrade HTTP/1.1\r\n"
	send "Host: foo.bar\r\n"
	send "Upgrade: h2c\r\n"
	send "HTTP2-Settings: #######\r\n"
	send "\r\n"
	expect_close
} -run

vinyl v1 -vsl_catchup

vinyl v1 -expect MEMPOOL.req0.live == 0
vinyl v1 -expect MEMPOOL.req1.live == 0
vinyl v1 -expect MEMPOOL.sess0.live == 0
vinyl v1 -expect MEMPOOL.sess1.live == 0

client c1 {
	# PRISM with error in last bit
	send "GET /noupgrade HTTP/1.1\r\n"
	send "Host: foo.bar\r\n"
	send "Upgrade: h2c\r\n"
	send "HTTP2-Settings: AAMAAABkAAQAAP__\r\n"
	send "\r\n"
	rxresp
	expect resp.status == 101
	expect resp.http.upgrade == h2c
	expect resp.http.connection == Upgrade
	sendhex "505249202a20485454502f322e300d0a0d0a534d0d0a0d0b"
	expect_close
} -run

vinyl v1 -expect MEMPOOL.req0.live == 0
vinyl v1 -expect MEMPOOL.req1.live == 0
vinyl v1 -expect MEMPOOL.sess0.live == 0
vinyl v1 -expect MEMPOOL.sess1.live == 0


client c1 {
	# Missing HTTP2-Settings
	send "GET /noupgrade HTTP/1.1\r\n"
	send "Host: foo.bar\r\n"
	send "Upgrade: h2c\r\n"
	send "\r\n"
	expect_close
} -run

vinyl v1 -vsl_catchup

vinyl v1 -expect MEMPOOL.req0.live == 0
vinyl v1 -expect MEMPOOL.req1.live == 0
vinyl v1 -expect MEMPOOL.sess0.live == 0
vinyl v1 -expect MEMPOOL.sess1.live == 0

# Upgrade: h2c followed by activity on the newly opened stream
client c1 {
	send "GET /upgrade3 HTTP/1.1\r\n"
	send "Host: foo.bar\r\n"
	send "Upgrade: h2c\r\n"
	send "HTTP2-Settings: AAMAAABkAAQAAP__\r\n"
	send "\r\n"
	rxresp
	expect resp.status == 101
	expect resp.http.upgrade == h2c
	expect resp.http.connection == Upgrade
	txpri
	stream 0 {
		rxsettings
		txsettings
		txsettings -ack
		rxsettings
		expect settings.ack == true
	} -run
	barrier b1 sync
	stream 1 {
		txwinup -size 256
		rxresp
		expect resp.status == 200
	} -run
} -run

vinyl v1 -vsl_catchup

vinyl v1 -expect MEMPOOL.req0.live == 0
vinyl v1 -expect MEMPOOL.req1.live == 0
vinyl v1 -expect MEMPOOL.sess0.live == 0
vinyl v1 -expect MEMPOOL.sess1.live == 0
