Skip to content

Commit 00cd5ab

Browse files
committed
Handle header lists in URLConnection client
1 parent 4a81f88 commit 00cd5ab

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public String clientName() {
152152
private HttpURLConnection createAndConfigureConnection(HttpExecuteRequest request) {
153153
SdkHttpRequest sdkHttpRequest = request.httpRequest();
154154
HttpURLConnection connection = connectionFactory.createConnection(sdkHttpRequest.getUri());
155-
sdkHttpRequest.forEachHeader((key, values) -> values.forEach(value -> connection.setRequestProperty(key, value)));
155+
setHeaders(connection, sdkHttpRequest.headers());
156156

157157
// connection.setRequestProperty("Transfer-Encoding", "chunked") does not work, i.e., property does not get set
158158
if (sdkHttpRequest.matchingHeaders("Transfer-Encoding").contains("chunked")) {
@@ -180,6 +180,13 @@ private HttpURLConnection createAndConfigureConnection(HttpExecuteRequest reques
180180
return connection;
181181
}
182182

183+
private void setHeaders(HttpURLConnection connection, Map<String, List<String>> headers) {
184+
headers.forEach((name, values) -> {
185+
String commaSeparated = String.join(",", values);
186+
connection.addRequestProperty(name, commaSeparated);
187+
});
188+
}
189+
183190
private HttpURLConnection createDefaultConnection(URI uri, SSLSocketFactory socketFactory) {
184191

185192
Optional<Proxy> proxy = determineProxy(uri);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.http.urlconnection;
17+
18+
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
19+
import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
21+
22+
import com.github.tomakehurst.wiremock.WireMockServer;
23+
import java.io.ByteArrayInputStream;
24+
import java.io.IOException;
25+
import java.util.Arrays;
26+
import org.junit.jupiter.api.AfterAll;
27+
import org.junit.jupiter.api.BeforeAll;
28+
import org.junit.jupiter.api.Test;
29+
import software.amazon.awssdk.http.ContentStreamProvider;
30+
import software.amazon.awssdk.http.HttpExecuteRequest;
31+
import software.amazon.awssdk.http.SdkHttpClient;
32+
import software.amazon.awssdk.http.SdkHttpFullRequest;
33+
import software.amazon.awssdk.http.SdkHttpMethod;
34+
35+
public class HeadersListTest {
36+
private static final WireMockServer WIRE_MOCK = new WireMockServer(0);
37+
private static SdkHttpClient client;
38+
39+
@BeforeAll
40+
public static void setup() {
41+
WIRE_MOCK.start();
42+
client = UrlConnectionHttpClient.create();
43+
}
44+
45+
@AfterAll
46+
public static void teardown() {
47+
client.close();
48+
WIRE_MOCK.stop();
49+
}
50+
51+
@Test
52+
public void execute_requestHeaderHasMultipleValues_allValuesSent() throws IOException {
53+
ContentStreamProvider provider = () -> new ByteArrayInputStream(new byte[0]);
54+
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder()
55+
.method(SdkHttpMethod.PUT)
56+
.host("localhost")
57+
.port(WIRE_MOCK.port())
58+
.protocol("http")
59+
.putHeader("my-header", Arrays.asList("value1", "value2"))
60+
.encodedPath("/test")
61+
.build();
62+
63+
HttpExecuteRequest request = HttpExecuteRequest.builder()
64+
.request(httpRequest)
65+
.contentStreamProvider(provider)
66+
.build();
67+
68+
client.prepareRequest(request).call();
69+
70+
WIRE_MOCK.verify(putRequestedFor(urlEqualTo("/test"))
71+
.withHeader("my-header", equalTo("value1,value2")));
72+
}
73+
}

0 commit comments

Comments
 (0)