Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes case insensitive headerMap #3517

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jooby/src/main/java/io/jooby/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ public interface Context extends Registry {
/**
* Header as single-value map.
*
* @return Header as single-value map.
* @return Header as single-value map, with case insensitive keys.
*/
@NonNull Map<String, String> headerMap();

Expand Down
6 changes: 5 additions & 1 deletion jooby/src/main/java/io/jooby/DefaultContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.slf4j.Logger;
Expand Down Expand Up @@ -221,7 +222,10 @@ default boolean matches(String pattern) {

@Override
@NonNull default Map<String, String> headerMap() {
return header().toMap();
final var header = header();
Map<String, String> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
header.toMultimap().forEach((k, v) -> map.put(k, v.get(0)));
return map;
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions tests/src/test/java/io/jooby/test/Issue2357.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void headersShouldBeCaseInsensitive(ServerTestRunner runner) {
Assertions.assertEquals("value1", ctx.header("x-header1").value());
Assertions.assertEquals("value1", ctx.header("X-HEADER1").value());
Assertions.assertEquals("value1", ctx.header("X-hEaDeR1").value());
Assertions.assertEquals("value1", ctx.headerMap().get("x-header1"));
Assertions.assertEquals("value1", ctx.headerMap().get("X-HEADER1"));
Assertions.assertEquals("value1", ctx.headerMap().get("X-hEaDeR1"));
return "OK";
}))
.ready(
Expand Down