Skip to content

Commit 66d4393

Browse files
committed
add more upstream tests
1 parent 913b893 commit 66d4393

12 files changed

+412
-251
lines changed

test/test_h2_upgrade.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010
import base64
1111

12+
from h2.utilities import utf8_encode_headers
1213
import pytest
1314

1415
import h2.config
@@ -18,16 +19,23 @@
1819
import h2.exceptions
1920

2021

22+
EXAMPLE_REQUEST_HEADERS = [
23+
(':authority', 'example.com'),
24+
(':path', '/'),
25+
(':scheme', 'https'),
26+
(':method', 'GET'),
27+
]
28+
EXAMPLE_REQUEST_HEADERS_BYTES = [
29+
(b':authority', b'example.com'),
30+
(b':path', b'/'),
31+
(b':scheme', b'https'),
32+
(b':method', b'GET'),
33+
]
34+
2135
class TestClientUpgrade(object):
2236
"""
2337
Tests of the client-side of the HTTP/2 upgrade dance.
2438
"""
25-
example_request_headers = [
26-
(b':authority', b'example.com'),
27-
(b':path', b'/'),
28-
(b':scheme', b'https'),
29-
(b':method', b'GET'),
30-
]
3139
example_response_headers = [
3240
(b':status', b'200'),
3341
(b'server', b'fake-serv/0.1.0')
@@ -97,7 +105,8 @@ def test_can_receive_response(self, frame_factory):
97105

98106
assert not c.data_to_send()
99107

100-
def test_can_receive_pushed_stream(self, frame_factory):
108+
@pytest.mark.parametrize("headers", [EXAMPLE_REQUEST_HEADERS, EXAMPLE_REQUEST_HEADERS_BYTES])
109+
def test_can_receive_pushed_stream(self, frame_factory, headers):
101110
"""
102111
After upgrading, we can safely receive a pushed stream.
103112
"""
@@ -108,17 +117,18 @@ def test_can_receive_pushed_stream(self, frame_factory):
108117
f = frame_factory.build_push_promise_frame(
109118
stream_id=1,
110119
promised_stream_id=2,
111-
headers=self.example_request_headers,
120+
headers=headers
112121
)
113122
events = c.receive_data(f.serialize())
114123
assert len(events) == 1
115124

116125
assert isinstance(events[0], h2.events.PushedStreamReceived)
117-
assert events[0].headers == self.example_request_headers
126+
assert events[0].headers == utf8_encode_headers(headers)
118127
assert events[0].parent_stream_id == 1
119128
assert events[0].pushed_stream_id == 2
120129

121-
def test_cannot_send_headers_stream_1(self, frame_factory):
130+
@pytest.mark.parametrize("headers", [EXAMPLE_REQUEST_HEADERS, EXAMPLE_REQUEST_HEADERS_BYTES])
131+
def test_cannot_send_headers_stream_1(self, frame_factory, headers):
122132
"""
123133
After upgrading, we cannot send headers on stream 1.
124134
"""
@@ -127,7 +137,7 @@ def test_cannot_send_headers_stream_1(self, frame_factory):
127137
c.clear_outbound_data_buffer()
128138

129139
with pytest.raises(h2.exceptions.ProtocolError):
130-
c.send_headers(stream_id=1, headers=self.example_request_headers)
140+
c.send_headers(stream_id=1, headers=headers)
131141

132142
def test_cannot_send_data_stream_1(self, frame_factory):
133143
"""
@@ -145,12 +155,6 @@ class TestServerUpgrade(object):
145155
"""
146156
Tests of the server-side of the HTTP/2 upgrade dance.
147157
"""
148-
example_request_headers = [
149-
(b':authority', b'example.com'),
150-
(b':path', b'/'),
151-
(b':scheme', b'https'),
152-
(b':method', b'GET'),
153-
]
154158
example_response_headers = [
155159
(b':status', b'200'),
156160
(b'server', b'fake-serv/0.1.0')
@@ -203,7 +207,8 @@ def test_can_send_response(self, frame_factory):
203207
expected_data = f1.serialize() + f2.serialize()
204208
assert c.data_to_send() == expected_data
205209

206-
def test_can_push_stream(self, frame_factory):
210+
@pytest.mark.parametrize("headers", [EXAMPLE_REQUEST_HEADERS, EXAMPLE_REQUEST_HEADERS_BYTES])
211+
def test_can_push_stream(self, frame_factory, headers):
207212
"""
208213
After upgrading, we can safely push a stream.
209214
"""
@@ -214,17 +219,18 @@ def test_can_push_stream(self, frame_factory):
214219
c.push_stream(
215220
stream_id=1,
216221
promised_stream_id=2,
217-
request_headers=self.example_request_headers
222+
request_headers=headers
218223
)
219224

220225
f = frame_factory.build_push_promise_frame(
221226
stream_id=1,
222227
promised_stream_id=2,
223-
headers=self.example_request_headers,
228+
headers=headers,
224229
)
225230
assert c.data_to_send() == f.serialize()
226231

227-
def test_cannot_receive_headers_stream_1(self, frame_factory):
232+
@pytest.mark.parametrize("headers", [EXAMPLE_REQUEST_HEADERS, EXAMPLE_REQUEST_HEADERS_BYTES])
233+
def test_cannot_receive_headers_stream_1(self, frame_factory, headers):
228234
"""
229235
After upgrading, we cannot receive headers on stream 1.
230236
"""
@@ -235,7 +241,7 @@ def test_cannot_receive_headers_stream_1(self, frame_factory):
235241

236242
f = frame_factory.build_headers_frame(
237243
stream_id=1,
238-
headers=self.example_request_headers,
244+
headers=headers,
239245
)
240246
c.receive_data(f.serialize())
241247

test/test_head_request.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,32 @@
77
import pytest
88

99

10-
class TestHeadRequest(object):
11-
example_request_headers = [
12-
(b':authority', b'example.com'),
13-
(b':path', b'/'),
14-
(b':scheme', b'https'),
15-
(b':method', b'HEAD'),
16-
]
10+
EXAMPLE_REQUEST_HEADERS_BYTES = [
11+
(b':authority', b'example.com'),
12+
(b':path', b'/'),
13+
(b':scheme', b'https'),
14+
(b':method', b'HEAD'),
15+
]
16+
17+
EXAMPLE_REQUEST_HEADERS = [
18+
(':authority', 'example.com'),
19+
(':path', '/'),
20+
(':scheme', 'https'),
21+
(':method', 'HEAD'),
22+
]
1723

24+
class TestHeadRequest(object):
1825
example_response_headers = [
1926
(b':status', b'200'),
2027
(b'server', b'fake-serv/0.1.0'),
2128
(b'content_length', b'1'),
2229
]
2330

24-
def test_non_zero_content_and_no_body(self, frame_factory):
25-
31+
@pytest.mark.parametrize('headers', [EXAMPLE_REQUEST_HEADERS, EXAMPLE_REQUEST_HEADERS_BYTES])
32+
def test_non_zero_content_and_no_body(self, frame_factory, headers):
2633
c = h2.connection.H2Connection()
2734
c.initiate_connection()
28-
c.send_headers(1, self.example_request_headers, end_stream=True)
35+
c.send_headers(1, headers, end_stream=True)
2936

3037
f = frame_factory.build_headers_frame(
3138
self.example_response_headers,
@@ -40,10 +47,11 @@ def test_non_zero_content_and_no_body(self, frame_factory):
4047
assert event.stream_id == 1
4148
assert event.headers == self.example_response_headers
4249

43-
def test_reject_non_zero_content_and_body(self, frame_factory):
50+
@pytest.mark.parametrize('headers', [EXAMPLE_REQUEST_HEADERS, EXAMPLE_REQUEST_HEADERS_BYTES])
51+
def test_reject_non_zero_content_and_body(self, frame_factory, headers):
4452
c = h2.connection.H2Connection()
4553
c.initiate_connection()
46-
c.send_headers(1, self.example_request_headers)
54+
c.send_headers(1, headers)
4755

4856
headers = frame_factory.build_headers_frame(
4957
self.example_response_headers

test/test_header_indexing.py

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ class TestHeaderIndexing(object):
3232
the appropriate hpack data structures.
3333
"""
3434
example_request_headers = [
35-
HeaderTuple(u':authority', u'example.com'),
36-
HeaderTuple(u':path', u'/'),
37-
HeaderTuple(u':scheme', u'https'),
38-
HeaderTuple(u':method', u'GET'),
35+
HeaderTuple(':authority', 'example.com'),
36+
HeaderTuple(':path', '/'),
37+
HeaderTuple(':scheme', 'https'),
38+
HeaderTuple(':method', 'GET'),
3939
]
4040
bytes_example_request_headers = [
4141
HeaderTuple(b':authority', b'example.com'),
@@ -45,11 +45,11 @@ class TestHeaderIndexing(object):
4545
]
4646

4747
extended_request_headers = [
48-
HeaderTuple(u':authority', u'example.com'),
49-
HeaderTuple(u':path', u'/'),
50-
HeaderTuple(u':scheme', u'https'),
51-
HeaderTuple(u':method', u'GET'),
52-
NeverIndexedHeaderTuple(u'authorization', u'realpassword'),
48+
HeaderTuple(':authority', 'example.com'),
49+
HeaderTuple(':path', '/'),
50+
HeaderTuple(':scheme', 'https'),
51+
HeaderTuple(':method', 'GET'),
52+
NeverIndexedHeaderTuple('authorization', 'realpassword'),
5353
]
5454
bytes_extended_request_headers = [
5555
HeaderTuple(b':authority', b'example.com'),
@@ -60,18 +60,18 @@ class TestHeaderIndexing(object):
6060
]
6161

6262
example_response_headers = [
63-
HeaderTuple(u':status', u'200'),
64-
HeaderTuple(u'server', u'fake-serv/0.1.0')
63+
HeaderTuple(':status', '200'),
64+
HeaderTuple('server', 'fake-serv/0.1.0')
6565
]
6666
bytes_example_response_headers = [
6767
HeaderTuple(b':status', b'200'),
6868
HeaderTuple(b'server', b'fake-serv/0.1.0')
6969
]
7070

7171
extended_response_headers = [
72-
HeaderTuple(u':status', u'200'),
73-
HeaderTuple(u'server', u'fake-serv/0.1.0'),
74-
NeverIndexedHeaderTuple(u'secure', u'you-bet'),
72+
HeaderTuple(':status', '200'),
73+
HeaderTuple('server', 'fake-serv/0.1.0'),
74+
NeverIndexedHeaderTuple('secure', 'you-bet'),
7575
]
7676
bytes_extended_response_headers = [
7777
HeaderTuple(b':status', b'200'),
@@ -228,7 +228,7 @@ def test_header_tuples_are_decoded_info_response(self,
228228
# to avoid breaking the example headers.
229229
headers = headers[:]
230230
if encoding:
231-
headers[0] = HeaderTuple(u':status', u'100')
231+
headers[0] = HeaderTuple(':status', '100')
232232
else:
233233
headers[0] = HeaderTuple(b':status', b'100')
234234

@@ -334,10 +334,10 @@ class TestSecureHeaders(object):
334334
Certain headers should always be transformed to their never-indexed form.
335335
"""
336336
example_request_headers = [
337-
(u':authority', u'example.com'),
338-
(u':path', u'/'),
339-
(u':scheme', u'https'),
340-
(u':method', u'GET'),
337+
(':authority', 'example.com'),
338+
(':path', '/'),
339+
(':scheme', 'https'),
340+
(':method', 'GET'),
341341
]
342342
bytes_example_request_headers = [
343343
(b':authority', b'example.com'),
@@ -346,15 +346,15 @@ class TestSecureHeaders(object):
346346
(b':method', b'GET'),
347347
]
348348
possible_auth_headers = [
349-
(u'authorization', u'test'),
350-
(u'Authorization', u'test'),
351-
(u'authorization', u'really long test'),
352-
HeaderTuple(u'authorization', u'test'),
353-
HeaderTuple(u'Authorization', u'test'),
354-
HeaderTuple(u'authorization', u'really long test'),
355-
NeverIndexedHeaderTuple(u'authorization', u'test'),
356-
NeverIndexedHeaderTuple(u'Authorization', u'test'),
357-
NeverIndexedHeaderTuple(u'authorization', u'really long test'),
349+
('authorization', 'test'),
350+
('Authorization', 'test'),
351+
('authorization', 'really long test'),
352+
HeaderTuple('authorization', 'test'),
353+
HeaderTuple('Authorization', 'test'),
354+
HeaderTuple('authorization', 'really long test'),
355+
NeverIndexedHeaderTuple('authorization', 'test'),
356+
NeverIndexedHeaderTuple('Authorization', 'test'),
357+
NeverIndexedHeaderTuple('authorization', 'really long test'),
358358
(b'authorization', b'test'),
359359
(b'Authorization', b'test'),
360360
(b'authorization', b'really long test'),
@@ -364,15 +364,15 @@ class TestSecureHeaders(object):
364364
NeverIndexedHeaderTuple(b'authorization', b'test'),
365365
NeverIndexedHeaderTuple(b'Authorization', b'test'),
366366
NeverIndexedHeaderTuple(b'authorization', b'really long test'),
367-
(u'proxy-authorization', u'test'),
368-
(u'Proxy-Authorization', u'test'),
369-
(u'proxy-authorization', u'really long test'),
370-
HeaderTuple(u'proxy-authorization', u'test'),
371-
HeaderTuple(u'Proxy-Authorization', u'test'),
372-
HeaderTuple(u'proxy-authorization', u'really long test'),
373-
NeverIndexedHeaderTuple(u'proxy-authorization', u'test'),
374-
NeverIndexedHeaderTuple(u'Proxy-Authorization', u'test'),
375-
NeverIndexedHeaderTuple(u'proxy-authorization', u'really long test'),
367+
('proxy-authorization', 'test'),
368+
('Proxy-Authorization', 'test'),
369+
('proxy-authorization', 'really long test'),
370+
HeaderTuple('proxy-authorization', 'test'),
371+
HeaderTuple('Proxy-Authorization', 'test'),
372+
HeaderTuple('proxy-authorization', 'really long test'),
373+
NeverIndexedHeaderTuple('proxy-authorization', 'test'),
374+
NeverIndexedHeaderTuple('Proxy-Authorization', 'test'),
375+
NeverIndexedHeaderTuple('proxy-authorization', 'really long test'),
376376
(b'proxy-authorization', b'test'),
377377
(b'Proxy-Authorization', b'test'),
378378
(b'proxy-authorization', b'really long test'),
@@ -384,16 +384,16 @@ class TestSecureHeaders(object):
384384
NeverIndexedHeaderTuple(b'proxy-authorization', b'really long test'),
385385
]
386386
secured_cookie_headers = [
387-
(u'cookie', u'short'),
388-
(u'Cookie', u'short'),
389-
(u'cookie', u'nineteen byte cooki'),
390-
HeaderTuple(u'cookie', u'short'),
391-
HeaderTuple(u'Cookie', u'short'),
392-
HeaderTuple(u'cookie', u'nineteen byte cooki'),
393-
NeverIndexedHeaderTuple(u'cookie', u'short'),
394-
NeverIndexedHeaderTuple(u'Cookie', u'short'),
395-
NeverIndexedHeaderTuple(u'cookie', u'nineteen byte cooki'),
396-
NeverIndexedHeaderTuple(u'cookie', u'longer manually secured cookie'),
387+
('cookie', 'short'),
388+
('Cookie', 'short'),
389+
('cookie', 'nineteen byte cooki'),
390+
HeaderTuple('cookie', 'short'),
391+
HeaderTuple('Cookie', 'short'),
392+
HeaderTuple('cookie', 'nineteen byte cooki'),
393+
NeverIndexedHeaderTuple('cookie', 'short'),
394+
NeverIndexedHeaderTuple('Cookie', 'short'),
395+
NeverIndexedHeaderTuple('cookie', 'nineteen byte cooki'),
396+
NeverIndexedHeaderTuple('cookie', 'longer manually secured cookie'),
397397
(b'cookie', b'short'),
398398
(b'Cookie', b'short'),
399399
(b'cookie', b'nineteen byte cooki'),
@@ -406,12 +406,12 @@ class TestSecureHeaders(object):
406406
NeverIndexedHeaderTuple(b'cookie', b'longer manually secured cookie'),
407407
]
408408
unsecured_cookie_headers = [
409-
(u'cookie', u'twenty byte cookie!!'),
410-
(u'Cookie', u'twenty byte cookie!!'),
411-
(u'cookie', u'substantially longer than 20 byte cookie'),
412-
HeaderTuple(u'cookie', u'twenty byte cookie!!'),
413-
HeaderTuple(u'cookie', u'twenty byte cookie!!'),
414-
HeaderTuple(u'Cookie', u'twenty byte cookie!!'),
409+
('cookie', 'twenty byte cookie!!'),
410+
('Cookie', 'twenty byte cookie!!'),
411+
('cookie', 'substantially longer than 20 byte cookie'),
412+
HeaderTuple('cookie', 'twenty byte cookie!!'),
413+
HeaderTuple('cookie', 'twenty byte cookie!!'),
414+
HeaderTuple('Cookie', 'twenty byte cookie!!'),
415415
(b'cookie', b'twenty byte cookie!!'),
416416
(b'Cookie', b'twenty byte cookie!!'),
417417
(b'cookie', b'substantially longer than 20 byte cookie'),

0 commit comments

Comments
 (0)