HttpHeaders#equals handles wrapping correctly

Closes gh-25034
This commit is contained in:
Rossen Stoyanchev
2020-05-08 09:37:37 +01:00
parent 436e9d8cdc
commit b22e670668
3 changed files with 26 additions and 20 deletions

View File

@@ -1677,8 +1677,14 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
if (!(other instanceof HttpHeaders)) {
return false;
}
HttpHeaders otherHeaders = (HttpHeaders) other;
return this.headers.equals(otherHeaders.headers);
return unwrap(this).equals(unwrap((HttpHeaders) other));
}
private static MultiValueMap<String, String> unwrap(HttpHeaders headers) {
while (headers.headers instanceof HttpHeaders) {
headers = (HttpHeaders) headers.headers;
}
return headers.headers;
}
@Override
@@ -1693,20 +1699,17 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
/**
* Return an {@code HttpHeaders} object that can only be read, not written to.
* Apply a read-only {@code HttpHeaders} wrapper around the given headers.
*/
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
public static HttpHeaders readOnlyHttpHeaders(MultiValueMap<String, String> headers) {
Assert.notNull(headers, "HttpHeaders must not be null");
if (headers instanceof ReadOnlyHttpHeaders) {
return headers;
}
else {
return new ReadOnlyHttpHeaders(headers);
}
return (headers instanceof ReadOnlyHttpHeaders ?
(HttpHeaders) headers : new ReadOnlyHttpHeaders(headers));
}
/**
* Return an {@code HttpHeaders} object that can be read and written to.
* Remove any read-only wrapper that may have been previously applied around
* the given headers via {@link #readOnlyHttpHeaders(MultiValueMap)}.
* @since 5.1.1
*/
public static HttpHeaders writableHttpHeaders(HttpHeaders headers) {
@@ -1714,12 +1717,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
if (headers == EMPTY) {
return new HttpHeaders();
}
else if (headers instanceof ReadOnlyHttpHeaders) {
return new HttpHeaders(headers.headers);
}
else {
return headers;
}
return (headers instanceof ReadOnlyHttpHeaders ? new HttpHeaders(headers.headers) : headers);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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,8 +46,8 @@ class ReadOnlyHttpHeaders extends HttpHeaders {
private List<MediaType> cachedAccept;
ReadOnlyHttpHeaders(HttpHeaders headers) {
super(headers.headers);
ReadOnlyHttpHeaders(MultiValueMap<String, String> headers) {
super(headers);
}