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-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;
}
}