From 8e41e8f10ea29273864984d65d6ca668fe394fe7 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Mon, 10 Dec 2018 18:03:59 -0500 Subject: [PATCH] Fixes NPE when a Response does not provide headers 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. --- core/src/main/java/feign/Response.java | 5 ++++- core/src/test/java/feign/ResponseTest.java | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/feign/Response.java b/core/src/main/java/feign/Response.java index 46499067a6..bb4b60cd90 100644 --- a/core/src/main/java/feign/Response.java +++ b/core/src/main/java/feign/Response.java @@ -22,6 +22,7 @@ import java.nio.charset.Charset; import java.util.Collection; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Locale; import java.util.Map; @@ -49,7 +50,9 @@ private Response(Builder builder) { this.status = builder.status; this.request = builder.request; this.reason = builder.reason; // nullable - this.headers = Collections.unmodifiableMap(caseInsensitiveCopyOf(builder.headers)); + this.headers = (builder.headers != null) + ? Collections.unmodifiableMap(caseInsensitiveCopyOf(builder.headers)) + : new LinkedHashMap<>(); this.body = builder.body; // nullable } diff --git a/core/src/test/java/feign/ResponseTest.java b/core/src/test/java/feign/ResponseTest.java index 01de421e97..e43d04ae79 100644 --- a/core/src/test/java/feign/ResponseTest.java +++ b/core/src/test/java/feign/ResponseTest.java @@ -71,4 +71,14 @@ public void headerValuesWithSameNameOnlyVaryingInCaseAreMerged() { Arrays.asList("Cookie-A=Value", "Cookie-B=Value", "Cookie-C=Value"); assertThat(response.headers()).containsOnly(entry(("set-cookie"), expectedHeaderValue)); } + + @Test + public void headersAreOptional() { + Response response = Response.builder() + .status(200) + .request(Request.create(HttpMethod.GET, "/api", Collections.emptyMap(), null, Util.UTF_8)) + .body(new byte[0]) + .build(); + assertThat(response.headers()).isNotNull().isEmpty(); + } }