Skip to content
Permalink
Browse files
fix: allow using case-insensitive user-agent key (#1110)
  • Loading branch information
olavloite committed Apr 29, 2021
1 parent 1106644 commit f4f9e43ce102788b81c032df8da223108e484252
@@ -166,6 +166,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
@@ -493,7 +494,14 @@ public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCalla

private static HeaderProvider headerProviderWithUserAgentFrom(HeaderProvider headerProvider) {
final Map<String, String> headersWithUserAgent = new HashMap<>(headerProvider.getHeaders());
final String userAgent = headersWithUserAgent.get(USER_AGENT_KEY);
String userAgent = null;
for (Entry<String, String> entry : headersWithUserAgent.entrySet()) {
if (entry.getKey().equalsIgnoreCase(USER_AGENT_KEY)) {
userAgent = entry.getValue();
headersWithUserAgent.remove(entry.getKey());
break;
}
}
headersWithUserAgent.put(
USER_AGENT_KEY,
userAgent == null ? DEFAULT_USER_AGENT : userAgent + " " + DEFAULT_USER_AGENT);
@@ -509,27 +509,29 @@ public void testDefaultUserAgent() {

@Test
public void testCustomUserAgent() {
final HeaderProvider userAgentHeaderProvider =
new HeaderProvider() {
@Override
public Map<String, String> getHeaders() {
final Map<String, String> headers = new HashMap<>();
headers.put("user-agent", "test-agent");
return headers;
}
};
final SpannerOptions options =
createSpannerOptions().toBuilder().setHeaderProvider(userAgentHeaderProvider).build();
try (Spanner spanner = options.getService()) {
final DatabaseClient databaseClient =
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
for (String headerId : new String[] {"user-agent", "User-Agent", "USER-AGENT"}) {
final HeaderProvider userAgentHeaderProvider =
new HeaderProvider() {
@Override
public Map<String, String> getHeaders() {
final Map<String, String> headers = new HashMap<>();
headers.put(headerId, "test-agent");
return headers;
}
};
final SpannerOptions options =
createSpannerOptions().toBuilder().setHeaderProvider(userAgentHeaderProvider).build();
try (Spanner spanner = options.getService()) {
final DatabaseClient databaseClient =
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));

try (final ResultSet rs = databaseClient.singleUse().executeQuery(SELECT1AND2)) {
rs.next();
}

try (final ResultSet rs = databaseClient.singleUse().executeQuery(SELECT1AND2)) {
rs.next();
assertThat(seenHeaders.get(Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER)))
.contains("test-agent " + defaultUserAgent);
}

assertThat(seenHeaders.get(Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER)))
.contains("test-agent " + defaultUserAgent);
}
}

0 comments on commit f4f9e43

Please sign in to comment.