Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit b7a5758

Browse files
committed
adding itegration tests for HTTPConnection
1 parent e76f185 commit b7a5758

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

test/test_integration.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from h2.frame_buffer import FrameBuffer
1616
from hyper.compat import ssl
1717
from hyper.contrib import HTTP20Adapter
18+
from hyper.common.util import HTTPVersion
1819
from hyperframe.frame import (
1920
Frame, SettingsFrame, WindowUpdateFrame, DataFrame, HeadersFrame,
2021
GoAwayFrame, RstStreamFrame
@@ -935,6 +936,108 @@ def socket_handler(listener):
935936

936937
self.tear_down()
937938

939+
def test_version_after_tls_upgrade(self, monkeypatch):
940+
self.set_up()
941+
942+
# We need to patch the ssl_wrap_socket method to ensure that we
943+
# forcefully upgrade.
944+
old_wrap_socket = hyper.http11.connection.wrap_socket
945+
946+
def wrap(*args):
947+
sock, _ = old_wrap_socket(*args)
948+
return sock, 'h2'
949+
950+
monkeypatch.setattr(hyper.http11.connection, 'wrap_socket', wrap)
951+
952+
send_event = threading.Event()
953+
954+
def socket_handler(listener):
955+
sock = listener.accept()[0]
956+
957+
receive_preamble(sock)
958+
# Read first frame off the line.
959+
sock.recv(65535)
960+
961+
# Send the headers for the response. This response has no body.
962+
f = build_headers_frame(
963+
[(':status', '200'), ('content-length', '0')]
964+
)
965+
f.flags.add('END_STREAM')
966+
f.stream_id = 1
967+
sock.sendall(f.serialize())
968+
969+
# Wait for the message from the main thread.
970+
send_event.set()
971+
sock.close()
972+
973+
self._start_server(socket_handler)
974+
c = hyper.HTTPConnection(self.host, self.port, secure=True)
975+
976+
assert c.version is HTTPVersion.http11
977+
assert c.version is not HTTPVersion.http20
978+
c.request('GET', '/')
979+
send_event.set()
980+
assert c.version is HTTPVersion.http20
981+
982+
self.tear_down()
983+
984+
def test_version_after_http_upgrade(self):
985+
self.set_up()
986+
self.secure = False
987+
988+
send_event = threading.Event()
989+
990+
def socket_handler(listener):
991+
sock = listener.accept()[0]
992+
993+
# We should get the initial request.
994+
data = b''
995+
while not data.endswith(b'\r\n\r\n'):
996+
data += sock.recv(65535)
997+
assert b'upgrade: h2c\r\n' in data
998+
999+
send_event.wait()
1000+
1001+
# We need to send back a response.
1002+
resp = (
1003+
b'HTTP/1.1 101 Upgrade\r\n'
1004+
b'Server: socket-level-server\r\n'
1005+
b'Content-Length: 0\r\n'
1006+
b'Connection: upgrade\r\n'
1007+
b'Upgrade: h2c\r\n'
1008+
b'\r\n'
1009+
)
1010+
sock.sendall(resp)
1011+
1012+
# We get a message for connection open, specifically the preamble.
1013+
receive_preamble(sock)
1014+
1015+
# Now, send the headers for the response. This response has a body.
1016+
f = build_headers_frame([(':status', '200')])
1017+
f.stream_id = 1
1018+
sock.sendall(f.serialize())
1019+
1020+
# Send the first two chunks.
1021+
f = DataFrame(1)
1022+
f.data = b'hello'
1023+
sock.sendall(f.serialize())
1024+
f = DataFrame(1)
1025+
f.data = b'there'
1026+
f.flags.add('END_STREAM')
1027+
sock.sendall(f.serialize())
1028+
1029+
self._start_server(socket_handler)
1030+
1031+
c = hyper.HTTPConnection(self.host, self.port)
1032+
assert c.version is HTTPVersion.http11
1033+
c.request('GET', '/')
1034+
send_event.set()
1035+
resp = c.get_response()
1036+
assert c.version is HTTPVersion.http20
1037+
assert resp.version is HTTPVersion.http20
1038+
1039+
self.tear_down()
1040+
9381041

9391042
class TestRequestsAdapter(SocketLevelTest):
9401043
# This uses HTTP/2.

0 commit comments

Comments
 (0)