mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(s3): move website folder-resolution policy into S3Service (#2189)
S3Controller carried the website endpoint's index resolution, folder redirect, exact-object precedence and error-document fallback, reading the raw Vert.x request path to do it. Introduce S3Service.WebsiteResolution and resolveWebsiteRequest/resolveWebsiteError so the service decides what to serve and the controller only renders it as HTTP. The controller keeps the one thing it alone can answer — whether the client asked for a directory — and passes it in, so S3Service stays free of jakarta.ws.rs and io.vertx types. Fold the duplicated isWebsiteErrorDocumentTrigger predicate into the service as the single source of truth, and drop an unreachable xmlErrorResponse fallback (serveErrorDocument never returned null). Behaviour is unchanged: S3WebsiteIntegrationTest's 25 tests pass untouched. Add 11 S3ServiceTest cases covering the policy directly, which now needs no HTTP layer to exercise. Closes #2140
This commit is contained in:
1 parent
75c8cfc4d9
commit
96288c104a
3 files changed
+303
-86
No files matched your search
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.services.s3.model.ObjectAttributeName;
|
||||
import io.github.hectorvent.floci.services.s3.model.PutObjectOptions;
|
||||
import io.github.hectorvent.floci.services.s3.model.Bucket;
|
||||
import io.github.hectorvent.floci.services.s3.model.S3Object;
|
||||
import io.github.hectorvent.floci.services.s3.model.WebsiteConfiguration;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
@@ -619,4 +620,137 @@ class S3ServiceTest {
|
||||
S3Object got = s3Service.getObject("test-bucket", "docs/../file.txt");
|
||||
assertArrayEquals(data, got.getData());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Website request resolution
|
||||
//
|
||||
// The HTTP rendering of these outcomes is covered end-to-end by
|
||||
// S3WebsiteIntegrationTest; these pin the policy itself, with no HTTP layer.
|
||||
// =========================================================================
|
||||
|
||||
private static final S3Service.RequestAuthorization UNSIGNED =
|
||||
new S3Service.RequestAuthorization(false, null);
|
||||
|
||||
private void websiteBucket(String index, String errorDoc) {
|
||||
s3Service.createBucket("site", "us-east-1");
|
||||
s3Service.putBucketWebsite("site", new WebsiteConfiguration(index, errorDoc));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestServesIndexForDirectoryRequest() {
|
||||
websiteBucket("index.html", null);
|
||||
s3Service.putObject("site", "docs/index.html", "hi".getBytes(), "text/html", null);
|
||||
|
||||
var resolution = s3Service.resolveWebsiteRequest("site", "docs", true, UNSIGNED);
|
||||
|
||||
var serve = assertInstanceOf(S3Service.WebsiteResolution.ServeObject.class, resolution);
|
||||
assertEquals("docs/index.html", serve.key());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestServesRootIndexWhenKeyIsEmpty() {
|
||||
websiteBucket("index.html", null);
|
||||
s3Service.putObject("site", "index.html", "root".getBytes(), "text/html", null);
|
||||
|
||||
// The site root is a directory request even though the caller saw no trailing slash.
|
||||
var resolution = s3Service.resolveWebsiteRequest("site", "", false, UNSIGNED);
|
||||
|
||||
assertEquals("index.html",
|
||||
assertInstanceOf(S3Service.WebsiteResolution.ServeObject.class, resolution).key());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestRedirectsFolderWithoutTrailingSlash() {
|
||||
websiteBucket("index.html", null);
|
||||
s3Service.putObject("site", "docs/index.html", "hi".getBytes(), "text/html", null);
|
||||
|
||||
// No trailing slash, no object at "docs", but an index lives beneath it.
|
||||
var resolution = s3Service.resolveWebsiteRequest("site", "docs", false, UNSIGNED);
|
||||
|
||||
assertInstanceOf(S3Service.WebsiteResolution.RedirectToDirectory.class, resolution);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestPrefersExactObjectOverFolderRedirect() {
|
||||
websiteBucket("index.html", null);
|
||||
s3Service.putObject("site", "docs", "exact".getBytes(), "text/plain", null);
|
||||
s3Service.putObject("site", "docs/index.html", "hi".getBytes(), "text/html", null);
|
||||
|
||||
// An exact object hit is served by the normal object path, not redirected.
|
||||
var resolution = s3Service.resolveWebsiteRequest("site", "docs", false, UNSIGNED);
|
||||
|
||||
assertInstanceOf(S3Service.WebsiteResolution.NotAWebsite.class, resolution);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestFallsBackToErrorDocumentWhenIndexMissing() {
|
||||
websiteBucket("index.html", "error.html");
|
||||
s3Service.putObject("site", "error.html", "oops".getBytes(), "text/html", null);
|
||||
|
||||
var resolution = s3Service.resolveWebsiteRequest("site", "missing", true, UNSIGNED);
|
||||
|
||||
var err = assertInstanceOf(S3Service.WebsiteResolution.ErrorDocument.class, resolution);
|
||||
assertEquals(404, err.status());
|
||||
assertArrayEquals("oops".getBytes(), err.object().getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestFallsBackToDefaultErrorWhenNoErrorDocumentConfigured() {
|
||||
websiteBucket("index.html", null);
|
||||
|
||||
var resolution = s3Service.resolveWebsiteRequest("site", "missing", true, UNSIGNED);
|
||||
|
||||
assertEquals(404,
|
||||
assertInstanceOf(S3Service.WebsiteResolution.DefaultError.class, resolution).status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestFallsBackToDefaultErrorWhenErrorDocumentItselfMissing() {
|
||||
// Configured but never uploaded: S3 serves its built-in page rather than 500-ing.
|
||||
websiteBucket("index.html", "error.html");
|
||||
|
||||
var resolution = s3Service.resolveWebsiteRequest("site", "missing", true, UNSIGNED);
|
||||
|
||||
assertEquals(404,
|
||||
assertInstanceOf(S3Service.WebsiteResolution.DefaultError.class, resolution).status());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestIsNotAWebsiteWithoutConfiguration() {
|
||||
s3Service.createBucket("plain", "us-east-1");
|
||||
|
||||
var resolution = s3Service.resolveWebsiteRequest("plain", "any", true, UNSIGNED);
|
||||
|
||||
assertInstanceOf(S3Service.WebsiteResolution.NotAWebsite.class, resolution);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteRequestPropagatesRealBucketErrors() {
|
||||
// A missing bucket must surface as NoSuchBucket, not be masked as "not a website".
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> s3Service.resolveWebsiteRequest("no-such-bucket", "any", true, UNSIGNED));
|
||||
assertEquals("NoSuchBucket", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteErrorReturnsNotAWebsiteWithoutConfiguration() {
|
||||
s3Service.createBucket("plain", "us-east-1");
|
||||
|
||||
assertInstanceOf(S3Service.WebsiteResolution.NotAWebsite.class,
|
||||
s3Service.resolveWebsiteError("plain", UNSIGNED, 404));
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveWebsiteErrorMapsForbiddenToItsOwnStatus() {
|
||||
websiteBucket("index.html", "error.html");
|
||||
s3Service.putObject("site", "error.html", "denied".getBytes(), "text/html", null);
|
||||
|
||||
// 403 keeps its status; everything else collapses to 404, matching S3.
|
||||
assertEquals(403,
|
||||
assertInstanceOf(S3Service.WebsiteResolution.ErrorDocument.class,
|
||||
s3Service.resolveWebsiteError("site", UNSIGNED, 403)).status());
|
||||
assertEquals(404,
|
||||
assertInstanceOf(S3Service.WebsiteResolution.ErrorDocument.class,
|
||||
s3Service.resolveWebsiteError("site", UNSIGNED, 500)).status());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user