Skip to content

Commit 69ea2d9

Browse files
committed
Test byte decoding of _raise_for_5xx.
1 parent 1e0f279 commit 69ea2d9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/test_oauth2_session.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,34 @@ def fake_send(r, **kwargs):
535535
sess.fetch_token(url)
536536
self.assertTrue(sess.authorized)
537537

538+
def test_raise_for_5xx(self):
539+
for reason_bytes in [
540+
b"\xa1An error occurred!", # iso-8859-i
541+
b"\xc2\xa1An error occurred!", # utf-8
542+
]:
543+
fake_resp = mock.MagicMock()
544+
fake_resp.status_code = 504
545+
fake_resp.reason = reason_bytes
546+
reason_unicode = "¡An error occurred!"
547+
fake_resp.url = "https://example.com/token"
548+
expected = (
549+
"504 Server Error: " + reason_unicode + " for url: " + fake_resp.url
550+
)
551+
552+
# Verify that our sample is unicode in both Python2 and Python3
553+
try:
554+
unicode("")
555+
except NameError:
556+
unicode = str
557+
self.assertIsInstance(reason_unicode, unicode)
558+
559+
sess = OAuth2Session("test-id")
560+
561+
with self.assertRaises(HTTPError) as cm:
562+
sess._raise_for_5xx(fake_resp)
563+
564+
self.assertEqual(cm.exception.args[0], expected)
565+
538566

539567
class OAuth2SessionNetrcTest(OAuth2SessionTest):
540568
"""Ensure that there is no magic auth handling.

0 commit comments

Comments
 (0)