Skip to content

Commit 659472f

Browse files
committed
Use HTTP methods in JdkClientHttpRequest when possible
Prior to this commit, we would use the `java.net.http.HttpRequest.Builder#method(String, BodyPublisher)` to create HTTP requests for the JDK HttpClient. This method requires a non-null body publisher; providing an empty publisher writes a "Content-Length: 0" header to all requests. As of Java 19, this behavior changes for `HttpRequest.Builder#GET` and similar methods, where the body publisher is considered as null and no "Content-Length" header is written. This commit aligns with this behavior and favors dedicated HTTP methods whenever available.` Closes gh-34971
1 parent c2d6788 commit 659472f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

spring-web/src/main/java/org/springframework/http/client/JdkClientHttpRequest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,16 @@ private HttpRequest buildRequest(HttpHeaders headers, @Nullable Body body) {
149149
}
150150
});
151151

152-
builder.method(this.method.name(), bodyPublisher(headers, body));
152+
switch (this.method.name()) {
153+
case "GET" :
154+
builder.GET();
155+
break;
156+
case "DELETE" :
157+
builder.DELETE();
158+
break;
159+
default :
160+
builder.method(this.method.name(), bodyPublisher(headers, body));
161+
}
153162
return builder.build();
154163
}
155164

spring-web/src/test/java/org/springframework/http/client/JdkClientHttpRequestFactoryTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,27 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21+
import java.nio.charset.StandardCharsets;
2122

2223
import org.junit.jupiter.api.AfterAll;
2324
import org.junit.jupiter.api.BeforeAll;
2425
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.condition.EnabledForJreRange;
27+
import org.junit.jupiter.api.condition.JRE;
2528

2629
import org.springframework.http.HttpMethod;
2730
import org.springframework.http.HttpStatus;
2831
import org.springframework.http.HttpStatusCode;
2932
import org.springframework.lang.Nullable;
33+
import org.springframework.util.StreamUtils;
3034

3135
import static org.assertj.core.api.Assertions.assertThat;
3236

3337
/**
3438
* Tests for {@link JdkClientHttpRequestFactory}.
3539
*
3640
* @author Marten Deinum
41+
* @author Brian Clozel
3742
*/
3843
class JdkClientHttpRequestFactoryTests extends AbstractHttpRequestFactoryTests {
3944

@@ -88,4 +93,22 @@ public void contentLength0() throws IOException {
8893
}
8994
}
9095

96+
97+
@Test // gh-34971
98+
@EnabledForJreRange(min = JRE.JAVA_19) // behavior fixed in Java 19
99+
void requestContentLengthHeader() throws Exception {
100+
URI uri = URI.create(baseUrl + "/header/Content-Length");
101+
assertNoContentLength(uri, HttpMethod.GET);
102+
assertNoContentLength(uri, HttpMethod.DELETE);
103+
}
104+
105+
protected void assertNoContentLength(URI uri, HttpMethod method) throws Exception {
106+
ClientHttpRequest request = factory.createRequest(uri, method);
107+
try (ClientHttpResponse response = request.execute()) {
108+
assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK);
109+
assertThat(StreamUtils.copyToString(response.getBody(), StandardCharsets.ISO_8859_1))
110+
.as("Invalid Content-Length request header").isEqualTo("Content-Length:null");
111+
}
112+
}
113+
91114
}

0 commit comments

Comments
 (0)