Skip to content

Commit 0202d79

Browse files
committed
Merge branch '6.2.x'
2 parents c5fd57d + 45d887f commit 0202d79

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -149,38 +149,38 @@ private HttpRequest buildRequest(HttpHeaders headers, @Nullable Body body) {
149149
}
150150
});
151151

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));
152+
if (body != null) {
153+
builder.method(this.method.name(), bodyPublisher(headers, body));
154+
}
155+
else {
156+
switch (this.method.name()) {
157+
case "GET" :
158+
builder.GET();
159+
break;
160+
case "DELETE" :
161+
builder.DELETE();
162+
break;
163+
default :
164+
builder.method(this.method.name(), HttpRequest.BodyPublishers.noBody());
165+
}
161166
}
162167
return builder.build();
163168
}
164169

165-
private HttpRequest.BodyPublisher bodyPublisher(HttpHeaders headers, @Nullable Body body) {
166-
if (body != null) {
167-
Flow.Publisher<ByteBuffer> publisher = new OutputStreamPublisher<>(
168-
os -> body.writeTo(StreamUtils.nonClosing(os)), BYTE_MAPPER, this.executor, null);
170+
private HttpRequest.BodyPublisher bodyPublisher(HttpHeaders headers, Body body) {
171+
Flow.Publisher<ByteBuffer> publisher = new OutputStreamPublisher<>(
172+
os -> body.writeTo(StreamUtils.nonClosing(os)), BYTE_MAPPER, this.executor, null);
169173

170-
long contentLength = headers.getContentLength();
171-
if (contentLength > 0) {
172-
return HttpRequest.BodyPublishers.fromPublisher(publisher, contentLength);
173-
}
174-
else if (contentLength == 0) {
175-
return HttpRequest.BodyPublishers.noBody();
176-
}
177-
else {
178-
return HttpRequest.BodyPublishers.fromPublisher(publisher);
179-
}
174+
long contentLength = headers.getContentLength();
175+
if (contentLength > 0) {
176+
return HttpRequest.BodyPublishers.fromPublisher(publisher, contentLength);
180177
}
181-
else {
178+
else if (contentLength == 0) {
182179
return HttpRequest.BodyPublishers.noBody();
183180
}
181+
else {
182+
return HttpRequest.BodyPublishers.fromPublisher(publisher);
183+
}
184184
}
185185

186186
/**

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,21 @@ public void contentLength0() throws IOException {
9696
}
9797
}
9898

99+
@Test // gh-35068
100+
void deleteRequestWithBody() throws Exception {
101+
URI uri = URI.create(baseUrl + "/echo");
102+
ClientHttpRequest request = this.factory.createRequest(uri, HttpMethod.DELETE);
103+
StreamUtils.copy("body", StandardCharsets.ISO_8859_1, request.getBody());
104+
try (ClientHttpResponse response = request.execute()) {
105+
assertThat(response.getStatusCode()).as("Invalid response status").isEqualTo(HttpStatus.OK);
106+
assertThat(StreamUtils.copyToString(response.getBody(), StandardCharsets.ISO_8859_1))
107+
.as("Invalid request body").isEqualTo("body");
108+
}
109+
}
99110

100111
@Test // gh-34971
101112
@EnabledForJreRange(min = JRE.JAVA_19) // behavior fixed in Java 19
102-
void requestContentLengthHeader() throws Exception {
113+
void requestContentLengthHeaderWhenNoBody() throws Exception {
103114
URI uri = URI.create(baseUrl + "/header/Content-Length");
104115
assertNoContentLength(uri, HttpMethod.GET);
105116
assertNoContentLength(uri, HttpMethod.DELETE);

0 commit comments

Comments
 (0)