22import hyper .common .connection
33
44from hyper .common .connection import HTTPConnection
5- from hyper .common .exceptions import TLSUpgrade
5+ from hyper .common .exceptions import TLSUpgrade , HTTPUpgrade
66
77class 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
5270class 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
6091class 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'
0 commit comments