Skip to content

Commit c88ba6c

Browse files
committed
Polishing contribution
Closes gh-34812
1 parent d7c13d6 commit c88ba6c

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

spring-web/src/main/java/org/springframework/http/HttpEntity.java

+3-3
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-2025 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.
@@ -49,7 +49,6 @@
4949
*
5050
* @author Arjen Poutsma
5151
* @author Juergen Hoeller
52-
* @author Yanming Zhou
5352
* @since 3.0.2
5453
* @param <T> the body type
5554
* @see org.springframework.web.client.RestTemplate
@@ -59,7 +58,8 @@
5958
public class HttpEntity<T> {
6059

6160
/**
62-
* The empty {@code HttpEntity}, with no body or headers.
61+
* An {@code HttpEntity} instance with a {@code null} body and
62+
* {@link HttpHeaders#EMPTY empty headers}.
6363
*/
6464
public static final HttpEntity<?> EMPTY = new HttpEntity<>(HttpHeaders.EMPTY);
6565

spring-web/src/test/java/org/springframework/http/HttpEntityTests.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -26,6 +26,8 @@
2626
import static org.assertj.core.api.Assertions.assertThat;
2727

2828
/**
29+
* Unit tests for {@link HttpEntity}.
30+
*
2931
* @author Arjen Poutsma
3032
* @author Yanming Zhou
3133
*/
@@ -35,6 +37,7 @@ class HttpEntityTests {
3537
void noHeaders() {
3638
String body = "foo";
3739
HttpEntity<String> entity = new HttpEntity<>(body);
40+
3841
assertThat(entity.getBody()).isSameAs(body);
3942
assertThat(entity.getHeaders()).isEmpty();
4043
}
@@ -45,6 +48,7 @@ void httpHeaders() {
4548
headers.setContentType(MediaType.TEXT_PLAIN);
4649
String body = "foo";
4750
HttpEntity<String> entity = new HttpEntity<>(body, headers);
51+
4852
assertThat(entity.getBody()).isEqualTo(body);
4953
assertThat(entity.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
5054
assertThat(entity.getHeaders().getFirst("Content-Type")).isEqualTo("text/plain");
@@ -56,6 +60,7 @@ void multiValueMap() {
5660
map.set("Content-Type", "text/plain");
5761
String body = "foo";
5862
HttpEntity<String> entity = new HttpEntity<>(body, map);
63+
5964
assertThat(entity.getBody()).isEqualTo(body);
6065
assertThat(entity.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
6166
assertThat(entity.getHeaders().getFirst("Content-Type")).isEqualTo("text/plain");
@@ -124,11 +129,14 @@ void requestEntity() {
124129
assertThat(requestEntity2).isEqualTo(requestEntity);
125130
}
126131

127-
@Test
128-
void emptyHttpEntityShouldBeImmutable() {
129-
HttpHeaders newHeaders = new HttpHeaders(HttpEntity.EMPTY.getHeaders());
130-
newHeaders.add("Authorization", "Bearer some-token");
131-
assertThat(HttpEntity.EMPTY.getHeaders().headerNames()).isEmpty();
132+
@Test // gh-34806
133+
void mutateEmptyInstanceHeaders() {
134+
HttpHeaders headers = new HttpHeaders(HttpEntity.EMPTY.getHeaders());
135+
headers.add("Authorization", "Bearer some-token");
136+
137+
assertThat(HttpEntity.EMPTY.getHeaders())
138+
.as("Headers of HttpEntity.EMPTY should remain empty")
139+
.isEmpty();
132140
}
133141

134142
}

0 commit comments

Comments
 (0)