def test_reference_read(self): fp = BytesIO(b"foo") r = HTTPResponse(fp) assert r.read(1) == b"f" assert r.read(2) == b"oo" assert r.read() == b"" assert r.read() == b""
def test_chunked_decoding_gzip(self): compress = zlib.compressobj(6, zlib.DEFLATED, 16 + zlib.MAX_WBITS) data = compress.compress(b"foo") data += compress.flush() fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "gzip"}) assert r.read(1) == b"f" assert r.read(2) == b"oo" assert r.read() == b"" assert r.read() == b""
def test_chunked_decoding_deflate(self): data = zlib.compress(b"foo") fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "deflate"}) assert r.read(1) == b"f" # Buffer in case we need to switch to the raw stream assert r._decoder._data is None assert r.read(2) == b"oo" assert r.read() == b"" assert r.read() == b""
def test_chunked_decoding_deflate2(self): compress = zlib.compressobj(6, zlib.DEFLATED, -zlib.MAX_WBITS) data = compress.compress(b"foo") data += compress.flush() fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "deflate"}) assert r.read(1) == b"f" # Once we've decoded data, we just stream to the decoder; no buffering assert r._decoder._data is None assert r.read(2) == b"oo" assert r.read() == b"" assert r.read() == b""
def test_chunked_decoding_brotli(self): data = brotli.compress(b"foobarbaz") fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "br"}) ret = b"" for _ in range(100): ret += r.read(1) if r.closed: break assert ret == b"foobarbaz"
def test_decode_gzip_swallow_garbage(self): # When data comes from multiple calls to read(), data after # the first zlib error (here triggered by garbage) should be # ignored. compress = zlib.compressobj(6, zlib.DEFLATED, 16 + zlib.MAX_WBITS) data = compress.compress(b"foo") data += compress.flush() data = data * 3 + b"foo" fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "gzip"}) ret = b"" for _ in range(100): ret += r.read(1) if r.closed: break assert ret == b"foofoofoo"