Skip to content

Commit 073e46a

Browse files
committed
Test byte decoding of _raise_for_5xx.
1 parent 1e0f279 commit 073e46a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/test_oauth2_session.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,31 @@ 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+
# Make sure our expected unicode string literal is indeed unicode
553+
# in both py2 and py3
554+
self.assertEqual(reason_unicode[0].encode("utf-8"), b"\xc2\xa1")
555+
556+
sess = OAuth2Session("test-id")
557+
558+
with self.assertRaises(HTTPError) as cm:
559+
sess._raise_for_5xx(fake_resp)
560+
561+
self.assertEqual(cm.exception.args[0], expected)
562+
538563

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

0 commit comments

Comments
 (0)