Skip to content

Commit 2d05064

Browse files
authored
test(3.1.x): adds test for setting custom user-agent (#1120)
* test: for user agent header provider * test: closes spanner service in test
1 parent 333cd02 commit 2d05064

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/spi/v1/GapicSpannerRpcTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.google.api.core.ApiFunction;
2727
import com.google.api.gax.rpc.ApiCallContext;
28+
import com.google.api.gax.rpc.HeaderProvider;
2829
import com.google.auth.oauth2.AccessToken;
2930
import com.google.auth.oauth2.OAuth2Credentials;
3031
import com.google.cloud.spanner.DatabaseAdminClient;
@@ -151,6 +152,7 @@ public class GapicSpannerRpcTest {
151152
private Server server;
152153
private InetSocketAddress address;
153154
private final Map<SpannerRpc.Option, Object> optionsMap = new HashMap<>();
155+
private Metadata seenHeaders;
154156

155157
@BeforeClass
156158
public static void checkNotEmulator() {
@@ -183,6 +185,7 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
183185
ServerCall<ReqT, RespT> call,
184186
Metadata headers,
185187
ServerCallHandler<ReqT, RespT> next) {
188+
seenHeaders = headers;
186189
String auth =
187190
headers.get(Key.of("authorization", Metadata.ASCII_STRING_MARSHALLER));
188191
assertThat(auth).isEqualTo("Bearer " + VARIABLE_OAUTH_TOKEN);
@@ -502,6 +505,34 @@ public void testAdminRequestsLimitExceededRetryAlgorithm() {
502505
assertThat(alg.shouldRetry(new Exception("random exception"), null)).isFalse();
503506
}
504507

508+
@Test
509+
public void testCustomUserAgent() {
510+
for (final String headerId : new String[] {"user-agent", "User-Agent", "USER-AGENT"}) {
511+
final HeaderProvider userAgentHeaderProvider =
512+
new HeaderProvider() {
513+
@Override
514+
public Map<String, String> getHeaders() {
515+
final Map<String, String> headers = new HashMap<>();
516+
headers.put(headerId, "test-agent");
517+
return headers;
518+
}
519+
};
520+
final SpannerOptions options =
521+
createSpannerOptions().toBuilder().setHeaderProvider(userAgentHeaderProvider).build();
522+
try (Spanner spanner = options.getService()) {
523+
final DatabaseClient databaseClient =
524+
spanner.getDatabaseClient(DatabaseId.of("[PROJECT]", "[INSTANCE]", "[DATABASE]"));
525+
526+
try (final ResultSet rs = databaseClient.singleUse().executeQuery(SELECT1AND2)) {
527+
rs.next();
528+
}
529+
530+
assertThat(seenHeaders.get(Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER)))
531+
.contains("test-agent");
532+
}
533+
}
534+
}
535+
505536
@SuppressWarnings("rawtypes")
506537
private SpannerOptions createSpannerOptions() {
507538
String endpoint = address.getHostString() + ":" + server.getPort();

0 commit comments

Comments
 (0)