Skip to content

Commit

Permalink
HttpHeaders#equals handles wrapping correctly
Browse files Browse the repository at this point in the history
Closes gh-25034
  • Loading branch information
rstoyanchev committed May 8, 2020
1 parent 436e9d8 commit b22e670
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
32 changes: 15 additions & 17 deletions spring-web/src/main/java/org/springframework/http/HttpHeaders.java
Expand Up @@ -1677,8 +1677,14 @@ public boolean equals(Object other) {
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
Expand All @@ -1693,33 +1699,25 @@ public String toString() {


/**
* 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) {
Assert.notNull(headers, "HttpHeaders must not be null");
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);
}

/**
Expand Down
@@ -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.
Expand Down Expand Up @@ -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);
}


Expand Down
Expand Up @@ -678,4 +678,12 @@ public void readOnlyHttpHeadersRetainEntrySetOrder() {
assertArrayEquals(expectedKeys, readOnlyHttpHeaders.entrySet().stream().map(Entry::getKey).toArray());
}

@Test // gh-25034
public void equalsUnwrapsHttpHeaders() {
HttpHeaders headers1 = new HttpHeaders();
HttpHeaders headers2 = new HttpHeaders(new HttpHeaders(headers1));

assertEquals(headers1, headers2);
assertEquals(headers2, headers1);
}
}

0 comments on commit b22e670

Please sign in to comment.