diff --git a/spring-web/src/main/java/org/springframework/http/client/JdkClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/JdkClientHttpRequest.java index 965f18a9373..886b921aaf4 100644 --- a/spring-web/src/main/java/org/springframework/http/client/JdkClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/JdkClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2023 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -129,9 +129,12 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest { BYTE_MAPPER, this.executor); long contentLength = headers.getContentLength(); - if (contentLength != -1) { + if (contentLength > 0) { return HttpRequest.BodyPublishers.fromPublisher(outputStreamPublisher, contentLength); } + else if (contentLength == 0) { + return HttpRequest.BodyPublishers.noBody(); + } else { return HttpRequest.BodyPublishers.fromPublisher(outputStreamPublisher); } diff --git a/spring-web/src/test/java/org/springframework/http/client/JdkClientHttpRequestFactoryTests.java b/spring-web/src/test/java/org/springframework/http/client/JdkClientHttpRequestFactoryTests.java index cce3a37989e..45a6bb5dc59 100644 --- a/spring-web/src/test/java/org/springframework/http/client/JdkClientHttpRequestFactoryTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/JdkClientHttpRequestFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2023 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.lang.Nullable; @@ -67,7 +68,7 @@ public class JdkClientHttpRequestFactoryTests extends AbstractHttpRequestFactory @Test public void customizeDisallowedHeaders() throws IOException { - ClientHttpRequest request = factory.createRequest(URI.create(this.baseUrl + "/status/299"), HttpMethod.PUT); + ClientHttpRequest request = this.factory.createRequest(URI.create(this.baseUrl + "/status/299"), HttpMethod.PUT); request.getHeaders().set("Expect", "299"); try (ClientHttpResponse response = request.execute()) { @@ -75,4 +76,14 @@ public class JdkClientHttpRequestFactoryTests extends AbstractHttpRequestFactory } } + @Test // gh-31451 + public void contentLength0() throws IOException { + BufferingClientHttpRequestFactory bufferingFactory = new BufferingClientHttpRequestFactory(this.factory); + ClientHttpRequest request = bufferingFactory.createRequest(URI.create(this.baseUrl + "/methods/get"), HttpMethod.GET); + + try (ClientHttpResponse response = request.execute()) { + assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK); + } + } + }