Skip to content

Commit b6d8bb1

Browse files
alzipurskyvelo
authored andcommitted
support PATCH with empty body paramter. (OpenFeign#824)
Fixes OpenFeign#665
1 parent 4da6d5c commit b6d8bb1

6 files changed

Lines changed: 52 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Version 10.1
2+
* Supports PATCH without a body paramter
3+
14
### Version 10.0
25
* Feign baseline is now JDK 8
36
- Feign is now being built and tested with OpenJDK 11 as well. Releases and code base will use JDK 8, we are just testing compatibility with JDK 11.

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,20 @@ public void noResponseBodyForPut() {
195195
api.noPutBody();
196196
}
197197

198+
/**
199+
* Some client implementation tests should override this test if the PATCH operation is
200+
* unsupported.
201+
*/
202+
@Test
203+
public void noResponseBodyForPatch() {
204+
server.enqueue(new MockResponse());
205+
206+
TestInterface api = newBuilder()
207+
.target(TestInterface.class, "http://localhost:" + server.getPort());
208+
209+
api.noPatchBody();
210+
}
211+
198212
@Test
199213
public void parsesResponseMissingLength() throws IOException {
200214
server.enqueue(new MockResponse().setChunkedBody("foo", 1));
@@ -388,6 +402,9 @@ public interface TestInterface {
388402
@RequestLine("PUT")
389403
String noPutBody();
390404

405+
@RequestLine("PATCH")
406+
String noPatchBody();
407+
391408
@RequestLine("POST /?foo=bar&foo=baz&qux=")
392409
@Headers({"Foo: Bar", "Foo: Baz", "Qux: ", "Content-Type: {contentType}"})
393410
Response postWithContentType(String body, @Param("contentType") String contentType);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ public void testPatch() throws Exception {
8383
super.testPatch();
8484
}
8585

86+
@Test
87+
@Override
88+
public void noResponseBodyForPatch() {
89+
thrown.expect(RetryableException.class);
90+
thrown.expectCause(isA(ProtocolException.class));
91+
super.noResponseBodyForPatch();
92+
}
8693

8794
@Test
8895
public void canOverrideHostnameVerifier() throws IOException, InterruptedException {

java11/src/test/java/feign/httpclient/test/Http2ClientTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public interface TestInterface {
3131
@RequestLine("PATCH /patch")
3232
@Headers({"Accept: text/plain"})
3333
String patch(String var1);
34+
35+
@RequestLine("PATCH /patch")
36+
@Headers({"Accept: text/plain"})
37+
String patch();
3438
}
3539

3640
@Override
@@ -42,6 +46,14 @@ public void testPatch() throws Exception {
4246
.contains("https://nghttp2.org/httpbin/patch");
4347
}
4448

49+
@Override
50+
@Test
51+
public void noResponseBodyForPatch() {
52+
final TestInterface api =
53+
newBuilder().target(TestInterface.class, "https://nghttp2.org/httpbin/");
54+
Assertions.assertThat(api.patch())
55+
.contains("https://nghttp2.org/httpbin/patch");
56+
}
4557

4658
@Override
4759
@Test

jaxrs2/src/test/java/feign/jaxrs2/JAXRSClientTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ public void noResponseBodyForPut() {
6262
}
6363
}
6464

65+
@Override
66+
public void noResponseBodyForPatch() {
67+
try {
68+
super.noResponseBodyForPatch();
69+
} catch (final IllegalStateException e) {
70+
Assume.assumeNoException("JaxRS client do not support PATCH requests", e);
71+
}
72+
}
73+
6574
@Test
6675
public void reasonPhraseIsOptional() throws IOException, InterruptedException {
6776
server.enqueue(new MockResponse().setStatus("HTTP/1.1 " + 200));

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313
*/
1414
package feign.okhttp;
1515

16-
import feign.Request.HttpMethod;
17-
import okhttp3.Headers;
18-
import okhttp3.MediaType;
19-
import okhttp3.Request;
20-
import okhttp3.RequestBody;
21-
import okhttp3.Response;
22-
import okhttp3.ResponseBody;
2316
import java.io.IOException;
2417
import java.io.InputStream;
2518
import java.io.Reader;
@@ -28,6 +21,8 @@
2821
import java.util.Map;
2922
import java.util.concurrent.TimeUnit;
3023
import feign.Client;
24+
import feign.Request.HttpMethod;
25+
import okhttp3.*;
3126

3227
/**
3328
* This module directs Feign's http requests to
@@ -78,7 +73,8 @@ static Request toOkHttpRequest(feign.Request input) {
7873

7974
byte[] inputBody = input.body();
8075
boolean isMethodWithBody =
81-
HttpMethod.POST == input.httpMethod() || HttpMethod.PUT == input.httpMethod();
76+
HttpMethod.POST == input.httpMethod() || HttpMethod.PUT == input.httpMethod()
77+
|| HttpMethod.PATCH == input.httpMethod();
8278
if (isMethodWithBody) {
8379
requestBuilder.removeHeader("Content-Type");
8480
if (inputBody == null) {

0 commit comments

Comments
 (0)