|
| 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.protocol.tests.contentlength; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 25 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 26 | +import static software.amazon.awssdk.http.Header.CONTENT_LENGTH; |
| 27 | + |
| 28 | +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; |
| 29 | +import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
| 30 | +import java.net.URI; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import software.amazon.awssdk.core.SdkBytes; |
| 34 | +import software.amazon.awssdk.core.interceptor.Context; |
| 35 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 36 | +import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; |
| 37 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 38 | +import software.amazon.awssdk.http.crt.AwsCrtHttpClient; |
| 39 | +import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; |
| 40 | +import software.amazon.awssdk.services.protocolrestjson.model.OperationWithExplicitPayloadStructureResponse; |
| 41 | +import software.amazon.awssdk.services.protocolrestjson.model.SimpleStruct; |
| 42 | +import software.amazon.awssdk.services.protocolrestxml.ProtocolRestXmlClient; |
| 43 | +import software.amazon.awssdk.services.protocolrestxml.model.OperationWithExplicitPayloadStringResponse; |
| 44 | + |
| 45 | +@WireMockTest |
| 46 | +public class MarshallersAddContentLengthTest { |
| 47 | + public static final String STRING_PAYLOAD = "TEST_STRING_PAYLOAD"; |
| 48 | + |
| 49 | + @Test |
| 50 | + void jsonMarshallers_AddContentLength_for_explicitBinaryPayload(WireMockRuntimeInfo wireMock) { |
| 51 | + stubSuccessfulResponse(); |
| 52 | + CaptureRequestInterceptor captureRequestInterceptor = new CaptureRequestInterceptor(); |
| 53 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 54 | + .httpClient(AwsCrtHttpClient.builder().build()) |
| 55 | + .overrideConfiguration(o -> o.addExecutionInterceptor(captureRequestInterceptor)) |
| 56 | + .endpointOverride(URI.create("http://localhost:" + wireMock.getHttpPort())) |
| 57 | + .build(); |
| 58 | + client.operationWithExplicitPayloadBlob(p -> p.payloadMember(SdkBytes.fromString(STRING_PAYLOAD, |
| 59 | + StandardCharsets.UTF_8))); |
| 60 | + verify(postRequestedFor(anyUrl()).withHeader(CONTENT_LENGTH, equalTo(String.valueOf(STRING_PAYLOAD.length())))); |
| 61 | + assertThat(captureRequestInterceptor.requestAfterMarshalling().firstMatchingHeader(CONTENT_LENGTH)) |
| 62 | + .contains(String.valueOf(STRING_PAYLOAD.length())); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void jsonMarshallers_AddContentLength_for_explicitStringPayload(WireMockRuntimeInfo wireMock) { |
| 67 | + stubSuccessfulResponse(); |
| 68 | + String expectedPayload = String.format("{\"StringMember\":\"%s\"}", STRING_PAYLOAD); |
| 69 | + CaptureRequestInterceptor captureRequestInterceptor = new CaptureRequestInterceptor(); |
| 70 | + ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() |
| 71 | + .httpClient(AwsCrtHttpClient.builder().build()) |
| 72 | + .overrideConfiguration(o -> o.addExecutionInterceptor(captureRequestInterceptor)) |
| 73 | + .endpointOverride(URI.create("http://localhost:" + wireMock.getHttpPort())) |
| 74 | + .build(); |
| 75 | + OperationWithExplicitPayloadStructureResponse response = |
| 76 | + client.operationWithExplicitPayloadStructure(p -> p.payloadMember(SimpleStruct.builder().stringMember(STRING_PAYLOAD).build())); |
| 77 | + verify(postRequestedFor(anyUrl()) |
| 78 | + .withRequestBody(equalTo(expectedPayload)) |
| 79 | + .withHeader(CONTENT_LENGTH, equalTo(String.valueOf(expectedPayload.length())))); |
| 80 | + assertThat(captureRequestInterceptor.requestAfterMarshalling().firstMatchingHeader(CONTENT_LENGTH)) |
| 81 | + .contains(String.valueOf(expectedPayload.length())); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void xmlMarshallers_AddContentLength_for_explicitBinaryPayload(WireMockRuntimeInfo wireMock) { |
| 86 | + stubSuccessfulResponse(); |
| 87 | + CaptureRequestInterceptor captureRequestInterceptor = new CaptureRequestInterceptor(); |
| 88 | + ProtocolRestXmlClient client = ProtocolRestXmlClient.builder() |
| 89 | + .httpClient(AwsCrtHttpClient.builder().build()) |
| 90 | + .overrideConfiguration(o -> o.addExecutionInterceptor(captureRequestInterceptor)) |
| 91 | + .endpointOverride(URI.create("http://localhost:" + wireMock.getHttpPort())) |
| 92 | + .build(); |
| 93 | + client.operationWithExplicitPayloadBlob(r -> r.payloadMember(SdkBytes.fromString(STRING_PAYLOAD, |
| 94 | + StandardCharsets.UTF_8))); |
| 95 | + verify(postRequestedFor(anyUrl()).withRequestBody(equalTo(STRING_PAYLOAD)) |
| 96 | + .withHeader(CONTENT_LENGTH, equalTo(String.valueOf(STRING_PAYLOAD.length())))); |
| 97 | + assertThat(captureRequestInterceptor.requestAfterMarshalling().firstMatchingHeader(CONTENT_LENGTH)) |
| 98 | + .contains(String.valueOf(STRING_PAYLOAD.length())); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void xmlMarshallers_AddContentLength_for_explicitStringPayload(WireMockRuntimeInfo wireMock) { |
| 103 | + stubSuccessfulResponse(); |
| 104 | + String expectedPayload = STRING_PAYLOAD; |
| 105 | + CaptureRequestInterceptor captureRequestInterceptor = new CaptureRequestInterceptor(); |
| 106 | + ProtocolRestXmlClient client = ProtocolRestXmlClient.builder() |
| 107 | + .httpClient(AwsCrtHttpClient.builder().build()) |
| 108 | + .overrideConfiguration(o -> o.addExecutionInterceptor(captureRequestInterceptor)) |
| 109 | + .endpointOverride(URI.create("http://localhost:" + wireMock.getHttpPort())) |
| 110 | + .build(); |
| 111 | + OperationWithExplicitPayloadStringResponse stringResponse = |
| 112 | + client.operationWithExplicitPayloadString(p -> p.payloadMember(STRING_PAYLOAD)); |
| 113 | + verify(postRequestedFor(anyUrl()) |
| 114 | + .withRequestBody(equalTo(expectedPayload)) |
| 115 | + .withHeader(CONTENT_LENGTH, equalTo(String.valueOf(expectedPayload.length())))); |
| 116 | + assertThat(captureRequestInterceptor.requestAfterMarshalling().firstMatchingHeader(CONTENT_LENGTH)) |
| 117 | + .contains(String.valueOf(expectedPayload.length())); |
| 118 | + } |
| 119 | + |
| 120 | + private void stubSuccessfulResponse() { |
| 121 | + stubFor(post(anyUrl()).willReturn(aResponse().withStatus(200))); |
| 122 | + } |
| 123 | + |
| 124 | + private static class CaptureRequestInterceptor implements ExecutionInterceptor { |
| 125 | + private SdkHttpRequest requestAfterMarshilling; |
| 126 | + |
| 127 | + public SdkHttpRequest requestAfterMarshalling() { |
| 128 | + return requestAfterMarshilling; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void afterMarshalling(Context.AfterMarshalling context, ExecutionAttributes executionAttributes) { |
| 133 | + this.requestAfterMarshilling = context.httpRequest(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments