|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage.it; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import com.google.api.gax.paging.Page; |
| 22 | +import com.google.cloud.storage.Bucket; |
| 23 | +import com.google.cloud.storage.BucketInfo; |
| 24 | +import com.google.cloud.storage.GrpcStorageOptions; |
| 25 | +import com.google.cloud.storage.Storage; |
| 26 | +import com.google.cloud.storage.Storage.BucketListOption; |
| 27 | +import com.google.cloud.storage.StorageOptions; |
| 28 | +import com.google.cloud.storage.TransportCompatibility.Transport; |
| 29 | +import com.google.cloud.storage.it.runner.StorageITRunner; |
| 30 | +import com.google.cloud.storage.it.runner.annotations.Backend; |
| 31 | +import com.google.cloud.storage.it.runner.annotations.Inject; |
| 32 | +import com.google.cloud.storage.it.runner.annotations.SingleBackend; |
| 33 | +import com.google.cloud.storage.it.runner.annotations.StorageFixture; |
| 34 | +import com.google.common.collect.ImmutableList; |
| 35 | +import io.grpc.Attributes; |
| 36 | +import io.grpc.CallOptions; |
| 37 | +import io.grpc.Channel; |
| 38 | +import io.grpc.ClientCall; |
| 39 | +import io.grpc.ClientInterceptor; |
| 40 | +import io.grpc.ClientStreamTracer; |
| 41 | +import io.grpc.ClientStreamTracer.StreamInfo; |
| 42 | +import io.grpc.Metadata; |
| 43 | +import io.grpc.MethodDescriptor; |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.Collections; |
| 46 | +import java.util.List; |
| 47 | +import java.util.stream.Collectors; |
| 48 | +import org.junit.Test; |
| 49 | +import org.junit.runner.RunWith; |
| 50 | + |
| 51 | +@RunWith(StorageITRunner.class) |
| 52 | +@SingleBackend(Backend.PROD) |
| 53 | +public class ITGrpcInterceptorTest { |
| 54 | + private static final Metadata.Key<String> X_GOOG_REQUEST_PARAMS = |
| 55 | + Metadata.Key.of("x-goog-request-params", Metadata.ASCII_STRING_MARSHALLER); |
| 56 | + |
| 57 | + @Inject |
| 58 | + @StorageFixture(Transport.GRPC) |
| 59 | + public Storage storage; |
| 60 | + |
| 61 | + @Inject public BucketInfo bucket; |
| 62 | + |
| 63 | + @Test |
| 64 | + public void grpcStorageOptions_allowSpecifyingInterceptor() throws Exception { |
| 65 | + TracerFactory factory = new TracerFactory(); |
| 66 | + Interceptor interceptor = new Interceptor(factory); |
| 67 | + StorageOptions options = |
| 68 | + ((GrpcStorageOptions) storage.getOptions()) |
| 69 | + .toBuilder() |
| 70 | + .setGrpcInterceptorProvider(() -> ImmutableList.of(interceptor)) |
| 71 | + .build(); |
| 72 | + |
| 73 | + try (Storage storage = options.getService()) { |
| 74 | + Page<Bucket> page = storage.list(BucketListOption.prefix(bucket.getName())); |
| 75 | + List<String> bucketNames = |
| 76 | + page.streamAll().map(BucketInfo::getName).collect(Collectors.toList()); |
| 77 | + assertThat(bucketNames).contains(bucket.getName()); |
| 78 | + } |
| 79 | + |
| 80 | + assertThat(factory.metadatas).isNotEmpty(); |
| 81 | + List<String> requestParams = |
| 82 | + factory.metadatas.stream() |
| 83 | + .map(m -> m.get(X_GOOG_REQUEST_PARAMS)) |
| 84 | + .collect(Collectors.toList()); |
| 85 | + |
| 86 | + System.out.println("requestParams = " + requestParams); |
| 87 | + |
| 88 | + String expected = String.format("project=projects/%s", options.getProjectId()); |
| 89 | + assertThat(requestParams).contains(expected); |
| 90 | + } |
| 91 | + |
| 92 | + private static final class Interceptor implements ClientInterceptor { |
| 93 | + |
| 94 | + private final TracerFactory factory; |
| 95 | + |
| 96 | + public Interceptor(TracerFactory factory) { |
| 97 | + this.factory = factory; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 102 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { |
| 103 | + CallOptions callOptions1 = callOptions.withStreamTracerFactory(factory); |
| 104 | + return next.newCall(method, callOptions1); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static final class TracerFactory extends ClientStreamTracer.Factory { |
| 109 | + |
| 110 | + private final List<Metadata> metadatas = Collections.synchronizedList(new ArrayList<>()); |
| 111 | + |
| 112 | + @Override |
| 113 | + public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) { |
| 114 | + return new ClientStreamTracer() { |
| 115 | + @Override |
| 116 | + public void streamCreated(Attributes transportAttrs, Metadata headers) { |
| 117 | + metadatas.add(headers); |
| 118 | + } |
| 119 | + }; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments