Polishing contribution

Closes gh-34812
This commit is contained in:
rstoyanchev
2025-04-28 14:22:01 +01:00
parent d7c13d6518
commit c88ba6c90e
2 changed files with 17 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@@ -26,6 +26,8 @@ import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link HttpEntity}.
*
* @author Arjen Poutsma
* @author Yanming Zhou
*/
@@ -35,6 +37,7 @@ class HttpEntityTests {
void noHeaders() {
String body = "foo";
HttpEntity<String> entity = new HttpEntity<>(body);
assertThat(entity.getBody()).isSameAs(body);
assertThat(entity.getHeaders()).isEmpty();
}
@@ -45,6 +48,7 @@ class HttpEntityTests {
headers.setContentType(MediaType.TEXT_PLAIN);
String body = "foo";
HttpEntity<String> entity = new HttpEntity<>(body, headers);
assertThat(entity.getBody()).isEqualTo(body);
assertThat(entity.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
assertThat(entity.getHeaders().getFirst("Content-Type")).isEqualTo("text/plain");
@@ -56,6 +60,7 @@ class HttpEntityTests {
map.set("Content-Type", "text/plain");
String body = "foo";
HttpEntity<String> entity = new HttpEntity<>(body, map);
assertThat(entity.getBody()).isEqualTo(body);
assertThat(entity.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
assertThat(entity.getHeaders().getFirst("Content-Type")).isEqualTo("text/plain");
@@ -124,11 +129,14 @@ 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();
@Test // gh-34806
void mutateEmptyInstanceHeaders() {
HttpHeaders headers = new HttpHeaders(HttpEntity.EMPTY.getHeaders());
headers.add("Authorization", "Bearer some-token");
assertThat(HttpEntity.EMPTY.getHeaders())
.as("Headers of HttpEntity.EMPTY should remain empty")
.isEmpty();
}
}