mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(sqs): page ListQueues with MaxResults and NextToken (#6222)
ListQueues ignored MaxResults and NextToken on every protocol path and returned all matching queues without a token. The paging now lives in SqsService, so the Query, JSON and CBOR paths share it. It follows the API reference and the botocore model: MaxResults must be 1 to 1000, a NextToken is returned only when MaxResults was set and more queues remain, and without MaxResults the response holds at most 1,000 queues and no token. Queues are ordered by name, the token is the shared Pagination cursor under its own namespace, and QueueNamePrefix filtering applies before paging. An out-of-range MaxResults or an unreadable NextToken returns InvalidParameterValue. Fixes #6219
This commit is contained in:
1 parent
007ac9268e
commit
f74d7a34a3
7 files changed
+385
-5
No files matched your search
+189
@@ -0,0 +1,189 @@
|
||||
package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.path.json.JsonPath;
|
||||
import io.restassured.path.xml.XmlPath;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasKey;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
@QuarkusTest
|
||||
class SqsListQueuesPaginationIntegrationTest {
|
||||
|
||||
private static final String JSON_CONTENT_TYPE = "application/x-amz-json-1.0";
|
||||
private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
|
||||
private static final String PREFIX = "list-paging-";
|
||||
private static final List<String> QUEUE_NAMES =
|
||||
List.of(PREFIX + "a", PREFIX + "b", PREFIX + "c");
|
||||
|
||||
private final List<String> queueUrls = new ArrayList<>();
|
||||
private String otherQueueUrl;
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void createQueues() {
|
||||
otherQueueUrl = given().contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "CreateQueue")
|
||||
.formParam("QueueName", "other-list-paging")
|
||||
.post("/").then().statusCode(200)
|
||||
.extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl");
|
||||
for (String name : QUEUE_NAMES) {
|
||||
queueUrls.add(given().contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "CreateQueue")
|
||||
.formParam("QueueName", name)
|
||||
.post("/").then().statusCode(200)
|
||||
.extract().xmlPath().getString("CreateQueueResponse.CreateQueueResult.QueueUrl"));
|
||||
}
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void deleteQueues() {
|
||||
List<String> all = new ArrayList<>(queueUrls);
|
||||
all.add(otherQueueUrl);
|
||||
for (String url : all) {
|
||||
given().contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "DeleteQueue")
|
||||
.formParam("QueueUrl", url)
|
||||
.post("/");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryProtocolPagesThroughEveryQueueOnce() {
|
||||
List<String> collected = new ArrayList<>();
|
||||
String token = null;
|
||||
do {
|
||||
XmlPath xml = queryListQueues("1", token).then().statusCode(200).extract().xmlPath();
|
||||
List<String> page = xml.getList("ListQueuesResponse.ListQueuesResult.QueueUrl");
|
||||
assertEquals(1, page.size());
|
||||
collected.addAll(page);
|
||||
token = xml.getString("ListQueuesResponse.ListQueuesResult.NextToken");
|
||||
if (token != null && token.isEmpty()) {
|
||||
token = null;
|
||||
}
|
||||
} while (token != null);
|
||||
|
||||
assertEquals(queueUrls, collected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryProtocolOmitsNextTokenWithoutMaxResults() {
|
||||
queryListQueues(null, null)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(not(containsString("<NextToken>")))
|
||||
.body(containsString(PREFIX + "c"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryProtocolRejectsOutOfRangeMaxResults() {
|
||||
queryListQueues("1001", null)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterValue</Code>"));
|
||||
queryListQueues("0", null)
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterValue</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryProtocolRejectsInvalidNextToken() {
|
||||
queryListQueues("1", "garbage-token!")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterValue</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonProtocolPagesThroughEveryQueueOnce() {
|
||||
List<String> collected = new ArrayList<>();
|
||||
String token = null;
|
||||
do {
|
||||
String body = "{\"QueueNamePrefix\":\"" + PREFIX + "\",\"MaxResults\":1"
|
||||
+ (token != null ? ",\"NextToken\":\"" + token + "\"" : "") + "}";
|
||||
JsonPath json = jsonListQueues(body).then().statusCode(200).extract().jsonPath();
|
||||
List<String> page = json.getList("QueueUrls");
|
||||
assertEquals(1, page.size());
|
||||
collected.addAll(page);
|
||||
token = json.getString("NextToken");
|
||||
} while (token != null);
|
||||
|
||||
assertEquals(queueUrls, collected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonProtocolReturnsNextTokenOnlyWhileMoreQueuesRemain() {
|
||||
String first = jsonListQueues("{\"QueueNamePrefix\":\"" + PREFIX + "\",\"MaxResults\":2}")
|
||||
.then().statusCode(200).extract().jsonPath().getString("NextToken");
|
||||
assertNotNull(first);
|
||||
|
||||
JsonPath last = jsonListQueues("{\"QueueNamePrefix\":\"" + PREFIX + "\",\"MaxResults\":2,"
|
||||
+ "\"NextToken\":\"" + first + "\"}").then().statusCode(200).extract().jsonPath();
|
||||
assertEquals(List.of(queueUrls.get(2)), last.getList("QueueUrls"));
|
||||
assertNull(last.getString("NextToken"));
|
||||
|
||||
jsonListQueues("{\"QueueNamePrefix\":\"" + PREFIX + "\"}")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("$", not(hasKey("NextToken")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonProtocolRejectsOutOfRangeMaxResults() {
|
||||
jsonListQueues("{\"MaxResults\":1001}")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void jsonProtocolRejectsInvalidNextToken() {
|
||||
jsonListQueues("{\"MaxResults\":1,\"NextToken\":\"garbage-token!\"}")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("InvalidParameterValue"));
|
||||
}
|
||||
|
||||
private static Response queryListQueues(String maxResults, String nextToken) {
|
||||
RequestSpecification spec = given()
|
||||
.contentType(FORM_CONTENT_TYPE)
|
||||
.formParam("Action", "ListQueues")
|
||||
.formParam("QueueNamePrefix", PREFIX);
|
||||
if (maxResults != null) {
|
||||
spec.formParam("MaxResults", maxResults);
|
||||
}
|
||||
if (nextToken != null) {
|
||||
spec.formParam("NextToken", nextToken);
|
||||
}
|
||||
return spec.post("/");
|
||||
}
|
||||
|
||||
private static Response jsonListQueues(String body) {
|
||||
return given()
|
||||
.contentType(JSON_CONTENT_TYPE)
|
||||
.header("X-Amz-Target", "AmazonSQS.ListQueues")
|
||||
.body(body)
|
||||
.post("/");
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig.SqsServiceConfig.EndpointStrategy;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
@@ -385,6 +386,120 @@ class SqsServiceTest {
|
||||
assertEquals(2, alpha.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listQueuesPagesEveryQueueExactlyOnce() {
|
||||
String region = "eu-west-1";
|
||||
sqsService.createQueue("page-c", null, region);
|
||||
sqsService.createQueue("page-a", null, region);
|
||||
sqsService.createQueue("page-b", null, region);
|
||||
|
||||
List<String> names = new ArrayList<>();
|
||||
String token = null;
|
||||
int pages = 0;
|
||||
do {
|
||||
PaginatedResult<Queue> page = sqsService.listQueues(null, 1, token, region);
|
||||
assertEquals(1, page.items().size());
|
||||
names.add(page.items().getFirst().getQueueName());
|
||||
token = page.nextToken();
|
||||
pages++;
|
||||
} while (token != null);
|
||||
|
||||
assertEquals(3, pages);
|
||||
assertEquals(List.of("page-a", "page-b", "page-c"), names);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listQueuesOmitsNextTokenOnLastPage() {
|
||||
String region = "eu-west-1";
|
||||
sqsService.createQueue("last-a", null, region);
|
||||
sqsService.createQueue("last-b", null, region);
|
||||
|
||||
PaginatedResult<Queue> first = sqsService.listQueues(null, 1, null, region);
|
||||
assertNotNull(first.nextToken());
|
||||
PaginatedResult<Queue> second = sqsService.listQueues(null, 1, first.nextToken(), region);
|
||||
assertEquals("last-b", second.items().getFirst().getQueueName());
|
||||
assertNull(second.nextToken());
|
||||
|
||||
assertNull(sqsService.listQueues(null, 2, null, region).nextToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listQueuesWithoutMaxResultsNeverReturnsNextToken() {
|
||||
String region = "eu-west-1";
|
||||
sqsService.createQueue("nomax-a", null, region);
|
||||
sqsService.createQueue("nomax-b", null, region);
|
||||
|
||||
PaginatedResult<Queue> page = sqsService.listQueues(null, null, null, region);
|
||||
assertEquals(2, page.items().size());
|
||||
assertNull(page.nextToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listQueuesResumesAfterCursorQueueIsDeleted() {
|
||||
String region = "eu-west-1";
|
||||
sqsService.createQueue("gone-a", null, region);
|
||||
Queue cursorQueue = sqsService.createQueue("gone-b", null, region);
|
||||
sqsService.createQueue("gone-c", null, region);
|
||||
sqsService.createQueue("gone-d", null, region);
|
||||
|
||||
PaginatedResult<Queue> first = sqsService.listQueues(null, 2, null, region);
|
||||
assertEquals(List.of("gone-a", "gone-b"), first.items().stream().map(Queue::getQueueName).toList());
|
||||
|
||||
sqsService.deleteQueue(cursorQueue.getQueueUrl(), region);
|
||||
|
||||
PaginatedResult<Queue> second = sqsService.listQueues(null, 2, first.nextToken(), region);
|
||||
assertEquals(List.of("gone-c", "gone-d"), second.items().stream().map(Queue::getQueueName).toList());
|
||||
assertNull(second.nextToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listQueuesWithoutMaxResultsCapsAtOneThousand() {
|
||||
String region = "eu-west-1";
|
||||
for (int i = 0; i < 1001; i++) {
|
||||
sqsService.createQueue(String.format("cap-%04d", i), null, region);
|
||||
}
|
||||
|
||||
PaginatedResult<Queue> page = sqsService.listQueues(null, null, null, region);
|
||||
|
||||
assertEquals(1000, page.items().size());
|
||||
assertEquals("cap-0000", page.items().getFirst().getQueueName());
|
||||
assertEquals("cap-0999", page.items().getLast().getQueueName());
|
||||
assertNull(page.nextToken());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listQueuesPagesWithinPrefix() {
|
||||
String region = "eu-west-1";
|
||||
sqsService.createQueue("pfx-one", null, region);
|
||||
sqsService.createQueue("other-queue", null, region);
|
||||
sqsService.createQueue("pfx-two", null, region);
|
||||
|
||||
PaginatedResult<Queue> first = sqsService.listQueues("pfx-", 1, null, region);
|
||||
PaginatedResult<Queue> second = sqsService.listQueues("pfx-", 1, first.nextToken(), region);
|
||||
|
||||
assertEquals("pfx-one", first.items().getFirst().getQueueName());
|
||||
assertEquals("pfx-two", second.items().getFirst().getQueueName());
|
||||
assertNull(second.nextToken());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(ints = {0, -1, 1001})
|
||||
void listQueuesRejectsOutOfRangeMaxResults(int maxResults) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> sqsService.listQueues(null, maxResults, null, "eu-west-1"));
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"not-a-token!", "Z2FyYmFnZQ"})
|
||||
void listQueuesRejectsInvalidNextToken(String token) {
|
||||
sqsService.createQueue("token-queue", null, "eu-west-1");
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> sqsService.listQueues(null, 1, token, "eu-west-1"));
|
||||
assertEquals("InvalidParameterValue", e.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getQueueUrl() {
|
||||
String region = "eu-west-1";
|
||||
|
||||
Reference in new issue
Block a user