Skip to content

Decoding the response content according to the supplied encoding. #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion intercom/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def send_request_to_path(cls, method, url, auth, params=None):
@classmethod
def parse_body(cls, resp):
try:
decoded_body = resp.content.decode()
# use supplied encoding to decode the response content
decoded_body = resp.content.decode(resp.encoding)
if not decoded_body: # return early for empty responses (issue-72)
return
body = json.loads(decoded_body)
Expand Down
15 changes: 13 additions & 2 deletions tests/unit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@ def _call(*args, **kwargs):
return _call


def get_user(email="bob@example.com"):
def mock_response(content, status_code=200, encoding='utf-8', headers=None):
if headers is None:
headers = {
'x-ratelimit-limit': 500,
'x-ratelimit-remaining': 500,
'x-ratelimit-reset': 1427932858
}
return Mock(
content=content, status_code=status_code, encoding=encoding, headers=headers)


def get_user(email="bob@example.com", name="Joe Schmoe"):
return {
"type": "user",
"id": "aaaaaaaaaaaaaaaaaaaaaaaa",
"user_id": 'id-from-customers-app',
"email": email,
"name": "Joe Schmoe",
"name": name,
"avatar": {
"type": "avatar",
"image_url": "https://graph.facebook.com/1/picture?width=24&height=24"
Expand Down
35 changes: 15 additions & 20 deletions tests/unit/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,54 @@
from nose.tools import eq_
from nose.tools import ok_
from nose.tools import istest

headers = {
'x-ratelimit-limit': 500,
'x-ratelimit-remaining': 500,
'x-ratelimit-reset': 1427932858
}
from tests.unit import mock_response


class RequestTest(unittest.TestCase):

@istest
def it_raises_resource_not_found(self):
resp = Mock(content='{}', status_code=404)
resp = mock_response('{}', status_code=404)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.ResourceNotFound):
Request.send_request_to_path('GET', 'notes', ('x', 'y'), resp)

@istest
def it_raises_authentication_error_unauthorized(self):
resp = Mock(content='{}', status_code=401)
resp = mock_response('{}', status_code=401)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.AuthenticationError):
Request.send_request_to_path('GET', 'notes', ('x', 'y'), resp)

@istest
def it_raises_authentication_error_forbidden(self):
resp = Mock(content='{}', status_code=403)
resp = mock_response('{}', status_code=403)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.AuthenticationError):
Request.send_request_to_path('GET', 'notes', ('x', 'y'), resp)

@istest
def it_raises_server_error(self):
resp = Mock(content='{}', status_code=500)
resp = Mock(encoding="utf-8", content='{}', status_code=500)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.ServerError):
Request.send_request_to_path('GET', 'notes', ('x', 'y'), resp)

@istest
def it_raises_bad_gateway_error(self):
resp = Mock(content='{}', status_code=502)
resp = mock_response('{}', status_code=502)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.BadGatewayError):
Request.send_request_to_path('GET', 'notes', ('x', 'y'), resp)

@istest
def it_raises_service_unavailable_error(self):
resp = Mock(content='{}', status_code=503)
resp = mock_response('{}', status_code=503)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.ServiceUnavailableError):
Expand All @@ -83,7 +78,7 @@ def it_raises_an_unexpected_typed_error(self):
]
}
content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
try:
Expand All @@ -105,7 +100,7 @@ def it_raises_an_unexpected_untyped_error(self):
]
}
content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
try:
Expand All @@ -131,7 +126,7 @@ def it_raises_a_bad_request_error(self):
payload['errors'][0]['type'] = code

content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.BadRequestError):
Expand All @@ -152,7 +147,7 @@ def it_raises_an_authentication_error(self):
payload['errors'][0]['type'] = code

content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.AuthenticationError):
Expand All @@ -170,7 +165,7 @@ def it_raises_resource_not_found_by_type(self):
]
}
content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.ResourceNotFound):
Expand All @@ -188,7 +183,7 @@ def it_raises_rate_limit_exceeded(self):
]
}
content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.RateLimitExceeded):
Expand All @@ -206,7 +201,7 @@ def it_raises_a_service_unavailable_error(self):
]
}
content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.ServiceUnavailableError):
Expand All @@ -224,7 +219,7 @@ def it_raises_a_multiple_matching_users_error(self):
]
}
content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(intercom.MultipleMatchingUsersError):
Expand Down
31 changes: 22 additions & 9 deletions tests/unit/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
from intercom import User
from intercom import MultipleMatchingUsersError
from intercom.utils import create_class_instance
from mock import Mock
from mock import patch
from nose.tools import assert_raises
from nose.tools import eq_
from nose.tools import ok_
from nose.tools import istest
from tests.unit import get_user
from tests.unit import mock_response


class UserTest(unittest.TestCase):
Expand Down Expand Up @@ -221,7 +221,6 @@ def it_saves_a_user_with_companies(self):
user = User(
email="jo@example.com", user_id="i-1224242",
companies=[{'company_id': 6, 'name': 'Intercom'}])
print ("+++++>", user.changed_attributes)
body = {
'email': 'jo@example.com',
'user_id': 'i-1224242',
Expand Down Expand Up @@ -345,7 +344,6 @@ def it_returns_the_total_number_of_users(self):
eq_(100, User.count())

@istest
# @httpretty.activate
def it_raises_a_multiple_matching_users_error_when_receiving_a_conflict(self): # noqa
payload = {
'type': 'error.list',
Expand All @@ -356,18 +354,33 @@ def it_raises_a_multiple_matching_users_error_when_receiving_a_conflict(self):
}
]
}
headers = {
'x-ratelimit-limit': 500,
'x-ratelimit-remaining': 500,
'x-ratelimit-reset': 1427932858
}
# create bytes content
content = json.dumps(payload).encode('utf-8')
resp = Mock(content=content, status_code=200, headers=headers)
# create mock response
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
with assert_raises(MultipleMatchingUsersError):
Intercom.get('/users')

@istest
def it_handles_accented_characters(self):
# create a user dict with a name that contains accented characters
payload = get_user(name='Jóe Schmö')
# create bytes content
content = json.dumps(payload).encode('utf-8')
# create mock response
resp = mock_response(content)
with patch('requests.request') as mock_method:
mock_method.return_value = resp
user = User.find(email='bob@example.com')
try:
# Python 2
eq_(unicode('Jóe Schmö', 'utf-8'), user.name)
except NameError:
# Python 3
eq_('Jóe Schmö', user.name)


class DescribeIncrementingCustomAttributeFields(unittest.TestCase):

Expand Down