Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
import java.net.URI;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
Expand Down Expand Up @@ -178,7 +179,8 @@ public DefaultEventSource(final CloseableHttpAsyncClient client,
final SseParser parser) {
this.client = Objects.requireNonNull(client, "client");
this.uri = Objects.requireNonNull(uri, "uri");
this.headers = new ConcurrentHashMap<>(Objects.requireNonNull(headers, "headers"));
this.headers = new ConcurrentSkipListMap<>(String.CASE_INSENSITIVE_ORDER);
this.headers.putAll(Objects.requireNonNull(headers, "headers"));
this.listener = listener != null ? listener : (id, type, data) -> { /* no-op */ };

if (scheduler != null) {
Expand Down Expand Up @@ -273,7 +275,9 @@ public void removeHeader(final String name) {
*/
@Override
public Map<String, String> getHeaders() {
return new ConcurrentHashMap<>(headers);
final Map<String, String> copy = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
copy.putAll(headers);
return copy;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@
*/
package org.apache.hc.client5.http.sse.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.net.URI;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -265,4 +268,43 @@ public V get(final long timeout, final TimeUnit unit) {
return null;
}
}

@Test
void headerNamesAreCaseInsensitive() {
final DefaultEventSource eventSource = new DefaultEventSource(
new CapturingClient(),
URI.create("http://localhost/sse"),
Collections.emptyMap(),
null);

eventSource.setHeader("Authorization", "one");
eventSource.setHeader("authorization", "two");

final Map<String, String> headers = eventSource.getHeaders();

assertEquals(1, headers.size());
assertEquals("two", headers.get("AUTHORIZATION"));

eventSource.removeHeader("AUTHORIZATION");

assertTrue(eventSource.getHeaders().isEmpty());
}

@Test
void initialHeaderNamesAreCaseInsensitive() {
final Map<String, String> initialHeaders = new LinkedHashMap<>();
initialHeaders.put("X-Test", "one");
initialHeaders.put("x-test", "two");

final DefaultEventSource eventSource = new DefaultEventSource(
new CapturingClient(),
URI.create("http://localhost/sse"),
initialHeaders,
null);

final Map<String, String> headers = eventSource.getHeaders();

assertEquals(1, headers.size());
assertEquals("two", headers.get("X-TEST"));
}
}
Loading