|
3 | 3 | import os |
4 | 4 | import unittest |
5 | 5 |
|
6 | | -from mock import patch |
| 6 | +from mock import patch, Mock |
7 | 7 | from nose.tools import eq_, ok_, assert_is_not, assert_raises |
8 | 8 | import http_ece |
9 | 9 | from cryptography.hazmat.primitives.asymmetric import ec |
@@ -339,3 +339,32 @@ def test_send_using_requests_session(self, mock_session): |
339 | 339 | ckey = pheaders.get('crypto-key') |
340 | 340 | ok_('pre-existing' in ckey) |
341 | 341 | eq_(pheaders.get('content-encoding'), 'aesgcm') |
| 342 | + |
| 343 | + |
| 344 | +class WebpushExceptionTestCase(unittest.TestCase): |
| 345 | + |
| 346 | + def test_exception(self): |
| 347 | + from requests import Response |
| 348 | + |
| 349 | + exp = WebPushException("foo") |
| 350 | + assert ("{}".format(exp) == "WebPushException: foo") |
| 351 | + # Really should try to load the response to verify, but this mock |
| 352 | + # covers what we need. |
| 353 | + response = Mock(spec=Response) |
| 354 | + response.text = ( |
| 355 | + '{"code": 401, "errno": 109, "error": ' |
| 356 | + '"Unauthorized", "more_info": "http://' |
| 357 | + 'autopush.readthedocs.io/en/latest/htt' |
| 358 | + 'p.html#error-codes", "message": "Requ' |
| 359 | + 'est did not validate missing authoriz' |
| 360 | + 'ation header"}') |
| 361 | + response.json.return_value = json.loads(response.text) |
| 362 | + response.status_code = 401 |
| 363 | + response.reason = "Unauthorized" |
| 364 | + exp = WebPushException("foo", response) |
| 365 | + assert "{}".format(exp) == "WebPushException: foo, Response {}".format( |
| 366 | + response.text) |
| 367 | + assert '{}'.format(exp.response), '<Response [401]>' |
| 368 | + assert exp.response.json().get('errno') == 109 |
| 369 | + exp = WebPushException("foo", [1, 2, 3]) |
| 370 | + assert '{}'.format(exp) == "WebPushException: foo, Response [1, 2, 3]" |
0 commit comments