Skip to content

Commit 17cb578

Browse files
a-k-gadriancole
authored andcommitted
Lower case headers in response and use TreeMap to allow case insensitive access (OpenFeign#418)
1 parent 968f35f commit 17cb578

4 files changed

Lines changed: 66 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
* Previously the OkhttpClient would throw an exception, and ApacheHttpClient
99
would report a wrong, possibly negative value
1010
* Adds support for encoded query parameters in `@QueryMap` via `@QueryMap(encoded = true)`
11+
* Keys in `Response.headers` are now lower-cased. This map is now case-insensitive with regards to keys,
12+
and iterates in lexicographic order.
13+
* This is a step towards supporting http2, as header names in http1 are treated as case-insensitive
14+
and http2 down-cases header names.
1115

1216
### Version 8.17
1317
* Adds support to RxJava Completable via `HystrixFeign` builder with fallback support

‎core/src/main/java/feign/Response.java‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import java.nio.charset.Charset;
2525
import java.util.Collection;
2626
import java.util.Collections;
27-
import java.util.LinkedHashMap;
27+
import java.util.LinkedList;
28+
import java.util.Locale;
2829
import java.util.Map;
30+
import java.util.TreeMap;
2931

3032
import static feign.Util.UTF_8;
3133
import static feign.Util.checkNotNull;
@@ -47,10 +49,7 @@ private Response(int status, String reason, Map<String, Collection<String>> head
4749
checkState(status >= 200, "Invalid status code: %s", status);
4850
this.status = status;
4951
this.reason = reason; //nullable
50-
LinkedHashMap<String, Collection<String>> copyOf =
51-
new LinkedHashMap<String, Collection<String>>();
52-
copyOf.putAll(checkNotNull(headers, "headers"));
53-
this.headers = Collections.unmodifiableMap(copyOf);
52+
this.headers = Collections.unmodifiableMap(caseInsensitiveCopyOf(headers));
5453
this.body = body; //nullable
5554
}
5655

@@ -92,6 +91,9 @@ public String reason() {
9291
return reason;
9392
}
9493

94+
/**
95+
* Returns a case-insensitive mapping of header names to their values.
96+
*/
9597
public Map<String, Collection<String>> headers() {
9698
return headers;
9799
}
@@ -242,4 +244,17 @@ public String toString() {
242244
return decodeOrDefault(data, UTF_8, "Binary data");
243245
}
244246
}
247+
248+
private static Map<String, Collection<String>> caseInsensitiveCopyOf(Map<String, Collection<String>> headers) {
249+
Map<String, Collection<String>> result = new TreeMap<String, Collection<String>>(String.CASE_INSENSITIVE_ORDER);
250+
251+
for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
252+
String headerName = entry.getKey();
253+
if (!result.containsKey(headerName)) {
254+
result.put(headerName.toLowerCase(Locale.ROOT), new LinkedList<String>());
255+
}
256+
result.get(headerName).addAll(entry.getValue());
257+
}
258+
return result;
259+
}
245260
}

