From 64b0283042000d93a4dd6f2436e0b7da2bc23228 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Thu, 2 May 2024 09:11:54 +0200 Subject: [PATCH] Revert "Do not set 0 Content-Length header in BufferingClientHttpRequestFactory" This reverts commit b3a45670f9b8b193a34c022b63cc2b587feae6ef. See gh-32650 --- .../AbstractBufferingClientHttpRequest.java | 4 ++-- .../http/client/AbstractMockWebServerTests.java | 4 ---- .../BufferingClientHttpRequestFactoryTests.java | 16 +++++----------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java b/spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java index 769ea1ebc817..df0936a11e39 100644 --- a/spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java +++ b/spring-web/src/main/java/org/springframework/http/client/AbstractBufferingClientHttpRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 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. @@ -42,7 +42,7 @@ protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException { @Override protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException { byte[] bytes = this.bufferedOutput.toByteArrayUnsafe(); - if (bytes.length > 0 && headers.getContentLength() < 0) { + if (headers.getContentLength() < 0) { headers.setContentLength(bytes.length); } ClientHttpResponse result = executeInternal(headers, bytes); diff --git a/spring-web/src/test/java/org/springframework/http/client/AbstractMockWebServerTests.java b/spring-web/src/test/java/org/springframework/http/client/AbstractMockWebServerTests.java index c8c88be4ec78..c2237dd61d84 100644 --- a/spring-web/src/test/java/org/springframework/http/client/AbstractMockWebServerTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/AbstractMockWebServerTests.java @@ -106,10 +106,6 @@ else if(request.getPath().startsWith("/methods/")) { assertThat(request.getMethod()).isEqualTo(expectedMethod); return new MockResponse(); } - else if(request.getPath().startsWith("/header/")) { - String headerName = request.getPath().replace("/header/",""); - return new MockResponse().setBody(headerName + ":" + request.getHeader(headerName)).setResponseCode(200); - } return new MockResponse().setResponseCode(404); } catch (Throwable exc) { diff --git a/spring-web/src/test/java/org/springframework/http/client/BufferingClientHttpRequestFactoryTests.java b/spring-web/src/test/java/org/springframework/http/client/BufferingClientHttpRequestFactoryTests.java index 8fba0a715127..4716e351af4c 100644 --- a/spring-web/src/test/java/org/springframework/http/client/BufferingClientHttpRequestFactoryTests.java +++ b/spring-web/src/test/java/org/springframework/http/client/BufferingClientHttpRequestFactoryTests.java @@ -16,7 +16,6 @@ package org.springframework.http.client; -import java.io.InputStreamReader; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -50,7 +49,12 @@ void repeatableRead() throws Exception { FileCopyUtils.copy(body, request.getBody()); try (ClientHttpResponse response = request.execute()) { assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK); + assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK); + + assertThat(response.getHeaders().containsKey(headerName)).as("Header not found").isTrue(); assertThat(response.getHeaders().containsKey(headerName)).as("Header not found").isTrue(); + + assertThat(response.getHeaders().get(headerName)).as("Header value not found").isEqualTo(Arrays.asList(headerValue1, headerValue2)); assertThat(response.getHeaders().get(headerName)).as("Header value not found").isEqualTo(Arrays.asList(headerValue1, headerValue2)); byte[] result = FileCopyUtils.copyToByteArray(response.getBody()); @@ -60,14 +64,4 @@ void repeatableRead() throws Exception { } } - @Test - void shouldNotSetContentLengthWhenEmptyBody() throws Exception { - ClientHttpRequest request = factory.createRequest(URI.create(baseUrl + "/header/Content-Length"), HttpMethod.POST); - try (ClientHttpResponse response = request.execute()) { - assertThat(response.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK); - String result = FileCopyUtils.copyToString(new InputStreamReader(response.getBody())); - assertThat(result).as("Invalid body").isEqualTo("Content-Length:null"); - } - } - }