|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 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.handlers; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.function.Supplier; |
| 23 | +import org.junit.Test; |
| 24 | +import software.amazon.awssdk.core.SdkRequest; |
| 25 | +import software.amazon.awssdk.core.SdkResponse; |
| 26 | +import software.amazon.awssdk.core.interceptor.Context; |
| 27 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 28 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 29 | +import software.amazon.awssdk.http.SdkHttpFullResponse; |
| 30 | +import software.amazon.awssdk.services.s3.model.EncodingType; |
| 31 | +import software.amazon.awssdk.services.s3.model.ListObjectsResponse; |
| 32 | +import software.amazon.awssdk.services.s3.model.ListObjectsV2Response; |
| 33 | +import software.amazon.awssdk.services.s3.model.S3Object; |
| 34 | + |
| 35 | +/** |
| 36 | + * Unit tests for {@link DecodeUrlEncodedResponseInterceptor}. |
| 37 | + * <p> |
| 38 | + * See <a |
| 39 | + * href="https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html">https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGET.html</a> |
| 40 | + * and <a |
| 41 | + * href="https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html">https://docs.aws.amazon.com/AmazonS3/latest/API/v2-RESTBucketGET.html</a> |
| 42 | + * for information on which parts of the response must are affected by the EncodingType member. |
| 43 | + */ |
| 44 | +public class DecodeUrlEncodedResponseInterceptorTest { |
| 45 | + private static final String TEST_URL_ENCODED = "foo+%3D+bar+baz+%CE%B1+%CE%B2+%F0%9F%98%8A"; |
| 46 | + |
| 47 | + // foo = bar baz α β 😊 |
| 48 | + private static final String TEST_URL_DECODED = "foo = bar baz α β \uD83D\uDE0A"; |
| 49 | + |
| 50 | + private static final DecodeUrlEncodedResponseInterceptor INTERCEPTOR = new DecodeUrlEncodedResponseInterceptor(); |
| 51 | + |
| 52 | + private static final List<S3Object> TEST_CONTENTS = Arrays.asList( |
| 53 | + S3Object.builder().key(TEST_URL_ENCODED).build(), |
| 54 | + S3Object.builder().key(TEST_URL_ENCODED).build(), |
| 55 | + S3Object.builder().key(TEST_URL_ENCODED).build() |
| 56 | + ); |
| 57 | + |
| 58 | + private static final ListObjectsResponse V1_TEST_ENCODED_RESPONSE = ListObjectsResponse.builder() |
| 59 | + .encodingType(EncodingType.URL) |
| 60 | + .delimiter(TEST_URL_ENCODED) |
| 61 | + .nextMarker(TEST_URL_ENCODED) |
| 62 | + .prefix(TEST_URL_ENCODED) |
| 63 | + .marker(TEST_URL_ENCODED) |
| 64 | + .contents(TEST_CONTENTS) |
| 65 | + .build(); |
| 66 | + |
| 67 | + private static final ListObjectsV2Response V2_TEST_ENCODED_RESPONSE = ListObjectsV2Response.builder() |
| 68 | + .encodingType(EncodingType.URL) |
| 69 | + .delimiter(TEST_URL_ENCODED) |
| 70 | + .prefix(TEST_URL_ENCODED) |
| 71 | + .startAfter(TEST_URL_ENCODED) |
| 72 | + .contents(TEST_CONTENTS) |
| 73 | + .build(); |
| 74 | + |
| 75 | + @Test |
| 76 | + public void encodingTypeSet_decodesListObjectsResponseParts() { |
| 77 | + Context.ModifyResponse ctx = newContext(V1_TEST_ENCODED_RESPONSE); |
| 78 | + |
| 79 | + ListObjectsResponse decoded = (ListObjectsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes()); |
| 80 | + |
| 81 | + assertDecoded(decoded::delimiter); |
| 82 | + assertDecoded(decoded::nextMarker); |
| 83 | + assertDecoded(decoded::prefix); |
| 84 | + assertDecoded(decoded::marker); |
| 85 | + assertKeysAreDecoded(decoded.contents()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void encodingTypeSet_decodesListObjectsV2ResponseParts() { |
| 90 | + Context.ModifyResponse ctx = newContext(V2_TEST_ENCODED_RESPONSE); |
| 91 | + |
| 92 | + ListObjectsV2Response decoded = (ListObjectsV2Response) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes()); |
| 93 | + |
| 94 | + assertDecoded(decoded::delimiter); |
| 95 | + assertDecoded(decoded::prefix); |
| 96 | + assertDecoded(decoded::startAfter); |
| 97 | + assertKeysAreDecoded(decoded.contents()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void encodingTypeNotSet_doesNotDecodeListObjectsResponseParts() { |
| 102 | + ListObjectsResponse original = V1_TEST_ENCODED_RESPONSE.toBuilder() |
| 103 | + .encodingType((String) null) |
| 104 | + .build(); |
| 105 | + |
| 106 | + Context.ModifyResponse ctx = newContext(original); |
| 107 | + |
| 108 | + ListObjectsResponse fromInterceptor = (ListObjectsResponse) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes()); |
| 109 | + |
| 110 | + assertThat(fromInterceptor).isEqualTo(original); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void encodingTypeNotSet_doesNotDecodeListObjectsV2ResponseParts() { |
| 115 | + ListObjectsV2Response original = V2_TEST_ENCODED_RESPONSE.toBuilder() |
| 116 | + .encodingType((String) null) |
| 117 | + .build(); |
| 118 | + |
| 119 | + Context.ModifyResponse ctx = newContext(original); |
| 120 | + |
| 121 | + ListObjectsV2Response fromInterceptor = (ListObjectsV2Response) INTERCEPTOR.modifyResponse(ctx, new ExecutionAttributes()); |
| 122 | + |
| 123 | + assertThat(fromInterceptor).isEqualTo(original); |
| 124 | + } |
| 125 | + |
| 126 | + private void assertKeysAreDecoded(List<S3Object> objects) { |
| 127 | + objects.forEach(o -> assertDecoded(o::key)); |
| 128 | + } |
| 129 | + |
| 130 | + private void assertDecoded(Supplier<String> supplier) { |
| 131 | + assertThat(supplier.get()).isEqualTo(TEST_URL_DECODED); |
| 132 | + } |
| 133 | + |
| 134 | + private static Context.ModifyResponse newContext(SdkResponse response) { |
| 135 | + return new Context.ModifyResponse() { |
| 136 | + @Override |
| 137 | + public SdkResponse response() { |
| 138 | + return response; |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public SdkHttpFullResponse httpResponse() { |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public SdkHttpFullRequest httpRequest() { |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public SdkRequest request() { |
| 153 | + return null; |
| 154 | + } |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments