Migrate to new HttpHeaders API

See gh-45487

Co-authored-by: Phillip Webb <phil.webb@broadcom.com>
This commit is contained in:
Stéphane Nicoll 2025-05-07 13:09:53 -07:00 committed by Phillip Webb
parent 6fceab2c90
commit 5ea0674bad
17 changed files with 73 additions and 64 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-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.
@ -19,7 +19,9 @@ package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPOutputStream;
import zipkin2.reporter.BaseHttpSender;
@ -27,8 +29,6 @@ import zipkin2.reporter.BytesMessageSender;
import zipkin2.reporter.Encoding;
import zipkin2.reporter.HttpEndpointSupplier.Factory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.unit.DataSize;
/**
@ -61,20 +61,20 @@ abstract class HttpSender extends BaseHttpSender<URI, byte[]> {
@Override
protected void postSpans(URI endpoint, byte[] body) throws IOException {
MultiValueMap<String, String> headers = getDefaultHeaders();
Map<String, String> headers = getDefaultHeaders();
if (needsCompression(body)) {
body = compress(body);
headers.add("Content-Encoding", "gzip");
headers.put("Content-Encoding", "gzip");
}
postSpans(endpoint, headers, body);
}
abstract void postSpans(URI endpoint, MultiValueMap<String, String> headers, byte[] body) throws IOException;
abstract void postSpans(URI endpoint, Map<String, String> headers, byte[] body) throws IOException;
MultiValueMap<String, String> getDefaultHeaders() {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("b3", "0");
headers.add("Content-Type", this.encoding.mediaType());
Map<String, String> getDefaultHeaders() {
Map<String, String> headers = new LinkedHashMap<>();
headers.put("b3", "0");
headers.put("Content-Type", this.encoding.mediaType());
return headers;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-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.
@ -25,12 +25,11 @@ import java.net.http.HttpRequest.Builder;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.util.Map;
import zipkin2.reporter.Encoding;
import zipkin2.reporter.HttpEndpointSupplier.Factory;
import org.springframework.util.MultiValueMap;
/**
* A {@link HttpSender} which uses the JDK {@link HttpClient} for HTTP communication.
*
@ -50,12 +49,12 @@ class ZipkinHttpClientSender extends HttpSender {
}
@Override
void postSpans(URI endpoint, MultiValueMap<String, String> headers, byte[] body) throws IOException {
void postSpans(URI endpoint, Map<String, String> headers, byte[] body) throws IOException {
Builder request = HttpRequest.newBuilder()
.POST(BodyPublishers.ofByteArray(body))
.uri(endpoint)
.timeout(this.readTimeout);
headers.forEach((name, values) -> values.forEach((value) -> request.header(name, value)));
headers.forEach((name, value) -> request.header(name, value));
try {
HttpResponse<Void> response = this.httpClient.send(request.build(), BodyHandlers.discarding());
if (response.statusCode() / 100 != 2) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-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.
@ -19,11 +19,13 @@ package org.springframework.boot.actuate.web.exchanges.reactive;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.actuate.web.exchanges.RecordableHttpRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
/**
@ -35,7 +37,7 @@ class RecordableServerHttpRequest implements RecordableHttpRequest {
private final String method;
private final Map<String, List<String>> headers;
private final HttpHeaders headers;
private final URI uri;
@ -66,7 +68,9 @@ class RecordableServerHttpRequest implements RecordableHttpRequest {
@Override
public Map<String, List<String>> getHeaders() {
return new LinkedHashMap<>(this.headers);
Map<String, List<String>> headers = new LinkedHashMap<>();
this.headers.forEach(headers::put);
return Collections.unmodifiableMap(headers);
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-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.
@ -16,6 +16,7 @@
package org.springframework.boot.actuate.web.exchanges.reactive;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -38,7 +39,9 @@ class RecordableServerHttpResponse implements RecordableHttpResponse {
RecordableServerHttpResponse(ServerHttpResponse response) {
this.status = (response.getStatusCode() != null) ? response.getStatusCode().value() : HttpStatus.OK.value();
this.headers = new LinkedHashMap<>(response.getHeaders());
Map<String, List<String>> headers = new LinkedHashMap<>();
response.getHeaders().forEach(headers::put);
this.headers = Collections.unmodifiableMap(headers);
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-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.
@ -23,11 +23,8 @@ import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;
@ -47,14 +44,11 @@ import static org.mockito.Mockito.mock;
*/
class HttpExchangeTests {
private static final Map<String, List<String>> AUTHORIZATION_HEADER = Map.of(HttpHeaders.AUTHORIZATION,
Arrays.asList("secret"));
private static final HttpHeaders AUTHORIZATION_HEADER = ofSingleHttpHeader(HttpHeaders.AUTHORIZATION, "secret");
private static final Map<String, List<String>> COOKIE_HEADER = Map.of(HttpHeaders.COOKIE,
Arrays.asList("test=test"));
private static final HttpHeaders COOKIE_HEADER = ofSingleHttpHeader(HttpHeaders.COOKIE, "test=test");
private static final Map<String, List<String>> SET_COOKIE_HEADER = Map.of(HttpHeaders.SET_COOKIE,
Arrays.asList("test=test"));
private static final HttpHeaders SET_COOKIE_HEADER = ofSingleHttpHeader(HttpHeaders.SET_COOKIE, "test=test");
private static final Supplier<Principal> NO_PRINCIPAL = () -> null;
@ -298,31 +292,33 @@ class HttpExchangeTests {
}
private RecordableHttpRequest createRequest() {
return createRequest(Collections.singletonMap(HttpHeaders.ACCEPT, Arrays.asList("application/json")));
return createRequest(ofSingleHttpHeader(HttpHeaders.ACCEPT, "application/json"));
}
private RecordableHttpRequest createRequest(Map<String, List<String>> headers) {
@SuppressWarnings("removal")
private RecordableHttpRequest createRequest(HttpHeaders headers) {
RecordableHttpRequest request = mock(RecordableHttpRequest.class);
given(request.getMethod()).willReturn("GET");
given(request.getUri()).willReturn(URI.create("https://api.example.com"));
given(request.getHeaders()).willReturn(new HashMap<>(headers));
given(request.getHeaders()).willReturn(new HashMap<>(headers.asMultiValueMap()));
given(request.getRemoteAddress()).willReturn("127.0.0.1");
return request;
}
private RecordableHttpResponse createResponse() {
return createResponse(Collections.singletonMap(HttpHeaders.CONTENT_TYPE, Arrays.asList("application/json")));
return createResponse(ofSingleHttpHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
}
private RecordableHttpResponse createResponse(Map<String, List<String>> headers) {
@SuppressWarnings("removal")
private RecordableHttpResponse createResponse(HttpHeaders headers) {
RecordableHttpResponse response = mock(RecordableHttpResponse.class);
given(response.getStatus()).willReturn(204);
given(response.getHeaders()).willReturn(new HashMap<>(headers));
given(response.getHeaders()).willReturn(new HashMap<>(headers.asMultiValueMap()));
return response;
}
private Map<String, List<String>> mixedCase(Map<String, List<String>> headers) {
Map<String, List<String>> result = new LinkedHashMap<>();
private HttpHeaders mixedCase(HttpHeaders headers) {
HttpHeaders result = new HttpHeaders();
headers.forEach((key, value) -> result.put(mixedCase(key), value));
return result;
}
@ -336,4 +332,10 @@ class HttpExchangeTests {
return output.toString();
}
private static HttpHeaders ofSingleHttpHeader(String header, String... values) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.put(header, List.of(values));
return httpHeaders;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-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.
@ -45,6 +45,7 @@ import org.springframework.mock.http.client.MockClientHttpResponse;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
@ -168,7 +169,7 @@ class RestTemplateAutoConfigurationTests {
given(requestFactory.createRequest(any(), any())).willReturn(request);
RestTemplate restTemplate = builder.requestFactory(() -> requestFactory).build();
restTemplate.getForEntity("http://localhost:8080/test", String.class);
assertThat(request.getHeaders()).containsEntry("spring", Collections.singletonList("boot"));
assertThat(request.getHeaders().headerSet()).contains(entry("spring", Collections.singletonList("boot")));
});
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-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.
@ -342,7 +342,7 @@ class LiveReloadServerTests {
requestHeaders.forEach((key, value) -> uppercaseRequestHeaders.put(key.toUpperCase(Locale.ROOT), value));
requestHeaders.clear();
requestHeaders.putAll(uppercaseRequestHeaders);
requestHeaders.putAll(this.headers);
this.headers.forEach(requestHeaders::put);
}
@Override

View File

@ -481,10 +481,10 @@ class TestRestTemplateTests {
ClientHttpRequest request = ReflectionTestUtils.invokeMethod(testRestTemplate.getRestTemplate(),
"createRequest", URI.create("http://localhost"), HttpMethod.POST);
if (username == null) {
assertThat(request.getHeaders()).doesNotContainKey(HttpHeaders.AUTHORIZATION);
assertThat(request.getHeaders().headerNames()).doesNotContain(HttpHeaders.AUTHORIZATION);
}
else {
assertThat(request.getHeaders()).containsKeys(HttpHeaders.AUTHORIZATION);
assertThat(request.getHeaders().headerNames()).contains(HttpHeaders.AUTHORIZATION);
assertThat(request.getHeaders().get(HttpHeaders.AUTHORIZATION)).containsExactly("Basic "
+ Base64.getEncoder().encodeToString(String.format("%s:%s", username, password).getBytes()));
}

View File

@ -44,7 +44,7 @@ class BasicAuthentication {
}
void applyTo(HttpHeaders headers) {
if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
if (!headers.containsHeader(HttpHeaders.AUTHORIZATION)) {
headers.setBasicAuth(this.username, this.password, this.charset);
}
}

View File

@ -319,7 +319,7 @@ class RestTemplateBuilderTests {
void basicAuthenticationShouldApply() {
RestTemplate template = this.builder.basicAuthentication("spring", "boot", StandardCharsets.UTF_8).build();
ClientHttpRequest request = createRequest(template);
assertThat(request.getHeaders()).containsOnlyKeys(HttpHeaders.AUTHORIZATION);
assertThat(request.getHeaders().headerNames()).containsOnly(HttpHeaders.AUTHORIZATION);
assertThat(request.getHeaders().get(HttpHeaders.AUTHORIZATION)).containsExactly("Basic c3ByaW5nOmJvb3Q=");
}
@ -327,7 +327,7 @@ class RestTemplateBuilderTests {
void defaultHeaderAddsHeader() {
RestTemplate template = this.builder.defaultHeader("spring", "boot").build();
ClientHttpRequest request = createRequest(template);
assertThat(request.getHeaders()).contains(entry("spring", Collections.singletonList("boot")));
assertThat(request.getHeaders().headerSet()).contains(entry("spring", Collections.singletonList("boot")));
}
@Test
@ -336,7 +336,7 @@ class RestTemplateBuilderTests {
String[] values = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE };
RestTemplate template = this.builder.defaultHeader(name, values).build();
ClientHttpRequest request = createRequest(template);
assertThat(request.getHeaders()).contains(entry(name, Arrays.asList(values)));
assertThat(request.getHeaders().headerSet()).contains(entry(name, Arrays.asList(values)));
}
@Test // gh-17885
@ -344,7 +344,7 @@ class RestTemplateBuilderTests {
RestTemplate template = this.builder.defaultHeader("spring", "boot").build();
MockRestServiceServer.bindTo(template).build();
ClientHttpRequest request = createRequest(template);
assertThat(request.getHeaders()).contains(entry("spring", Collections.singletonList("boot")));
assertThat(request.getHeaders().headerSet()).contains(entry("spring", Collections.singletonList("boot")));
}
@Test
@ -361,7 +361,7 @@ class RestTemplateBuilderTests {
.requestCustomizers((request) -> request.getHeaders().add("spring", "framework"))
.build();
ClientHttpRequest request = createRequest(template);
assertThat(request.getHeaders()).contains(entry("spring", Collections.singletonList("framework")));
assertThat(request.getHeaders().headerSet()).contains(entry("spring", Collections.singletonList("framework")));
}
@Test
@ -371,7 +371,7 @@ class RestTemplateBuilderTests {
.additionalRequestCustomizers((request) -> request.getHeaders().add("for", "java"))
.build();
ClientHttpRequest request = createRequest(template);
assertThat(request.getHeaders()).contains(entry("spring", Collections.singletonList("framework")))
assertThat(request.getHeaders().headerSet()).contains(entry("spring", Collections.singletonList("framework")))
.contains(entry("for", Collections.singletonList("java")));
}

View File

@ -662,7 +662,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
protected void assertResponseIsNotCompressed(ResponseEntity<Void> response) {
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getHeaders().keySet()).doesNotContain("X-Test-Compressed");
assertThat(response.getHeaders().headerNames()).doesNotContain("X-Test-Compressed");
}
protected void assertForwardHeaderIsUsed(AbstractReactiveWebServerFactory factory) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-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.
@ -46,7 +46,7 @@ abstract class AbstractSampleActuatorCustomSecurityTests {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = restTemplate().getForEntity(getPath() + "/", Map.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
assertThat(entity.getHeaders().headerNames()).doesNotContain("Set-Cookie");
}
@Test

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-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.
@ -53,7 +53,7 @@ class ManagementPathSampleActuatorApplicationTests {
void testHomeIsSecure() {
ResponseEntity<Map<String, Object>> entity = asMapEntity(this.restTemplate.getForEntity("/", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
assertThat(entity.getHeaders().headerNames()).doesNotContain("Set-Cookie");
}
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-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.
@ -57,7 +57,7 @@ class SampleActuatorApplicationTests {
void testHomeIsSecure() {
ResponseEntity<Map<String, Object>> entity = asMapEntity(this.restTemplate.getForEntity("/", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
assertThat(entity.getHeaders().headerNames()).doesNotContain("Set-Cookie");
}
@Test

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-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.
@ -61,7 +61,7 @@ class ServletPathSampleActuatorApplicationTests {
void testHomeIsSecure() {
ResponseEntity<Map<String, Object>> entity = asMapEntity(this.restTemplate.getForEntity("/spring/", Map.class));
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
assertThat(entity.getHeaders()).doesNotContainKey("Set-Cookie");
assertThat(entity.getHeaders().headerNames()).doesNotContain("Set-Cookie");
}
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-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.
@ -54,7 +54,7 @@ class MessageControllerWebTests {
assertThat(this.mvc.post().uri("/").param("text", "FOO text").param("summary", "FOO"))
.hasStatus(HttpStatus.FOUND)
.headers()
.hasEntrySatisfying("Location",
.hasHeaderSatisfying("Location",
(values) -> assertThat(values).hasSize(1)
.element(0)
.satisfies(HamcrestCondition.matching(RegexMatcher.matches("/[0-9]+"))));

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-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.
@ -54,7 +54,7 @@ class MessageControllerWebTests {
assertThat(this.mvc.post().uri("/").param("text", "FOO text").param("summary", "FOO"))
.hasStatus(HttpStatus.FOUND)
.headers()
.hasEntrySatisfying("Location",
.hasHeaderSatisfying("Location",
(values) -> assertThat(values).hasSize(1)
.element(0)
.satisfies(HamcrestCondition.matching(RegexMatcher.matches("/[0-9]+"))));