From 30510f114c4124e4c94beb44a39689340bba78c2 Mon Sep 17 00:00:00 2001 From: detmerl Date: Mon, 7 Oct 2024 15:57:50 -0400 Subject: [PATCH] update ITDynamicRoutingHeaders to use new HttpJsonCapturingClientInterceptor and GrpcCapturingClientInterceptor classes --- .../v1beta1/it/ITApiKeyCredentials.java | 4 +- .../v1beta1/it/ITApiVersionHeaders.java | 10 +- .../v1beta1/it/ITDynamicRoutingHeaders.java | 112 ++++-------------- .../HttpJsonCapturingClientInterceptor.java | 6 +- 4 files changed, 38 insertions(+), 94 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiKeyCredentials.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiKeyCredentials.java index 5867bcfea9..0f4e7cf44b 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiKeyCredentials.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiKeyCredentials.java @@ -169,7 +169,7 @@ void testCreateHttpClient_withApiKey_sendsApiHeaderToServer() throws Exception { ArrayList headerValues = (ArrayList) - 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); } @@ -211,7 +211,7 @@ void testCreateHttpClient_withApiKeySetViaSetterAndHeader_dedupsHeader() throws ArrayList headerValues = (ArrayList) - 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); diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java index cb823db498..19545bc7e8 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITApiVersionHeaders.java @@ -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); } @@ -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 @@ -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); } diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITDynamicRoutingHeaders.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITDynamicRoutingHeaders.java index 7828da8b53..31035afa65 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITDynamicRoutingHeaders.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITDynamicRoutingHeaders.java @@ -18,14 +18,6 @@ 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; @@ -33,14 +25,10 @@ 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; @@ -55,69 +43,6 @@ class ITDynamicRoutingHeaders { private static final Metadata.Key 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 ClientCall interceptCall( - MethodDescriptor method, final CallOptions callOptions, Channel next) { - ClientCall call = next.newCall(method, callOptions); - return new ForwardingClientCall.SimpleForwardingClientCall(call) { - @Override - public void start(ClientCall.Listener 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 HttpJsonClientCall interceptCall( - ApiMethodDescriptor method, - HttpJsonCallOptions callOptions, - HttpJsonChannel next) { - HttpJsonClientCall call = next.newCall(method, callOptions); - return new ForwardingHttpJsonClientCall.SimpleForwardingHttpJsonClientCall< - RequestT, ResponseT>(call) { - @Override - public void start(Listener responseListener, HttpJsonMetadata requestHeaders) { - Listener 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; @@ -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 @@ -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(); } @@ -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(); } @@ -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 requestHeaders = Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList()); @@ -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 requestHeaders = Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList()); @@ -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 requestHeaders = Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList()); @@ -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 requestHeaders = Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList()); @@ -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 requestHeaders = Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList()); @@ -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 requestHeaders = Arrays.stream(headerValue.split(SPLIT_TOKEN)).collect(Collectors.toList()); @@ -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; + } } diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/HttpJsonCapturingClientInterceptor.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/HttpJsonCapturingClientInterceptor.java index 777398cc96..4947a51829 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/HttpJsonCapturingClientInterceptor.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/HttpJsonCapturingClientInterceptor.java @@ -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 HttpJsonClientCall interceptCall( @@ -64,7 +65,7 @@ public void start(Listener responseListener, HttpJsonMetadata request ResponseT>(responseListener) { @Override public void onHeaders(HttpJsonMetadata responseHeaders) { - metadata = responseHeaders; + responseMetadata = responseHeaders; super.onHeaders(responseHeaders); } @@ -80,6 +81,7 @@ public void onClose(int statusCode, HttpJsonMetadata trailers) { }; super.start(forwardingResponseListener, requestHeaders); + requestMetadata = requestHeaders; } }; }