Skip to content

Commit 7146e35

Browse files
selecteeAlexander Zadvinskiyvelo
authored
Add ability use case insensitive headers for default Client (OpenFeign#1586)
* Add ability use case insensitive headers for default Client * Add ability use case insensitive headers for default Client Co-authored-by: Alexander Zadvinskiy <azadvinskiy@it-one.ru> Co-authored-by: Marvin Froeder <velo@users.noreply.github.com>
1 parent d27fef7 commit 7146e35

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

core/src/main/java/feign/Client.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static feign.Util.checkArgument;
2121
import static feign.Util.checkNotNull;
2222
import static feign.Util.isNotBlank;
23+
import static java.lang.String.CASE_INSENSITIVE_ORDER;
2324
import static java.lang.String.format;
2425
import feign.Request.Options;
2526
import java.io.IOException;
@@ -31,9 +32,9 @@
3132
import java.nio.charset.StandardCharsets;
3233
import java.util.Base64;
3334
import java.util.Collection;
34-
import java.util.LinkedHashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.TreeMap;
3738
import java.util.zip.DeflaterOutputStream;
3839
import java.util.zip.GZIPInputStream;
3940
import java.util.zip.GZIPOutputStream;
@@ -114,7 +115,7 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
114115
connection.getRequestMethod(), connection.getURL()));
115116
}
116117

117-
Map<String, Collection<String>> headers = new LinkedHashMap<>();
118+
Map<String, Collection<String>> headers = new TreeMap<>(CASE_INSENSITIVE_ORDER);
118119
for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
119120
// response message
120121
if (field.getKey() != null) {
@@ -130,9 +131,9 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
130131
if (status >= 400) {
131132
stream = connection.getErrorStream();
132133
} else {
133-
if (this.isGzip(connection.getHeaderFields().get(CONTENT_ENCODING))) {
134+
if (this.isGzip(headers.get(CONTENT_ENCODING))) {
134135
stream = new GZIPInputStream(connection.getInputStream());
135-
} else if (this.isDeflate(connection.getHeaderFields().get(CONTENT_ENCODING))) {
136+
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
136137
stream = new InflaterInputStream(connection.getInputStream());
137138
} else {
138139
stream = connection.getInputStream();

core/src/test/java/feign/client/DefaultClientTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ public void canSupportGzip() throws Exception {
170170

171171
}
172172

173+
@Test
174+
public void canExeptCaseInsensitiveHeader() throws Exception {
175+
/* enqueue a zipped response */
176+
final String responseData = "Compressed Data";
177+
server.enqueue(new MockResponse()
178+
.addHeader("content-encoding", "gzip")
179+
.setBody(new Buffer().write(compress(responseData))));
180+
181+
TestInterface api = newBuilder()
182+
.target(TestInterface.class, "http://localhost:" + server.getPort());
183+
184+
String result = api.get();
185+
186+
/* verify that the response is unzipped */
187+
assertThat(result).isNotNull()
188+
.isEqualToIgnoringCase(responseData);
189+
190+
}
191+
173192
@Test
174193
public void canSupportDeflate() throws Exception {
175194
/* enqueue a zipped response */

0 commit comments

Comments
 (0)