Skip to content

Commit a0bd796

Browse files
authored
Enable UrlConnection http client chunked encoding (#4207)
* Fix chunked-encoding in UrlConnection client * Verify payload in test
1 parent a644942 commit a0bd796

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

http-clients/url-connection-client/src/main/java/software/amazon/awssdk/http/urlconnection/UrlConnectionHttpClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ private HttpURLConnection createAndConfigureConnection(HttpExecuteRequest reques
154154
HttpURLConnection connection = connectionFactory.createConnection(sdkHttpRequest.getUri());
155155
sdkHttpRequest.forEachHeader((key, values) -> values.forEach(value -> connection.setRequestProperty(key, value)));
156156

157+
// connection.setRequestProperty("Transfer-Encoding", "chunked") does not work, i.e., property does not get set
158+
if (sdkHttpRequest.matchingHeaders("Transfer-Encoding").contains("chunked")) {
159+
connection.setChunkedStreamingMode(-1);
160+
}
161+
157162
if (!sdkHttpRequest.firstMatchingHeader(ACCEPT).isPresent()) {
158163
// Override Accept header because the default one in JDK does not comply with RFC 7231
159164
// See: https://bugs.openjdk.org/browse/JDK-8163921

http-clients/url-connection-client/src/test/java/software/amazon/awssdk/http/urlconnection/UrlConnectionHttpClientWireMockTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
1919
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2020
import static software.amazon.awssdk.http.Header.ACCEPT;
21+
import static software.amazon.awssdk.http.Header.CHUNKED;
22+
import static software.amazon.awssdk.http.Header.TRANSFER_ENCODING;
2123
import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES;
2224

2325
import java.io.IOException;
@@ -92,6 +94,25 @@ public void hasAcceptHeader_shouldNotOverride() throws IOException {
9294
mockServer.verify(postRequestedFor(urlPathEqualTo("/")).withHeader(ACCEPT, equalTo("text/html")));
9395
}
9496

97+
@Test
98+
public void hasTransferEncodingHeader_shouldBeSet() throws IOException {
99+
SdkHttpClient client = createSdkHttpClient();
100+
101+
stubForMockRequest(200);
102+
103+
SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), SdkHttpMethod.POST);
104+
req = req.toBuilder().putHeader(TRANSFER_ENCODING, CHUNKED).build();
105+
HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder()
106+
.request(req)
107+
.contentStreamProvider(req.contentStreamProvider()
108+
.orElse(null))
109+
.build())
110+
.call();
111+
112+
mockServer.verify(postRequestedFor(urlPathEqualTo("/")).withHeader(TRANSFER_ENCODING, equalTo(CHUNKED)));
113+
mockServer.verify(postRequestedFor(urlPathEqualTo("/")).withRequestBody(equalTo("Body")));
114+
}
115+
95116

96117
@After
97118
public void reset() {

0 commit comments

Comments
 (0)