Skip to content

Commit fe0cf20

Browse files
author
Mohit Agarwal
committed
Added test cases
1 parent 3f7ca9b commit fe0cf20

5 files changed

Lines changed: 50 additions & 4 deletions

File tree

grpc-client-utils/src/main/java/org/hypertrace/core/grpcutils/client/RequestContextAsCreds.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected void applyRequestContext(MetadataApplier applier, RequestContext reque
3333
// Exclude null headers
3434
if (entry.getValue() != null) {
3535
String key = entry.getKey();
36-
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
36+
if (key.toLowerCase().endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
3737
metadata.put(Metadata.Key.of(entry.getKey(), BINARY_BYTE_MARSHALLER), entry.getValue().getBytes());
3838
} else {
3939
metadata.put(Metadata.Key.of(entry.getKey(), ASCII_STRING_MARSHALLER), entry.getValue());

grpc-client-utils/src/test/java/org/hypertrace/core/grpcutils/client/ContextKeyBasedCredsTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.jupiter.api.Assertions;
1313
import org.junit.jupiter.api.Test;
1414

15+
import static io.grpc.Metadata.BINARY_BYTE_MARSHALLER;
1516
import static org.mockito.Mockito.mock;
1617

1718
public class ContextKeyBasedCredsTest {
@@ -29,6 +30,8 @@ public void testApplyRequestMetadata_shouldApplyAllHeaders() {
2930
requestContext.add(RequestContextConstants.TENANT_ID_HEADER_KEY, TENANT_ID);
3031
requestContext.add(RequestContextConstants.AUTHORIZATION_HEADER, TEST_AUTH_HEADER);
3132
requestContext.add("x-some-tenant-header", "v1");
33+
requestContext.add("grpc-trace-bin", "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE");
34+
3235

3336
Context ctx = Context.current().withValue(RequestContext.CURRENT, requestContext);
3437

@@ -38,14 +41,23 @@ public void testApplyRequestMetadata_shouldApplyAllHeaders() {
3841
@Override
3942
public void apply(Metadata headers) {
4043
Map<String, String> headersMap = headers.keys().stream()
41-
.collect(Collectors.toUnmodifiableMap(k -> k, k -> headers.get(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER))));
44+
.collect(Collectors.toUnmodifiableMap(k -> k, k -> {
45+
String value;
46+
if (k.toLowerCase().endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
47+
value = new String(headers.get(Metadata.Key.of(k, Metadata.BINARY_BYTE_MARSHALLER)));
48+
} else {
49+
value = headers.get(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER));
50+
}
51+
return value;
52+
} ));
4253

4354
// Should filter out the TENANT_ID_HEADER_KEY
4455
Assertions.assertEquals(
4556
Map.of(
4657
RequestContextConstants.TENANT_ID_HEADER_KEY, TENANT_ID,
4758
RequestContextConstants.AUTHORIZATION_HEADER, TEST_AUTH_HEADER,
48-
"x-some-tenant-header", "v1"
59+
"x-some-tenant-header", "v1",
60+
"grpc-trace-bin", "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE"
4961
),
5062
headersMap);
5163
}

grpc-client-utils/src/test/java/org/hypertrace/core/grpcutils/client/GrpcClientRequestContextUtilTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ public void testExecuteWithHeadersContextJustAuthHeader_shouldPropagateAllHeader
6161
}
6262
}
6363

64+
@Test
65+
public void testExecuteWithHeadersContextAuthAndTracing_shouldPropagateAllHeaders() {
66+
Context ctx = Context.current();
67+
Context previous = ctx.attach();
68+
try {
69+
Map<String, String> requestHeaders = Map.of("authorization", "v1", "a2", "v2",
70+
"grpc-trace-bin", "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE");
71+
72+
GrpcClientRequestContextUtil.executeWithHeadersContext(requestHeaders, () -> {
73+
RequestContext requestContext = RequestContext.CURRENT.get();
74+
Assertions.assertEquals(Map.of("authorization", "v1", "a2", "v2",
75+
"grpc-trace-bin", "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE"), requestContext.getRequestHeaders());
76+
return new Object();
77+
});
78+
} finally {
79+
ctx.detach(previous);
80+
}
81+
}
82+
83+
6484
@Test
6585
public void testExecuteInTenantIdContext() {
6686
Assertions.assertNull(RequestContext.CURRENT.get());

grpc-server-utils/src/main/java/org/hypertrace/core/grpcutils/server/RequestContextServerInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ RequestContext createRequestContextFromMetadata(Metadata metadata) {
4545
.forEach(k -> {
4646
String value;
4747
//check if key ends with binary suffix
48-
if (k.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
48+
if (k.toLowerCase().endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
4949
byte[] bytes = metadata.get(Metadata.Key.of(k, Metadata.BINARY_BYTE_MARSHALLER));
5050
value = new String(bytes, StandardCharsets.UTF_8);
5151
} else {

grpc-server-utils/src/test/java/org/hypertrace/core/grpcutils/server/RequestContextServerInterceptorTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,19 @@ public void testMetadataKeys() {
4343
Assertions.assertEquals(2, requestContext.getAll().size());
4444
Assertions.assertEquals("Bearer Some-bearer-auth-2", requestContext.get("authorization").get());
4545
Assertions.assertEquals("test-tenant-id-2", requestContext.get(RequestContextConstants.TENANT_ID_METADATA_KEY.name()).get());
46+
47+
48+
metadata = new Metadata();
49+
metadata.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), "Bearer Some-bearer-auth-3");
50+
metadata.put(Metadata.Key.of("X-tenant-Id", Metadata.ASCII_STRING_MARSHALLER), "test-tenant-id-3");
51+
metadata.put(Metadata.Key.of("X-Some-Other-Header", Metadata.ASCII_STRING_MARSHALLER), "Some-other-header-val-3");
52+
metadata.put(Metadata.Key.of("grpc-trace-bin", Metadata.BINARY_BYTE_MARSHALLER), "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE".getBytes());
53+
54+
requestContext = interceptor.createRequestContextFromMetadata(metadata);
55+
56+
Assertions.assertEquals(3, requestContext.getAll().size());
57+
Assertions.assertEquals("Bearer Some-bearer-auth-3", requestContext.get("authorization").get());
58+
Assertions.assertEquals("test-tenant-id-3", requestContext.get(RequestContextConstants.TENANT_ID_METADATA_KEY.name()).get());
59+
Assertions.assertEquals("AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE", requestContext.get("grpc-trace-bin").get());
4660
}
4761
}

0 commit comments

Comments
 (0)