Light, fluffy, and always free - The AWS Local Emulator alternative
test: fix CloudFormationVirtualHostTests.java (#467)
Conor Heine4d1f8b4907ce
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationVirtualHostTests.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationVirtualHostTests.javaindex d26bdb3bee..d3da41d8d0 100644--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationVirtualHostTests.java+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudFormationVirtualHostTests.java@@ -4,36 +4,40 @@ import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.Test;+import software.amazon.awssdk.core.interceptor.Context;+import software.amazon.awssdk.core.interceptor.ExecutionAttributes;+import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;+import software.amazon.awssdk.http.SdkHttpRequest;import software.amazon.awssdk.services.cloudformation.CloudFormationClient;import software.amazon.awssdk.services.cloudformation.model.ListStacksResponse;-import java.net.URI;-import static org.assertj.core.api.Assertions.*;/*** Ensures that requests to non-S3 service hostnames (e.g. cloudformation.amazonaws.com)* are not incorrectly hijacked by the S3 virtual host filter.+ *+ * <p>The TCP connection lands on the configured Floci endpoint; an execution+ * interceptor overrides the Host header so Floci sees the request as though it+ * came from {@code cloudformation.us-east-1.amazonaws.com}. This decouples the+ * test from DNS setup so it works equally against localhost, a docker service+ * name (e.g. {@code http://floci:4566}), or a remote Floci deployment.*/@DisplayName("CloudFormation Virtual Host")class CloudFormationVirtualHostTests {+ private static final String VIRTUAL_HOST = "cloudformation.us-east-1.amazonaws.com";+private static CloudFormationClient cfn;@BeforeAllstatic void setup() {- // We override the endpoint to simulate a virtual host style request for CloudFormation- URI endpoint = TestFixtures.endpoint();- URI cfnEndpoint = URI.create("http://cloudformation.us-east-1.amazonaws.com:" + endpoint.getPort());- if (endpoint.getHost().equals("localhost")) {- cfnEndpoint = URI.create("http://cloudformation.localhost:" + endpoint.getPort());- }-cfn = CloudFormationClient.builder()- .endpointOverride(cfnEndpoint)+ .endpointOverride(TestFixtures.endpoint()).region(software.amazon.awssdk.regions.Region.US_EAST_1).credentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider.create(software.amazon.awssdk.auth.credentials.AwsBasicCredentials.create("test", "test")))+ .overrideConfiguration(c -> c.addExecutionInterceptor(new HostHeaderSpoofInterceptor(VIRTUAL_HOST))).build();}@@ -50,4 +54,26 @@ class CloudFormationVirtualHostTests {ListStacksResponse resp = cfn.listStacks();assertThat(resp.sdkHttpResponse().isSuccessful()).isTrue();}++ /**+ * Rewrites the Host header on outbound requests so Floci sees the incoming+ * request as virtual-hosted under {@code hostHeader}, while the underlying+ * TCP connection still goes to the endpoint configured via+ * {@code endpointOverride}.+ */+ private static final class HostHeaderSpoofInterceptor implements ExecutionInterceptor {+ private final String hostHeader;++ HostHeaderSpoofInterceptor(String hostHeader) {+ this.hostHeader = hostHeader;+ }++ @Override+ public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context,+ ExecutionAttributes executionAttributes) {+ return context.httpRequest().toBuilder()+ .putHeader("Host", hostHeader)+ .build();+ }+ }}