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

Commit

Permalink
adding itegration tests for HTTPConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Dec 6, 2016
1 parent e76f185 commit b7a5758
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions test/test_integration.py
Expand Up @@ -15,6 +15,7 @@
from h2.frame_buffer import FrameBuffer
from hyper.compat import ssl
from hyper.contrib import HTTP20Adapter
from hyper.common.util import HTTPVersion
from hyperframe.frame import (
Frame, SettingsFrame, WindowUpdateFrame, DataFrame, HeadersFrame,
GoAwayFrame, RstStreamFrame
Expand Down Expand Up @@ -935,6 +936,108 @@ def socket_handler(listener):

self.tear_down()

def test_version_after_tls_upgrade(self, monkeypatch):
self.set_up()

# We need to patch the ssl_wrap_socket method to ensure that we
# forcefully upgrade.
old_wrap_socket = hyper.http11.connection.wrap_socket

def wrap(*args):
sock, _ = old_wrap_socket(*args)
return sock, 'h2'

monkeypatch.setattr(hyper.http11.connection, 'wrap_socket', wrap)

send_event = threading.Event()

def socket_handler(listener):
sock = listener.accept()[0]

receive_preamble(sock)
# Read first frame off the line.
sock.recv(65535)

# Send the headers for the response. This response has no body.
f = build_headers_frame(
[(':status', '200'), ('content-length', '0')]
)
f.flags.add('END_STREAM')
f.stream_id = 1
sock.sendall(f.serialize())

# Wait for the message from the main thread.
send_event.set()
sock.close()

self._start_server(socket_handler)
c = hyper.HTTPConnection(self.host, self.port, secure=True)

assert c.version is HTTPVersion.http11
assert c.version is not HTTPVersion.http20
c.request('GET', '/')
send_event.set()
assert c.version is HTTPVersion.http20

self.tear_down()

def test_version_after_http_upgrade(self):
self.set_up()
self.secure = False

send_event = threading.Event()

def socket_handler(listener):
sock = listener.accept()[0]

# We should get the initial request.
data = b''
while not data.endswith(b'\r\n\r\n'):
data += sock.recv(65535)
assert b'upgrade: h2c\r\n' in data

send_event.wait()

# We need to send back a response.
resp = (
b'HTTP/1.1 101 Upgrade\r\n'
b'Server: socket-level-server\r\n'
b'Content-Length: 0\r\n'
b'Connection: upgrade\r\n'
b'Upgrade: h2c\r\n'
b'\r\n'
)
sock.sendall(resp)

# We get a message for connection open, specifically the preamble.
receive_preamble(sock)

# Now, send the headers for the response. This response has a body.
f = build_headers_frame([(':status', '200')])
f.stream_id = 1
sock.sendall(f.serialize())

# Send the first two chunks.
f = DataFrame(1)
f.data = b'hello'
sock.sendall(f.serialize())
f = DataFrame(1)
f.data = b'there'
f.flags.add('END_STREAM')
sock.sendall(f.serialize())

self._start_server(socket_handler)

c = hyper.HTTPConnection(self.host, self.port)
assert c.version is HTTPVersion.http11
c.request('GET', '/')
send_event.set()
resp = c.get_response()
assert c.version is HTTPVersion.http20
assert resp.version is HTTPVersion.http20

self.tear_down()


class TestRequestsAdapter(SocketLevelTest):
# This uses HTTP/2.
Expand Down

0 comments on commit b7a5758

Please sign in to comment.