Skip to content

Commit f253448

Browse files
author
Adrian Cole
committed
Ensures Accept headers default to */*
Closes OpenFeign#123
1 parent 310904f commit f253448

5 files changed

Lines changed: 16 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Introduces feign.@Param to annotate template parameters. Users must migrate from `javax.inject.@Named` to `feign.@Param` before updating to Feign 8.0.
33
* Adds OkHttp integration
44
* Allows multiple headers with the same name.
5+
* Ensures Accept headers default to `*/*`
56

67
### Version 7.0
78
* Expose reflective dispatch hook: InvocationHandlerFactory

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
8484
Collection<String> contentEncodingValues = request.headers().get(CONTENT_ENCODING);
8585
boolean gzipEncodedRequest = contentEncodingValues != null && contentEncodingValues.contains(ENCODING_GZIP);
8686

87+
boolean hasAcceptHeader = false;
8788
Integer contentLength = null;
8889
for (String field : request.headers().keySet()) {
90+
if (field.equalsIgnoreCase("Accept")) hasAcceptHeader = true;
8991
for (String value : request.headers().get(field)) {
9092
if (field.equals(CONTENT_LENGTH)) {
9193
if (!gzipEncodedRequest) {
@@ -97,6 +99,8 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
9799
}
98100
}
99101
}
102+
// Some servers choke on the default accept string.
103+
if (!hasAcceptHeader) connection.addRequestProperty("Accept", "*/*");
100104

101105
if (request.body() != null) {
102106
if (contentLength != null) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ interface TestInterface {
5050
@RequestLine("POST /?foo=bar&foo=baz&qux=")
5151
@Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: text/plain"}) Response post(String body);
5252

53-
@RequestLine("PATCH /") String patch();
53+
@RequestLine("PATCH /") @Headers("Accept: text/plain") String patch();
5454
}
5555

5656
@Test public void parsesRequestAndResponse() throws IOException, InterruptedException {
@@ -69,7 +69,7 @@ interface TestInterface {
6969

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

okhttp/src/main/java/feign/okhttp/OkHttpClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ static Request toOkHttpRequest(feign.Request input) {
6868
requestBuilder.url(input.url());
6969

7070
MediaType mediaType = null;
71+
boolean hasAcceptHeader = false;
7172
for (String field : input.headers().keySet()) {
73+
if (field.equalsIgnoreCase("Accept")) hasAcceptHeader = true;
74+
7275
for (String value : input.headers().get(field)) {
7376
if (field.equalsIgnoreCase("Content-Type")) {
7477
mediaType = MediaType.parse(value);
@@ -78,6 +81,9 @@ static Request toOkHttpRequest(feign.Request input) {
7881
}
7982
}
8083
}
84+
// Some servers choke on the default accept string.
85+
if (!hasAcceptHeader) requestBuilder.addHeader("Accept", "*/*");
86+
8187
RequestBody body = input.body() != null ? RequestBody.create(mediaType, input.body()) : null;
8288
requestBuilder.method(input.method(), body);
8389
return requestBuilder.build();

okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ interface TestInterface {
4747
@RequestLine("POST /?foo=bar&foo=baz&qux=")
4848
@Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: text/plain"}) Response post(String body);
4949

50-
@RequestLine("PATCH /") String patch();
50+
@RequestLine("PATCH /") @Headers("Accept: text/plain") String patch();
5151
}
5252

5353
@Test public void parsesRequestAndResponse() throws IOException, InterruptedException {
@@ -68,7 +68,7 @@ interface TestInterface {
6868

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

@@ -96,7 +96,7 @@ interface TestInterface {
9696
assertEquals("foo", api.patch());
9797

9898
assertThat(server.takeRequest())
99-
.hasHeaders("Content-Length: 0") // Note: OkHttp adds content length.
99+
.hasHeaders("Accept: text/plain", "Content-Length: 0") // Note: OkHttp adds content length.
100100
.hasNoHeaderNamed("Content-Type")
101101
.hasMethod("PATCH");
102102
}

0 commit comments

Comments
 (0)