This repository was archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathtest_http11.py
905 lines (714 loc) · 26.1 KB
/
test_http11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
# -*- coding: utf-8 -*-
"""
test_http11.py
~~~~~~~~~~~~~~
Unit tests for hyper's HTTP/1.1 implementation.
"""
import os
import zlib
from collections import namedtuple
from io import BytesIO, StringIO
import mock
import pytest
import hyper
from hyper.http11.connection import HTTP11Connection
from hyper.http11.response import HTTP11Response
from hyper.common.headers import HTTPHeaderMap
from hyper.common.exceptions import ChunkedDecodeError, ConnectionResetError
from hyper.compat import bytes, zlib_compressobj
class TestHTTP11Connection(object):
def test_pycohttpparser_installs_correctly(self):
# This test is a debugging tool: if pycohttpparser is being tested by
# Travis, we need to confirm it imports correctly. Hyper will normally
# hide the import failure, so let's discover it here.
# Alternatively, if we are *not* testing with nghttp2, this test should
# confirm that it's not available.
if os.environ.get('HYPER_FAST_PARSE') == 'true':
import pycohttpparser
else:
with pytest.raises(ImportError):
import pycohttpparser # noqa
assert True
def test_initialization_no_port(self):
c = HTTP11Connection('httpbin.org')
assert c.host == 'httpbin.org'
assert c.port == 80
assert not c.secure
assert not c.proxy_host
def test_initialization_inline_port(self):
c = HTTP11Connection('httpbin.org:443')
assert c.host == 'httpbin.org'
assert c.port == 443
assert c.secure
assert not c.proxy_host
def test_initialization_separate_port(self):
c = HTTP11Connection('localhost', 8080)
assert c.host == 'localhost'
assert c.port == 8080
assert not c.secure
assert not c.proxy_host
def test_can_override_security(self):
c = HTTP11Connection('localhost', 443, secure=False)
assert c.host == 'localhost'
assert c.port == 443
assert not c.secure
assert not c.proxy_host
def test_initialization_proxy(self):
c = HTTP11Connection('httpbin.org', proxy_host='localhost')
assert c.host == 'httpbin.org'
assert c.port == 80
assert not c.secure
assert c.proxy_host == 'localhost'
assert c.proxy_port == 8080
def test_initialization_proxy_with_inline_port(self):
c = HTTP11Connection('httpbin.org', proxy_host='localhost:8443')
assert c.host == 'httpbin.org'
assert c.port == 80
assert not c.secure
assert c.proxy_host == 'localhost'
assert c.proxy_port == 8443
def test_initialization_proxy_with_separate_port(self):
c = HTTP11Connection(
'httpbin.org', proxy_host='localhost', proxy_port=8443
)
assert c.host == 'httpbin.org'
assert c.port == 80
assert not c.secure
assert c.proxy_host == 'localhost'
assert c.proxy_port == 8443
def test_initialization_with_ipv6_addresses_proxy_inline_port(self):
c = HTTP11Connection(
'[abcd:dcba::1234]', proxy_host='[ffff:aaaa::1]:8443'
)
assert c.host == 'abcd:dcba::1234'
assert c.port == 80
assert not c.secure
assert c.proxy_host == 'ffff:aaaa::1'
assert c.proxy_port == 8443
def test_basic_request(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
c.request('GET', '/get', headers={'User-Agent': 'hyper'})
expected = (
b"GET /get HTTP/1.1\r\n"
b"User-Agent: hyper\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP__\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
)
received = b''.join(sock.queue)
assert received == expected
def test_iterable_header(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
c.request('GET', '/get', headers=(
('User-Agent', 'hyper'),
('Custom-field', 'test'),
('Custom-field2', 'test'),
('Custom-field', 'test2'),
))
expected = (
b"GET /get HTTP/1.1\r\n"
b"User-Agent: hyper\r\n"
b"Custom-field: test\r\n"
b"Custom-field2: test\r\n"
b"Custom-field: test2\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP__\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
)
received = b''.join(sock.queue)
assert received == expected
def test_invalid_header(self):
c = HTTP11Connection('httpbin.org')
c._sock = DummySocket()
with pytest.raises(ValueError):
c.request('GET', '/get', headers=42)
def test_proxy_request(self):
c = HTTP11Connection('httpbin.org', proxy_host='localhost')
c._sock = sock = DummySocket()
c.request('GET', '/get', headers={'User-Agent': 'hyper'})
expected = (
b"GET /get HTTP/1.1\r\n"
b"User-Agent: hyper\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP__\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
)
received = b''.join(sock.queue)
assert received == expected
def test_request_with_bytestring_body(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
c.request(
'POST',
'/post',
headers=HTTPHeaderMap([('User-Agent', 'hyper')]),
body=b'hi'
)
expected = (
b"POST /post HTTP/1.1\r\n"
b"User-Agent: hyper\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP__\r\n"
b"content-length: 2\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
b"hi"
)
received = b''.join(sock.queue)
assert received == expected
def test_request_with_file_body(self):
# Testing this is tricksy: in practice, we do this by passing a fake
# file and monkeypatching out 'os.fstat'. This makes it look like a
# real file.
FstatRval = namedtuple('FstatRval', ['st_size'])
def fake_fstat(*args):
return FstatRval(16)
old_fstat = hyper.http11.connection.os.fstat
try:
hyper.http11.connection.os.fstat = fake_fstat
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
f = DummyFile(b'some binary data')
c.request('POST', '/post', body=f)
expected = (
b"POST /post HTTP/1.1\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP__\r\n"
b"content-length: 16\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
b"some binary data"
)
received = b''.join(sock.queue)
assert received == expected
finally:
# Put back the monkeypatch.
hyper.http11.connection.os.fstat = old_fstat
def test_request_with_generator_body(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
def body():
yield b'hi'
yield b'there'
yield b'sir'
c.request('POST', '/post', body=body())
expected = (
b"POST /post HTTP/1.1\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP__\r\n"
b"transfer-encoding: chunked\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
b"2\r\nhi\r\n"
b"5\r\nthere\r\n"
b"3\r\nsir\r\n"
b"0\r\n\r\n"
)
received = b''.join(sock.queue)
assert received == expected
def test_content_length_overrides_generator(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
def body():
yield b'hi'
yield b'there'
yield b'sir'
c.request(
'POST', '/post', body=body(), headers={b'content-length': b'10'}
)
expected = (
b"POST /post HTTP/1.1\r\n"
b"content-length: 10\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP__\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
b"hitheresir"
)
received = b''.join(sock.queue)
assert received == expected
def test_chunked_overrides_body(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
f = DummyFile(b'oneline\nanotherline')
c.request(
'POST',
'/post',
headers={'transfer-encoding': 'chunked'},
body=f
)
expected = (
b"POST /post HTTP/1.1\r\n"
b"transfer-encoding: chunked\r\n"
b"connection: Upgrade, HTTP2-Settings\r\n"
b"upgrade: h2c\r\n"
b"HTTP2-Settings: AAQAAP__\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
b"8\r\noneline\n\r\n"
b"b\r\nanotherline\r\n"
b"0\r\n\r\n"
)
received = b''.join(sock.queue)
assert received == expected
def test_get_response(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
sock._buffer = BytesIO(
b"HTTP/1.1 201 No Content\r\n"
b"Connection: close\r\n"
b"Server: Socket\r\n"
b"Content-Length: 0\r\n"
b"\r\n"
)
r = c.get_response()
assert r.status == 201
assert r.reason == b'No Content'
assert list(r.headers.iter_raw()) == [
(b'Connection', b'close'),
(b'Server', b'Socket'),
(b'Content-Length', b'0')
]
assert r.read() == b''
def test_response_short_reads(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
sock._buffer = BytesIO(
b"HTTP/1.1 200 OK\r\n"
b"Content-Length: 15\r\n"
b"\r\n"
b"hellotherechamp"
)
r = c.get_response()
assert r.status == 200
assert r.reason == b'OK'
assert r.read(5) == b'hello'
assert r.read(5) == b'there'
assert r.read(5) == b'champ'
assert r.read(5) == b''
def test_request_with_unicodestring_body(self):
c = HTTP11Connection('httpbin.org')
c._sock = DummySocket()
with pytest.raises(ValueError):
c.request(
'POST',
'/post',
headers=HTTPHeaderMap([('User-Agent', 'hyper')]),
body=u'hi'
)
def test_request_with_file_body_in_text_mode(self):
# Testing this is tricksy: in practice, we do this by passing a fake
# file and monkeypatching out 'os.fstat'. This makes it look like a
# real file.
FstatRval = namedtuple('FstatRval', ['st_size'])
def fake_fstat(*args):
return FstatRval(16)
old_fstat = hyper.http11.connection.os.fstat
try:
hyper.http11.connection.os.fstat = fake_fstat
c = HTTP11Connection('httpbin.org')
c._sock = DummySocket()
f = DummyFile(b'')
f.buffer = StringIO(u'some binary data')
with pytest.raises(ValueError):
c.request('POST', '/post', body=f)
finally:
# Put back the monkeypatch.
hyper.http11.connection.os.fstat = old_fstat
def test_request_with_unicode_generator_body(self):
c = HTTP11Connection('httpbin.org')
c._sock = DummySocket()
def body():
yield u'hi'
yield u'there'
yield u'sir'
with pytest.raises(ValueError):
c.request('POST', '/post', body=body())
def test_content_length_overrides_generator_unicode(self):
c = HTTP11Connection('httpbin.org')
c._sock = DummySocket()
def body():
yield u'hi'
yield u'there'
yield u'sir'
with pytest.raises(ValueError):
c.request(
'POST',
'/post',
headers={b'content-length': b'10'},
body=body()
)
def test_http_upgrade_headers_only_sent_once(self):
c = HTTP11Connection('httpbin.org')
c._sock = sock = DummySocket()
c.request('GET', '/get', headers={'User-Agent': 'hyper'})
sock.queue = []
c.request('GET', '/get', headers={'User-Agent': 'hyper'})
received = b''.join(sock.queue)
expected = (
b"GET /get HTTP/1.1\r\n"
b"User-Agent: hyper\r\n"
b"host: httpbin.org\r\n"
b"\r\n"
)
assert received == expected
def test_exception_raised_for_illegal_body_type(self):
c = HTTP11Connection('httpbin.org')
with pytest.raises(ValueError) as exc_info:
body = 1234
# content-length set so body type is set to BODY_FLAT. value
# doesn't matter
c.request(
'GET',
'/get',
body=body,
headers={'content-length': str(len(str(body)))}
)
assert 'Request body must be a bytestring, a file-like object ' \
'returning bytestrings or an iterable of bytestrings. ' \
'Got: {}'.format(type(body)) in str(exc_info)
def test_exception_raised_for_illegal_elements_in_iterable_body(self):
c = HTTP11Connection('httpbin.org')
rogue_element = 123
with pytest.raises(ValueError) as exc_info:
# content-length set so body type is set to BODY_FLAT. value
# doesn't matter
body = ['legal1', 'legal2', rogue_element]
c.request(
'GET',
'/get',
body=body,
headers={'content-length': str(len(map(str, body)))}
)
assert 'Elements in iterable body must be bytestrings. Illegal ' \
'element: {}'.format(rogue_element) \
in str(exc_info)
def test_exception_raised_for_filelike_body_not_returning_bytes(self):
c = HTTP11Connection('httpbin.org')
class RogueFile(object):
def read(self, size):
return 42
with pytest.raises(ValueError) as exc_info:
# content-length set so body type is BODY_FLAT. value doesn't
# matter
c.request(
'GET',
'/get',
body=RogueFile(),
headers={'content-length': str(10)}
)
assert 'File-like bodies must return bytestrings. ' \
'Got: {}'.format(int) in str(exc_info)
class TestHTTP11Response(object):
def test_short_circuit_read(self):
r = HTTP11Response(200, 'OK', {b'content-length': [b'0']}, None, None)
assert r.read() == b''
def test_aborted_reads(self):
d = DummySocket()
r = HTTP11Response(200, 'OK', {b'content-length': [b'15']}, d, None)
with pytest.raises(ConnectionResetError):
r.read()
def test_read_expect_close(self):
d = DummySocket()
r = HTTP11Response(200, 'OK', {b'connection': [b'close']}, d, None)
assert r.read() == b''
def test_response_as_context_manager(self):
r = HTTP11Response(
200, 'OK', {b'content-length': [b'0']}, DummySocket(), None
)
with r:
assert r.read() == b''
assert r._sock is None
def test_response_transparently_decrypts_gzip(self):
d = DummySocket()
headers = {b'content-encoding': [b'gzip'], b'connection': [b'close']}
r = HTTP11Response(200, 'OK', headers, d, None)
c = zlib_compressobj(wbits=24)
body = c.compress(b'this is test data')
body += c.flush()
d._buffer = BytesIO(body)
assert r.read() == b'this is test data'
def test_response_transparently_decrypts_real_deflate(self):
d = DummySocket()
headers = {
b'content-encoding': [b'deflate'],
b'connection': [b'close'],
}
r = HTTP11Response(200, 'OK', headers, d, None)
c = zlib_compressobj(wbits=zlib.MAX_WBITS)
body = c.compress(b'this is test data')
body += c.flush()
d._buffer = BytesIO(body)
assert r.read() == b'this is test data'
def test_response_transparently_decrypts_wrong_deflate(self):
c = zlib_compressobj(wbits=-zlib.MAX_WBITS)
body = c.compress(b'this is test data')
body += c.flush()
body_len = ('%s' % len(body)).encode('ascii')
headers = {
b'content-encoding': [b'deflate'], b'content-length': [body_len]
}
d = DummySocket()
d._buffer = BytesIO(body)
r = HTTP11Response(200, 'OK', headers, d, None)
assert r.read() == b'this is test data'
def test_basic_chunked_read(self):
d = DummySocket()
r = HTTP11Response(
200, 'OK', {b'transfer-encoding': [b'chunked']}, d, None
)
data = (
b'4\r\nwell\r\n'
b'4\r\nwell\r\n'
b'4\r\nwhat\r\n'
b'4\r\nhave\r\n'
b'2\r\nwe\r\n'
b'a\r\nhereabouts\r\n'
b'0\r\n\r\n'
)
d._buffer = BytesIO(data)
results = [
b'well', b'well', b'what', b'have', b'we', b'hereabouts'
]
for c1, c2 in zip(results, r.read_chunked()):
assert c1 == c2
assert not list(r.read_chunked())
def test_chunked_read_of_non_chunked(self):
r = HTTP11Response(200, 'OK', {b'content-length': [b'0']}, None, None)
with pytest.raises(ChunkedDecodeError):
list(r.read_chunked())
def test_chunked_read_aborts_early(self):
r = HTTP11Response(
200, 'OK', {b'transfer-encoding': [b'chunked']}, None, None
)
assert not list(r.read_chunked())
def test_response_transparently_decrypts_chunked_gzip(self):
d = DummySocket()
headers = {
b'content-encoding': [b'gzip'],
b'transfer-encoding': [b'chunked'],
}
r = HTTP11Response(200, 'OK', headers, d, None)
c = zlib_compressobj(wbits=24)
body = c.compress(b'this is test data')
body += c.flush()
data = b''
for index in range(0, len(body), 2):
data += b'2\r\n' + body[index:index+2] + b'\r\n'
data += b'0\r\n\r\n'
d._buffer = BytesIO(data)
received_body = b''
for chunk in r.read_chunked():
received_body += chunk
assert received_body == b'this is test data'
def test_chunked_normal_read(self):
d = DummySocket()
r = HTTP11Response(
200, 'OK', {b'transfer-encoding': [b'chunked']}, d, None)
data = (
b'4\r\nwell\r\n'
b'4\r\nwell\r\n'
b'4\r\nwhat\r\n'
b'4\r\nhave\r\n'
b'2\r\nwe\r\n'
b'a\r\nhereabouts\r\n'
b'0\r\n\r\n'
)
d._buffer = BytesIO(data)
assert r.read() == b'wellwellwhathavewehereabouts'
def test_chunk_length_read(self):
d = DummySocket()
r = HTTP11Response(
200, 'OK', {b'transfer-encoding': [b'chunked']}, d, None
)
data = (
b'4\r\nwell\r\n'
b'4\r\nwell\r\n'
b'4\r\nwhat\r\n'
b'4\r\nhave\r\n'
b'2\r\nwe\r\n'
b'a\r\nhereabouts\r\n'
b'0\r\n\r\n'
)
d._buffer = BytesIO(data)
assert r.read(5) == b'wellw'
assert r.read(15) == b'ellwhathavewehe'
assert r.read(20) == b'reabouts'
assert r.read() == b''
def test_bounded_read_expect_close_no_content_length(self):
d = DummySocket()
r = HTTP11Response(200, 'OK', {b'connection': [b'close']}, d, None)
d._buffer = BytesIO(b'hello there sir')
assert r.read(5) == b'hello'
assert r.read(6) == b' there'
assert r.read(8) == b' sir'
assert r.read(9) == b''
assert r._sock is None
def test_bounded_read_expect_close_with_content_length(self):
headers = {b'connection': [b'close'], b'content-length': [b'15']}
d = DummySocket()
r = HTTP11Response(200, 'OK', headers, d, None)
d._buffer = BytesIO(b'hello there sir')
assert r.read(5) == b'hello'
assert r.read(6) == b' there'
assert r.read(8) == b' sir'
assert r.read(9) == b''
assert r._sock is None
def test_compressed_bounded_read_expect_close(self):
headers = {b'connection': [b'close'], b'content-encoding': [b'gzip']}
c = zlib_compressobj(wbits=24)
body = c.compress(b'hello there sir')
body += c.flush()
d = DummySocket()
r = HTTP11Response(200, 'OK', headers, d, None)
d._buffer = BytesIO(body)
response = b''
while True:
# 12 is magic here: it's the smallest read that actually
# decompresses immediately.
chunk = r.read(15)
if not chunk:
break
response += chunk
assert response == b'hello there sir'
assert r._sock is None
def test_expect_close_reads_call_close_callback(self):
connection = mock.MagicMock()
d = DummySocket()
r = HTTP11Response(
200, 'OK', {b'connection': [b'close']}, d, connection
)
d._buffer = BytesIO(b'hello there sir')
assert r.read(5) == b'hello'
assert r.read(6) == b' there'
assert r.read(8) == b' sir'
assert r.read(9) == b''
assert r._sock is None
assert connection.close.call_count == 1
def test_expect_close_unbounded_reads_call_close_callback(self):
connection = mock.MagicMock()
d = DummySocket()
r = HTTP11Response(
200, 'OK', {b'connection': [b'close']}, d, connection
)
d._buffer = BytesIO(b'hello there sir')
r.read()
assert r._sock is None
assert connection.close.call_count == 1
def test_content_length_expect_close_reads_call_close_callback(self):
connection = mock.MagicMock()
headers = {b'connection': [b'close'], b'content-length': [b'15']}
d = DummySocket()
r = HTTP11Response(200, 'OK', headers, d, connection)
d._buffer = BytesIO(b'hello there sir')
r.read()
assert r._sock is None
assert connection.close.call_count == 1
def test_content_length_reads_dont_call_close_callback(self):
connection = mock.MagicMock()
headers = {b'content-length': [b'15']}
d = DummySocket()
r = HTTP11Response(200, 'OK', headers, d, connection)
d._buffer = BytesIO(b'hello there sir')
r.read()
assert r._sock is None
assert connection.close.call_count == 0
def test_chunked_reads_dont_call_close_callback(self):
connection = mock.MagicMock()
headers = {b'transfer-encoding': [b'chunked']}
d = DummySocket()
r = HTTP11Response(200, 'OK', headers, d, connection)
data = (
b'4\r\nwell\r\n'
b'4\r\nwell\r\n'
b'4\r\nwhat\r\n'
b'4\r\nhave\r\n'
b'2\r\nwe\r\n'
b'a\r\nhereabouts\r\n'
b'0\r\n\r\n'
)
d._buffer = BytesIO(data)
list(r.read_chunked())
assert r._sock is None
assert connection.close.call_count == 0
def test_closing_chunked_reads_dont_call_close_callback(self):
connection = mock.MagicMock()
headers = {
b'transfer-encoding': [b'chunked'], b'connection': [b'close']
}
d = DummySocket()
r = HTTP11Response(200, 'OK', headers, d, connection)
data = (
b'4\r\nwell\r\n'
b'4\r\nwell\r\n'
b'4\r\nwhat\r\n'
b'4\r\nhave\r\n'
b'2\r\nwe\r\n'
b'a\r\nhereabouts\r\n'
b'0\r\n\r\n'
)
d._buffer = BytesIO(data)
list(r.read_chunked())
assert r._sock is None
assert connection.close.call_count == 1
def test_regular_response_with_missing_headers_raises_error(self):
headers = {}
sock = DummySocket()
connection = mock.MagicMock()
with pytest.raises(ValueError) as exc_info:
HTTP11Response(200, 'OK', headers, sock, connection)
assert 'A response must either specify a content-length, ' \
'be a chunked response, or specify that the ' \
'connection be closed after the response. ' \
in str(exc_info)
class DummySocket(object):
def __init__(self):
self.queue = []
self._buffer = BytesIO()
self._read_counter = 0
self.can_read = False
@property
def buffer(self):
return memoryview(self._buffer.getvalue()[self._read_counter:])
def advance_buffer(self, amt):
self._read_counter += amt
self._buffer.read(amt)
def send(self, data):
if not isinstance(data, bytes):
raise TypeError()
self.queue.append(data)
def recv(self, l):
data = self._buffer.read(l)
self._read_counter += len(data)
return memoryview(data)
def close(self):
pass
def readline(self):
line = self._buffer.readline()
self._read_counter += len(line)
return memoryview(line)
def fill(self):
pass
class DummyFile(object):
def __init__(self, data):
self.buffer = BytesIO(data)
def read(self, amt=None):
return self.buffer.read(amt)
def fileno(self):
return -1
def readline(self):
self.buffer.readline()
def __iter__(self):
return self.buffer.__iter__()