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

Commit ed0067c

Browse files
committed
Add abstraction test case for http upgrade
1 parent e965aa3 commit ed0067c

File tree

2 files changed

+58
-11
lines changed

2 files changed

+58
-11
lines changed

test/test_abstraction.py

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import hyper.common.connection
33

44
from hyper.common.connection import HTTPConnection
5-
from hyper.common.exceptions import TLSUpgrade
5+
from hyper.common.exceptions import TLSUpgrade, HTTPUpgrade
66

77
class TestHTTPConnection(object):
88
def test_h1_kwargs(self):
@@ -31,7 +31,7 @@ def test_h2_kwargs(self):
3131
'other_kwarg': True,
3232
}
3333

34-
def test_upgrade(self, monkeypatch):
34+
def test_tls_upgrade(self, monkeypatch):
3535
monkeypatch.setattr(
3636
hyper.common.connection, 'HTTP11Connection', DummyH1Connection
3737
)
@@ -46,23 +46,70 @@ def test_upgrade(self, monkeypatch):
4646

4747
assert r == 'h2'
4848
assert isinstance(c._conn, DummyH2Connection)
49-
assert c._conn._sock == 'totally a socket'
49+
assert c._conn._sock == 'totally a secure socket'
50+
51+
def test_http_upgrade(self, monkeypatch):
52+
monkeypatch.setattr(
53+
hyper.common.connection, 'HTTP11Connection', DummyH1Connection
54+
)
55+
monkeypatch.setattr(
56+
hyper.common.connection, 'HTTP20Connection', DummyH2Connection
57+
)
58+
c = HTTPConnection('test', 80)
59+
60+
assert isinstance(c._conn, DummyH1Connection)
61+
62+
c.request('GET', '/')
63+
resp = c.get_response()
64+
65+
assert resp == 'h2c'
66+
assert isinstance(c._conn, DummyH2Connection)
67+
assert c._conn._sock == 'totally a non-secure socket'
5068

5169

5270
class DummyH1Connection(object):
53-
def __init__(self, *args, **kwargs):
54-
pass
71+
def __init__(self, host, port=None, secure=None, **kwargs):
72+
self.host = host
73+
self.port = port
74+
75+
if secure is not None:
76+
self.secure = secure
77+
elif self.port == 443:
78+
self.secure = True
79+
else:
80+
self.secure = False
5581

5682
def request(self, *args, **kwargs):
57-
raise TLSUpgrade('h2', 'totally a socket')
83+
if(self.secure):
84+
raise TLSUpgrade('h2', 'totally a secure socket')
85+
86+
def get_response(self):
87+
if(not self.secure):
88+
raise HTTPUpgrade('h2c', 'totally a non-secure socket')
5889

5990

6091
class DummyH2Connection(object):
61-
def __init__(self, *args, **kwargs):
62-
pass
92+
def __init__(self, host, port=None, secure=None, **kwargs):
93+
self.host = host
94+
self.port = port
95+
96+
if secure is not None:
97+
self.secure = secure
98+
elif self.port == 443:
99+
self.secure = True
100+
else:
101+
self.secure = False
63102

64103
def _send_preamble(self):
65104
pass
66105

106+
def _new_stream(self, *args, **kwargs):
107+
pass
108+
67109
def request(self, *args, **kwargs):
68-
return 'h2'
110+
if(self.secure):
111+
return 'h2'
112+
113+
def get_response(self, *args, **kwargs):
114+
if(not self.secure):
115+
return 'h2c'

test_release.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ def test_hitting_http2bin_org(self):
8686
assert all(map(lambda r: r.status_code == 200, responses))
8787
assert all(map(lambda r: r.text, responses))
8888

89-
def test_hitting_http2bin_org_http11(self):
89+
def test_hitting_httpbin_org_http11(self):
9090
"""
91-
This test function uses hyper's HTTP/1.1 support to talk to http2bin
91+
This test function uses hyper's HTTP/1.1 support to talk to httpbin
9292
"""
9393
c = HTTP11Connection('httpbin.org')
9494

0 commit comments

Comments
 (0)