Skip to content

Commit

Permalink
Merge branch 'test_to_function' of https://github.com/iscai-msft/azur…
Browse files Browse the repository at this point in the history
…e-sdk-for-python into have_pipelines_support_rest

* 'test_to_function' of https://github.com/iscai-msft/azure-sdk-for-python:
  remove unnecessary if else statement
  remove unnecessary content setting
  convert to unicode str
  unify text and encoding accross transports
  • Loading branch information
iscai-msft committed Aug 19, 2021
2 parents 78b02b1 + 59fc8d5 commit a282493
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 32 deletions.
2 changes: 2 additions & 0 deletions sdk/core/azure-core/azure/core/rest/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ def get_charset_encoding(response):

def decode_to_text(encoding, content):
# type: (Optional[str], bytes) -> str
if not content:
return ""
if encoding == "utf-8":
encoding = "utf-8-sig"
if encoding:
Expand Down
13 changes: 0 additions & 13 deletions sdk/core/azure-core/azure/core/rest/_requests_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,6 @@ def content(self):
# requests throws a RuntimeError if the content for a response is already consumed
raise ResponseNotReadError(self)

@_HttpResponseBase.encoding.setter # type: ignore
def encoding(self, value):
# type: (str) -> None
_HttpResponseBase.encoding.fset(self, value)
self._internal_response.encoding = value

def text(self, encoding=None):
# this will trigger errors if response is not read in
self.content # pylint: disable=pointless-statement
if encoding:
self._internal_response.encoding = encoding
return self._internal_response.text

def _stream_download_helper(decompress, response):
if response.is_stream_consumed:
raise StreamConsumedError(response)
Expand Down
8 changes: 2 additions & 6 deletions sdk/core/azure-core/azure/core/rest/_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,8 @@ def text(self, encoding=None):
:return: The response's content decoded as a string.
"""
if self._text is None or encoding:
content = self.content
if not content:
self._text = ""
else:
encoding_to_pass = encoding or self.encoding
self._text = decode_to_text(encoding_to_pass, self.content)
encoding_to_pass = encoding or self.encoding
self._text = decode_to_text(encoding_to_pass, self.content)
return self._text

def json(self):
Expand Down
8 changes: 2 additions & 6 deletions sdk/core/azure-core/azure/core/rest/_rest_py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,8 @@ def text(self, encoding: Optional[str] = None) -> str:
:return: The response's content decoded as a string.
"""
if self._text is None or encoding:
content = self.content
if not content:
self._text = ""
else:
encoding_to_pass = encoding or self.encoding
self._text = decode_to_text(encoding_to_pass, self.content)
encoding_to_pass = encoding or self.encoding
self._text = decode_to_text(encoding_to_pass, self.content)
return self._text

def json(self) -> Any:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ async def test_response_no_charset_with_ascii_content(send_request):
@pytest.mark.asyncio
async def test_response_no_charset_with_iso_8859_1_content(send_request):
"""
A response with ISO 8859-1 encoded content should decode correctly,
even with no charset specified.
We don't support iso-8859-1 by default following conversations
about endoding flow
"""
response = await send_request(
request=HttpRequest("GET", "/encoding/iso-8859-1"),
)
await response.read()
assert response.text() == "Accented: �sterreich" # aiohttp is having diff behavior than requests
assert response.text() == "Accented: �sterreich"
assert response.encoding is None

# NOTE: aiohttp isn't liking this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ def test_response_no_charset_with_ascii_content(send_request):

def test_response_no_charset_with_iso_8859_1_content(send_request):
"""
A response with ISO 8859-1 encoded content should decode correctly,
even with no charset specified.
We don't support iso-8859-1 by default following conversations
about endoding flow
"""
response = send_request(
request=HttpRequest("GET", "/encoding/iso-8859-1"),
)
assert response.text() == u"Accented: Österreich"
assert response.text() == u"Accented: �sterreich"
assert response.encoding is None

def test_response_set_explicit_encoding(send_request):
Expand Down Expand Up @@ -309,5 +309,5 @@ def test_text_and_encoding(send_request):
assert response.text() == u"鿰ꦑ" == response.content.decode(response.encoding)

# assert latin-1 changes text decoding without changing encoding property
assert response.text("latin-1") == \x9f\x91©' == response.content.decode("latin-1")
assert response.text("latin-1") == u\x9f\x91©' == response.content.decode("latin-1")
assert response.encoding == "utf-16"

0 comments on commit a282493

Please sign in to comment.