mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(s3): virtual-hosted style addressing support (#805)
*.s3.localhost.floci.io, *.localhost.localstack.cloud, and
*.s3.localhost.localstack.cloud as virtual-hosted style regardless of
FLOCI_HOSTNAME — fixes 405 when SDK uses endpointOverride("http://localhost:4566")
without forcePathStyle (closes #801)
- EmbeddedDnsServer ships localhost.localstack.cloud and localhost.floci.io
as built-in suffixes so spawned containers resolve these domains to Floci's
IP without extra config
- Compatibility test S3VirtualHostStyleTest covers the full virtual-host path
end-to-end; TestFixtures.s3VirtualHostClient() prefers s3.localhost.floci.io,
falls back to s3.localhost.localstack.cloud, or uses FLOCI_S3_VHOST_ENDPOINT
in Docker
- run-docker-tests.sh and compatibility.yml inject --dns <floci-ip> and
FLOCI_S3_VHOST_ENDPOINT=http://floci:4566 into test containers
- S3 and migrate-from-localstack docs updated with virtual-hosted style details
This commit is contained in:
1 parent
ec9631e8c5
commit
c8c77c9e6e
11 files changed
+462
-15
No files matched your search
+155
@@ -0,0 +1,155 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import software.amazon.awssdk.core.sync.RequestBody;
|
||||
import software.amazon.awssdk.services.s3.S3Client;
|
||||
import software.amazon.awssdk.services.s3.model.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
/**
|
||||
* Verifies S3 virtual-hosted style addressing via s3.localhost.localstack.cloud.
|
||||
*
|
||||
* The SDK sends requests to {@code <bucket>.s3.localhost.localstack.cloud:<port>},
|
||||
* which resolves to 127.0.0.1 via LocalStack's public wildcard DNS. Floci's
|
||||
* S3VirtualHostFilter extracts the bucket name from the Host header and rewrites
|
||||
* the request to path-style internally.
|
||||
*
|
||||
* All tests are skipped when s3.localhost.localstack.cloud is not resolvable
|
||||
* (e.g., no DNS / offline environment).
|
||||
*/
|
||||
@DisplayName("S3 Virtual-Hosted Style Addressing")
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class S3VirtualHostStyleTest {
|
||||
|
||||
private static S3Client s3;
|
||||
private static final String BUCKET = "vhost-test-bucket";
|
||||
private static final String KEY = "test-object.txt";
|
||||
private static final String COPIED_KEY = "copied-object.txt";
|
||||
private static final String CONTENT = "Hello from virtual-hosted style!";
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
assumeTrue(TestFixtures.isS3VirtualHostResolvable(),
|
||||
"Skipping: S3 virtual-host DNS is not resolvable (set FLOCI_S3_VHOST_ENDPOINT or ensure s3.localhost.localstack.cloud resolves)");
|
||||
s3 = TestFixtures.s3VirtualHostClient();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
if (s3 != null) {
|
||||
try {
|
||||
s3.deleteObject(DeleteObjectRequest.builder().bucket(BUCKET).key(KEY).build());
|
||||
} catch (Exception ignored) {}
|
||||
try {
|
||||
s3.deleteObject(DeleteObjectRequest.builder().bucket(BUCKET).key(COPIED_KEY).build());
|
||||
} catch (Exception ignored) {}
|
||||
try {
|
||||
s3.deleteBucket(DeleteBucketRequest.builder().bucket(BUCKET).build());
|
||||
} catch (Exception ignored) {}
|
||||
s3.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createBucket() {
|
||||
s3.createBucket(CreateBucketRequest.builder().bucket(BUCKET).build());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void headBucket() {
|
||||
HeadBucketResponse response = s3.headBucket(HeadBucketRequest.builder()
|
||||
.bucket(BUCKET).build());
|
||||
|
||||
assertThat(response.sdkHttpResponse().isSuccessful()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void headBucketNonExistent() {
|
||||
assertThatThrownBy(() -> s3.headBucket(HeadBucketRequest.builder()
|
||||
.bucket("vhost-nonexistent-xyz").build()))
|
||||
.satisfiesAnyOf(
|
||||
e -> assertThat(e).isInstanceOf(NoSuchBucketException.class),
|
||||
e -> assertThat(((S3Exception) e).statusCode()).isEqualTo(404)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void putObject() {
|
||||
s3.putObject(PutObjectRequest.builder()
|
||||
.bucket(BUCKET).key(KEY).contentType("text/plain").build(),
|
||||
RequestBody.fromString(CONTENT));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void headObject() {
|
||||
HeadObjectResponse response = s3.headObject(HeadObjectRequest.builder()
|
||||
.bucket(BUCKET).key(KEY).build());
|
||||
|
||||
assertThat(response.contentLength()).isEqualTo(CONTENT.length());
|
||||
assertThat(response.lastModified()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void getObject() throws Exception {
|
||||
var response = s3.getObject(GetObjectRequest.builder()
|
||||
.bucket(BUCKET).key(KEY).build());
|
||||
String downloaded = new String(response.readAllBytes(), StandardCharsets.UTF_8);
|
||||
|
||||
assertThat(downloaded).isEqualTo(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void listObjectsV2() {
|
||||
ListObjectsV2Response response = s3.listObjectsV2(
|
||||
ListObjectsV2Request.builder().bucket(BUCKET).build());
|
||||
|
||||
assertThat(response.contents())
|
||||
.anyMatch(o -> KEY.equals(o.key()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void copyObject() throws Exception {
|
||||
CopyObjectResponse response = s3.copyObject(CopyObjectRequest.builder()
|
||||
.sourceBucket(BUCKET).sourceKey(KEY)
|
||||
.destinationBucket(BUCKET).destinationKey(COPIED_KEY)
|
||||
.build());
|
||||
|
||||
assertThat(response.copyObjectResult().eTag()).isNotNull();
|
||||
|
||||
var getResponse = s3.getObject(GetObjectRequest.builder()
|
||||
.bucket(BUCKET).key(COPIED_KEY).build());
|
||||
String downloaded = new String(getResponse.readAllBytes(), StandardCharsets.UTF_8);
|
||||
assertThat(downloaded).isEqualTo(CONTENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void deleteObject() {
|
||||
s3.deleteObject(DeleteObjectRequest.builder().bucket(BUCKET).key(KEY).build());
|
||||
s3.deleteObject(DeleteObjectRequest.builder().bucket(BUCKET).key(COPIED_KEY).build());
|
||||
|
||||
ListObjectsV2Response response = s3.listObjectsV2(
|
||||
ListObjectsV2Request.builder().bucket(BUCKET).build());
|
||||
assertThat(response.contents()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void deleteBucket() {
|
||||
s3.deleteBucket(DeleteBucketRequest.builder().bucket(BUCKET).build());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user