2
2
import hyper .common .connection
3
3
4
4
from hyper .common .connection import HTTPConnection
5
- from hyper .common .exceptions import TLSUpgrade
5
+ from hyper .common .exceptions import TLSUpgrade , HTTPUpgrade
6
6
7
7
class TestHTTPConnection (object ):
8
8
def test_h1_kwargs (self ):
@@ -31,7 +31,7 @@ def test_h2_kwargs(self):
31
31
'other_kwarg' : True ,
32
32
}
33
33
34
- def test_upgrade (self , monkeypatch ):
34
+ def test_tls_upgrade (self , monkeypatch ):
35
35
monkeypatch .setattr (
36
36
hyper .common .connection , 'HTTP11Connection' , DummyH1Connection
37
37
)
@@ -46,23 +46,70 @@ def test_upgrade(self, monkeypatch):
46
46
47
47
assert r == 'h2'
48
48
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'
50
68
51
69
52
70
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
55
81
56
82
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' )
58
89
59
90
60
91
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
63
102
64
103
def _send_preamble (self ):
65
104
pass
66
105
106
+ def _new_stream (self , * args , ** kwargs ):
107
+ pass
108
+
67
109
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