Skip to content

Commit

Permalink
Remove Content-Type Header when request with empty body POST method (…
Browse files Browse the repository at this point in the history
…Default Client).

- add Content-Length Header with 0 value when `sun.net.http.allowRestrictedHeaders` System Property is set true

- fix not running test to run
  • Loading branch information
jay-choe committed Sep 18, 2024
1 parent 27f6aa6 commit bbec180
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
12 changes: 6 additions & 6 deletions core/src/main/java/feign/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,14 @@ else if (field.equals(ACCEPT_ENCODING)) {
connection.addRequestProperty("Accept", "*/*");
}

boolean hasEmptyBody = false;
byte[] body = request.body();
if (body == null && request.httpMethod().isWithBody()) {
body = new byte[0];
hasEmptyBody = true;
}

if (body != null) {
/*
* Ignore disableRequestBuffering flag if the empty body was set, to ensure that internal
* retry logic applies to such requests.
*/
if (disableRequestBuffering && !hasEmptyBody) {
if (disableRequestBuffering) {
if (contentLength != null) {
connection.setFixedLengthStreamingMode(contentLength);
} else {
Expand All @@ -236,6 +231,11 @@ else if (field.equals(ACCEPT_ENCODING)) {
}
}
}

if (body == null && request.httpMethod().isWithBody()) {
connection.addRequestProperty("Content-Length", "0");
}

return connection;
}

Expand Down
21 changes: 21 additions & 0 deletions core/src/test/java/feign/client/DefaultClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import feign.assertj.MockWebServerAssertions;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.SocketPolicy;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

/**
* Tests client-specific behavior, such as ensuring Content-Length is sent when specified.
Expand Down Expand Up @@ -79,17 +80,37 @@ public void patch() throws Exception {
assertThat(exception).hasCauseInstanceOf(ProtocolException.class);
}

@Test
@Override
public void noResponseBodyForPost() throws Exception {
super.noResponseBodyForPost();
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST")
.hasNoHeaderNamed("Content-Type");
}

@Test
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
public void noRequestBodyForPostWithAllowRestrictedHeaders() throws Exception {
super.noResponseBodyForPost();
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("POST")
.hasNoHeaderNamed("Content-Type")
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
}

@Test
@Override
public void noResponseBodyForPut() throws Exception {
super.noResponseBodyForPut();
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("PUT")
.hasNoHeaderNamed("Content-Type");
}

@Test
@EnabledIfSystemProperty(named = "sun.net.http.allowRestrictedHeaders", matches = "true")
public void noResponseBodyForPutWithAllowRestrictedHeaders() throws Exception {
super.noResponseBodyForPut();
MockWebServerAssertions.assertThat(server.takeRequest()).hasMethod("PUT")
.hasNoHeaderNamed("Content-Type")
.hasHeaders(entry("Content-Length", Collections.singletonList("0")));
}

Expand Down

0 comments on commit bbec180

Please sign in to comment.