Skip to content

Commit

Permalink
Merge pull request #92 from p5k6/fix_bytes_issue
Browse files Browse the repository at this point in the history
update response to no longer encode to utf-8; include tests indicating why
  • Loading branch information
okigan authored Oct 21, 2020
2 parents a9dc0d6 + db75c1a commit 86f5c1e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.6
3.8.5
2 changes: 1 addition & 1 deletion awscurl/awscurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def inner_main(argv):

if args.include or IS_VERBOSE:
print(response.headers, end='\n\n')
print(response.text.encode('utf-8'))
print(response.text)

response.raise_for_status()

Expand Down
45 changes: 45 additions & 0 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import datetime
import logging
import sys

from unittest import TestCase

Expand All @@ -10,6 +12,7 @@
from awscurl.awscurl import make_request

from requests.exceptions import SSLError
from requests import Response

import pytest
__author__ = 'iokulist'
Expand Down Expand Up @@ -271,3 +274,45 @@ def test_make_request(self, *args, **kvargs):
self.assertEqual(expected, headers)

pass

class TestRequestResponse(TestCase):
maxDiff = None

@patch('awscurl.awscurl.__send_request')
def test_make_request(self, mocked_resp):
resp = Response()
resp.status_code=200
resp._content = b'{"file_name": "test.yml", "env": "staging", "hash": "\xe5\xad\x97"}'
resp.encoding = 'UTF-8'
mocked_resp.return_value = resp

headers = {}
params = {'method': 'GET',
'service': 'ec2',
'region': 'region',
'uri': 'https://user:pass@host:123/path/?a=b&c=d',
'headers': headers,
'data': b'C\xcfI\x91\xc1\xd0\tw<\xa8\x13\x06{=\x9b\xb3\x1c\xfcl\xfe\xb9\xb18zS\xf4%i*Q\xc9v',
'access_key': '',
'secret_key': '',
'security_token': '',
'data_binary': True}
r = make_request(**params)

expected = u'\u5b57'

### assert that the unicode character is in the response.text output
self.assertTrue(expected in r.text)

### assert that the unicode character is _not_ in the response.text.encode('utf-8')
### which has been converted to 8-bit string with unicode characters escaped
### in py2 this raises an exception on the assertion (`expected in x` below)
### in py3 we can compare the two directly, and the assertion should be false
if sys.version_info[0] == 2:
with self.assertRaises(UnicodeDecodeError):
x = str(r.text.encode('utf-8'))
expected in x
else:
self.assertFalse(expected in str(r.text.encode('utf-8')))

pass

0 comments on commit 86f5c1e

Please sign in to comment.