Skip to content

Commit

Permalink
case insensitive grpc headers/metadata (#120)
Browse files Browse the repository at this point in the history
* lowercase key

* test

* pr fix
  • Loading branch information
hcura authored Jan 3, 2025
1 parent ef591da commit 723c025
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;

Expand All @@ -57,7 +58,7 @@ public static Metadata readMetadata(MultiMap headers) {
byte[][] array = new byte[entries.size() * 2][];
int idx = 0;
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String key = entry.getKey().toLowerCase(Locale.ROOT);
array[idx++] = key.getBytes(StandardCharsets.UTF_8);
String value = entry.getValue();
byte[] data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,32 @@ public void testReadMetadata() {
headers.add("key0", "value2");

Metadata metadata = Utils.readMetadata(headers);
assertEquals(metadata.keys().size(), 2);
assertEquals(2, metadata.keys().size());

List<String> l = StreamSupport.stream(metadata.getAll(Metadata.Key.of("key0", Metadata.ASCII_STRING_MARSHALLER)).spliterator(), false)
.collect(Collectors.toList());
assertEquals(l.size(), 2);
assertEquals(2, l.size());
assertTrue(l.contains("value0"));
assertTrue(l.contains("value2"));

l = StreamSupport.stream(metadata.getAll(Metadata.Key.of("key1", Metadata.ASCII_STRING_MARSHALLER)).spliterator(), false)
.collect(Collectors.toList());
assertEquals(l.size(), 1);
assertEquals(1, l.size());
assertTrue(l.contains("value1"));
}

@Test
public void lowercaseMetadata() {
MultiMap headers = MultiMap.caseInsensitiveMultiMap();

headers.add("Authorization", "test");

Metadata metadata = Utils.readMetadata(headers);
assertEquals(1, metadata.keys().size());

String authorization = metadata.get(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER));

assertEquals("test", authorization);
}

}

0 comments on commit 723c025

Please sign in to comment.