Skip to content
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

Add ability use case insensitive headers for default Client #1586

Merged
merged 4 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions core/src/main/java/feign/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static feign.Util.checkArgument;
import static feign.Util.checkNotNull;
import static feign.Util.isNotBlank;
import static java.lang.String.CASE_INSENSITIVE_ORDER;
import static java.lang.String.format;
import feign.Request.Options;
import java.io.IOException;
Expand All @@ -31,9 +32,9 @@
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
Expand Down Expand Up @@ -114,7 +115,7 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
connection.getRequestMethod(), connection.getURL()));
}

Map<String, Collection<String>> headers = new LinkedHashMap<>();
Map<String, Collection<String>> headers = new TreeMap<>(CASE_INSENSITIVE_ORDER);
for (Map.Entry<String, List<String>> field : connection.getHeaderFields().entrySet()) {
// response message
if (field.getKey() != null) {
Expand All @@ -130,9 +131,9 @@ Response convertResponse(HttpURLConnection connection, Request request) throws I
if (status >= 400) {
stream = connection.getErrorStream();
} else {
if (this.isGzip(connection.getHeaderFields().get(CONTENT_ENCODING))) {
if (this.isGzip(headers.get(CONTENT_ENCODING))) {
stream = new GZIPInputStream(connection.getInputStream());
} else if (this.isDeflate(connection.getHeaderFields().get(CONTENT_ENCODING))) {
} else if (this.isDeflate(headers.get(CONTENT_ENCODING))) {
stream = new InflaterInputStream(connection.getInputStream());
} else {
stream = connection.getInputStream();
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/feign/client/DefaultClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,25 @@ public void canSupportGzip() throws Exception {

}

@Test
public void canExeptCaseInsensitiveHeader() throws Exception {
/* enqueue a zipped response */
final String responseData = "Compressed Data";
server.enqueue(new MockResponse()
.addHeader("content-encoding", "gzip")
.setBody(new Buffer().write(compress(responseData))));

TestInterface api = newBuilder()
.target(TestInterface.class, "http://localhost:" + server.getPort());

String result = api.get();

/* verify that the response is unzipped */
assertThat(result).isNotNull()
.isEqualToIgnoringCase(responseData);

}

@Test
public void canSupportDeflate() throws Exception {
/* enqueue a zipped response */
Expand Down