Skip to content

Commit d7c13d6

Browse files
quaffrstoyanchev
authored andcommitted
HttpEntity.EMPTY should be immutable
See gh-34812 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 5013d6d commit d7c13d6

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
*
5050
* @author Arjen Poutsma
5151
* @author Juergen Hoeller
52+
* @author Yanming Zhou
5253
* @since 3.0.2
5354
* @param <T> the body type
5455
* @see org.springframework.web.client.RestTemplate
@@ -60,7 +61,7 @@ public class HttpEntity<T> {
6061
/**
6162
* The empty {@code HttpEntity}, with no body or headers.
6263
*/
63-
public static final HttpEntity<?> EMPTY = new HttpEntity<>();
64+
public static final HttpEntity<?> EMPTY = new HttpEntity<>(HttpHeaders.EMPTY);
6465

6566

6667
private final HttpHeaders headers;

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

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
/**
2929
* @author Arjen Poutsma
30+
* @author Yanming Zhou
3031
*/
3132
class HttpEntityTests {
3233

@@ -123,4 +124,11 @@ void requestEntity() {
123124
assertThat(requestEntity2).isEqualTo(requestEntity);
124125
}
125126

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+
}
133+
126134
}

0 commit comments

Comments
 (0)