Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 8649380

Browse files
committed
Decompression tests.
1 parent feb101c commit 8649380

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

hyper/http11/response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def __init__(self, code, reason, headers, sock):
6666
# This 16 + MAX_WBITS nonsense is to force gzip. See this
6767
# Stack Overflow answer for more:
6868
# http://stackoverflow.com/a/2695466/1401686
69-
if b'gzip' in self.headers.get('content-encoding', []):
69+
if b'gzip' in self.headers.get(b'content-encoding', []):
7070
self._decompressobj = zlib.decompressobj(16 + zlib.MAX_WBITS)
71-
elif b'deflate' in self.headers.get('content-encoding', []):
71+
elif b'deflate' in self.headers.get(b'content-encoding', []):
7272
self._decompressobj = DeflateDecoder()
7373
else:
7474
self._decompressobj = None

test/test_http11.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Unit tests for hyper's HTTP/1.1 implementation.
77
"""
88
import os
9+
import zlib
910

1011
from collections import namedtuple
1112
from io import BytesIO, StringIO
@@ -17,7 +18,7 @@
1718
from hyper.http11.response import HTTP11Response
1819
from hyper.http20.exceptions import ConnectionResetError
1920
from hyper.common.headers import HTTPHeaderMap
20-
from hyper.compat import bytes
21+
from hyper.compat import bytes, zlib_compressobj
2122

2223

2324
class TestHTTP11Connection(object):
@@ -344,6 +345,44 @@ def test_response_as_context_manager(self):
344345

345346
assert r._sock == None
346347

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+
347386
class DummySocket(object):
348387
def __init__(self):
349388
self.queue = []

0 commit comments

Comments
 (0)