def test_deflate_streaming_tell_intermediate_point(self): # Ensure that ``tell()`` returns the correct number of bytes when # part-way through streaming compressed content. NUMBER_OF_READS = 10 class MockCompressedDataReading(BytesIO): """ A BytesIO-like reader returning ``payload`` in ``NUMBER_OF_READS`` calls to ``read``. """ def __init__(self, payload, payload_part_size): self.payloads = [ payload[i * payload_part_size:(i + 1) * payload_part_size] for i in range(NUMBER_OF_READS + 1) ] self.consumed = 0 assert b"".join(self.payloads) == payload def read(self, _): # Amount is unused. if len(self.payloads) > 0: payload = self.payloads.pop(0) self.consumed += len(payload) return payload return b"" def __iter__(self): return self def __next__(self): if not self.payloads: raise StopIteration() return self.read(None) next = __next__ uncompressed_data = zlib.decompress(ZLIB_PAYLOAD) payload_part_size = len(ZLIB_PAYLOAD) // NUMBER_OF_READS fp = MockCompressedDataReading(ZLIB_PAYLOAD, payload_part_size) resp = HTTPResponse(fp, headers={"content-encoding": "deflate"}) parts = [] stream = resp.stream(1) for part in stream: parts.append(part) assert resp.tell() == fp.consumed end_of_stream = resp.tell() with pytest.raises(StopIteration): next(stream) # Check that the payload is equal to the uncompressed data payload = b"".join(parts) assert uncompressed_data == payload # Check that the end of the stream is in the correct place assert len(ZLIB_PAYLOAD) == end_of_stream
def test_streaming_tell(self): fp = [b"fo", b"o"] resp = HTTPResponse(fp) stream = resp.stream(decode_content=False) position = 0 position += len(next(stream)) assert 2 == position assert 2 == resp.tell() position += len(next(stream)) assert 3 == position assert 3 == resp.tell() with pytest.raises(StopIteration): next(stream)
def test_gzipped_streaming_tell(self): compress = zlib.compressobj(6, zlib.DEFLATED, 16 + zlib.MAX_WBITS) uncompressed_data = b"foo" data = compress.compress(uncompressed_data) data += compress.flush() fp = BytesIO(data) resp = HTTPResponse(fp, headers={"content-encoding": "gzip"}) stream = resp.stream() # Read everything payload = next(stream) assert payload == uncompressed_data assert len(data) == resp.tell() with pytest.raises(StopIteration): next(stream)