def test_decode_brotli(self): data = brotli.compress(b"foo") fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "br"}) r.preload_content() assert r.data == b"foo"
def test_io(self): fp = BytesIO(b"foo") resp = HTTPResponse(fp) assert not resp.closed assert resp.readable() assert not resp.writable() with pytest.raises(IOError): resp.fileno() resp.close() assert resp.closed # Try closing with a base Response hlr = get_response() resp2 = HTTPResponse(hlr.body) assert not resp2.closed resp2.close() assert resp2.closed # also try when only data is present. resp3 = HTTPResponse("foodata") resp3.preload_content() with pytest.raises(IOError): resp3.fileno()
def test_preload(self): fp = BytesIO(b"foo") r = HTTPResponse(fp) r.preload_content() assert fp.tell() == len(b"foo") assert r.data == b"foo"
def test_multi_decoding_deflate_deflate(self): data = zlib.compress(zlib.compress(b"foo")) fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "deflate, deflate"}) r.preload_content() assert r.data == b"foo"
def test_decode_deflate_case_insensitve(self): data = zlib.compress(b"foo") fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "DeFlAtE"}) r.preload_content() assert r.data == b"foo"
def test_multi_decoding_deflate_gzip(self): compress = zlib.compressobj(6, zlib.DEFLATED, 16 + zlib.MAX_WBITS) data = compress.compress(zlib.compress(b"foo")) data += compress.flush() fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "deflate, gzip"}) r.preload_content() assert r.data == b"foo"
def test_chunked_decoding_gzip_swallow_garbage(self): 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"}) r.preload_content() assert r.data == b"foofoofoo"
def test_decode_gzip_multi_member(self): compress = zlib.compressobj(6, zlib.DEFLATED, 16 + zlib.MAX_WBITS) data = compress.compress(b"foo") data += compress.flush() data = data * 3 fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "gzip"}) r.preload_content() assert r.data == b"foofoofoo"
def test_decode_bad_data(self): fp = BytesIO(b"\x00" * 10) with pytest.raises(DecodeError): r = HTTPResponse(fp, headers={"content-encoding": "deflate"}) r.preload_content()
def test_none(self): r = HTTPResponse(None) r.preload_content() assert r.data == b""
def test_default(self): r = HTTPResponse() r.preload_content() assert r.data == b""
def test_body_blob(self): resp = HTTPResponse(b"foo") resp.preload_content() assert resp.data == b"foo" assert resp.closed
def test_decode_brotli_error(self): fp = BytesIO(b"foo") with pytest.raises(DecodeError): r = HTTPResponse(fp, headers={"content-encoding": "br"}) r.preload_content()