8000 feat: add details to TwilioRestException by ashish-s · Pull Request #517 · twilio/twilio-python · GitHub
[go: up one dir, main page]

Skip to content

feat: add details to TwilioRestException #517

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 4 commits into from
Apr 27, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add unit test for rest exception
  • Loading branch information
ashish-s committed Apr 25, 2020
commit aaf88f39e73b6d9186dc0743671b5a6d6eab9ee8
33 changes: 32 additions & 1 deletion tests/unit/http/test_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from mock import patch, Mock
from requests import Session

from twilio.base.version import Version
from twilio.base.exceptions import TwilioRestException
from twilio.http.http_client import TwilioHttpClient
from twilio.http.response import Response

Expand Down Expand Up @@ -64,7 +66,7 @@ def test_request_where_class_timeout_manually_set(self):
self.client.timeout = 30

response = self.client.request(
'doesnt matter', 'doesnt matter')
'doesnt matter', 'doesnt matter')
self.assertEqual('other.twilio.com', self.request_mock.headers['Host'])
self.assertEqual(200, response.status_code)
self.assertEqual('testing-unicode: Ω≈ç√, 💩', response.content)
Expand Down Expand Up @@ -143,6 +145,28 @@ def test_request_behind_proxy(self):
self.client.request('doesnt matter', 'doesnt matter')
self.assertEqual(proxies, self.session_mock.proxies)

def test_exception_with_details(self):
v1 = MyVersion(self.client)
error_text = """{
"code": 20001,
"message": "Bad request",
"more_info": "https://www.twilio.com/docs/errors/20001",
"status": 400,
"details": {
"foo":"bar"
}
}"""
self.session_mock.send.return_value = Response(400, error_text)
try:
v1.fetch("get", "none", None, None, None, None, None)
self.fail('should not happen')
except TwilioRestException as err:
self.assertEqual(400, err.status)
self.assertEqual(20001, err.code)
self.assertEqual("get", err.method)
self.assertEqual("Unable to fetch record: Bad request", err.msg)
self.assertEqual({"foo": "bar"}, err.details)


class TestHttpClientSession(unittest.TestCase):

Expand Down Expand Up @@ -186,3 +210,10 @@ def test_session_not_preserved(self):
# Used different session, responses should be different
self.assertEqual(response_1.content, 'response_1')
self.assertEqual(response_2.content, 'response_2')


class MyVersion(Version):
def __init__(self, domain):
super(MyVersion, self).__init__(domain)
self.version = 'v1'
self._credentials = None
0