diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 57ba2778b3f..1838231184d 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -582,8 +582,8 @@ class ClientResponse(HeadersMixin): reason = None # Reason-Phrase content = None # Payload stream - headers = None # Response headers, CIMultiDictProxy - raw_headers = None # Response raw headers, a sequence of pairs + _headers = None # Response headers, CIMultiDictProxy + _raw_headers = None # Response raw headers, a sequence of pairs _connection = None # current connection _reader = None # input stream @@ -600,7 +600,6 @@ def __init__(self, method, url, *, assert isinstance(url, URL) self.method = method - self.headers = None self.cookies = SimpleCookie() self._real_url = url @@ -619,29 +618,33 @@ def __init__(self, method, url, *, if loop.get_debug(): self._source_traceback = traceback.extract_stack(sys._getframe(1)) - @property + @reify def url(self): return self._url - @property + @reify def url_obj(self): warnings.warn( "Deprecated, use .url #1654", DeprecationWarning, stacklevel=2) return self._url - @property + @reify def real_url(self): return self._real_url - @property + @reify def host(self): return self._url.host - @property - def _headers(self): - return self.headers + @reify + def headers(self): + return self._headers - @property + @reify + def raw_headers(self): + return self._raw_headers + + @reify def request_info(self): return self._request_info @@ -696,12 +699,12 @@ def __repr__(self): def connection(self): return self._connection - @property + @reify def history(self): """A sequence of of responses, if redirects occurred.""" return self._history - @property + @reify def links(self): links_str = ", ".join(self.headers.getall("link", [])) @@ -766,8 +769,8 @@ async def start(self, connection): self.reason = message.reason # headers - self.headers = CIMultiDictProxy(message.headers) - self.raw_headers = tuple(message.raw_headers) + self._headers = CIMultiDictProxy(message.headers) + self._raw_headers = tuple(message.raw_headers) # payload self.content = payload diff --git a/tests/test_client_request.py b/tests/test_client_request.py index 0087fcaec1f..07939bebc15 100644 --- a/tests/test_client_request.py +++ b/tests/test_client_request.py @@ -1195,7 +1195,7 @@ async def start(self, connection, read_until_eof=False): conn = connection self.status = 123 self.reason = 'Test OK' - self.headers = CIMultiDictProxy(CIMultiDict()) + self._headers = CIMultiDictProxy(CIMultiDict()) self.cookies = SimpleCookie() return diff --git a/tests/test_client_response.py b/tests/test_client_response.py index 9dbf4d05036..12852941205 100644 --- a/tests/test_client_response.py +++ b/tests/test_client_response.py @@ -320,7 +320,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = { + response._headers = { 'Content-Type': 'application/json;charset=cp1251'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -346,7 +346,7 @@ def side_effect(*args, **kwargs): return fut # lie about the encoding - response.headers = { + response._headers = { 'Content-Type': 'application/json;charset=utf-8'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -373,7 +373,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = { + response._headers = { 'Content-Type': 'application/json'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -400,7 +400,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = {'Content-Type': 'text/plain'} + response._headers = {'Content-Type': 'text/plain'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -425,7 +425,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = {'Content-Type': 'text/plain;charset=invalid'} + response._headers = {'Content-Type': 'text/plain;charset=invalid'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -451,7 +451,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = { + response._headers = { 'Content-Type': 'application/json;charset=cp1251'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -476,7 +476,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = { + response._headers = { 'Content-Type': 'application/json;charset=cp1251'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -501,7 +501,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = { + response._headers = { 'Content-Type': 'application/this.is-1_content+subtype+json;charset=cp1251'} content = response.content = mock.Mock() @@ -527,7 +527,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = { + response._headers = { 'Content-Type': 'custom/type;charset=cp1251'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -546,7 +546,7 @@ async def test_json_custom_loader(loop, session): traces=[], loop=loop, session=session) - response.headers = { + response._headers = { 'Content-Type': 'application/json;charset=cp1251'} response._body = b'data' @@ -566,7 +566,7 @@ async def test_json_invalid_content_type(loop, session): traces=[], loop=loop, session=session) - response.headers = { + response._headers = { 'Content-Type': 'data/octet-stream'} response._body = b'' @@ -585,7 +585,7 @@ async def test_json_no_content(loop, session): traces=[], loop=loop, session=session) - response.headers = { + response._headers = { 'Content-Type': 'data/octet-stream'} response._body = b'' @@ -608,7 +608,7 @@ def side_effect(*args, **kwargs): fut.set_result('{"тест": "пройден"}'.encode('cp1251')) return fut - response.headers = { + response._headers = { 'Content-Type': 'application/json;charset=utf8'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -630,7 +630,7 @@ def test_get_encoding_unknown(loop, session): loop=loop, session=session) - response.headers = {'Content-Type': 'application/json'} + response._headers = {'Content-Type': 'application/json'} with mock.patch('aiohttp.client_reqrep.chardet') as m_chardet: m_chardet.detect.return_value = {'encoding': None} assert response.get_encoding() == 'utf-8' @@ -688,7 +688,7 @@ def test_content_type(): traces=[], loop=mock.Mock(), session=mock.Mock()) - response.headers = {'Content-Type': 'application/json;charset=cp1251'} + response._headers = {'Content-Type': 'application/json;charset=cp1251'} assert 'application/json' == response.content_type @@ -702,7 +702,7 @@ def test_content_type_no_header(): traces=[], loop=mock.Mock(), session=mock.Mock()) - response.headers = {} + response._headers = {} assert 'application/octet-stream' == response.content_type @@ -716,7 +716,7 @@ def test_charset(): traces=[], loop=mock.Mock(), session=mock.Mock()) - response.headers = {'Content-Type': 'application/json;charset=cp1251'} + response._headers = {'Content-Type': 'application/json;charset=cp1251'} assert 'cp1251' == response.charset @@ -730,7 +730,7 @@ def test_charset_no_header(): traces=[], loop=mock.Mock(), session=mock.Mock()) - response.headers = {} + response._headers = {} assert response.charset is None @@ -744,7 +744,7 @@ def test_charset_no_charset(): traces=[], loop=mock.Mock(), session=mock.Mock()) - response.headers = {'Content-Type': 'application/json'} + response._headers = {'Content-Type': 'application/json'} assert response.charset is None @@ -758,8 +758,8 @@ def test_content_disposition_full(): traces=[], loop=mock.Mock(), session=mock.Mock()) - response.headers = {'Content-Disposition': - 'attachment; filename="archive.tar.gz"; foo=bar'} + response._headers = {'Content-Disposition': + 'attachment; filename="archive.tar.gz"; foo=bar'} assert 'attachment' == response.content_disposition.type assert 'bar' == response.content_disposition.parameters["foo"] @@ -777,7 +777,7 @@ def test_content_disposition_no_parameters(): traces=[], loop=mock.Mock(), session=mock.Mock()) - response.headers = {'Content-Disposition': 'attachment'} + response._headers = {'Content-Disposition': 'attachment'} assert 'attachment' == response.content_disposition.type assert response.content_disposition.filename is None @@ -793,26 +793,11 @@ def test_content_disposition_no_header(): traces=[], loop=mock.Mock(), session=mock.Mock()) - response.headers = {} + response._headers = {} assert response.content_disposition is None -def test_content_disposition_cache(): - response = ClientResponse('get', URL('http://def-cl-resp.org'), - request_info=mock.Mock(), - writer=mock.Mock(), - continue100=None, - timer=TimerNoop(), - traces=[], - loop=mock.Mock(), - session=mock.Mock()) - response.headers = {'Content-Disposition': 'attachment'} - cd = response.content_disposition - ClientResponse.headers = {'Content-Disposition': 'spam'} - assert cd is response.content_disposition - - def test_response_request_info(): url = 'http://def-cl-resp.org' headers = {'Content-Type': 'application/json;charset=cp1251'} @@ -926,7 +911,7 @@ def test_redirect_history_in_exception(): session=mock.Mock() ) - hist_response.headers = hist_headers + hist_response._headers = hist_headers hist_response.status = 301 hist_response.reason = 'REDIRECT' @@ -957,7 +942,7 @@ def side_effect(*args, **kwargs): fut.set_result(response_body) return fut - response.headers = { + response._headers = { 'Content-Type': 'application/json;charset=cp1251'} content = response.content = mock.Mock() content.read.side_effect = side_effect @@ -997,7 +982,7 @@ def test_response_links_comma_separated(loop, session): traces=[], loop=loop, session=session) - response.headers = CIMultiDict([ + response._headers = CIMultiDict([ ( "Link", ('; rel=next, ' @@ -1026,7 +1011,7 @@ def test_response_links_multiple_headers(loop, session): traces=[], loop=loop, session=session) - response.headers = CIMultiDict([ + response._headers = CIMultiDict([ ( "Link", '; rel=next' @@ -1058,7 +1043,7 @@ def test_response_links_no_rel(loop, session): traces=[], loop=loop, session=session) - response.headers = CIMultiDict([ + response._headers = CIMultiDict([ ( "Link", '' @@ -1083,7 +1068,7 @@ def test_response_links_quoted(loop, session): traces=[], loop=loop, session=session) - response.headers = CIMultiDict([ + response._headers = CIMultiDict([ ( "Link", '; rel="home-page"' @@ -1108,7 +1093,7 @@ def test_response_links_relative(loop, session): traces=[], loop=loop, session=session) - response.headers = CIMultiDict([ + response._headers = CIMultiDict([ ( "Link", '; rel=rel' @@ -1133,5 +1118,5 @@ def test_response_links_empty(loop, session): traces=[], loop=loop, session=session) - response.headers = CIMultiDict() + response._headers = CIMultiDict() assert response.links == {}