|
6 | 6 | Unit tests for hyper's HTTP/1.1 implementation. |
7 | 7 | """ |
8 | 8 | import os |
| 9 | +import zlib |
9 | 10 |
|
10 | 11 | from collections import namedtuple |
11 | 12 | from io import BytesIO, StringIO |
|
17 | 18 | from hyper.http11.response import HTTP11Response |
18 | 19 | from hyper.http20.exceptions import ConnectionResetError |
19 | 20 | from hyper.common.headers import HTTPHeaderMap |
20 | | -from hyper.compat import bytes |
| 21 | +from hyper.compat import bytes, zlib_compressobj |
21 | 22 |
|
22 | 23 |
|
23 | 24 | class TestHTTP11Connection(object): |
@@ -344,6 +345,44 @@ def test_response_as_context_manager(self): |
344 | 345 |
|
345 | 346 | assert r._sock == None |
346 | 347 |
|
| 348 | + def test_response_transparently_decrypts_gzip(self): |
| 349 | + d = DummySocket() |
| 350 | + headers = {b'content-encoding': [b'gzip'], b'connection': [b'close']} |
| 351 | + r = HTTP11Response(200, 'OK', headers, d) |
| 352 | + |
| 353 | + c = zlib_compressobj(wbits=24) |
| 354 | + body = c.compress(b'this is test data') |
| 355 | + body += c.flush() |
| 356 | + d._buffer = BytesIO(body) |
| 357 | + |
| 358 | + assert r.read() == b'this is test data' |
| 359 | + |
| 360 | + def test_response_transparently_decrypts_real_deflate(self): |
| 361 | + d = DummySocket() |
| 362 | + headers = {b'content-encoding': [b'deflate'], b'connection': [b'close']} |
| 363 | + r = HTTP11Response(200, 'OK', headers, d) |
| 364 | + c = zlib_compressobj(wbits=zlib.MAX_WBITS) |
| 365 | + body = c.compress(b'this is test data') |
| 366 | + body += c.flush() |
| 367 | + d._buffer = BytesIO(body) |
| 368 | + |
| 369 | + assert r.read() == b'this is test data' |
| 370 | + |
| 371 | + def test_response_transparently_decrypts_wrong_deflate(self): |
| 372 | + c = zlib_compressobj(wbits=-zlib.MAX_WBITS) |
| 373 | + body = c.compress(b'this is test data') |
| 374 | + body += c.flush() |
| 375 | + body_len = ('%s' % len(body)).encode('ascii') |
| 376 | + |
| 377 | + headers = { |
| 378 | + b'content-encoding': [b'deflate'], b'content-length': [body_len] |
| 379 | + } |
| 380 | + d = DummySocket() |
| 381 | + d._buffer = BytesIO(body) |
| 382 | + r = HTTP11Response(200, 'OK', headers, d) |
| 383 | + |
| 384 | + assert r.read() == b'this is test data' |
| 385 | + |
347 | 386 | class DummySocket(object): |
348 | 387 | def __init__(self): |
349 | 388 | self.queue = [] |
|
0 commit comments