Skip to content

Commit d997eaf

Browse files
kdavisk6velo
authored andcommitted
Fixes NPE when a Response does not provide headers (#855)
Fixes #853 There are some scenarios reported where a server does not provide headers with the response. While this is not typically expected, it's simple enough for Feign to be resilient to it. This change checks the headers provided in the builder and if none are provided, an empty map is used in it's place.
1 parent 535af96 commit d997eaf

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.nio.charset.Charset;
2929
import java.util.Collection;
3030
import java.util.Collections;
31+
import java.util.LinkedHashMap;
3132
import java.util.LinkedList;
3233
import java.util.Locale;
3334
import java.util.Map;
@@ -48,7 +49,10 @@ private Response(Builder builder) {
4849
this.status = builder.status;
4950
this.request = builder.request;
5051
this.reason = builder.reason; // nullable
51-
this.headers = Collections.unmodifiableMap(caseInsensitiveCopyOf(builder.headers));
52+
this.headers =
53+
(builder.headers != null)
54+
? Collections.unmodifiableMap(caseInsensitiveCopyOf(builder.headers))
55+
: new LinkedHashMap<>();
5256
this.body = builder.body; // nullable
5357
}
5458

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,16 @@ public void headerValuesWithSameNameOnlyVaryingInCaseAreMerged() {
7878
Arrays.asList("Cookie-A=Value", "Cookie-B=Value", "Cookie-C=Value");
7979
assertThat(response.headers()).containsOnly(entry(("set-cookie"), expectedHeaderValue));
8080
}
81+
82+
@Test
83+
public void headersAreOptional() {
84+
Response response =
85+
Response.builder()
86+
.status(200)
87+
.request(
88+
Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8))
89+
.body(new byte[0])
90+
.build();
91+
assertThat(response.headers()).isNotNull().isEmpty();
92+
}
8193
}

0 commit comments

Comments
 (0)