From e17aaed40df3e6527b47cb050ac14b38747d5099 Mon Sep 17 00:00:00 2001 From: Paul Nepywoda Date: Mon, 25 Jan 2016 21:23:14 -0800 Subject: [PATCH] support PUT with empty body parameter follow-up commit to https://github.com/Netflix/feign/pull/271 --- CHANGELOG.md | 3 +++ .../java/feign/client/DefaultClientTest.java | 15 ++++++++++++++- .../feign/httpclient/ApacheHttpClientTest.java | 16 +++++++++++++++- .../src/main/java/feign/okhttp/OkHttpClient.java | 3 ++- .../test/java/feign/okhttp/OkHttpClientTest.java | 16 +++++++++++++++- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41625371..d4c531a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### Version 8.15 +* Supports PUT without a body parameter + ### Version 8.14 * Add support for RxJava Observable and Single return types via the `HystrixFeign` builder. * Adds fallback implementation configuration to the `HystrixFeign` builder diff --git a/core/src/test/java/feign/client/DefaultClientTest.java b/core/src/test/java/feign/client/DefaultClientTest.java index c59a9afb..bee0aade 100644 --- a/core/src/test/java/feign/client/DefaultClientTest.java +++ b/core/src/test/java/feign/client/DefaultClientTest.java @@ -184,7 +184,7 @@ public class DefaultClientTest { } @Test - public void noResponseBody() { + public void noResponseBodyForPost() { server.enqueue(new MockResponse()); TestInterface api = Feign.builder() @@ -192,6 +192,16 @@ public class DefaultClientTest { api.noPostBody(); } + + @Test + public void noResponseBodyForPut() { + server.enqueue(new MockResponse()); + + TestInterface api = Feign.builder() + .target(TestInterface.class, "http://localhost:" + server.getPort()); + + api.noPutBody(); + } interface TestInterface { @@ -209,5 +219,8 @@ public class DefaultClientTest { @RequestLine("POST") String noPostBody(); + + @RequestLine("PUT") + String noPutBody(); } } diff --git a/httpclient/src/test/java/feign/httpclient/ApacheHttpClientTest.java b/httpclient/src/test/java/feign/httpclient/ApacheHttpClientTest.java index f5ccb7cd..d57d54dc 100644 --- a/httpclient/src/test/java/feign/httpclient/ApacheHttpClientTest.java +++ b/httpclient/src/test/java/feign/httpclient/ApacheHttpClientTest.java @@ -153,7 +153,7 @@ public class ApacheHttpClientTest { } @Test - public void noResponseBody() { + public void noResponseBodyForPost() { server.enqueue(new MockResponse()); TestInterface api = Feign.builder() @@ -162,6 +162,17 @@ public class ApacheHttpClientTest { api.noPostBody(); } + + @Test + public void noResponseBodyForPut() { + server.enqueue(new MockResponse()); + + TestInterface api = Feign.builder() + .client(new ApacheHttpClient()) + .target(TestInterface.class, "http://localhost:" + server.getPort()); + + api.noPutBody(); + } @Test public void postWithSpacesInPath() throws IOException, InterruptedException { server.enqueue(new MockResponse().setBody("foo")); @@ -193,6 +204,9 @@ public class ApacheHttpClientTest { @RequestLine("POST") String noPostBody(); + + @RequestLine("PUT") + String noPutBody(); @RequestLine("POST /path/{to}/resource") @Headers("Accept: text/plain") diff --git a/okhttp/src/main/java/feign/okhttp/OkHttpClient.java b/okhttp/src/main/java/feign/okhttp/OkHttpClient.java index 2e6abc9a..58df544f 100644 --- a/okhttp/src/main/java/feign/okhttp/OkHttpClient.java +++ b/okhttp/src/main/java/feign/okhttp/OkHttpClient.java @@ -79,7 +79,8 @@ public final class OkHttpClient implements Client { } byte[] inputBody = input.body(); - if ("POST".equals(input.method()) && inputBody == null) { + boolean isMethodWithBody = "POST".equals(input.method()) || "PUT".equals(input.method()); + if (isMethodWithBody && inputBody == null) { // write an empty BODY to conform with okhttp 2.4.0+ // http://johnfeng.github.io/blog/2015/06/30/okhttp-updates-post-wouldnt-be-allowed-to-have-null-body/ inputBody = new byte[0]; diff --git a/okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java b/okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java index 0bbd2f16..3056c694 100644 --- a/okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java +++ b/okhttp/src/test/java/feign/okhttp/OkHttpClientTest.java @@ -135,7 +135,7 @@ public class OkHttpClientTest { } @Test - public void noResponseBody() { + public void noResponseBodyForPost() { server.enqueue(new MockResponse()); TestInterface api = Feign.builder() @@ -144,6 +144,17 @@ public class OkHttpClientTest { api.noPostBody(); } + + @Test + public void noResponseBodyForPut() { + server.enqueue(new MockResponse()); + + TestInterface api = Feign.builder() + .client(new OkHttpClient()) + .target(TestInterface.class, "http://localhost:" + server.getPort()); + + api.noPutBody(); + } interface TestInterface { @@ -161,5 +172,8 @@ public class OkHttpClientTest { @RequestLine("POST") String noPostBody(); + + @RequestLine("PUT") + String noPutBody(); } }