|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). | 
|  | 5 | + * You may not use this file except in compliance with the License. | 
|  | 6 | + * A copy of the License is located at | 
|  | 7 | + * | 
|  | 8 | + *  http://aws.amazon.com/apache2.0 | 
|  | 9 | + * | 
|  | 10 | + * or in the "license" file accompanying this file. This file is distributed | 
|  | 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | 
|  | 12 | + * express or implied. See the License for the specific language governing | 
|  | 13 | + * permissions and limitations under the License. | 
|  | 14 | + */ | 
|  | 15 | + | 
|  | 16 | +package software.amazon.awssdk.services.s3.s3express; | 
|  | 17 | + | 
|  | 18 | +import static org.assertj.core.api.Assertions.assertThat; | 
|  | 19 | + | 
|  | 20 | +import java.util.List; | 
|  | 21 | +import java.util.function.UnaryOperator; | 
|  | 22 | +import org.junit.jupiter.api.BeforeEach; | 
|  | 23 | +import org.junit.jupiter.api.Test; | 
|  | 24 | +import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; | 
|  | 25 | +import software.amazon.awssdk.core.sync.RequestBody; | 
|  | 26 | +import software.amazon.awssdk.core.sync.ResponseTransformer; | 
|  | 27 | +import software.amazon.awssdk.http.AbortableInputStream; | 
|  | 28 | +import software.amazon.awssdk.http.HttpExecuteResponse; | 
|  | 29 | +import software.amazon.awssdk.http.SdkHttpRequest; | 
|  | 30 | +import software.amazon.awssdk.http.SdkHttpResponse; | 
|  | 31 | +import software.amazon.awssdk.regions.Region; | 
|  | 32 | +import software.amazon.awssdk.services.s3.S3Client; | 
|  | 33 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; | 
|  | 34 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; | 
|  | 35 | +import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient; | 
|  | 36 | +import software.amazon.awssdk.utils.StringInputStream; | 
|  | 37 | + | 
|  | 38 | +/** | 
|  | 39 | + * Unit test to verify that S3 Express operations include the correct business metric feature ID | 
|  | 40 | + * in the User-Agent header. | 
|  | 41 | + */ | 
|  | 42 | +public class S3ExpressUserAgentTest { | 
|  | 43 | +    private static final String KEY = "test-feature-id.txt"; | 
|  | 44 | +    private static final String CONTENTS = "test content for feature id validation"; | 
|  | 45 | +    private static final String S3_EXPRESS_BUCKET = "my-test-bucket--use1-az4--x-s3"; | 
|  | 46 | +    private static final String REGULAR_BUCKET = "my-test-bucket-regular"; | 
|  | 47 | + | 
|  | 48 | +    public static final UnaryOperator<String> METRIC_SEARCH_PATTERN =  | 
|  | 49 | +        metric -> ".*m/[a-zA-Z0-9+-,]*" + metric + ".*"; | 
|  | 50 | +     | 
|  | 51 | +    private MockSyncHttpClient mockHttpClient; | 
|  | 52 | +    private S3Client s3Client; | 
|  | 53 | + | 
|  | 54 | +    @BeforeEach | 
|  | 55 | +    void setup() { | 
|  | 56 | +        // Mock HTTP client | 
|  | 57 | +        mockHttpClient = new MockSyncHttpClient(); | 
|  | 58 | +         | 
|  | 59 | +        // Mock CreateSession response for S3 Express authentication | 
|  | 60 | +        String createSessionResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | 
|  | 61 | +                "<CreateSessionResult>\n" + | 
|  | 62 | +                "    <Credentials>\n" + | 
|  | 63 | +                "        <SessionToken>mock-session-token</SessionToken>\n" + | 
|  | 64 | +                "        <SecretAccessKey>mock-secret-key</SecretAccessKey>\n" + | 
|  | 65 | +                "        <AccessKeyId>mock-access-key</AccessKeyId>\n" + | 
|  | 66 | +                "        <Expiration>2025-12-31T23:59:59Z</Expiration>\n" + | 
|  | 67 | +                "    </Credentials>\n" + | 
|  | 68 | +                "</CreateSessionResult>"; | 
|  | 69 | +         | 
|  | 70 | +        HttpExecuteResponse createSessionHttpResponse = HttpExecuteResponse.builder() | 
|  | 71 | +                .response(SdkHttpResponse.builder().statusCode(200).build()) | 
|  | 72 | +                .responseBody(AbortableInputStream.create(new StringInputStream(createSessionResponse))) | 
|  | 73 | +                .build(); | 
|  | 74 | + | 
|  | 75 | +        HttpExecuteResponse putResponse = HttpExecuteResponse.builder() | 
|  | 76 | +                .response(SdkHttpResponse.builder().statusCode(200).build()) | 
|  | 77 | +                .responseBody(AbortableInputStream.create(new StringInputStream(""))) | 
|  | 78 | +                .build(); | 
|  | 79 | +         | 
|  | 80 | +        HttpExecuteResponse getResponse = HttpExecuteResponse.builder() | 
|  | 81 | +                .response(SdkHttpResponse.builder().statusCode(200).build()) | 
|  | 82 | +                .responseBody(AbortableInputStream.create(new StringInputStream(CONTENTS))) | 
|  | 83 | +                .build(); | 
|  | 84 | + | 
|  | 85 | +        mockHttpClient.stubResponses( | 
|  | 86 | +                createSessionHttpResponse, // First CreateSession call for S3 Express bucket | 
|  | 87 | +                putResponse,               // PUT operation | 
|  | 88 | +                createSessionHttpResponse, // Second CreateSession call for S3 Express bucket   | 
|  | 89 | +                getResponse,               // GET operation | 
|  | 90 | +                putResponse,               // PUT operation for regular bucket | 
|  | 91 | +                getResponse                // GET operation for regular bucket | 
|  | 92 | +        ); | 
|  | 93 | +         | 
|  | 94 | +        // S3 client with mocked HTTP client | 
|  | 95 | +        s3Client = S3Client.builder() | 
|  | 96 | +                .region(Region.US_EAST_1) | 
|  | 97 | +                .credentialsProvider(AnonymousCredentialsProvider.create()) | 
|  | 98 | +                .httpClient(mockHttpClient) | 
|  | 99 | +                .build(); | 
|  | 100 | +    } | 
|  | 101 | + | 
|  | 102 | +    @Test | 
|  | 103 | +    void putObject_whenS3ExpressBucket_shouldIncludeS3ExpressFeatureIdInUserAgent() { | 
|  | 104 | +        PutObjectRequest putRequest = PutObjectRequest.builder() | 
|  | 105 | +                .bucket(S3_EXPRESS_BUCKET) | 
|  | 106 | +                .key(KEY) | 
|  | 107 | +                .build(); | 
|  | 108 | + | 
|  | 109 | +        s3Client.putObject(putRequest, RequestBody.fromString(CONTENTS)); | 
|  | 110 | + | 
|  | 111 | +        SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); | 
|  | 112 | +        assertThat(lastRequest).isNotNull(); | 
|  | 113 | + | 
|  | 114 | +        List<String> userAgentHeaders = lastRequest.headers().get("User-Agent"); | 
|  | 115 | +        assertThat(userAgentHeaders).isNotNull().hasSize(1); | 
|  | 116 | + | 
|  | 117 | +        assertThat(userAgentHeaders.get(0)).matches(METRIC_SEARCH_PATTERN.apply("J")); | 
|  | 118 | +    } | 
|  | 119 | + | 
|  | 120 | +    @Test | 
|  | 121 | +    void getObject_whenS3ExpressBucket_shouldIncludeS3ExpressFeatureIdInUserAgent() { | 
|  | 122 | +        GetObjectRequest getRequest = GetObjectRequest.builder() | 
|  | 123 | +                .bucket(S3_EXPRESS_BUCKET) | 
|  | 124 | +                .key(KEY) | 
|  | 125 | +                .build(); | 
|  | 126 | + | 
|  | 127 | +        s3Client.getObject(getRequest, ResponseTransformer.toBytes()); | 
|  | 128 | + | 
|  | 129 | +        SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); | 
|  | 130 | +        assertThat(lastRequest).isNotNull(); | 
|  | 131 | + | 
|  | 132 | +        List<String> userAgentHeaders = lastRequest.headers().get("User-Agent"); | 
|  | 133 | +        assertThat(userAgentHeaders).isNotNull().hasSize(1); | 
|  | 134 | + | 
|  | 135 | +        assertThat(userAgentHeaders.get(0)).matches(METRIC_SEARCH_PATTERN.apply("J")); | 
|  | 136 | +    } | 
|  | 137 | + | 
|  | 138 | +    @Test | 
|  | 139 | +    void putObject_whenRegularS3Bucket_shouldNotIncludeS3ExpressFeatureIdInUserAgent() { | 
|  | 140 | +        PutObjectRequest putRequest = PutObjectRequest.builder() | 
|  | 141 | +                .bucket(REGULAR_BUCKET) | 
|  | 142 | +                .key(KEY) | 
|  | 143 | +                .build(); | 
|  | 144 | + | 
|  | 145 | +        s3Client.putObject(putRequest, RequestBody.fromString(CONTENTS)); | 
|  | 146 | + | 
|  | 147 | +        SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); | 
|  | 148 | +        assertThat(lastRequest).isNotNull(); | 
|  | 149 | + | 
|  | 150 | +        List<String> userAgentHeaders = lastRequest.headers().get("User-Agent"); | 
|  | 151 | +        assertThat(userAgentHeaders).isNotNull().hasSize(1); | 
|  | 152 | + | 
|  | 153 | +        assertThat(userAgentHeaders.get(0)).doesNotMatch(METRIC_SEARCH_PATTERN.apply("J")); | 
|  | 154 | +    } | 
|  | 155 | + | 
|  | 156 | +    @Test | 
|  | 157 | +    void getObject_whenRegularS3Bucket_shouldNotIncludeS3ExpressFeatureIdInUserAgent() { | 
|  | 158 | +        GetObjectRequest getRequest = GetObjectRequest.builder() | 
|  | 159 | +                .bucket(REGULAR_BUCKET) | 
|  | 160 | +                .key(KEY) | 
|  | 161 | +                .build(); | 
|  | 162 | + | 
|  | 163 | +        s3Client.getObject(getRequest, ResponseTransformer.toBytes()); | 
|  | 164 | + | 
|  | 165 | +        SdkHttpRequest lastRequest = mockHttpClient.getLastRequest(); | 
|  | 166 | +        assertThat(lastRequest).isNotNull(); | 
|  | 167 | + | 
|  | 168 | +        List<String> userAgentHeaders = lastRequest.headers().get("User-Agent"); | 
|  | 169 | +        assertThat(userAgentHeaders).isNotNull().hasSize(1); | 
|  | 170 | + | 
|  | 171 | +        assertThat(userAgentHeaders.get(0)).doesNotMatch(METRIC_SEARCH_PATTERN.apply("J")); | 
|  | 172 | +    } | 
|  | 173 | + | 
|  | 174 | +} | 
0 commit comments