diff --git a/compatibility-tests/sdk-test-awscli/test/pipes.bats b/compatibility-tests/sdk-test-awscli/test/pipes.bats index 65846870f8..5f0e452338 100644 --- a/compatibility-tests/sdk-test-awscli/test/pipes.bats +++ b/compatibility-tests/sdk-test-awscli/test/pipes.bats @@ -301,6 +301,6 @@ target_arn() { out=$(aws_cmd sqs receive-message \ --queue-url "$TARGET_QUEUE_URL" \ --max-number-of-messages 1) - msgs=$(echo "$out" | jq '.Messages // [] | length') + msgs=$(echo "${out:-"{}"}" | jq '.Messages // [] | length') [ "$msgs" = "0" ] } diff --git a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java index 38c67fec85..970bce390e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/sqs/SqsJsonHandler.java @@ -175,6 +175,12 @@ public class SqsJsonHandler { visibilityTimeout, waitTimeSeconds, region); ObjectNode response = objectMapper.createObjectNode(); + // Match AWS: omit the Messages field entirely when no messages are + // available, so SDK clients with InitializeCollections=false see null + // instead of an empty list. + if (messages.isEmpty()) { + return Response.ok(response).build(); + } ArrayNode messagesArray = response.putArray("Messages"); for (Message msg : messages) { ObjectNode msgNode = objectMapper.createObjectNode(); diff --git a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java index c2865b8091..5ff43e423d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/sqs/SqsJsonProtocolTest.java @@ -174,6 +174,38 @@ class SqsJsonProtocolTest { @Test @Order(7) + void receiveMessageOnEmptyQueueOmitsMessagesField() { + // AWS omits the Messages field entirely when no messages are available. + // The .NET SDK with InitializeCollections=false then leaves the + // response collection as null. Floci previously returned "Messages": [] + // which broke parity tests asserting null. + String emptyQueueName = QUEUE_NAME + "-empty"; + String createBody = "{\"QueueName\":\"" + emptyQueueName + "\"}"; + String emptyQueueUrl = given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.CreateQueue") + .body(createBody) + .when() + .post("/") + .then() + .statusCode(200) + .extract().jsonPath().getString("QueueUrl"); + + String body = "{\"QueueUrl\":\"" + emptyQueueUrl + "\",\"MaxNumberOfMessages\":1}"; + + given() + .contentType(CONTENT_TYPE) + .header("X-Amz-Target", "AmazonSQS.ReceiveMessage") + .body(body) + .when() + .post("/" + ACCOUNT_ID + "/" + emptyQueueName) + .then() + .statusCode(200) + .body("$", not(hasKey("Messages"))); + } + + @Test + @Order(8) void deleteQueueViaQueueUrlPath() { String body = "{\"QueueUrl\":\"" + queueUrl + "\"}";