-
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 1 commit
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 CRT HTTP Client", | ||
| "contributor": "", | ||
| "description": "Add content-length header to the Request if the Request with Request body does not have one." | ||
| } |
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
120 changes: 120 additions & 0 deletions
120
.../aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtSyncWireMockTest.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,120 @@ | ||
| /* | ||
| * 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.http.crt; | ||
|
|
||
| import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.put; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; | ||
| import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; | ||
| import static java.util.Collections.emptyMap; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import com.github.tomakehurst.wiremock.WireMockServer; | ||
| import com.github.tomakehurst.wiremock.core.WireMockConfiguration; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.awssdk.crt.CrtResource; | ||
| import software.amazon.awssdk.crt.io.EventLoopGroup; | ||
| import software.amazon.awssdk.crt.io.HostResolver; | ||
| import software.amazon.awssdk.http.ExecutableHttpRequest; | ||
| import software.amazon.awssdk.http.HttpExecuteRequest; | ||
| import software.amazon.awssdk.http.HttpExecuteResponse; | ||
| import software.amazon.awssdk.http.SdkHttpClient; | ||
| import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
| import software.amazon.awssdk.http.SdkHttpMethod; | ||
| import software.amazon.awssdk.http.SdkHttpRequest; | ||
|
|
||
| public class AwsCrtSyncWireMockTest { | ||
| private final WireMockServer mockServer = new WireMockServer(new WireMockConfiguration() | ||
| .dynamicPort() | ||
| .dynamicHttpsPort()); | ||
| private SdkHttpClient client; | ||
|
|
||
| @BeforeEach | ||
| public void setup() { | ||
| mockServer.start(); | ||
| mockServer.stubFor(put(urlMatching(".*")).willReturn(aResponse().withStatus(200).withBody("hello"))); | ||
| client = AwsCrtHttpClient.builder().build(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void teardown() { | ||
| mockServer.stop(); | ||
| client.close(); | ||
| EventLoopGroup.closeStaticDefault(); | ||
| HostResolver.closeStaticDefault(); | ||
| CrtResource.waitForNoResources(); | ||
| } | ||
|
|
||
| @Test | ||
| public void sdkAddsContentLength_For_Requests_with_RequestBody() throws Throwable { | ||
| URI uri = URI.create("http://localhost:" + mockServer.port()); | ||
| String stringRequestBody = "Body"; | ||
| byte[] reqBody = stringRequestBody.getBytes(StandardCharsets.UTF_8); | ||
| String resourcePath = "/server/test"; | ||
|
|
||
| Map<String, String> params = emptyMap(); | ||
| SdkHttpRequest request = SdkHttpFullRequest.builder() | ||
| .uri(uri) | ||
| .method(SdkHttpMethod.PUT) | ||
| .contentStreamProvider(() -> new ByteArrayInputStream(reqBody)) | ||
| .encodedPath(resourcePath) | ||
| .applyMutation(b -> params.forEach(b::putRawQueryParameter)) | ||
| .applyMutation(b -> | ||
| b.putHeader("Host", uri.getHost())).build(); | ||
| HttpExecuteRequest.Builder executeRequestBuilder = HttpExecuteRequest.builder(); | ||
| executeRequestBuilder.request(request); | ||
| executeRequestBuilder.contentStreamProvider(() -> new ByteArrayInputStream(reqBody)); | ||
| ExecutableHttpRequest executableRequest = client.prepareRequest(executeRequestBuilder.build()); | ||
| HttpExecuteResponse response = executableRequest.call(); | ||
| assertThat(response.httpResponse().statusCode()).isEqualTo(200); | ||
| mockServer.verify(putRequestedFor(urlPathEqualTo(resourcePath)) | ||
| .withRequestBody(equalTo(stringRequestBody)) | ||
| .withHeader("content-length", | ||
| equalTo(String.valueOf(stringRequestBody.length())))); | ||
| } | ||
|
|
||
| @Test | ||
| public void sdkDoesNotAddsContentLength_For_Requests_with_NoRequestBody() throws Throwable { | ||
| URI uri = URI.create("http://localhost:" + mockServer.port()); | ||
| String stringRequestBody = "Body"; | ||
| String resourcePath = "/server/test"; | ||
|
|
||
| Map<String, String> params = emptyMap(); | ||
| SdkHttpRequest request = SdkHttpFullRequest.builder() | ||
| .uri(uri) | ||
| .method(SdkHttpMethod.PUT) | ||
| .encodedPath(resourcePath) | ||
| .applyMutation(b -> params.forEach(b::putRawQueryParameter)) | ||
| .applyMutation(b -> | ||
| b.putHeader("Host", uri.getHost())).build(); | ||
| HttpExecuteRequest.Builder executeRequestBuilder = HttpExecuteRequest.builder(); | ||
| executeRequestBuilder.request(request); | ||
| ExecutableHttpRequest executableRequest = client.prepareRequest(executeRequestBuilder.build()); | ||
| HttpExecuteResponse response = executableRequest.call(); | ||
| assertThat(response.httpResponse().statusCode()).isEqualTo(200); | ||
| mockServer.verify(putRequestedFor(urlPathEqualTo(resourcePath)) | ||
| .withoutHeader("content-length")); | ||
| } | ||
| } |
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.
Wont that buffer the whole body content in memory? Do we really want to do that?
Uh oh!
There was an error while loading. Please reload this page.
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.
Need to rework on this , due to internal comment from Matthew Miller to handle this in Marshallers. Will update the PR with that approach.