本文整理汇总了Python中urllib3.response.HTTPResponse类的典型用法代码示例。如果您正苦于以下问题:Python HTTPResponse类的具体用法?Python HTTPResponse怎么用?Python HTTPResponse使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HTTPResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_empty_stream
def test_empty_stream(self):
fp = BytesIO(b'')
resp = HTTPResponse(fp, preload_content=False)
stream = resp.stream(2, decode_content=False)
with pytest.raises(StopIteration):
next(stream)
示例2: test_io
def test_io(self):
import socket
try:
from http.client import HTTPResponse as OldHTTPResponse
except:
from httplib import HTTPResponse as OldHTTPResponse
fp = BytesIO(b'foo')
resp = HTTPResponse(fp, preload_content=False)
self.assertEqual(resp.closed, False)
self.assertEqual(resp.readable(), True)
self.assertEqual(resp.writable(), False)
self.assertRaises(IOError, resp.fileno)
resp.close()
self.assertEqual(resp.closed, True)
# Try closing with an `httplib.HTTPResponse`, because it has an
# `isclosed` method.
hlr = OldHTTPResponse(socket.socket())
resp2 = HTTPResponse(hlr, preload_content=False)
self.assertEqual(resp2.closed, False)
resp2.close()
self.assertEqual(resp2.closed, True)
#also try when only data is present.
resp3 = HTTPResponse('foodata')
self.assertRaises(IOError, resp3.fileno)
resp3._fp = 2
# A corner case where _fp is present but doesn't have `closed`,
# `isclosed`, or `fileno`. Unlikely, but possible.
self.assertEqual(resp3.closed, True)
self.assertRaises(IOError, resp3.fileno)
示例3: test_mock_httpresponse_stream
def test_mock_httpresponse_stream(self):
# Mock out a HTTP Request that does enough to make it through urllib3's
# read() and close() calls, and also exhausts and underlying file
# object.
class MockHTTPRequest(object):
self.fp = None
def read(self, amt):
data = self.fp.read(amt)
if not data:
self.fp = None
return data
def close(self):
self.fp = None
bio = BytesIO(b'foo')
fp = MockHTTPRequest()
fp.fp = bio
resp = HTTPResponse(fp, preload_content=False)
stream = resp.stream(2)
self.assertEqual(next(stream), b'fo')
self.assertEqual(next(stream), b'o')
self.assertRaises(StopIteration, next, stream)
示例4: test_reference_read
def test_reference_read(self):
fp = BytesIO(b'foo')
r = HTTPResponse(fp, preload_content=False)
self.assertEqual(r.read(1), b'f')
self.assertEqual(r.read(2), b'oo')
self.assertEqual(r.read(), b'')
self.assertEqual(r.read(), b'')
示例5: test_reference_read
def test_reference_read(self):
fp = BytesIO(b'foo')
r = HTTPResponse(fp, preload_content=False)
assert r.read(1) == b'f'
assert r.read(2) == b'oo'
assert r.read() == b''
assert r.read() == b''
示例6: test_reference_read
def test_reference_read(self):
fp = BytesIO(b"foo")
r = HTTPResponse(fp, preload_content=False)
self.assertEqual(r.read(1), b"f")
self.assertEqual(r.read(2), b"oo")
self.assertEqual(r.read(), b"")
self.assertEqual(r.read(), b"")
示例7: test_streaming
def test_streaming(self):
fp = BytesIO(b'foo')
resp = HTTPResponse(fp, preload_content=False)
stream = resp.stream(2, decode_content=False)
self.assertEqual(next(stream), b'fo')
self.assertEqual(next(stream), b'o')
self.assertRaises(StopIteration, next, stream)
示例8: test_streaming
def test_streaming(self):
fp = BytesIO(b'foo')
resp = HTTPResponse(fp, preload_content=False)
stream = resp.stream(2, decode_content=False)
assert next(stream) == b'fo'
assert next(stream) == b'o'
with pytest.raises(StopIteration):
next(stream)
示例9: test_invalid_chunks
def test_invalid_chunks(self):
stream = [b"foooo", b"bbbbaaaaar"]
fp = MockChunkedInvalidEncoding(stream)
r = httplib.HTTPResponse(MockSock)
r.fp = fp
r.chunked = True
r.chunk_left = None
resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})
self.assertRaises(ProtocolError, next, resp.read_chunked())
示例10: test_chunked_response_with_extensions
def test_chunked_response_with_extensions(self):
stream = [b"foo", b"bar"]
fp = MockChunkedEncodingWithExtensions(stream)
r = httplib.HTTPResponse(MockSock)
r.fp = fp
r.chunked = True
r.chunk_left = None
resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})
assert stream == list(resp.stream())
示例11: test_mock_transfer_encoding_chunked_unlmtd_read
def test_mock_transfer_encoding_chunked_unlmtd_read(self):
stream = [b"foooo", b"bbbbaaaaar"]
fp = MockChunkedEncodingResponse(stream)
r = httplib.HTTPResponse(MockSock)
r.fp = fp
r.chunked = True
r.chunk_left = None
resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})
assert stream == list(resp.read_chunked())
示例12: test_mock_transfer_encoding_chunked
def test_mock_transfer_encoding_chunked(self):
stream = [b"fo", b"o", b"bar"]
fp = MockChunkedEncodingResponse(stream)
r = httplib.HTTPResponse(MockSock)
r.fp = fp
resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})
for i, c in enumerate(resp.stream()):
assert c == stream[i]
示例13: test_chunked_decoding_deflate
def test_chunked_decoding_deflate(self):
import zlib
data = zlib.compress(b'foo')
fp = BytesIO(data)
r = HTTPResponse(fp, headers={'content-encoding': 'deflate'},
preload_content=False)
self.assertEqual(r.read(3), b'')
self.assertEqual(r.read(1), b'f')
self.assertEqual(r.read(2), b'oo')
示例14: test_mock_transfer_encoding_chunked
def test_mock_transfer_encoding_chunked(self):
stream = [b"fo", b"o", b"bar"]
fp = MockChunkedEncodingResponse(stream)
r = httplib.HTTPResponse(MockSock)
r.fp = fp
resp = HTTPResponse(r, preload_content=False, headers={'transfer-encoding': 'chunked'})
i = 0
for c in resp.stream():
self.assertEqual(c, stream[i])
i += 1
示例15: test_geturl_retries
def test_geturl_retries(self):
fp = BytesIO(b'')
resp = HTTPResponse(fp, request_url='http://example.com')
request_histories = [
RequestHistory(method='GET', url='http://example.com', error=None,
status=301, redirect_location='https://example.com/'),
RequestHistory(method='GET', url='https://example.com/', error=None,
status=301, redirect_location='https://www.example.com')]
retry = Retry(history=request_histories)
resp = HTTPResponse(fp, retries=retry)
assert resp.geturl() == 'https://www.example.com'