Skip to content

Commit c03f798

Browse files
committed
Merge branch '6.1.x'
2 parents af23cc2 + b3a4567 commit c03f798

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,7 +42,7 @@ protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
4242
@Override
4343
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
4444
byte[] bytes = this.bufferedOutput.toByteArrayUnsafe();
45-
if (headers.getContentLength() < 0) {
45+
if (bytes.length > 0 && headers.getContentLength() < 0) {
4646
headers.setContentLength(bytes.length);
4747
}
4848
ClientHttpResponse result = executeInternal(headers, bytes);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ else if(request.getPath().startsWith("/methods/")) {
106106
assertThat(request.getMethod()).isEqualTo(expectedMethod);
107107
return new MockResponse();
108108
}
109+
else if(request.getPath().startsWith("/header/")) {
110+
String headerName = request.getPath().replace("/header/","");
111+
return new MockResponse().setBody(headerName + ":" + request.getHeader(headerName)).setResponseCode(200);
112+
}
109113
return new MockResponse().setResponseCode(404);
110114
}
111115
catch (Throwable exc) {

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.http.client;
1818

19+
import java.io.InputStreamReader;
1920
import java.net.URI;
2021
import java.nio.charset.StandardCharsets;
2122
import java.util.Arrays;
@@ -49,12 +50,7 @@ void repeatableRead() throws Exception {
4950
FileCopyUtils.copy(body, request.getBody());
5051
try (ClientHttpResponse response = request.execute()) {
5152
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
52-
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
53-
54-
assertThat(response.getHeaders().containsKey(headerName)).as("Header not found").isTrue();
5553
assertThat(response.getHeaders().containsKey(headerName)).as("Header not found").isTrue();
56-
57-
assertThat(response.getHeaders().get(headerName)).as("Header value not found").isEqualTo(Arrays.asList(headerValue1, headerValue2));
5854
assertThat(response.getHeaders().get(headerName)).as("Header value not found").isEqualTo(Arrays.asList(headerValue1, headerValue2));
5955

6056
byte[] result = FileCopyUtils.copyToByteArray(response.getBody());
@@ -64,4 +60,14 @@ void repeatableRead() throws Exception {
6460
}
6561
}
6662

63+
@Test
64+
void shouldNotSetContentLengthWhenEmptyBody() throws Exception {
65+
ClientHttpRequest request = factory.createRequest(URI.create(baseUrl + "/header/Content-Length"), HttpMethod.POST);
66+
try (ClientHttpResponse response = request.execute()) {
67+
assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
68+
String result = FileCopyUtils.copyToString(new InputStreamReader(response.getBody()));
69+
assertThat(result).as("Invalid body").isEqualTo("Content-Length:null");
70+
}
71+
}
72+
6773
}

0 commit comments

Comments
 (0)