|
| 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.http.auth.aws.internal.signer.util; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static software.amazon.awssdk.http.Header.CONTENT_LENGTH; |
| 20 | +import static software.amazon.awssdk.http.auth.aws.internal.signer.util.SignerConstant.X_AMZ_DECODED_CONTENT_LENGTH; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.util.stream.Stream; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.Arguments; |
| 28 | +import org.junit.jupiter.params.provider.MethodSource; |
| 29 | +import org.mockito.Mockito; |
| 30 | +import software.amazon.awssdk.http.ContentStreamProvider; |
| 31 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 32 | + |
| 33 | +public class SignerUtilsTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + void moveContentLength_decodedContentLengthPresent_shouldNotInvokeNewStream() { |
| 37 | + SdkHttpRequest.Builder request = SdkHttpRequest.builder() |
| 38 | + .appendHeader(X_AMZ_DECODED_CONTENT_LENGTH, "10") |
| 39 | + .appendHeader(CONTENT_LENGTH, "10"); |
| 40 | + |
| 41 | + ContentStreamProvider streamProvider = Mockito.mock(ContentStreamProvider.class); |
| 42 | + long contentLength = SignerUtils.moveContentLength(request, streamProvider); |
| 43 | + Mockito.verify(streamProvider, Mockito.never()).newStream(); |
| 44 | + assertThat(contentLength).isEqualTo(10L); |
| 45 | + assertThat(request.firstMatchingHeader(CONTENT_LENGTH)).isEmpty(); |
| 46 | + assertThat(request.firstMatchingHeader(X_AMZ_DECODED_CONTENT_LENGTH)).contains("10"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void moveContentLength_contentLengthPresent_shouldNotInvokeNewStream() { |
| 51 | + SdkHttpRequest.Builder request = SdkHttpRequest.builder() |
| 52 | + .appendHeader(CONTENT_LENGTH, "10"); |
| 53 | + |
| 54 | + ContentStreamProvider streamProvider = Mockito.mock(ContentStreamProvider.class); |
| 55 | + long contentLength = SignerUtils.moveContentLength(request, streamProvider); |
| 56 | + Mockito.verify(streamProvider, Mockito.never()).newStream(); |
| 57 | + assertThat(contentLength).isEqualTo(10L); |
| 58 | + assertThat(request.firstMatchingHeader(CONTENT_LENGTH)).isEmpty(); |
| 59 | + assertThat(request.firstMatchingHeader(X_AMZ_DECODED_CONTENT_LENGTH)).contains("10"); |
| 60 | + } |
| 61 | + |
| 62 | + public static Stream<Arguments> streams() { |
| 63 | + return Stream.of(Arguments.of(new ByteArrayInputStream("hello".getBytes()), 5), |
| 64 | + Arguments.of(null, 0)); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + @ParameterizedTest |
| 69 | + @MethodSource("streams") |
| 70 | + void moveContentLength_contentLengthNotPresent_shouldInvokeNewStream(InputStream inputStream, long expectedLength) { |
| 71 | + SdkHttpRequest.Builder request = SdkHttpRequest.builder(); |
| 72 | + |
| 73 | + ContentStreamProvider streamProvider = Mockito.mock(ContentStreamProvider.class); |
| 74 | + Mockito.when(streamProvider.newStream()).thenReturn(inputStream); |
| 75 | + |
| 76 | + long contentLength = SignerUtils.moveContentLength(request, streamProvider); |
| 77 | + Mockito.verify(streamProvider, Mockito.times(1)).newStream(); |
| 78 | + assertThat(contentLength).isEqualTo(expectedLength); |
| 79 | + assertThat(request.firstMatchingHeader(CONTENT_LENGTH)).isEmpty(); |
| 80 | + assertThat(request.firstMatchingHeader(X_AMZ_DECODED_CONTENT_LENGTH)).contains(String.valueOf(expectedLength)); |
| 81 | + } |
| 82 | +} |
0 commit comments