|
| 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.protocolquery; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.Mockito.atLeast; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.verify; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Optional; |
| 28 | +import org.junit.jupiter.api.AfterEach; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.mockito.ArgumentCaptor; |
| 32 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 33 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
| 34 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 35 | +import software.amazon.awssdk.core.exception.SdkClientException; |
| 36 | +import software.amazon.awssdk.core.interceptor.Context; |
| 37 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 38 | +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; |
| 39 | +import software.amazon.awssdk.http.ContentStreamProvider; |
| 40 | +import software.amazon.awssdk.http.ExecutableHttpRequest; |
| 41 | +import software.amazon.awssdk.http.HttpExecuteRequest; |
| 42 | +import software.amazon.awssdk.http.SdkHttpClient; |
| 43 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 44 | +import software.amazon.awssdk.regions.Region; |
| 45 | +import software.amazon.awssdk.utils.IoUtils; |
| 46 | + |
| 47 | +public class MoveQueryParamsToBodyTest { |
| 48 | + private static final AwsCredentialsProvider CREDENTIALS = StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")); |
| 49 | + |
| 50 | + private SdkHttpClient mockHttpClient; |
| 51 | + |
| 52 | + private ProtocolQueryClient client; |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + public void setup() throws IOException { |
| 56 | + mockHttpClient = mock(SdkHttpClient.class); |
| 57 | + ExecutableHttpRequest mockRequest = mock(ExecutableHttpRequest.class); |
| 58 | + when(mockRequest.call()).thenThrow(new IOException("IO error!")); |
| 59 | + when(mockHttpClient.prepareRequest(any())).thenReturn(mockRequest); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterEach |
| 63 | + public void teardown() { |
| 64 | + if (client != null) { |
| 65 | + client.close(); |
| 66 | + } |
| 67 | + client = null; |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void customInterceptor_additionalQueryParamsAdded_paramsAlsoMovedToBody() throws IOException { |
| 72 | + client = ProtocolQueryClient.builder() |
| 73 | + .overrideConfiguration(o -> o.addExecutionInterceptor(new AdditionalQueryParamInterceptor())) |
| 74 | + .region(Region.US_WEST_2) |
| 75 | + .credentialsProvider(CREDENTIALS) |
| 76 | + .httpClient(mockHttpClient) |
| 77 | + .build(); |
| 78 | + |
| 79 | + ArgumentCaptor<HttpExecuteRequest> requestCaptor = ArgumentCaptor.forClass(HttpExecuteRequest.class); |
| 80 | + |
| 81 | + assertThatThrownBy(() -> client.membersInQueryParams(r -> r.stringQueryParam("hello"))) |
| 82 | + .isInstanceOf(SdkClientException.class) |
| 83 | + .hasMessageContaining("IO"); |
| 84 | + |
| 85 | + verify(mockHttpClient, atLeast(1)).prepareRequest(requestCaptor.capture()); |
| 86 | + |
| 87 | + ContentStreamProvider requestContent = requestCaptor.getValue().contentStreamProvider().get(); |
| 88 | + |
| 89 | + String contentString = IoUtils.toUtf8String(requestContent.newStream()); |
| 90 | + |
| 91 | + assertThat(contentString).contains("CustomParamName=CustomParamValue"); |
| 92 | + } |
| 93 | + |
| 94 | + private static class AdditionalQueryParamInterceptor implements ExecutionInterceptor { |
| 95 | + @Override |
| 96 | + public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) { |
| 97 | + return context.httpRequest().toBuilder() |
| 98 | + .putRawQueryParameter("CustomParamName", "CustomParamValue") |
| 99 | + .build(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments