Skip to content

Commit

Permalink
Tidy up getting headers in HttpMessage
Browse files Browse the repository at this point in the history
This makes `getHeaders(String)` properly case-insensitive.
  • Loading branch information
shs96c committed Aug 7, 2018
1 parent 20e8ee0 commit f4d4451
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions java/client/src/org/openqa/selenium/remote/http/HttpMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -69,21 +71,29 @@ public Iterable<String> getHeaderNames() {
}

public Iterable<String> 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<String> initialHeaders = getHeaders(name);
if (initialHeaders == null) {
return null;
}

Iterator<String> 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) {
Expand Down

0 comments on commit f4d4451

Please sign in to comment.