From bbec18097e3f31b8b2d2ea08f76dc3c1ca859167 Mon Sep 17 00:00:00 2001 From: jay-choe Date: Wed, 18 Sep 2024 15:52:42 +0900 Subject: [PATCH] Remove Content-Type Header when request with empty body POST method (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 --- core/src/main/java/feign/Client.java | 12 +++++------ .../java/feign/client/DefaultClientTest.java | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/feign/Client.java b/core/src/main/java/feign/Client.java index 93a17dd90..bd21792f5 100644 --- a/core/src/main/java/feign/Client.java +++ b/core/src/main/java/feign/Client.java @@ -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 { @@ -236,6 +231,11 @@ else if (field.equals(ACCEPT_ENCODING)) { } } } + + if (body == null && request.httpMethod().isWithBody()) { + connection.addRequestProperty("Content-Length", "0"); + } + return connection; } diff --git a/core/src/test/java/feign/client/DefaultClientTest.java b/core/src/test/java/feign/client/DefaultClientTest.java index c8ee70a17..c8fccfb0d 100644 --- a/core/src/test/java/feign/client/DefaultClientTest.java +++ b/core/src/test/java/feign/client/DefaultClientTest.java @@ -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. @@ -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"))); }