def test_decode_bad_data(self): fp = BytesIO(b'\x00' * 10) with pytest.raises(DecodeError): HTTPResponse(fp, headers={'content-encoding': 'deflate'})
def test_deflate_streaming_tell_intermediate_point(self): # Ensure that ``tell()`` returns the correct number of bytes when # part-way through streaming compressed content. import zlib NUMBER_OF_READS = 10 class MockCompressedDataReading(BytesIO): """ A ByteIO-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'}, preload_content=False) 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_get_case_insensitive_headers(self): headers = {'host': 'example.com'} r = HTTPResponse(headers=headers) assert r.headers.get('host') == 'example.com' assert r.headers.get('Host') == 'example.com'
def test__iter__(self, payload, expected_stream): actual_stream = [] for chunk in HTTPResponse(BytesIO(payload), preload_content=False): actual_stream.append(chunk) assert actual_stream == expected_stream
def test_body_blob(self): resp = HTTPResponse(b'foo') assert resp.data == b'foo' assert resp.closed
def test_read_not_chunked_response_as_chunks(self): fp = BytesIO(b"foo") resp = HTTPResponse(fp, preload_content=False) r = resp.read_chunked() with pytest.raises(ResponseNotChunked): next(r)
def test_get_case_insensitive_headers(self): headers = {"host": "example.com"} r = HTTPResponse(headers=headers) assert r.headers.get("host") == "example.com" assert r.headers.get("Host") == "example.com"
def test_read_not_chunked_response_as_chunks(self): fp = BytesIO(b'foo') resp = HTTPResponse(fp, preload_content=False) r = resp.read_chunked() self.assertRaises(ResponseNotChunked, next, r)
def test_none(self): r = HTTPResponse(None) self.assertEqual(r.data, None)
def test_cache_content(self): r = HTTPResponse('foo') self.assertEqual(r.data, 'foo') self.assertEqual(r._body, 'foo')
def test_default(self): r = HTTPResponse() self.assertEqual(r.data, None)
def test_empty_stream(self): fp = BytesIO(b'') resp = HTTPResponse(fp, preload_content=False) stream = resp.stream(2, decode_content=False) self.assertRaises(StopIteration, next, stream)
def test_getheader(self): headers = {'host': 'example.com'} r = HTTPResponse(headers=headers) self.assertEqual(r.getheader('host'), 'example.com')
def test_body_blob(self): resp = HTTPResponse(b'foo') self.assertEqual(resp.data, b'foo') self.assertTrue(resp.closed)
def test_cache_content(self): r = HTTPResponse("foo") assert r.data == "foo" assert r._body == "foo"
def test_get_case_insensitive_headers(self): headers = {'host': 'example.com'} r = HTTPResponse(headers=headers) self.assertEqual(r.headers.get('host'), 'example.com') self.assertEqual(r.headers.get('Host'), 'example.com')
def test_default(self): r = HTTPResponse() assert r.data is None
def test_decode_brotli(self): data = brotli.compress(b"foo") fp = BytesIO(data) r = HTTPResponse(fp, headers={"content-encoding": "br"}) assert r.data == b"foo"
def test_none(self): r = HTTPResponse(None) assert r.data is None
def test_decode_brotli_error(self): fp = BytesIO(b"foo") with pytest.raises(DecodeError): HTTPResponse(fp, headers={"content-encoding": "br"})
def test_geturl(self): fp = BytesIO(b"") request_url = "https://example.com" resp = HTTPResponse(fp, request_url=request_url) assert resp.geturl() == request_url
def test_body_blob(self): resp = HTTPResponse(b"foo") assert resp.data == b"foo" assert resp.closed
def test_decode_bad_data(self): fp = BytesIO(b"\x00" * 10) with pytest.raises(DecodeError): HTTPResponse(fp, headers={"content-encoding": "deflate"})
def test_getheader(self): headers = {"host": "example.com"} r = HTTPResponse(headers=headers) assert r.getheader("host") == "example.com"
def test_closed_streaming(self): fp = BytesIO(b'foo') resp = HTTPResponse(fp, preload_content=False) resp.close() with pytest.raises(StopIteration): next(resp.stream())
def test_length_no_header(self): fp = BytesIO(b"12345") resp = HTTPResponse(fp, preload_content=False) assert resp.length_remaining is None
def test_getheader(self): headers = {'host': 'example.com'} r = HTTPResponse(headers=headers) assert r.getheader('host') == 'example.com'
def test_length_w_valid_header(self): headers = {"content-length": "5"} fp = BytesIO(b"12345") resp = HTTPResponse(fp, headers=headers, preload_content=False) assert resp.length_remaining == 5
def test_cache_content(self): r = HTTPResponse('foo') assert r.data == 'foo' assert r._body == 'foo'
def test_decode_gzip_error(self): fp = BytesIO(b'foo') with pytest.raises(DecodeError): HTTPResponse(fp, headers={'content-encoding': 'gzip'})