Skip to content

Commit

Permalink
update ITDynamicRoutingHeaders to use new HttpJsonCapturingClientInte…
Browse files Browse the repository at this point in the history
…rceptor and GrpcCapturingClientInterceptor classes
  • Loading branch information
ldetmer committed Oct 7, 2024
1 parent 9ad9591 commit 30510f1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void testCreateHttpClient_withApiKey_sendsApiHeaderToServer() throws Exception {

ArrayList<String> headerValues =
(ArrayList<String>)
httpJsonInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
httpJsonInterceptor.responseMetadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
String headerValue = headerValues.get(0);
assertThat(headerValue).isEqualTo(API_KEY);
}
Expand Down Expand Up @@ -211,7 +211,7 @@ void testCreateHttpClient_withApiKeySetViaSetterAndHeader_dedupsHeader() throws

ArrayList<String> headerValues =
(ArrayList<String>)
httpJsonInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
httpJsonInterceptor.responseMetadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
String headerValue = headerValues.get(0);
assertThat(headerValue).isEqualTo(API_KEY);
assertThat(headerValues.size()).isEqualTo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ void testGrpc_matchesApiVersion() {
void testHttpJson_matchesHeaderName() {
httpJsonClient.echo(EchoRequest.newBuilder().build());
ArrayList headerValues =
(ArrayList) httpJsonInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
(ArrayList)
httpJsonInterceptor.responseMetadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
String headerValue = (String) headerValues.get(0);
assertThat(headerValue).isEqualTo(EXPECTED_ECHO_API_VERSION);
}
Expand All @@ -134,7 +135,7 @@ void testHttpJson_noApiVersion() {
RepeatRequest.newBuilder().setInfo(ComplianceData.newBuilder().setFString("test")).build();
httpJsonComplianceClient.repeatDataSimplePath(request);
assertThat(API_VERSION_HEADER_KEY)
.isNotIn(httpJsonComplianceInterceptor.metadata.getHeaders().keySet());
.isNotIn(httpJsonComplianceInterceptor.responseMetadata.getHeaders().keySet());
}

@Test
Expand Down Expand Up @@ -224,7 +225,10 @@ void testHttpJsonCompliance_userApiVersionSetSuccess() throws IOException {

ArrayList headerValues =
(ArrayList)
httpJsonComplianceInterceptor.metadata.getHeaders().get(HTTP_RESPONSE_HEADER_STRING);
httpJsonComplianceInterceptor
.responseMetadata
.getHeaders()
.get(HTTP_RESPONSE_HEADER_STRING);
String headerValue = (String) headerValues.get(0);
assertThat(headerValue).isEqualTo(CUSTOM_API_VERSION);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,17 @@
import static com.google.api.gax.rpc.internal.Headers.DYNAMIC_ROUTING_HEADER_KEY;
import static com.google.common.truth.Truth.assertThat;

import com.google.api.gax.httpjson.ApiMethodDescriptor;
import com.google.api.gax.httpjson.ForwardingHttpJsonClientCall;
import com.google.api.gax.httpjson.ForwardingHttpJsonClientCallListener;
import com.google.api.gax.httpjson.HttpJsonCallOptions;
import com.google.api.gax.httpjson.HttpJsonChannel;
import com.google.api.gax.httpjson.HttpJsonClientCall;
import com.google.api.gax.httpjson.HttpJsonClientInterceptor;
import com.google.api.gax.httpjson.HttpJsonMetadata;
import com.google.common.collect.ImmutableList;
import com.google.showcase.v1beta1.ComplianceClient;
import com.google.showcase.v1beta1.ComplianceData;
import com.google.showcase.v1beta1.EchoClient;
import com.google.showcase.v1beta1.EchoRequest;
import com.google.showcase.v1beta1.RepeatRequest;
import com.google.showcase.v1beta1.RepeatResponse;
import com.google.showcase.v1beta1.it.util.GrpcCapturingClientInterceptor;
import com.google.showcase.v1beta1.it.util.HttpJsonCapturingClientInterceptor;
import com.google.showcase.v1beta1.it.util.TestClientInitializer;
import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.ForwardingClientCall;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand All @@ -55,69 +43,6 @@ class ITDynamicRoutingHeaders {
private static final Metadata.Key<String> REQUEST_PARAMS_HEADER_KEY =
Metadata.Key.of(DYNAMIC_ROUTING_HEADER_KEY, Metadata.ASCII_STRING_MARSHALLER);

// Implement a request interceptor to retrieve the headers being sent on the request.
// The headers being set are part of the Metadata
private static class GrpcCapturingClientInterceptor implements ClientInterceptor {
private Metadata metadata;

@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> interceptCall(
MethodDescriptor<RequestT, ResponseT> method, final CallOptions callOptions, Channel next) {
ClientCall<RequestT, ResponseT> call = next.newCall(method, callOptions);
return new ForwardingClientCall.SimpleForwardingClientCall<RequestT, ResponseT>(call) {
@Override
public void start(ClientCall.Listener<ResponseT> responseListener, Metadata headers) {
metadata = headers;
super.start(responseListener, headers);
}
};
}
}

// Implement a request interceptor to retrieve the headers being sent on the request
// The headers being set are part of the CallOptions
private static class HttpJsonCapturingClientInterceptor implements HttpJsonClientInterceptor {
private String requestParam;

@Override
public <RequestT, ResponseT> HttpJsonClientCall<RequestT, ResponseT> interceptCall(
ApiMethodDescriptor<RequestT, ResponseT> method,
HttpJsonCallOptions callOptions,
HttpJsonChannel next) {
HttpJsonClientCall<RequestT, ResponseT> call = next.newCall(method, callOptions);
return new ForwardingHttpJsonClientCall.SimpleForwardingHttpJsonClientCall<
RequestT, ResponseT>(call) {
@Override
public void start(Listener<ResponseT> responseListener, HttpJsonMetadata requestHeaders) {
Listener<ResponseT> forwardingResponseListener =
new ForwardingHttpJsonClientCallListener.SimpleForwardingHttpJsonClientCallListener<
ResponseT>(responseListener) {
@Override
public void onHeaders(HttpJsonMetadata responseHeaders) {
super.onHeaders(responseHeaders);
}

@Override
public void onMessage(ResponseT message) {
super.onMessage(message);
}

@Override
public void onClose(int statusCode, HttpJsonMetadata trailers) {
super.onClose(statusCode, trailers);
}
};

super.start(forwardingResponseListener, requestHeaders);
if (requestHeaders.getHeaders().containsKey(DYNAMIC_ROUTING_HEADER_KEY)) {
requestParam =
String.valueOf(requestHeaders.getHeaders().get(DYNAMIC_ROUTING_HEADER_KEY));
}
}
};
}
}

private static HttpJsonCapturingClientInterceptor httpJsonInterceptor;
private static HttpJsonCapturingClientInterceptor httpJsonComplianceInterceptor;
private static GrpcCapturingClientInterceptor grpcInterceptor;
Expand Down Expand Up @@ -158,8 +83,8 @@ static void createClients() throws Exception {
void cleanUpParams() {
grpcInterceptor.metadata = null;
grpcComplianceInterceptor.metadata = null;
httpJsonInterceptor.requestParam = null;
httpJsonComplianceInterceptor.requestParam = null;
httpJsonInterceptor.requestMetadata = null;
httpJsonComplianceInterceptor.requestMetadata = null;
}

@AfterAll
Expand Down Expand Up @@ -190,7 +115,7 @@ void testGrpc_noRoutingHeaderUsed() {
@Test
void testHttpJson_noRoutingHeaderUsed() {
httpJsonClient.echo(EchoRequest.newBuilder().build());
String headerValue = httpJsonInterceptor.requestParam;
String headerValue = getDynamicRoutingHeaderValueFromInterceptor(httpJsonInterceptor);
assertThat(headerValue).isNull();
}

Expand All @@ -204,7 +129,7 @@ void testGrpc_emptyHeader() {
@Test
void testHttpJson_emptyHeader() {
httpJsonClient.echo(EchoRequest.newBuilder().setHeader("").build());
String headerValue = httpJsonInterceptor.requestParam;
String headerValue = getDynamicRoutingHeaderValueFromInterceptor(httpJsonInterceptor);
assertThat(headerValue).isNull();
}

Expand Down Expand Up @@ -249,7 +174,7 @@ void testHttpJson_implicitHeaders_enumsEncodedasInt() {
.setFKingdomValue(3))
.build();
RepeatResponse actualResponse = httpJsonComplianceClient.repeatDataSimplePath(request);
String headerValue = httpJsonComplianceInterceptor.requestParam;
String headerValue = getDynamicRoutingHeaderValueFromInterceptor(httpJsonComplianceInterceptor);
assertThat(headerValue).isNotNull();
List<String> requestHeaders =
Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList());
Expand All @@ -267,7 +192,7 @@ void testHttpJson_implicitHeaders_enumsEncodedasInt() {
@Test
void testHttpJson_matchesHeaderName() {
httpJsonClient.echo(EchoRequest.newBuilder().setHeader("potato").build());
String headerValue = httpJsonInterceptor.requestParam;
String headerValue = getDynamicRoutingHeaderValueFromInterceptor(httpJsonInterceptor);
assertThat(headerValue).isNotNull();
List<String> requestHeaders =
Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList());
Expand All @@ -289,7 +214,7 @@ void testGrpc_matchesOtherHeaderName() {
@Test
void testHttpJson_matchesOtherHeaderName() {
httpJsonClient.echo(EchoRequest.newBuilder().setOtherHeader("instances/456").build());
String headerValue = httpJsonInterceptor.requestParam;
String headerValue = getDynamicRoutingHeaderValueFromInterceptor(httpJsonInterceptor);
assertThat(headerValue).isNotNull();
List<String> requestHeaders =
Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList());
Expand Down Expand Up @@ -317,7 +242,7 @@ void testGrpc_matchesMultipleOfSameRoutingHeader_usesHeader() {
@Test
void testHttpJson_matchesMultipleOfSameRoutingHeader_usesHeader() {
httpJsonClient.echo(EchoRequest.newBuilder().setHeader("projects/123/instances/456").build());
String headerValue = httpJsonInterceptor.requestParam;
String headerValue = getDynamicRoutingHeaderValueFromInterceptor(httpJsonInterceptor);
assertThat(headerValue).isNotNull();
List<String> requestHeaders =
Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList());
Expand Down Expand Up @@ -347,7 +272,7 @@ void testGrpc_matchesMultipleOfSameRoutingHeader_usesOtherHeader() {
void testHttpJson_matchesMultipleOfSameRoutingHeader_usesOtherHeader() {
httpJsonClient.echo(
EchoRequest.newBuilder().setOtherHeader("projects/123/instances/456").build());
String headerValue = httpJsonInterceptor.requestParam;
String headerValue = getDynamicRoutingHeaderValueFromInterceptor(httpJsonInterceptor);
assertThat(headerValue).isNotNull();
List<String> requestHeaders =
Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList());
Expand Down Expand Up @@ -384,7 +309,7 @@ void testHttpJson_matchesMultipleRoutingHeaders() {
.setHeader("regions/123/zones/456")
.setOtherHeader("projects/123/instances/456")
.build());
String headerValue = httpJsonInterceptor.requestParam;
String headerValue = getDynamicRoutingHeaderValueFromInterceptor(httpJsonInterceptor);
assertThat(headerValue).isNotNull();
List<String> requestHeaders =
Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList());
Expand All @@ -397,4 +322,17 @@ void testHttpJson_matchesMultipleRoutingHeaders() {
"routing_id=regions%2F123%2Fzones%2F456");
assertThat(requestHeaders).containsExactlyElementsIn(expectedHeaders);
}

private String getDynamicRoutingHeaderValueFromInterceptor(
HttpJsonCapturingClientInterceptor httpJsonInterceptor) {
if (httpJsonInterceptor.requestMetadata != null
&& httpJsonInterceptor
.requestMetadata
.getHeaders()
.containsKey(DYNAMIC_ROUTING_HEADER_KEY)) {
return String.valueOf(
httpJsonInterceptor.requestMetadata.getHeaders().get(DYNAMIC_ROUTING_HEADER_KEY));
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@

/** Implements a client interceptor to retrieve the response headers from a HTTP client request. */
public class HttpJsonCapturingClientInterceptor implements HttpJsonClientInterceptor {
public HttpJsonMetadata metadata;
public HttpJsonMetadata responseMetadata;
public HttpJsonMetadata requestMetadata;

@Override
public <RequestT, ResponseT> HttpJsonClientCall<RequestT, ResponseT> interceptCall(
Expand All @@ -64,7 +65,7 @@ public void start(Listener<ResponseT> responseListener, HttpJsonMetadata request
ResponseT>(responseListener) {
@Override
public void onHeaders(HttpJsonMetadata responseHeaders) {
metadata = responseHeaders;
responseMetadata = responseHeaders;
super.onHeaders(responseHeaders);
}

Expand All @@ -80,6 +81,7 @@ public void onClose(int statusCode, HttpJsonMetadata trailers) {
};

super.start(forwardingResponseListener, requestHeaders);
requestMetadata = requestHeaders;
}
};
}
Expand Down

0 comments on commit 30510f1

Please sign in to comment.