|
| 1 | +from http import HTTPStatus |
1 | 2 | import json |
2 | 3 | import os |
3 | 4 | import unittest |
@@ -678,6 +679,57 @@ def test_api_rate_limit_exception(self): |
678 | 679 | {API_RATE_LIMIT_RETRY_AFTER_HEADER: 10}, |
679 | 680 | ) |
680 | 681 |
|
| 682 | + def test_api_rate_limit_invalid_response_body(self): |
| 683 | + auth = Auth(self.dummy_project_id, self.public_key_dict) |
| 684 | + |
| 685 | + # Test do_post empty body |
| 686 | + with patch("requests.post") as mock_request: |
| 687 | + mock_request.return_value.ok = False |
| 688 | + mock_request.return_value.status_code = 429 |
| 689 | + mock_request.return_value.json.return_value = "aaa" |
| 690 | + with self.assertRaises(RateLimitException) as cm: |
| 691 | + auth.do_post("http://test.com", {}, None, None) |
| 692 | + the_exception = cm.exception |
| 693 | + self.assertEqual(the_exception.status_code, HTTPStatus.TOO_MANY_REQUESTS) |
| 694 | + self.assertEqual(the_exception.error_type, ERROR_TYPE_API_RATE_LIMIT) |
| 695 | + self.assertEqual(the_exception.error_description, ERROR_TYPE_API_RATE_LIMIT) |
| 696 | + self.assertEqual(the_exception.error_message, ERROR_TYPE_API_RATE_LIMIT) |
| 697 | + self.assertEqual(the_exception.rate_limit_parameters, {}) |
| 698 | + |
| 699 | + def test_api_rate_limit_empty_response_body(self): |
| 700 | + auth = Auth(self.dummy_project_id, self.public_key_dict) |
| 701 | + |
| 702 | + # Test do_post empty body |
| 703 | + with patch("requests.post") as mock_request: |
| 704 | + mock_request.return_value.ok = False |
| 705 | + mock_request.return_value.status_code = 429 |
| 706 | + mock_request.return_value.json.return_value = "" |
| 707 | + with self.assertRaises(RateLimitException) as cm: |
| 708 | + auth.do_post("http://test.com", {}, None, None) |
| 709 | + the_exception = cm.exception |
| 710 | + self.assertEqual(the_exception.status_code, HTTPStatus.TOO_MANY_REQUESTS) |
| 711 | + self.assertEqual(the_exception.error_type, ERROR_TYPE_API_RATE_LIMIT) |
| 712 | + self.assertEqual(the_exception.error_description, ERROR_TYPE_API_RATE_LIMIT) |
| 713 | + self.assertEqual(the_exception.error_message, ERROR_TYPE_API_RATE_LIMIT) |
| 714 | + self.assertEqual(the_exception.rate_limit_parameters, {}) |
| 715 | + |
| 716 | + def test_api_rate_limit_none_response_body(self): |
| 717 | + auth = Auth(self.dummy_project_id, self.public_key_dict) |
| 718 | + |
| 719 | + # Test do_post empty body |
| 720 | + with patch("requests.post") as mock_request: |
| 721 | + mock_request.return_value.ok = False |
| 722 | + mock_request.return_value.status_code = 429 |
| 723 | + mock_request.return_value.json.return_value = None |
| 724 | + with self.assertRaises(RateLimitException) as cm: |
| 725 | + auth.do_post("http://test.com", {}, None, None) |
| 726 | + the_exception = cm.exception |
| 727 | + self.assertEqual(the_exception.status_code, HTTPStatus.TOO_MANY_REQUESTS) |
| 728 | + self.assertEqual(the_exception.error_type, ERROR_TYPE_API_RATE_LIMIT) |
| 729 | + self.assertEqual(the_exception.error_description, ERROR_TYPE_API_RATE_LIMIT) |
| 730 | + self.assertEqual(the_exception.error_message, ERROR_TYPE_API_RATE_LIMIT) |
| 731 | + self.assertEqual(the_exception.rate_limit_parameters, {}) |
| 732 | + |
681 | 733 | def test_raise_from_response(self): |
682 | 734 | auth = Auth(self.dummy_project_id, self.public_key_dict) |
683 | 735 | with patch("requests.get") as mock_request: |
|
0 commit comments