Skip to content

Commit 800bd49

Browse files
Ensure that the response of an HTTP request is a valid string.
1 parent 7869c0c commit 800bd49

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
lines changed

connect/resources/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,20 @@ def _fix_request_kwargs(self, path, prev_kwargs, **kwargs):
9393
@staticmethod
9494
def _check_and_pack_response(response):
9595
# type: (requests.Response) -> Tuple[str, int]
96-
request_attrs = ('content', 'status_code', 'ok')
96+
request_attrs = ('text', 'status_code', 'ok')
9797
for attr in request_attrs:
9898
if not hasattr(response, attr):
9999
raise AttributeError(
100100
'Response does not have attribute `{}`. Check your request params. '
101101
'Response status - {}'.format(attr, response.status_code),
102102
)
103+
103104
if not response.ok:
104-
data, error = ServerErrorResponse.deserialize(response.content)
105+
data, error = ServerErrorResponse.deserialize(response.text)
105106
if data:
106107
raise ServerError(data)
107108

108-
return response.content, response.status_code
109+
return response.text, response.status_code
109110

110111

111112
class BaseResource(object):

tests/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from collections import namedtuple
77
from typing import Optional
88

9-
Response = namedtuple('Response', ('ok', 'content', 'status_code'))
9+
Response = namedtuple('Response', ('ok', 'text', 'status_code'))
10+
BinaryResponse = namedtuple('BinaryResponse', ('ok', 'content', 'status_code'))
1011

1112

1213
def load_str(filename):

tests/test_models.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@
1717
def _get_response_ok():
1818
return Response(
1919
ok=True,
20-
content=load_str(os.path.join(os.path.dirname(__file__), 'data', 'response.json')),
20+
text=load_str(os.path.join(os.path.dirname(__file__), 'data', 'response.json')),
2121
status_code=200)
2222

2323

2424
def _get_response_ok2():
2525
return Response(
2626
ok=True,
27-
content=load_str(os.path.join(os.path.dirname(__file__), 'data', 'response2.json')),
27+
text=load_str(os.path.join(os.path.dirname(__file__), 'data', 'response2.json')),
2828
status_code=200)
2929

3030

