Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion appstoreserverlibrary/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,8 @@ def _parse_response(self, status_code: int, headers: MutableMapping, json_suppli
else:
normalized_headers = self._normalize_headers(headers)
# Best effort parsing of the response body
if not 'content-type' in headers or headers['content-type'] != 'application/json':
content_type = headers.get('content-type', '').split(';', 1)[0].strip().lower()
if content_type != 'application/json':
raise APIException(status_code, headers=normalized_headers)
try:
response_body = json_supplier()
Expand Down
15 changes: 15 additions & 0 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,21 @@ def test_api_too_many_requests(self):

self.assertFalse(True)

def test_api_error_with_content_type_parameters(self):
for content_type in ['application/json; charset=utf-8', 'Application/JSON; charset=UTF-8']:
with self.subTest(content_type=content_type):
client = self.get_client_with_body_from_file(
'tests/resources/models/apiTooManyRequestsException.json',
'POST',
'https://local-testing-base-url/inApps/v1/notifications/test',
{}, None, 429,
response_headers={'Content-Type': content_type, 'Retry-After': '123'})
with self.assertRaises(APIException) as caught:
client.request_test_notification()
self.assertEqual(APIError.RATE_LIMIT_EXCEEDED, caught.exception.api_error)
self.assertEqual('Rate limit exceeded.', caught.exception.error_message)
self.assertEqual(123, caught.exception.retry_after)

def test_api_too_many_requests_with_retry_after(self):
client = self.get_client_with_body_from_file('tests/resources/models/apiTooManyRequestsException.json',
'POST',
Expand Down
15 changes: 15 additions & 0 deletions tests/test_api_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,21 @@ async def test_api_too_many_requests(self):

self.assertFalse(True)

async def test_api_error_with_content_type_parameters(self):
for content_type in ['application/json; charset=utf-8', 'Application/JSON; charset=UTF-8']:
with self.subTest(content_type=content_type):
client = self.get_client_with_body_from_file(
'tests/resources/models/apiTooManyRequestsException.json',
'POST',
'https://local-testing-base-url/inApps/v1/notifications/test',
{}, None, 429,
response_headers={'Content-Type': content_type, 'Retry-After': '123'})
with self.assertRaises(APIException) as caught:
await client.request_test_notification()
self.assertEqual(APIError.RATE_LIMIT_EXCEEDED, caught.exception.api_error)
self.assertEqual('Rate limit exceeded.', caught.exception.error_message)
self.assertEqual(123, caught.exception.retry_after)

async def test_api_too_many_requests_with_retry_after(self):
client = self.get_client_with_body_from_file('tests/resources/models/apiTooManyRequestsException.json',
'POST',
Expand Down