@@ -33,7 +33,7 @@ def test_pycohttpparser_installs_correctly(self):
33
33
import pycohttpparser
34
34
else :
35
35
with pytest .raises (ImportError ):
36
- import pycohttpparser
36
+ import pycohttpparser # noqa
37
37
38
38
assert True
39
39
@@ -88,7 +88,9 @@ def test_initialization_proxy_with_inline_port(self):
88
88
assert c .proxy_port == 8443
89
89
90
90
def test_initialization_proxy_with_separate_port (self ):
91
- c = HTTP11Connection ('httpbin.org' , proxy_host = 'localhost' , proxy_port = 8443 )
91
+ c = HTTP11Connection (
92
+ 'httpbin.org' , proxy_host = 'localhost' , proxy_port = 8443
93
+ )
92
94
93
95
assert c .host == 'httpbin.org'
94
96
assert c .port == 80
@@ -97,7 +99,9 @@ def test_initialization_proxy_with_separate_port(self):
97
99
assert c .proxy_port == 8443
98
100
99
101
def test_initialization_with_ipv6_addresses_proxy_inline_port (self ):
100
- c = HTTP11Connection ('[abcd:dcba::1234]' , proxy_host = '[ffff:aaaa::1]:8443' )
102
+ c = HTTP11Connection (
103
+ '[abcd:dcba::1234]' , proxy_host = '[ffff:aaaa::1]:8443'
104
+ )
101
105
102
106
assert c .host == 'abcd:dcba::1234'
103
107
assert c .port == 80
@@ -153,7 +157,7 @@ def test_iterable_header(self):
153
157
154
158
def test_invalid_header (self ):
155
159
c = HTTP11Connection ('httpbin.org' )
156
- c ._sock = sock = DummySocket ()
160
+ c ._sock = DummySocket ()
157
161
158
162
with pytest .raises (ValueError ):
159
163
c .request ('GET' , '/get' , headers = 42 )
@@ -208,6 +212,7 @@ def test_request_with_file_body(self):
208
212
# file and monkeypatching out 'os.fstat'. This makes it look like a
209
213
# real file.
210
214
FstatRval = namedtuple ('FstatRval' , ['st_size' ])
215
+
211
216
def fake_fstat (* args ):
212
217
return FstatRval (16 )
213
218
@@ -242,6 +247,7 @@ def fake_fstat(*args):
242
247
def test_request_with_generator_body (self ):
243
248
c = HTTP11Connection ('httpbin.org' )
244
249
c ._sock = sock = DummySocket ()
250
+
245
251
def body ():
246
252
yield b'hi'
247
253
yield b'there'
@@ -326,7 +332,7 @@ def test_get_response(self):
326
332
c = HTTP11Connection ('httpbin.org' )
327
333
c ._sock = sock = DummySocket ()
328
334
329
- sock ._buffer = BytesIO (
335
+ sock ._buffer = BytesIO (
330
336
b"HTTP/1.1 201 No Content\r \n "
331
337
b"Connection: close\r \n "
332
338
b"Server: Socket\r \n "
@@ -349,7 +355,7 @@ def test_response_short_reads(self):
349
355
c = HTTP11Connection ('httpbin.org' )
350
356
c ._sock = sock = DummySocket ()
351
357
352
- sock ._buffer = BytesIO (
358
+ sock ._buffer = BytesIO (
353
359
b"HTTP/1.1 200 OK\r \n "
354
360
b"Content-Length: 15\r \n "
355
361
b"\r \n "
@@ -382,6 +388,7 @@ def test_request_with_file_body_in_text_mode(self):
382
388
# file and monkeypatching out 'os.fstat'. This makes it look like a
383
389
# real file.
384
390
FstatRval = namedtuple ('FstatRval' , ['st_size' ])
391
+
385
392
def fake_fstat (* args ):
386
393
return FstatRval (16 )
387
394
@@ -404,6 +411,7 @@ def fake_fstat(*args):
404
411
def test_request_with_unicode_generator_body (self ):
405
412
c = HTTP11Connection ('httpbin.org' )
406
413
c ._sock = DummySocket ()
414
+
407
415
def body ():
408
416
yield u'hi'
409
417
yield u'there'
@@ -453,20 +461,34 @@ def test_exception_raised_for_illegal_body_type(self):
453
461
454
462
with pytest .raises (ValueError ) as exc_info :
455
463
body = 1234
456
- # content-length set so body type is set to BODY_FLAT. value doesn't matter
457
- c .request ('GET' , '/get' , body = body , headers = {'content-length' : str (len (str (body )))})
458
- assert 'Request body must be a bytestring, a file-like object returning bytestrings ' \
459
- 'or an iterable of bytestrings. Got: {}' .format (type (body )) in str (exc_info )
464
+ # content-length set so body type is set to BODY_FLAT. value
465
+ # doesn't matter
466
+ c .request (
467
+ 'GET' ,
468
+ '/get' ,
469
+ body = body ,
470
+ headers = {'content-length' : str (len (str (body )))}
471
+ )
472
+ assert 'Request body must be a bytestring, a file-like object ' \
473
+ 'returning bytestrings or an iterable of bytestrings. ' \
474
+ 'Got: {}' .format (type (body )) in str (exc_info )
460
475
461
476
def test_exception_raised_for_illegal_elements_in_iterable_body (self ):
462
477
c = HTTP11Connection ('httpbin.org' )
463
478
464
479
rogue_element = 123
465
480
with pytest .raises (ValueError ) as exc_info :
466
- # content-length set so body type is set to BODY_FLAT. value doesn't matter
481
+ # content-length set so body type is set to BODY_FLAT. value
482
+ # doesn't matter
467
483
body = ['legal1' , 'legal2' , rogue_element ]
468
- c .request ('GET' , '/get' , body = body , headers = {'content-length' : str (len (map (str , body )))})
469
- assert 'Elements in iterable body must be bytestrings. Illegal element: {}' .format (rogue_element )\
484
+ c .request (
485
+ 'GET' ,
486
+ '/get' ,
487
+ body = body ,
488
+ headers = {'content-length' : str (len (map (str , body )))}
489
+ )
490
+ assert 'Elements in iterable body must be bytestrings. Illegal ' \
491
+ 'element: {}' .format (rogue_element ) \
470
492
in str (exc_info )
471
493
472
494
def test_exception_raised_for_filelike_body_not_returning_bytes (self ):
@@ -477,9 +499,16 @@ def read(self, size):
477
499
return 42
478
500
479
501
with pytest .raises (ValueError ) as exc_info :
480
- # content-length set so body type is BODY_FLAT. value doesn't matter
481
- c .request ('GET' , '/get' , body = RogueFile (), headers = {'content-length' : str (10 )})
482
- assert 'File-like bodies must return bytestrings. Got: {}' .format (int ) in str (exc_info )
502
+ # content-length set so body type is BODY_FLAT. value doesn't
503
+ # matter
504
+ c .request (
505
+ 'GET' ,
506
+ '/get' ,
507
+ body = RogueFile (),
508
+ headers = {'content-length' : str (10 )}
509
+ )
510
+ assert 'File-like bodies must return bytestrings. ' \
511
+ 'Got: {}' .format (int ) in str (exc_info )
483
512
484
513
485
514
class TestHTTP11Response (object ):
@@ -509,7 +538,7 @@ def test_response_as_context_manager(self):
509
538
with r :
510
539
assert r .read () == b''
511
540
512
- assert r ._sock == None
541
+ assert r ._sock is None
513
542
514
543
def test_response_transparently_decrypts_gzip (self ):
515
544
d = DummySocket ()
@@ -525,7 +554,10 @@ def test_response_transparently_decrypts_gzip(self):
525
554
526
555
def test_response_transparently_decrypts_real_deflate (self ):
527
556
d = DummySocket ()
528
- headers = {b'content-encoding' : [b'deflate' ], b'connection' : [b'close' ]}
557
+ headers = {
558
+ b'content-encoding' : [b'deflate' ],
559
+ b'connection' : [b'close' ],
560
+ }
529
561
r = HTTP11Response (200 , 'OK' , headers , d , None )
530
562
c = zlib_compressobj (wbits = zlib .MAX_WBITS )
531
563
body = c .compress (b'this is test data' )
0 commit comments