3131
def _get_response_tier_config_ok():
3232
return Response(ok=True,
33-
content=load_str(os.path.join(
33+
text=load_str(os.path.join(
3434
os.path.dirname(__file__),
3535
'data',
3636
'response_tier_config_request.json')),
@@ -40,7 +40,7 @@ def _get_response_tier_config_ok():
4040
def _get_response_migration():
4141
return Response(
4242
ok=True,
43-
content=load_str(os.path.join(
43+
text=load_str(os.path.join(
4444
os.path.dirname(__file__),
4545
'data',
4646
'response_migration.json')),
@@ -62,7 +62,7 @@ def test_resource_urljoin():
6262
@patch('requests.get', MagicMock(return_value=_get_response_ok()))
6363
def test_create_model_from_response():
6464
# Parse JSON data from response file
65-
content = json.loads(_get_response_ok().content)[0]
65+
content = json.loads(_get_response_ok().text)[0]
6666

6767
# Get requests from response
6868
resource = FulfillmentAutomation()
@@ -171,7 +171,7 @@ def test_get_tier_config(get_mock):
171171
'configuration.account.id': 'tier_id'})
172172

173173

174-
@patch('requests.get', MagicMock(return_value=Response(ok=True, content='[]', status_code=200)))
174+
@patch('requests.get', MagicMock(return_value=Response(ok=True, text='[]', status_code=200)))
175175
def test_get_tier_config_empty():
176176
config = FulfillmentAutomation().get_tier_config('', '')
177177
assert not config

tests/test_tier_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
def _get_response_ok():
2222
return Response(
2323
ok=True,
24-
content=load_str(os.path.join(
24+
text=load_str(os.path.join(
2525
os.path.dirname(__file__),
2626
'data',
2727
'response_tier_config_request.json')),
@@ -32,7 +32,7 @@ def _get_response_ok_invalid_product():
3232
response_ok = _get_response_ok()
3333
return Response(
3434
ok=response_ok.ok,
35-
content=response_ok.content.replace('CN-631-322-000', 'PRD-000-000-000'),
35+
text=response_ok.text.replace('CN-631-322-000', 'PRD-000-000-000'),
3636
status_code=response_ok.status_code)
3737

3838

tests/test_usage.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
from connect.exceptions import FileRetrievalError
1414
from connect.models import Contract, Product, UsageRecord, UsageFile
1515
from connect.resources import UsageAutomation
16-
from .common import Response, load_str
16+
from .common import Response, load_str, BinaryResponse
1717

1818

1919
def _get_response_ok():
2020
return Response(
2121
ok=True,
22-
content=load_str(os.path.join(os.path.dirname(__file__), 'data', 'response_usage.json')),
22+
text=load_str(os.path.join(os.path.dirname(__file__), 'data', 'response_usage.json')),
2323
status_code=200)
2424

2525

2626
def _get_response_ok2():
2727
return Response(
2828
ok=True,
29-
content=load_str(os.path.join(os.path.dirname(__file__), 'data', 'response_usage2.json')),
29+
text=load_str(os.path.join(os.path.dirname(__file__), 'data', 'response_usage2.json')),
3030
status_code=201)
3131

3232

@@ -47,10 +47,10 @@ def test_process():
4747
@patch('requests.get')
4848
def test_get_usage_template_ok(get_mock):
4949
get_mock.side_effect = [
50-
Response(ok=True, content='{"template_link": "..."}', status_code=200),
51-
Response(ok=True, content='template_contents', status_code=200)]
50+
Response(ok=True, text='{"template_link": "..."}', status_code=200),
51+
BinaryResponse(ok=True, content=b'template_contents', status_code=200)]
5252
resource = UsageAutomation()
53-
assert resource.get_usage_template(Product(id='PRD-638-321-603')) == 'template_contents'
53+
assert resource.get_usage_template(Product(id='PRD-638-321-603')) == b'template_contents'
5454
get_mock.assert_has_calls([
5555
call(
5656
url='http://localhost:8080/api/public/v1//usage/products/PRD-638-321-603/template/',
@@ -60,16 +60,16 @@ def test_get_usage_template_ok(get_mock):
6060

6161

6262
@patch('requests.get', MagicMock(return_value=Response(
63-
ok=True, content='{}', status_code=200)))
63+
ok=True, text='{}', status_code=200)))
6464
def test_get_usage_template_no_link():
6565
resource = UsageAutomation()
6666
with pytest.raises(FileRetrievalError):
6767
resource.get_usage_template(Product(id='PRD-638-321-603'))
6868

6969

7070
@patch('requests.get', MagicMock(side_effect=[
71-
Response(ok=True, content='{"template_link": "..."}', status_code=200),
72-
Response(ok=True, content=None, status_code=200)]))
71+
Response(ok=True, text='{"template_link": "..."}', status_code=200),
72+
BinaryResponse(ok=True, content=None, status_code=200)]))
7373
def test_get_usage_template_no_file():
7474
resource = UsageAutomation()
7575
with pytest.raises(FileRetrievalError):

tests/test_usage_file.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
# noinspection PyUnusedLocal
2121
def _get_response_ok(*args, **kwargs):
22-
content = load_str(os.path.join(os.path.dirname(__file__), 'data', 'response_usage_file.json'))
22+
text = load_str(os.path.join(os.path.dirname(__file__), 'data', 'response_usage_file.json'))
2323
if current_action:
24-
content = content.replace('UF-2018-11-9878764342',
25-
'UF-2018-11-9878764342-' + current_action)
26-
return Response(ok=True, content=content, status_code=200)
24+
text = text.replace('UF-2018-11-9878764342',
25+
'UF-2018-11-9878764342-' + current_action)
26+
return Response(ok=True, text=text, status_code=200)
2727

2828

2929
@patch('requests.get', MagicMock(return_value=_get_response_ok()))

0 commit comments

Comments
 (0)