-
Notifications
You must be signed in to change notification settings - Fork 1k
Add content header if the Request doesnot has one for CRT sync Http clients #4920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1df0e9e
Add content header if the Request doesnot has one for CRT sync Http c…
joviegas 1f4a2da
Content length updated in the Marshallers
joviegas a7aba17
Content length updation done in the Marshallers after internal comment
joviegas 177717b
Handled Matts comments on PR
joviegas abf8871
Updated MarshallersAddContentLengthTest to Junit5
joviegas 0139c3f
Merge branch 'master' into joviegas/crt_sync_client
joviegas 0088a14
Merge branch 'master' into joviegas/crt_sync_client
joviegas 76e3839
Updated to initiate theCheckbuilds
joviegas 1380516
Handle sonar raised issues
joviegas c69cad7
Merge branch 'master' into joviegas/crt_sync_client
joviegas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "bugfix", | ||
| "category": "AWS SDK for Java v2", | ||
| "contributor": "", | ||
| "description": "Add content-length header in Json and Xml Protocol Marshaller for String and Binary explicit Payloads. " | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
.../software/amazon/awssdk/protocol/tests/contentlength/MarshallersAddContentLengthTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.protocol.tests.contentlength; | ||
|
|
||
| import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.post; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.verify; | ||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
| import static software.amazon.awssdk.http.Header.CONTENT_LENGTH; | ||
|
|
||
| import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import software.amazon.awssdk.core.SdkBytes; | ||
| import software.amazon.awssdk.core.interceptor.Context; | ||
| import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||
| import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; | ||
| import software.amazon.awssdk.http.SdkHttpRequest; | ||
| import software.amazon.awssdk.http.crt.AwsCrtHttpClient; | ||
| import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient; | ||
| import software.amazon.awssdk.services.protocolrestjson.model.OperationWithExplicitPayloadStructureResponse; | ||
| import software.amazon.awssdk.services.protocolrestjson.model.SimpleStruct; | ||
| import software.amazon.awssdk.services.protocolrestxml.ProtocolRestXmlClient; | ||
| import software.amazon.awssdk.services.protocolrestxml.model.OperationWithExplicitPayloadStringResponse; | ||
|
|
||
| public class MarshallersAddContentLengthTest { | ||
| public static final String STRING_PAYLOAD = "TEST_STRING_PAYLOAD"; | ||
| @Rule | ||
| public WireMockRule wireMock = new WireMockRule(0); | ||
|
|
||
| @Test | ||
| public void jsonMarshallers_AddContentLength_for_explicitBinaryPayload() { | ||
| stubSuccessfulResponse(); | ||
| CaptureRequestInterceptor captureRequestInterceptor = new CaptureRequestInterceptor(); | ||
| ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() | ||
| .httpClient(AwsCrtHttpClient.builder().build()) | ||
| .overrideConfiguration(o -> o.addExecutionInterceptor(captureRequestInterceptor)) | ||
| .endpointOverride(URI.create("http://localhost:" + wireMock.port())) | ||
| .build(); | ||
|
|
||
| client.operationWithExplicitPayloadBlob(p -> p.payloadMember(SdkBytes.fromString(STRING_PAYLOAD, | ||
| StandardCharsets.UTF_8))); | ||
| verify(postRequestedFor(anyUrl()).withHeader(CONTENT_LENGTH, equalTo(String.valueOf(STRING_PAYLOAD.length())))); | ||
| assertThat(captureRequestInterceptor.requestAfterMarshalling().firstMatchingHeader(CONTENT_LENGTH)) | ||
| .contains(String.valueOf(STRING_PAYLOAD.length())); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void jsonMarshallers_AddContentLength_for_explicitStringPayload() { | ||
| stubSuccessfulResponse(); | ||
| String expectedPayload = String.format("{\"StringMember\":\"%s\"}", STRING_PAYLOAD); | ||
| CaptureRequestInterceptor captureRequestInterceptor = new CaptureRequestInterceptor(); | ||
| ProtocolRestJsonClient client = ProtocolRestJsonClient.builder() | ||
| .httpClient(AwsCrtHttpClient.builder().build()) | ||
| .overrideConfiguration(o -> o.addExecutionInterceptor(captureRequestInterceptor)) | ||
| .endpointOverride(URI.create("http://localhost:" + wireMock.port())) | ||
| .build(); | ||
|
|
||
| OperationWithExplicitPayloadStructureResponse response = | ||
| client.operationWithExplicitPayloadStructure(p -> p.payloadMember(SimpleStruct.builder().stringMember(STRING_PAYLOAD).build())); | ||
| verify(postRequestedFor(anyUrl()) | ||
| .withRequestBody(equalTo(expectedPayload)) | ||
| .withHeader(CONTENT_LENGTH, equalTo(String.valueOf(expectedPayload.length())))); | ||
| assertThat(captureRequestInterceptor.requestAfterMarshalling().firstMatchingHeader(CONTENT_LENGTH)) | ||
| .contains(String.valueOf(expectedPayload.length())); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void xmlMarshallers_AddContentLength_for_explicitBinaryPayload() { | ||
| stubSuccessfulResponse(); | ||
| CaptureRequestInterceptor captureRequestInterceptor = new CaptureRequestInterceptor(); | ||
| ProtocolRestXmlClient client = ProtocolRestXmlClient.builder() | ||
| .httpClient(AwsCrtHttpClient.builder().build()) | ||
| .overrideConfiguration(o -> o.addExecutionInterceptor(captureRequestInterceptor)) | ||
| .endpointOverride(URI.create("http://localhost:" + wireMock.port())) | ||
| .build(); | ||
| client.operationWithExplicitPayloadBlob(r -> r.payloadMember(SdkBytes.fromString(STRING_PAYLOAD, | ||
| StandardCharsets.UTF_8))); | ||
|
|
||
| verify(postRequestedFor(anyUrl()) | ||
| .withRequestBody(equalTo(STRING_PAYLOAD)) | ||
|
|
||
| .withHeader(CONTENT_LENGTH, equalTo(String.valueOf(STRING_PAYLOAD.length())))); | ||
| assertThat(captureRequestInterceptor.requestAfterMarshalling().firstMatchingHeader(CONTENT_LENGTH)) | ||
| .contains(String.valueOf(STRING_PAYLOAD.length())); | ||
| } | ||
|
|
||
| @Test | ||
| public void xmlMarshallers_AddContentLength_for_explicitStringPayload() { | ||
| stubSuccessfulResponse(); | ||
| String expectedPayload = STRING_PAYLOAD; | ||
| CaptureRequestInterceptor captureRequestInterceptor = new CaptureRequestInterceptor(); | ||
| ProtocolRestXmlClient client = ProtocolRestXmlClient.builder() | ||
| .httpClient(AwsCrtHttpClient.builder().build()) | ||
| .overrideConfiguration(o -> o.addExecutionInterceptor(captureRequestInterceptor)) | ||
| .endpointOverride(URI.create("http://localhost:" + wireMock.port())) | ||
| .build(); | ||
| OperationWithExplicitPayloadStringResponse stringResponse = | ||
| client.operationWithExplicitPayloadString(p -> p.payloadMember(STRING_PAYLOAD)); | ||
| verify(postRequestedFor(anyUrl()) | ||
| .withRequestBody(equalTo(expectedPayload)) | ||
| .withHeader(CONTENT_LENGTH, equalTo(String.valueOf(expectedPayload.length())))); | ||
| assertThat(captureRequestInterceptor.requestAfterMarshalling().firstMatchingHeader(CONTENT_LENGTH)) | ||
| .contains(String.valueOf(expectedPayload.length())); | ||
|
|
||
| } | ||
|
|
||
| private void stubSuccessfulResponse() { | ||
| stubFor(post(anyUrl()).willReturn(aResponse().withStatus(200))); | ||
| } | ||
|
|
||
| private static class CaptureRequestInterceptor implements ExecutionInterceptor { | ||
| private SdkHttpRequest requestAfterMarshilling; | ||
|
|
||
| public SdkHttpRequest requestAfterMarshalling() { | ||
| return requestAfterMarshilling; | ||
| } | ||
|
|
||
| @Override | ||
| public void afterMarshalling(Context.AfterMarshalling context, ExecutionAttributes executionAttributes) { | ||
| this.requestAfterMarshilling = context.httpRequest(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use JUnit 5 with
@WireMockTestinstead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done