use of com.google.api.gax.rpc.HeaderProvider in project java-bigtable by googleapis.
the class HeadersTest method setUp.
@Before
public void setUp() throws Exception {
serviceHelper = new FakeServiceHelper(new MetadataInterceptor(), new FakeBigtableService());
serviceHelper.start();
BigtableDataSettings.Builder settings = BigtableDataSettings.newBuilderForEmulator(serviceHelper.getPort()).setProjectId(PROJECT_ID).setInstanceId(INSTANCE_ID).setAppProfileId(APP_PROFILE_ID);
HeaderProvider headerProvider = FixedHeaderProvider.create(TEST_FIXED_HEADER_STRING, "test_header_value");
// Force immediate flush
settings.stubSettings().setHeaderProvider(headerProvider).bulkMutateRowsSettings().setBatchingSettings(settings.stubSettings().bulkMutateRowsSettings().getBatchingSettings().toBuilder().setElementCountThreshold(1L).build());
client = BigtableDataClient.create(settings.build());
}
use of com.google.api.gax.rpc.HeaderProvider in project java-storage-nio by googleapis.
the class StorageOptionsUtilTest method ensureAddingUserAgentEntryDoesNotClobberAnyProvidedHeaders.
@Test
public void ensureAddingUserAgentEntryDoesNotClobberAnyProvidedHeaders() {
StorageOptions base = defaults().toBuilder().setHeaderProvider(FixedHeaderProvider.create("user-agent", "custom/", "x-custom-header", "value")).build();
StorageOptions merged = StorageOptionsUtil.mergeOptionsWithUserAgent(base);
String mergedUserAgent = merged.getUserAgent();
HeaderProvider mergedHeaderProvider = StorageOptionsUtil.getHeaderProvider(merged);
Map<String, String> headers = mergedHeaderProvider.getHeaders();
String customHeader = headers.get("x-custom-header");
assertNotNull(mergedUserAgent);
assertNotNull(customHeader);
assertEquals("value", customHeader);
assertTrue(mergedUserAgent.contains("custom/ gcloud-java-nio/"));
}
use of com.google.api.gax.rpc.HeaderProvider in project java-storage-nio by googleapis.
the class StorageOptionsUtil method mergeOptionsWithUserAgent.
static StorageOptions mergeOptionsWithUserAgent(StorageOptions providedStorageOptions) {
if (providedStorageOptions == DEFAULT_STORAGE_OPTIONS_INSTANCE) {
return providedStorageOptions;
}
String userAgent = providedStorageOptions.getUserAgent();
if (userAgent == null) {
return nullSafeSet(providedStorageOptions, DEFAULT_HEADER_PROVIDER);
} else {
if (!userAgent.contains(USER_AGENT_ENTRY_NAME)) {
HeaderProvider providedHeaderProvider = getHeaderProvider(providedStorageOptions);
Map<String, String> newHeaders = new HashMap<>(providedHeaderProvider.getHeaders());
newHeaders.put("user-agent", String.format("%s %s", userAgent, USER_AGENT_ENTRY));
FixedHeaderProvider headerProvider = FixedHeaderProvider.create(ImmutableMap.copyOf(newHeaders));
return nullSafeSet(providedStorageOptions, headerProvider);
} else {
return providedStorageOptions;
}
}
}
use of com.google.api.gax.rpc.HeaderProvider in project java-spanner by googleapis.
the class GapicSpannerRpcTest method testCustomUserAgent.
@Test
public void testCustomUserAgent() {
for (String headerId : new String[] { "user-agent", "User-Agent", "USER-AGENT" }) {
final HeaderProvider userAgentHeaderProvider = () -> {
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();
}
assertThat(lastSeenHeaders.get(Key.of("user-agent", Metadata.ASCII_STRING_MARSHALLER))).contains("test-agent " + defaultUserAgent);
}
}
}
Aggregations