|
| 1 | +from http import HTTPStatus |
1 | 2 | import json
|
2 | 3 | import os
|
3 | 4 | import unittest
|
@@ -678,6 +679,86 @@ 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_header(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 = { |
| 690 | + "errorCode": "E130429", |
| 691 | + "errorDescription": "https://docs.descope.com/rate-limit", |
| 692 | + "errorMessage": "API rate limit exceeded.", |
| 693 | + } |
| 694 | + mock_request.return_value.headers = { |
| 695 | + API_RATE_LIMIT_RETRY_AFTER_HEADER: "hello" |
| 696 | + } |
| 697 | + with self.assertRaises(RateLimitException) as cm: |
| 698 | + auth.do_post("http://test.com", {}, None, None) |
| 699 | + the_exception = cm.exception |
| 700 | + self.assertEqual(the_exception.status_code, "E130429") |
| 701 | + self.assertEqual(the_exception.error_type, ERROR_TYPE_API_RATE_LIMIT) |
| 702 | + self.assertEqual( |
| 703 | + the_exception.error_description, "https://docs.descope.com/rate-limit" |
| 704 | + ) |
| 705 | + self.assertEqual(the_exception.error_message, "API rate limit exceeded.") |
| 706 | + self.assertEqual( |
| 707 | + the_exception.rate_limit_parameters, |
| 708 | + {API_RATE_LIMIT_RETRY_AFTER_HEADER: 0}, |
| 709 | + ) |
| 710 | + |
| 711 | + def test_api_rate_limit_invalid_response_body(self): |
| 712 | + auth = Auth(self.dummy_project_id, self.public_key_dict) |
| 713 | + |
| 714 | + # Test do_post empty body |
| 715 | + with patch("requests.post") as mock_request: |
| 716 | + mock_request.return_value.ok = False |
| 717 | + mock_request.return_value.status_code = 429 |
| 718 | + mock_request.return_value.json.return_value = "aaa" |
| 719 | + with self.assertRaises(RateLimitException) as cm: |
| 720 | + auth.do_post("http://test.com", {}, None, None) |
| 721 | + the_exception = cm.exception |
| 722 | + self.assertEqual(the_exception.status_code, HTTPStatus.TOO_MANY_REQUESTS) |
| 723 | + self.assertEqual(the_exception.error_type, ERROR_TYPE_API_RATE_LIMIT) |
| 724 | + self.assertEqual(the_exception.error_description, ERROR_TYPE_API_RATE_LIMIT) |
| 725 | + self.assertEqual(the_exception.error_message, ERROR_TYPE_API_RATE_LIMIT) |
| 726 | + self.assertEqual(the_exception.rate_limit_parameters, {}) |
| 727 | + |
| 728 | + def test_api_rate_limit_empty_response_body(self): |
| 729 | + auth = Auth(self.dummy_project_id, self.public_key_dict) |
| 730 | + |
| 731 | + # Test do_post empty body |
| 732 | + with patch("requests.post") as mock_request: |
| 733 | + mock_request.return_value.ok = False |
| 734 | + mock_request.return_value.status_code = 429 |
| 735 | + mock_request.return_value.json.return_value = "" |
| 736 | + with self.assertRaises(RateLimitException) as cm: |
| 737 | + auth.do_post("http://test.com", {}, None, None) |
| 738 | + the_exception = cm.exception |
| 739 | + self.assertEqual(the_exception.status_code, HTTPStatus.TOO_MANY_REQUESTS) |
| 740 | + self.assertEqual(the_exception.error_type, ERROR_TYPE_API_RATE_LIMIT) |
| 741 | + self.assertEqual(the_exception.error_description, ERROR_TYPE_API_RATE_LIMIT) |
| 742 | + self.assertEqual(the_exception.error_message, ERROR_TYPE_API_RATE_LIMIT) |
| 743 | + self.assertEqual(the_exception.rate_limit_parameters, {}) |
| 744 | + |
| 745 | + def test_api_rate_limit_none_response_body(self): |
| 746 | + auth = Auth(self.dummy_project_id, self.public_key_dict) |
| 747 | + |
| 748 | + # Test do_post empty body |
| 749 | + with patch("requests.post") as mock_request: |
| 750 | + mock_request.return_value.ok = False |
| 751 | + mock_request.return_value.status_code = 429 |
| 752 | + mock_request.return_value.json.return_value = None |
| 753 | + with self.assertRaises(RateLimitException) as cm: |
| 754 | + auth.do_post("http://test.com", {}, None, None) |
| 755 | + the_exception = cm.exception |
| 756 | + self.assertEqual(the_exception.status_code, HTTPStatus.TOO_MANY_REQUESTS) |
| 757 | + self.assertEqual(the_exception.error_type, ERROR_TYPE_API_RATE_LIMIT) |
| 758 | + self.assertEqual(the_exception.error_description, ERROR_TYPE_API_RATE_LIMIT) |
| 759 | + self.assertEqual(the_exception.error_message, ERROR_TYPE_API_RATE_LIMIT) |
| 760 | + self.assertEqual(the_exception.rate_limit_parameters, {}) |
| 761 | + |
681 | 762 | def test_raise_from_response(self):
|
682 | 763 | auth = Auth(self.dummy_project_id, self.public_key_dict)
|
683 | 764 | with patch("requests.get") as mock_request:
|
|
0 commit comments