Skip to content

Commit 5281e62

Browse files
committed
Merge pull request OpenFeign#322 from pnepywoda/empty-body
support PUT with empty body parameter
2 parents e1137b0 + e17aaed commit 5281e62

5 files changed

Lines changed: 49 additions & 4 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 8.15
2+
* Supports PUT without a body parameter
3+
14
### Version 8.14
25
* Add support for RxJava Observable and Single return types via the `HystrixFeign` builder.
36
* Adds fallback implementation configuration to the `HystrixFeign` builder

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,24 @@ protected void log(String configKey, String format, Object... args) {
184184
}
185185

186186
@Test
187-
public void noResponseBody() {
187+
public void noResponseBodyForPost() {
188188
server.enqueue(new MockResponse());
189189

190190
TestInterface api = Feign.builder()
191191
.target(TestInterface.class, "http://localhost:" + server.getPort());
192192

193193
api.noPostBody();
194194
}
195+
196+
@Test
197+
public void noResponseBodyForPut() {
198+
server.enqueue(new MockResponse());
199+
200+
TestInterface api = Feign.builder()
201+
.target(TestInterface.class, "http://localhost:" + server.getPort());
202+
203+
api.noPutBody();
204+
}
195205

196206
interface TestInterface {
197207

@@ -209,5 +219,8 @@ interface TestInterface {
209219

210220
@RequestLine("POST")
211221
String noPostBody();
222+
223+
@RequestLine("PUT")
224+
String noPutBody();
212225
}
213226
}

httpclient/src/test/java/feign/httpclient/ApacheHttpClientTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected void log(String configKey, String format, Object... args) {
153153
}
154154

155155
@Test
156-
public void noResponseBody() {
156+
public void noResponseBodyForPost() {
157157
server.enqueue(new MockResponse());
158158

159159
TestInterface api = Feign.builder()
@@ -162,6 +162,17 @@ public void noResponseBody() {
162162

163163
api.noPostBody();
164164
}
165+
166+
@Test
167+
public void noResponseBodyForPut() {
168+
server.enqueue(new MockResponse());
169+
170+
TestInterface api = Feign.builder()
171+
.client(new ApacheHttpClient())
172+
.target(TestInterface.class, "http://localhost:" + server.getPort());
173+
174+
api.noPutBody();
175+
}
165176
@Test
166177
public void postWithSpacesInPath() throws IOException, InterruptedException {
167178
server.enqueue(new MockResponse().setBody("foo"));
@@ -193,6 +204,9 @@ interface TestInterface {
193204

194205
@RequestLine("POST")
195206
String noPostBody();
207+
208+
@RequestLine("PUT")
209+
String noPutBody();
196210

197211
@RequestLine("POST /path/{to}/resource")
198212
@Headers("Accept: text/plain")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ static Request toOkHttpRequest(feign.Request input) {
7979
}
8080

8181
byte[] inputBody = input.body();
82-
if ("POST".equals(input.method()) && inputBody == null) {
82+
boolean isMethodWithBody = "POST".equals(input.method()) || "PUT".equals(input.method());
83+
if (isMethodWithBody && inputBody == null) {
8384
// write an empty BODY to conform with okhttp 2.4.0+
8485
// http://johnfeng.github.io/blog/2015/06/30/okhttp-updates-post-wouldnt-be-allowed-to-have-null-body/
8586
inputBody = new byte[0];

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected void log(String configKey, String format, Object... args) {
135135
}
136136

137137
@Test
138-
public void noResponseBody() {
138+
public void noResponseBodyForPost() {
139139
server.enqueue(new MockResponse());
140140

141141
TestInterface api = Feign.builder()
@@ -144,6 +144,17 @@ public void noResponseBody() {
144144

145145
api.noPostBody();
146146
}
147+
148+
@Test
149+
public void noResponseBodyForPut() {
150+
server.enqueue(new MockResponse());
151+
152+
TestInterface api = Feign.builder()
153+
.client(new OkHttpClient())
154+
.target(TestInterface.class, "http://localhost:" + server.getPort());
155+
156+
api.noPutBody();
157+
}
147158

148159
interface TestInterface {
149160

@@ -161,5 +172,8 @@ interface TestInterface {
161172

162173
@RequestLine("POST")
163174
String noPostBody();
175+
176+
@RequestLine("PUT")
177+
String noPutBody();
164178
}
165179
}

0 commit comments

Comments
 (0)