‎core/src/test/java/feign/LoggerTest.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static Iterable<Object[]> data() {
8181
"\\[SendsStuff#login\\] Content-Length: 80",
8282
"\\[SendsStuff#login\\] ---> END HTTP \\(80-byte body\\)",
8383
"\\[SendsStuff#login\\] <--- HTTP/1.1 200 OK \\([0-9]+ms\\)",
84-
"\\[SendsStuff#login\\] Content-Length: 3",
84+
"\\[SendsStuff#login\\] content-length: 3",
8585
"\\[SendsStuff#login\\] <--- END HTTP \\(3-byte body\\)")},
8686
{Level.FULL, Arrays.asList(
8787
"\\[SendsStuff#login\\] ---> POST http://localhost:[0-9]+/ HTTP/1.1",
@@ -91,7 +91,7 @@ public static Iterable<Object[]> data() {
9191
"\\[SendsStuff#login\\] \\{\"customer_name\": \"netflix\", \"user_name\": \"denominator\", \"password\": \"password\"\\}",
9292
"\\[SendsStuff#login\\] ---> END HTTP \\(80-byte body\\)",
9393
"\\[SendsStuff#login\\] <--- HTTP/1.1 200 OK \\([0-9]+ms\\)",
94-
"\\[SendsStuff#login\\] Content-Length: 3",
94+
"\\[SendsStuff#login\\] content-length: 3",
9595
"\\[SendsStuff#login\\] ",
9696
"\\[SendsStuff#login\\] foo",
9797
"\\[SendsStuff#login\\] <--- END HTTP \\(3-byte body\\)")}
@@ -167,7 +167,7 @@ public static Iterable<Object[]> data() {
167167
"\\[SendsStuff#login\\] Content-Length: 80",
168168
"\\[SendsStuff#login\\] ---> END HTTP \\(80-byte body\\)",
169169
"\\[SendsStuff#login\\] <--- HTTP/1.1 200 OK \\([0-9]+ms\\)",
170-
"\\[SendsStuff#login\\] Content-Length: 3",
170+
"\\[SendsStuff#login\\] content-length: 3",
171171
"\\[SendsStuff#login\\] <--- ERROR SocketTimeoutException: Read timed out \\([0-9]+ms\\)")},
172172
{Level.FULL, Arrays.asList(
173173
"\\[SendsStuff#login\\] ---> POST http://localhost:[0-9]+/ HTTP/1.1",
@@ -177,7 +177,7 @@ public static Iterable<Object[]> data() {
177177
"\\[SendsStuff#login\\] \\{\"customer_name\": \"netflix\", \"user_name\": \"denominator\", \"password\": \"password\"\\}",
178178
"\\[SendsStuff#login\\] ---> END HTTP \\(80-byte body\\)",
179179
"\\[SendsStuff#login\\] <--- HTTP/1.1 200 OK \\([0-9]+ms\\)",
180-
"\\[SendsStuff#login\\] Content-Length: 3",
180+
"\\[SendsStuff#login\\] content-length: 3",
181181
"\\[SendsStuff#login\\] ",
182182
"\\[SendsStuff#login\\] <--- ERROR SocketTimeoutException: Read timed out \\([0-9]+ms\\)",
183183
"\\[SendsStuff#login\\] java.net.SocketTimeoutException: Read timed out.*",

‎core/src/test/java/feign/ResponseTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717

1818
import org.junit.Test;
1919

20+
import java.util.Arrays;
2021
import java.util.Collection;
2122
import java.util.Collections;
23+
import java.util.LinkedHashMap;
24+
import java.util.List;
25+
import java.util.Map;
2226

2327
import static feign.assertj.FeignAssertions.assertThat;
28+
import static org.assertj.core.api.Assertions.entry;
2429

2530
public class ResponseTest {
2631

@@ -32,4 +37,37 @@ public void reasonPhraseIsOptional() {
3237
assertThat(response.reason()).isNull();
3338
assertThat(response.toString()).isEqualTo("HTTP/1.1 200\n\n");
3439
}
40+
41+
@Test
42+
public void lowerCasesNamesOfHeaders() {
43+
Response response = Response.create(200,
44+
null,
45+
Collections.singletonMap("Content-Type",
46+
Collections.singletonList("application/json")),
47+
new byte[0]);
48+
assertThat(response.headers()).containsOnly(entry(("content-type"), Collections.singletonList("application/json")));
49+
}
50+
51+
@Test
52+
public void canAccessHeadersCaseInsensitively() {
53+
List<String> valueList = Collections.singletonList("application/json");
54+
Response response = Response.create(200,
55+
null,
56+
Collections.singletonMap("Content-Type", valueList),
57+
new byte[0]);
58+
assertThat(response.headers().get("content-type")).isEqualTo(valueList);
59+
assertThat(response.headers().get("Content-Type")).isEqualTo(valueList);
60+
}
61+
62+
@Test
63+
public void headerValuesWithSameNameOnlyVaryingInCaseAreMerged() {
64+
Map<String, Collection<String>> headersMap = new LinkedHashMap<>();
65+
headersMap.put("Set-Cookie", Arrays.asList("Cookie-A=Value", "Cookie-B=Value"));
66+
headersMap.put("set-cookie", Arrays.asList("Cookie-C=Value"));
67+
68+
Response response = Response.create(200, null, headersMap, new byte[0]);
69+
70+
List<String> expectedHeaderValue = Arrays.asList("Cookie-A=Value", "Cookie-B=Value", "Cookie-C=Value");
71+
assertThat(response.headers()).containsOnly(entry(("set-cookie"), expectedHeaderValue));
72+
}
3573
}

0 commit comments

Comments
 (0)