Skip to content

Commit 98a338e

Browse files
author
Maxime Belanger
committed
Handle different content types
1 parent 60f5aee commit 98a338e

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

tests/ubersmith_request_get_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_api_get_method_returns_with_arguments(self, request_mock):
6565
expected_call()
6666

6767
def expect_a_ubersmith_call(self, requests_mock, returning=None, **kwargs):
68-
response = MagicMock(status_code=200)
68+
response = MagicMock(status_code=200, headers={"content-type": "application/json"})
6969
requests_mock.get = MagicMock(return_value=response)
7070
response.json = MagicMock(return_value=returning)
7171

tests/ubersmith_request_post_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_api_raises_exception_with_if_data_status_is_false(self, requests_mock):
7676
assert_that(calling(ubersmith_api.client.miss), raises(ubersmith_client.exceptions.UbersmithException))
7777

7878
def expect_a_ubersmith_call_post(self, requests_mock, returning=None, status_code=200, **kwargs):
79-
response = MagicMock(status_code=status_code)
79+
response = MagicMock(status_code=status_code, headers={"content-type": "application/json"})
8080
requests_mock.post = MagicMock(return_value=response)
8181
response.json = MagicMock(return_value=returning)
8282

tests/ubersmith_request_test.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
import ubersmith_client
1717
from mock import Mock, patch
1818

19-
from hamcrest import assert_that, raises, calling
19+
from hamcrest import assert_that, raises, calling, equal_to
2020
from requests.exceptions import ConnectionError, Timeout
2121

22-
from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, UbersmithConnectionError, \
22+
from ubersmith_client.exceptions import UbersmithException, BadRequest, UnknownError, Forbidden, NotFound, Unauthorized, \
23+
UbersmithConnectionError, \
2324
UbersmithTimeout
2425
from tests.ubersmith_json.response_data_structure import a_response_data
2526
from ubersmith_client.ubersmith_request import UbersmithRequest
@@ -32,8 +33,8 @@ def setUp(self):
3233
self.password = 'test'
3334

3435
def test_process_ubersmith_response(self):
35-
response = Mock()
36-
response.status_code = 200
36+
response = Mock(status_code=200, headers={"content-type": "application/json"})
37+
3738
json_data = {
3839
'client_id': '1',
3940
'first': 'Rick',
@@ -45,9 +46,12 @@ def test_process_ubersmith_response(self):
4546

4647
self.assertDictEqual(json_data, UbersmithRequest.process_ubersmith_response(response))
4748

49+
def test_process_ubersmith_response_not_application_json(self):
50+
response = Mock(status_code=200, headers={"content-type": "text/html"}, content="42")
51+
assert_that(response.content, equal_to(UbersmithRequest.process_ubersmith_response(response)))
52+
4853
def test_process_ubersmith_response_raise_exception(self):
49-
response = Mock()
50-
response.status_code = 400
54+
response = Mock(status_code=400, headers={"content-type": "application/json"})
5155
assert_that(calling(UbersmithRequest.process_ubersmith_response).with_args(response), raises(BadRequest))
5256

5357
response.status_code = 401

ubersmith_client/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ def __init__(self, url):
7070
class UbersmithTimeout(UbersmithException):
7171
def __init__(self, url, timeout):
7272
super(UbersmithTimeout, self)\
73-
.__init__(message='Trying to connect to {url} times out after {timeout}'.format(url=url, timeout=timeout))
73+
.__init__(message='Trying to connect to {url} timed out after {timeout} seconds'.format(url=url, timeout=timeout))

ubersmith_client/ubersmith_request.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __getattr__(self, function):
3232

3333
@abstractmethod
3434
def __call__(self, **kwargs):
35-
raise
35+
raise AttributeError
3636

3737
def _process_request(self, method, **kwargs):
3838
try:
@@ -52,11 +52,14 @@ def process_ubersmith_response(response):
5252
if response.status_code < 200 or response.status_code >= 400:
5353
raise get_exception_for(status_code=response.status_code)
5454

55-
response_json = response.json()
56-
if not response_json['status']:
57-
raise UbersmithException(
58-
response_json['error_code'],
59-
response_json['error_message']
60-
)
55+
if response.headers['content-type'] == 'application/json':
56+
response_json = response.json()
57+
if not response_json['status']:
58+
raise UbersmithException(
59+
response_json['error_code'],
60+
response_json['error_message']
61+
)
6162

62-
return response.json()["data"]
63+
return response.json()["data"]
64+
65+
return response.content

0 commit comments

Comments
 (0)