Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix serialization for sqs http calls #10732

Merged
merged 8 commits into from May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 16 additions & 11 deletions localstack/aws/protocol/serializer.py
Expand Up @@ -1642,23 +1642,28 @@ def _default_serialize(self, xmlnode: ETree.Element, params: str, _, name: str,
node.text = (
str(params)
.replace('"', '__marker__"__marker__')
.replace("\r", "__marker__\r__marker__")
.replace("\r", "__marker__-r__marker__")
)

def _node_to_string(self, root: Optional[ETree.ElementTree], mime_type: str) -> Optional[str]:
"""Replaces the previously "marked" characters with their encoded value."""
generated_string = super()._node_to_string(root, mime_type)
return (
to_bytes(
to_str(generated_string)
# Undo the second escaping of the &
.replace('__marker__"__marker__', """)
# Undo the second escaping of the carriage return (\r)
.replace("__marker__\r__marker__", "
")
if generated_string is None:
return None
generated_string = to_str(generated_string)
# Undo the second escaping of the &
# Undo the second escaping of the carriage return (\r)
if mime_type == APPLICATION_JSON:
# At this point the json was already dumped and escaped, so we replace directly.
generated_string = generated_string.replace(r"__marker__\"__marker__", r"\"").replace(
"__marker__-r__marker__", r"\r"
)
if generated_string is not None
else None
)
else:
generated_string = generated_string.replace('__marker__"__marker__', """).replace(
"__marker__-r__marker__", "
"
)

return to_bytes(generated_string)

def _add_error_tags(
self, error: ServiceException, error_tag: ETree.Element, mime_type: str
Expand Down
50 changes: 50 additions & 0 deletions tests/aws/services/sqs/test_sqs.py
Expand Up @@ -1334,6 +1334,56 @@ def test_external_hostname_via_host_header(self, monkeypatch, sqs_create_queue,
kwargs = {"flags": re.MULTILINE | re.DOTALL}
assert re.match(rf".*<QueueUrl>\s*{url}/[^<]+</QueueUrl>.*", content, **kwargs)

@pytest.mark.parametrize(
argnames="json_body",
argvalues=['{"foo": "ba\rr", "foo2": "ba&quot;r&quot;"}', json.dumps('{"foo": "ba\rr"}')],
)
@markers.aws.validated
def test_marker_serialization_json_protocol(
self, sqs_create_queue, aws_client, aws_http_client_factory, json_body
):
queue_name = f"queue-{short_uid()}"
queue_url = sqs_create_queue(QueueName=queue_name)
message_body = json_body
aws_client.sqs.send_message(QueueUrl=queue_name, MessageBody=message_body)

client = aws_http_client_factory("sqs", region="us-east-1")

if is_aws_cloud():
endpoint_url = "https://queue.amazonaws.com"
else:
endpoint_url = config.internal_service_url()

response = client.get(
endpoint_url,
params={
"Action": "ReceiveMessage",
"QueueUrl": queue_url,
"Version": "2012-11-05",
"VisibilityTimeout": "0",
},
headers={"Accept": "application/json"},
)

parsed_content = json.loads(response.content.decode("utf-8"))

if is_aws_cloud():
assert (
parsed_content["ReceiveMessageResponse"]["ReceiveMessageResult"]["messages"][0][
"Body"
]
== message_body
)

# TODO: this is an error in LocalStack. Usually it should be messages[0]['Body']
else:
assert (
parsed_content["ReceiveMessageResponse"]["ReceiveMessageResult"]["Message"]["Body"]
== message_body
)
client_receive_response = aws_client.sqs.receive_message(QueueUrl=queue_url)
assert client_receive_response["Messages"][0]["Body"] == message_body

@markers.aws.only_localstack
def test_external_host_via_header_complete_message_lifecycle(
self, monkeypatch, account_id, region_name
Expand Down
9 changes: 9 additions & 0 deletions tests/aws/services/sqs/test_sqs.validation.json
Expand Up @@ -143,6 +143,15 @@
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_list_queues": {
"last_validated_date": "2024-04-30T13:39:55+00:00"
},
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_marker_serialization_json_protocol[\"{\\\\\"foo\\\\\": \\\\\"ba\\\\rr\\\\\"}\"]": {
"last_validated_date": "2024-05-07T13:33:39+00:00"
},
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_marker_serialization_json_protocol[{\"foo\": \"ba\\rr\", \"foo2\": \"ba&quot;r&quot;\"}]": {
"last_validated_date": "2024-05-07T13:33:34+00:00"
},
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_marker_serialization_query_protocol": {
"last_validated_date": "2024-04-29T06:07:04+00:00"
},
"tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_message_deduplication_id_too_long": {
"last_validated_date": "2024-04-30T13:35:34+00:00"
},
Expand Down