Skip to content

Commit

Permalink
Handle xml response with encoding line
Browse files Browse the repository at this point in the history
  • Loading branch information
d3QUone committed Jan 22, 2024
1 parent 52b39f6 commit 3afeaa8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
7 changes: 4 additions & 3 deletions aiosqs/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ def parse_xml_result_response(action: str, body: str, logger: Optional[LoggerTyp
remove_pis=True,
recover=False,
)
root = etree.fromstring(text=body, parser=parser)
root = etree.fromstring(text=body.encode("utf8"), parser=parser)

request_id = find_request_id(root=root)

# Check for errors first
xpath = "./*[local-name() = 'Error']"
if elements := collect_elements(root=root, xpath=xpath):
error = elements[0]
error: dict = elements[0]
error_type = error.get("Type") or "Sender"
raise SQSErrorResponse(
error=ErrorData(
type=error["Type"],
type=error_type,
code=error["Code"],
message=error["Message"],
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<ErrorResponse>
<Error>
<Message>The specified queue doesn't exist.</Message>
<Code>AWS.SimpleQueueService.NonExistentQueue</Code>
</Error>
<RequestId>bff33def-9a5591c9-9546a300-e62db9a8-eab6060aaa830c877ae944067a465ddb</RequestId>
</ErrorResponse>
9 changes: 9 additions & 0 deletions aiosqs/tests/test_get_queue_url.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from aiosqs.exceptions import SQSErrorResponse
from aiosqs.tests.cases import ActionTestCase


Expand All @@ -21,3 +22,11 @@ def test_response_with_namespace(self):
"QueueUrl": "http://sqs.mcs.mail.ru/account123/example_queue",
},
)

def test_queue_not_found(self):
with self.assertRaises(SQSErrorResponse) as e:
self.parseXMLResponse("get_queue_url_not_exists_yandex_cloud.xml")
exception = e.exception
self.assertEqual(exception.error.type, "Sender")
self.assertEqual(exception.error.code, "AWS.SimpleQueueService.NonExistentQueue")
self.assertEqual(exception.error.message, "The specified queue doesn't exist.")
13 changes: 8 additions & 5 deletions e2e/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ async def test_send_message_with_raw_text(self):
queue_url = response["QueueUrl"]

message_body = "a b c d"
response = await self.client.send_message(
queue_url=queue_url,
message_body=message_body,
)
self.assertEqual(set(response.keys()), {"MessageId", "MD5OfMessageBody"})
try:
response = await self.client.send_message(
queue_url=queue_url,
message_body=message_body,
)
self.assertEqual(set(response.keys()), {"MessageId", "MD5OfMessageBody"})
except SQSErrorResponse as e:
self.fail(f"Recieved error: {e.error}")

response = await self.client.receive_message(
queue_url=queue_url,
Expand Down

0 comments on commit 3afeaa8

Please sign in to comment.