def test_response_transparently_decrypts_wrong_deflate(self): headers = HTTPHeaderMap([(':status', '200'), ('content-encoding', 'deflate')]) c = zlib_compressobj(wbits=-zlib.MAX_WBITS) body = c.compress(b'this is test data') body += c.flush() resp = HTTP20Response(headers, DummyStream(body)) assert resp.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_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_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_read_compressed_frames(self): headers = HTTPHeaderMap([(':status', '200'), ('content-encoding', 'gzip')]) c = zlib_compressobj(wbits=24) body = c.compress(b'this is test data') body += c.flush() stream = DummyStream(None) chunks = [body[x:x+2] for x in range(0, len(body), 2)] stream.data_frames = chunks resp = HTTP20Response(headers, stream) received = b'' for chunk in resp.read_chunked(): received += chunk assert received == b'this is test data'
def test_read_compressed_frames(self): headers = HTTPHeaderMap([(':status', '200'), ('content-encoding', 'gzip')]) c = zlib_compressobj(wbits=24) body = c.compress(b'this is test data') body += c.flush() stream = DummyStream(None) chunks = [body[x:x + 2] for x in range(0, len(body), 2)] stream.data_frames = chunks resp = HTTP20Response(headers, stream) received = b'' for chunk in resp.read_chunked(): received += chunk assert received == b'this is test data'
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_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_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'