Retain entry set order in read-only HttpHeaders

Prior to this commit, the entry set of read-only HttpHeaders lost the
original headers' ordering.

The changes in commit ce7278aaf4 introduced a regression in the read-only
HttpHeaders support. Specifically, the implementation of entrySet() in
the internal ReadOnlyHttpHeaders class converted the original entry set
to an immutable, non-ordered set of immutable entries.

This commit fixes this issue by converting the original entry set to an
immutable, ordered set of immutable entries.

Closes gh-23551
This commit is contained in:
Sam Brannen
2019-08-31 12:26:06 +02:00
parent a496353770
commit 9729b460f1
2 changed files with 25 additions and 4 deletions

View File

@@ -32,6 +32,7 @@ import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.TimeZone;
import org.hamcrest.Matchers;
@@ -47,6 +48,7 @@ import static org.junit.Assert.*;
* @author Sebastien Deleuze
* @author Brian Clozel
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class HttpHeadersTests {
@@ -557,4 +559,20 @@ public class HttpHeadersTests {
assertEquals("Bearer foo", authorization);
}
@Test
public void readOnlyHttpHeadersRetainEntrySetOrder() {
headers.add("aardvark", "enigma");
headers.add("beaver", "enigma");
headers.add("cat", "enigma");
headers.add("dog", "enigma");
headers.add("elephant", "enigma");
String[] expectedKeys = new String[] { "aardvark", "beaver", "cat", "dog", "elephant" };
assertArrayEquals(expectedKeys, headers.entrySet().stream().map(Entry::getKey).toArray());
HttpHeaders readOnlyHttpHeaders = HttpHeaders.readOnlyHttpHeaders(headers);
assertArrayEquals(expectedKeys, readOnlyHttpHeaders.entrySet().stream().map(Entry::getKey).toArray());
}
}