HttpEntity.EMPTY should be immutable

See gh-34812

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
This commit is contained in:
Yanming Zhou
2025-04-24 17:36:29 +08:00
committed by rstoyanchev
parent 5013d6d771
commit d7c13d6518
2 changed files with 10 additions and 1 deletions

View File

@@ -49,6 +49,7 @@ import org.springframework.util.ObjectUtils;
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Yanming Zhou
* @since 3.0.2
* @param <T> the body type
* @see org.springframework.web.client.RestTemplate
@@ -60,7 +61,7 @@ public class HttpEntity<T> {
/**
* The empty {@code HttpEntity}, with no body or headers.
*/
public static final HttpEntity<?> EMPTY = new HttpEntity<>();
public static final HttpEntity<?> EMPTY = new HttpEntity<>(HttpHeaders.EMPTY);
private final HttpHeaders headers;

View File

@@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
* @author Yanming Zhou
*/
class HttpEntityTests {
@@ -123,4 +124,11 @@ class HttpEntityTests {
assertThat(requestEntity2).isEqualTo(requestEntity);
}
@Test
void emptyHttpEntityShouldBeImmutable() {
HttpHeaders newHeaders = new HttpHeaders(HttpEntity.EMPTY.getHeaders());
newHeaders.add("Authorization", "Bearer some-token");
assertThat(HttpEntity.EMPTY.getHeaders().headerNames()).isEmpty();
}
}