Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
clarify lowercasing implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pettermahlen committed Apr 28, 2016
1 parent 60f2360 commit 885eaa0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
41 changes: 25 additions & 16 deletions apollo-api/src/main/java/com/spotify/apollo/HeadersValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import javax.annotation.Nonnull;

Expand All @@ -44,28 +43,38 @@ public Optional<String> get(@Nonnull String name) {
}

static Headers create(Map<String, String> headers) {
Map<String, String> allLowerCaseKeys = headers.keySet().stream()
.filter(HeadersValue::hasNonLowerCase)
.findAny()
.map(lowerCaseKeys(headers))
.orElse(headers);
return new AutoValue_HeadersValue(convertKeysToLowerCase(headers));
}

return new AutoValue_HeadersValue(allLowerCaseKeys);
private static Map<String, String> convertKeysToLowerCase(Map<String, String> headers) {
// we expect the most common case to be that all header names are already lower-case,
// since most requests we handle are internal, originating from other Apollo services.
// hence, attempt to save some allocation through reusing the original map in that case.
if (keysAlreadyLowerCase(headers)) {
return headers;
}

return toLowerCaseKeys(headers);
}

private static Function<String, Map<String, String>> lowerCaseKeys(Map<String, String> headers) {
return s -> {
Map<String, String> result = new LinkedHashMap<>(headers.size());
private static boolean keysAlreadyLowerCase(Map<String, String> headers) {
return !headers.keySet().stream()
.filter(HeadersValue::hasUpperCase)
.findAny()
.isPresent();
}

for (Map.Entry<String, String> entry : headers.entrySet()) {
result.put(entry.getKey().toLowerCase(), entry.getValue());
}
private static Map<String, String> toLowerCaseKeys(Map<String, String> headers) {
Map<String, String> result = new LinkedHashMap<>(headers.size());

for (Map.Entry<String, String> entry : headers.entrySet()) {
result.put(entry.getKey().toLowerCase(), entry.getValue());
}

return result;
};
return result;
}

private static boolean hasNonLowerCase(String s) {
private static boolean hasUpperCase(String s) {
for (char c : s.toCharArray()) {
if (ASCII_UPPER_CASE.matches(c)) {
return true;
Expand Down
50 changes: 50 additions & 0 deletions apollo-api/src/test/java/com/spotify/apollo/HeadersValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* -\-\-
* Spotify Apollo API Interfaces
* --
* Copyright (C) 2013 - 2016 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.apollo;

import com.google.common.collect.ImmutableMap;

import org.junit.Test;

import java.util.Map;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.junit.Assert.assertThat;

public class HeadersValueTest {
private Map<String, String> headers;

@Test
public void shouldReuseMapIfAlreadyLowerCase() throws Exception {
headers = ImmutableMap.of("key", "value", "another-key", "val");

assertThat(HeadersValue.create(headers).asMap(), is(sameInstance(headers)));
}

@Test
public void shouldConvertKeysToLowerCase() throws Exception {
headers = ImmutableMap.of("KEY", "value", "Another-Key", "val");

assertThat(HeadersValue.create(headers).asMap(),
equalTo(ImmutableMap.of("key", "value", "another-key", "val")));
}
}

0 comments on commit 885eaa0

Please sign in to comment.