|
| 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.internal.handlers; |
| 17 | + |
| 18 | +import static java.util.Arrays.asList; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.MethodSource; |
| 29 | +import software.amazon.awssdk.core.SdkRequest; |
| 30 | +import software.amazon.awssdk.core.interceptor.Context; |
| 31 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 32 | +import software.amazon.awssdk.core.interceptor.SdkExecutionAttribute; |
| 33 | +import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest; |
| 34 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 35 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 36 | + |
| 37 | +public class ObjectMetadataInterceptorTest { |
| 38 | + private static final ObjectMetadataInterceptor INTERCEPTOR = new ObjectMetadataInterceptor(); |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | + public static List<TestCase> testCases() { |
| 43 | + return asList( |
| 44 | + tc(asList("a", "b", "c"), asList("a", "b", "c")), |
| 45 | + tc(asList(" a ", "b", "c"), asList("a", "b", "c")), |
| 46 | + tc(asList(" a", "\tb", "\tc"), asList("a", "b", "c")), |
| 47 | + tc(asList("a\n", "\tb", "\tc\r\n"), asList("a", "b", "c")) |
| 48 | + |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @ParameterizedTest |
| 53 | + @MethodSource("testCases") |
| 54 | + public void modifyRequest_putObject_metadataKeysAreTrimmed(TestCase tc) { |
| 55 | + Map<String, String> metadata = tc.inputKeys.stream() |
| 56 | + .collect(Collectors.toMap(k -> k, k -> "value")); |
| 57 | + |
| 58 | + Context.ModifyHttpRequest ctx = mock(Context.ModifyHttpRequest.class); |
| 59 | + |
| 60 | + PutObjectRequest put = PutObjectRequest.builder() |
| 61 | + .metadata(metadata) |
| 62 | + .build(); |
| 63 | + |
| 64 | + when(ctx.request()).thenReturn(put); |
| 65 | + |
| 66 | + ExecutionAttributes attrs = new ExecutionAttributes(); |
| 67 | + attrs.putAttribute(SdkExecutionAttribute.OPERATION_NAME, "PutObject"); |
| 68 | + |
| 69 | + PutObjectRequest modified = (PutObjectRequest) INTERCEPTOR.modifyRequest(ctx, attrs); |
| 70 | + |
| 71 | + assertThat(modified.metadata().keySet()).containsExactlyElementsOf(tc.expectedKeys); |
| 72 | + } |
| 73 | + |
| 74 | + @ParameterizedTest |
| 75 | + @MethodSource("testCases") |
| 76 | + public void modifyRequest_creatMultipartUpload_metadataKeysAreTrimmed(TestCase tc) { |
| 77 | + Map<String, String> metadata = tc.inputKeys.stream() |
| 78 | + .collect(Collectors.toMap(k -> k, k -> "value")); |
| 79 | + |
| 80 | + Context.ModifyHttpRequest ctx = mock(Context.ModifyHttpRequest.class); |
| 81 | + |
| 82 | + CreateMultipartUploadRequest mpu = CreateMultipartUploadRequest.builder() |
| 83 | + .metadata(metadata) |
| 84 | + .build(); |
| 85 | + |
| 86 | + when(ctx.request()).thenReturn(mpu); |
| 87 | + |
| 88 | + ExecutionAttributes attrs = new ExecutionAttributes(); |
| 89 | + attrs.putAttribute(SdkExecutionAttribute.OPERATION_NAME, "CreateMultipartUpload"); |
| 90 | + |
| 91 | + CreateMultipartUploadRequest modified = (CreateMultipartUploadRequest) INTERCEPTOR.modifyRequest(ctx, attrs); |
| 92 | + |
| 93 | + assertThat(modified.metadata().keySet()).containsExactlyElementsOf(tc.expectedKeys); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void modifyRequest_unknownOperation_ignores() { |
| 98 | + Context.ModifyHttpRequest ctx = mock(Context.ModifyHttpRequest.class); |
| 99 | + |
| 100 | + GetObjectRequest get = GetObjectRequest.builder().build(); |
| 101 | + |
| 102 | + when(ctx.request()).thenReturn(get); |
| 103 | + |
| 104 | + ExecutionAttributes attrs = new ExecutionAttributes(); |
| 105 | + attrs.putAttribute(SdkExecutionAttribute.OPERATION_NAME, "GetObject"); |
| 106 | + |
| 107 | + SdkRequest sdkRequest = INTERCEPTOR.modifyRequest(ctx, attrs); |
| 108 | + |
| 109 | + assertThat(sdkRequest).isEqualTo(get); |
| 110 | + } |
| 111 | + |
| 112 | + private static TestCase tc(List<String> input, List<String> expected) { |
| 113 | + return new TestCase(input, expected); |
| 114 | + } |
| 115 | + private static class TestCase { |
| 116 | + private List<String> inputKeys; |
| 117 | + private List<String> expectedKeys; |
| 118 | + |
| 119 | + public TestCase(List<String> inputKeys, List<String> expectedKeys) { |
| 120 | + this.inputKeys = inputKeys; |
| 121 | + this.expectedKeys = expectedKeys; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments