HttpHeaders#equals handles wrapping correctly

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

View File

@ -1747,8 +1747,14 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
if (!(other instanceof HttpHeaders)) { if (!(other instanceof HttpHeaders)) {
return false; return false;
} }
HttpHeaders otherHeaders = (HttpHeaders) other; return unwrap(this).equals(unwrap((HttpHeaders) other));
return this.headers.equals(otherHeaders.headers); }
private static MultiValueMap<String, String> unwrap(HttpHeaders headers) {
while (headers.headers instanceof HttpHeaders) {
headers = (HttpHeaders) headers.headers;
}
return headers.headers;
} }
@Override @Override
@ -1763,20 +1769,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"); Assert.notNull(headers, "HttpHeaders must not be null");
if (headers instanceof ReadOnlyHttpHeaders) { return (headers instanceof ReadOnlyHttpHeaders ?
return headers; (HttpHeaders) headers : new ReadOnlyHttpHeaders(headers));
}
else {
return 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 * @since 5.1.1
*/ */
public static HttpHeaders writableHttpHeaders(HttpHeaders headers) { public static HttpHeaders writableHttpHeaders(HttpHeaders headers) {
@ -1784,12 +1787,7 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
if (headers == EMPTY) { if (headers == EMPTY) {
return new HttpHeaders(); return new HttpHeaders();
} }
else if (headers instanceof ReadOnlyHttpHeaders) { return (headers instanceof ReadOnlyHttpHeaders ? new HttpHeaders(headers.headers) : headers);
return new HttpHeaders(headers.headers);
}
else {
return 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; private List<MediaType> cachedAccept;
ReadOnlyHttpHeaders(HttpHeaders headers) { ReadOnlyHttpHeaders(MultiValueMap<String, String> headers) {
super(headers.headers); super(headers);
} }

View File

@ -703,4 +703,12 @@ public class HttpHeadersTests {
assertThat(readOnlyHttpHeaders.entrySet()).extracting(Entry::getKey).containsExactly(expectedKeys); assertThat(readOnlyHttpHeaders.entrySet()).extracting(Entry::getKey).containsExactly(expectedKeys);
} }
@Test // gh-25034
public void equalsUnwrapsHttpHeaders() {
HttpHeaders headers1 = new HttpHeaders();
HttpHeaders headers2 = new HttpHeaders(new HttpHeaders(headers1));
assertThat(headers1).isEqualTo(headers2);
assertThat(headers2).isEqualTo(headers1);
}
} }