Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion core/src/main/java/feign/RequestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ private String encodeValueIfNotEncoded(String key,
/* roughly analogous to {@code javax.ws.rs.client.Target.request()}. */
public Request request() {
Map<String, Collection<String>> safeCopy = new LinkedHashMap<String, Collection<String>>();
safeCopy.putAll(headers);
safeCopy.putAll(headers());
return Request.create(
method, url + queryLine(),
Collections.unmodifiableMap(safeCopy),
Expand Down Expand Up @@ -535,6 +535,8 @@ public RequestTemplate headers(Map<String, Collection<String>> headers) {
* @see Request#headers()
*/
public Map<String, Collection<String>> headers() {
headers.forEach((key, value) -> value.removeIf(e -> e == null));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the idea of modifying the existing map...

may be just filter and create new one?!

headers.entrySet().removeIf(e -> e.getValue() == null || e.getValue().isEmpty());
return Collections.unmodifiableMap(headers);
}

Expand Down
5 changes: 5 additions & 0 deletions core/src/test/java/feign/assertj/RequestTemplateAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,9 @@ public RequestTemplateAssert hasHeaders(MapEntry... entries) {
maps.assertContainsExactly(info, actual.headers(), entries);
return this;
}

public RequestTemplateAssert hasNoHeader(final String encoded) {
objects.assertNull(info, actual.headers().get(encoded));
return this;
}
}
41 changes: 40 additions & 1 deletion core/src/test/java/feign/client/AbstractClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import okhttp3.mockwebserver.MockWebServer;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.Assert.assertEquals;
import static feign.Util.UTF_8;

Expand Down Expand Up @@ -93,7 +94,7 @@ public void parsesRequestAndResponse() throws IOException, InterruptedException

MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST")
.hasPath("/?foo=bar&foo=baz&qux=")
.hasHeaders("Foo: Bar", "Foo: Baz", "Qux: ", "Accept: */*", "Content-Length: 3")
.hasHeaders("Foo: Bar", "Foo: Baz", "Accept: */*", "Content-Length: 3")
.hasBody("foo");
}

Expand Down Expand Up @@ -282,6 +283,38 @@ public void testDefaultCollectionFormat() throws Exception {
.hasPath("/?foo=bar&foo=baz");
}

@Test
public void testHeadersWithNullParams() throws InterruptedException {
server.enqueue(new MockResponse().setBody("body"));

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

Response response = api.getWithHeaders(null);

assertThat(response.status()).isEqualTo(200);
assertThat(response.reason()).isEqualTo("OK");

MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("GET")
.hasPath("/").hasNoHeaderNamed("Authorization");
}

@Test
public void testHeadersWithNotEmptyParams() throws InterruptedException {
server.enqueue(new MockResponse().setBody("body"));

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

Response response = api.getWithHeaders("token");

assertThat(response.status()).isEqualTo(200);
assertThat(response.reason()).isEqualTo("OK");

MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("GET")
.hasPath("/").hasHeaders(entry("authorization", asList("token")));
}

@Test
public void testAlternativeCollectionFormat() throws Exception {
server.enqueue(new MockResponse().setBody("body"));
Expand Down Expand Up @@ -316,6 +349,12 @@ public interface TestInterface {
@RequestLine("GET /?foo={multiFoo}")
Response get(@Param("multiFoo") List<String> multiFoo);

@Headers({
"Authorization: {authorization}"
})
@RequestLine("GET /")
Response getWithHeaders(@Param("authorization") String authorization);

@RequestLine(value = "GET /?foo={multiFoo}", collectionFormat = CollectionFormat.CSV)
Response getCSV(@Param("multiFoo") List<String> multiFoo);

Expand Down