feat(s3): resolve index documents for static-website directory requests (#1815)

* feat(s3): resolve static website index documents

* fix(s3): drop the query string from the website folder redirect

Verified against a live S3 website endpoint in us-east-1 rather than inferred: a
bucket with photos/index.html answers

  GET /photos?code=abc&state=xyz  ->  302, Location: /photos/

with a bare Location. Same for HEAD and for nested prefixes (/a/b?x=1 -> /a/b/).

An earlier commit added query-string preservation on the strength of a review
comment asserting AWS behaved that way; the probe shows it does not, and LocalStack's
website_hosting.py agrees with the probe. An SPA whose OAuth callback lands on a
slash-less folder loses its parameters on real AWS too, so the emulator matches
rather than improving on it.
This commit is contained in:
Alex Banna authored and GitHub committed 2026-08-08 22:42:26 -05:00
1 parent 41d2a3cd19
commit 6d829ff7a4
5 files changed
+423 -52

No files matched your search

@@ -74,6 +74,21 @@ class S3ServiceTest {
assertNull(s3Service.getBucketRegion("default-bucket"));
}
@Test
void objectExistsReturnsFalseForMissingKey() {
s3Service.createBucket("exists-bucket", "us-east-1");
assertFalse(s3Service.objectExists("exists-bucket", "no-such-key"));
}
@Test
void objectExistsRethrowsWhenBucketMissing() {
// A missing key returns false, but a non-"not found" storage error (here NoSuchBucket) must
// surface rather than be masked as "object absent".
AwsException e = assertThrows(AwsException.class,
() -> s3Service.objectExists("no-such-bucket", "any-key"));
assertEquals("NoSuchBucket", e.getErrorCode());
}
@Test
void createDuplicateBucketInUsEast1IsIdempotent() {
Bucket first = s3Service.createBucket("test-bucket", "us-east-1");
@@ -129,6 +129,43 @@ class S3WebsiteIntegrationTest {
.body(equalTo("<html><body>Hello Website</body></html>"));
}
@Test
@Order(12)
void indexServedForRootEvenWithQueryString() {
// A website endpoint has no S3 REST API, so a query string on the site root (e.g. an SPA
// OAuth callback GET /?code=...&state=...) must still serve the index document rather than
// fall through to a ListObjects response.
given()
.header("Host", websiteHost())
.queryParam("code", "abc")
.queryParam("state", "xyz")
.when()
.get("/")
.then()
.statusCode(200)
.contentType("text/html")
.body(equalTo("<html><body>Hello Website</body></html>"));
}
@Test
@Order(13)
void headWebsiteRootServesIndexMetadataWithoutBody() {
given()
.header("Host", websiteHost())
.queryParam("code", "abc")
.queryParam("state", "xyz")
.when()
.head("/")
.then()
.statusCode(200)
.contentType("text/html")
.header("Content-Length", equalTo("39"))
.header("ETag", not(isEmptyOrNullString()))
.header("Last-Modified", not(isEmptyOrNullString()))
.header("Accept-Ranges", equalTo("bytes"))
.body(emptyString());
}
@Test
@Order(9)
void missingKeyServesErrorDocument() {
@@ -182,7 +219,198 @@ class S3WebsiteIntegrationTest {
}
@Test
@Order(12)
@Order(14)
void uploadSubdirectoryIndex() {
given()
.contentType("text/html")
.body("<html><body>Docs Home</body></html>")
.when()
.put("/" + BUCKET + "/docs/index.html")
.then()
.statusCode(200);
}
@Test
@Order(15)
void directoryRequestServesIndexDocumentWithQueryString() {
// GET /docs/ on the website endpoint serves docs/index.html (AWS index-document resolution),
// not the error document. Website endpoints expose only object GET/HEAD behavior, so a query
// name that is an S3 REST subresource must remain an ordinary website query string.
given()
.header("Host", websiteHost())
.queryParam("tagging", "")
.when()
.get("/docs/")
.then()
.statusCode(200)
.contentType("text/html")
.body(equalTo("<html><body>Docs Home</body></html>"));
}
@Test
@Order(16)
void directoryWithoutTrailingSlashRedirects() {
// GET /docs (a folder, not an object) 302-redirects to /docs/ so the page's relative asset
// URLs resolve against the correct base — matching real S3 website behavior.
given()
.header("Host", websiteHost())
.redirects().follow(false)
.when()
.get("/docs")
.then()
.statusCode(302)
.header("Location", equalTo("/docs/"));
}
/**
* Real S3 drops the query string from the folder redirect: a live us-east-1 website endpoint
* answers {@code GET /photos?code=abc&state=xyz} with a bare {@code Location: /photos/}.
* An SPA relying on the callback parameters surviving the redirect has to handle that on AWS,
* so the emulator matches rather than improving on it.
*/
@Test
@Order(17)
void directoryRedirectDropsQueryStringLikeRealS3() {
given()
.header("Host", websiteHost())
.redirects().follow(false)
.when()
.get("/docs?code=abc&state=xyz")
.then()
.statusCode(302)
.header("Location", equalTo("/docs/"));
}
@Test
@Order(18)
void nestedDirectoryServesIndexDocument() {
given()
.contentType("text/html")
.body("<html><body>Nested</body></html>")
.when()
.put("/" + BUCKET + "/a/b/index.html")
.then()
.statusCode(200);
given()
.header("Host", websiteHost())
.when()
.get("/a/b/")
.then()
.statusCode(200)
.body(equalTo("<html><body>Nested</body></html>"));
}
@Test
@Order(19)
void directoryWithoutIndexServesErrorDocument() {
// A trailing-slash request whose prefix has no index document falls back to the error document.
given()
.header("Host", websiteHost())
.when()
.get("/no-such-dir/")
.then()
.statusCode(404)
.contentType("text/html")
.header("x-amz-error-code", "NoSuchKey")
.body(equalTo("<html><body>Custom Error</body></html>"));
}
@Test
@Order(20)
void headWebsiteDirectoryServesIndexMetadataWithoutBody() {
given()
.header("Host", websiteHost())
.when()
.head("/docs/")
.then()
.statusCode(200)
.contentType("text/html")
.header("Content-Length", equalTo("35"))
.header("ETag", not(isEmptyOrNullString()))
.header("Last-Modified", not(isEmptyOrNullString()))
.header("Accept-Ranges", equalTo("bytes"))
.body(emptyString());
}
@Test
@Order(21)
void headWebsiteDirectoryWithoutTrailingSlashRedirectsWithoutBody() {
given()
.header("Host", websiteHost())
.redirects().follow(false)
.when()
.head("/docs?code=abc&state=xyz")
.then()
.statusCode(302)
.header("Location", equalTo("/docs/"))
.body(emptyString());
}
@Test
@Order(22)
void headWebsiteMissingKeyServesErrorMetadataWithoutBody() {
given()
.header("Host", websiteHost())
.when()
.head("/missing-page")
.then()
.statusCode(404)
.contentType("text/html")
.header("Content-Length", equalTo("38"))
.header("x-amz-error-code", equalTo("NoSuchKey"))
.header("x-amz-error-message", equalTo("The specified key does not exist."))
.body(emptyString());
}
@Test
@Order(23)
void websiteWithoutCustomErrorDocumentServesDefaultHtmlForGetAndHead() {
String xml = """
<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<IndexDocument>
<Suffix>index.html</Suffix>
</IndexDocument>
</WebsiteConfiguration>
""";
given()
.queryParam("website", "")
.contentType("application/xml")
.body(xml)
.when()
.put("/" + BUCKET)
.then()
.statusCode(200);
var getResponse = given()
.header("Host", websiteHost())
.when()
.get("/missing-page");
getResponse.then()
.statusCode(404)
.contentType("text/html")
.header("Content-Length", notNullValue())
.header("x-amz-error-code", equalTo("NoSuchKey"))
.header("x-amz-error-message", equalTo("The specified key does not exist."))
.body(containsString("404 Not Found"))
.body(containsString("NoSuchKey"));
given()
.header("Host", websiteHost())
.when()
.head("/missing-page")
.then()
.statusCode(404)
.contentType("text/html")
.header("Content-Length", equalTo(getResponse.header("Content-Length")))
.header("x-amz-error-code", equalTo("NoSuchKey"))
.header("x-amz-error-message", equalTo("The specified key does not exist."))
.body(emptyString());
}
@Test
@Order(24)
void deleteWebsiteConfiguration() {
given()
.queryParam("website", "")
@@ -200,10 +428,12 @@ class S3WebsiteIntegrationTest {
}
@Test
@Order(13)
@Order(25)
void cleanup() {
given().delete("/" + BUCKET + "/index.html");
given().delete("/" + BUCKET + "/error.html");
given().delete("/" + BUCKET + "/docs/index.html");
given().delete("/" + BUCKET + "/a/b/index.html");
given().delete("/" + BUCKET);
}
}