From f4d445112d1cd778bd4ee2756beb04b83dc1967a Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Tue, 7 Aug 2018 18:31:04 +0100 Subject: [PATCH] Tidy up getting headers in HttpMessage This makes `getHeaders(String)` properly case-insensitive. --- .../selenium/remote/http/HttpMessage.java | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/java/client/src/org/openqa/selenium/remote/http/HttpMessage.java b/java/client/src/org/openqa/selenium/remote/http/HttpMessage.java index 392396980feca..0372865b678d6 100644 --- a/java/client/src/org/openqa/selenium/remote/http/HttpMessage.java +++ b/java/client/src/org/openqa/selenium/remote/http/HttpMessage.java @@ -33,8 +33,10 @@ import java.io.InputStream; import java.nio.charset.Charset; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; class HttpMessage { @@ -69,21 +71,29 @@ public Iterable getHeaderNames() { } public Iterable getHeaders(String name) { - return headers.get(name); - } - - public String getHeader(String name) { return headers.entries().stream() .filter(e -> Objects.nonNull(e.getKey())) .filter(e -> e.getKey().equalsIgnoreCase(name.toLowerCase())) .map(Map.Entry::getValue) - .findFirst() - .orElse(null); + .collect(Collectors.toList()); + } + + public String getHeader(String name) { + Iterable initialHeaders = getHeaders(name); + if (initialHeaders == null) { + return null; + } + + Iterator headers = initialHeaders.iterator(); + if (headers.hasNext()) { + return headers.next(); + } + return null; } public void setHeader(String name, String value) { removeHeader(name); - headers.put(name, value); + addHeader(name, value); } public void addHeader(String name, String value) {