-
Notifications
You must be signed in to change notification settings - Fork 4k
Add a newAttachHeadersServerInterceptor() util #11458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
17a98a4
Establish a default connect timeout.
jdcormie ef43a27
Merge branch 'master' of https://github.com/jdcormie/grpc-java
jdcormie c83a010
Merge branch 'master' of https://github.com/jdcormie/grpc-java
jdcormie 8af05c9
Add a newAttachHeadersServerInterceptor() util.
jdcormie 888fe05
fix test
jdcormie 8f66ca1
Tag as experimental
jdcormie 10cedf9
Attach headers even in the onError() case.
jdcormie c563cd7
Don't mutate shared 'extraHeaders' in the no trailers case.
jdcormie a209c8f
Don't spoil retries by forcing headers
jdcormie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| /* | ||
| * Copyright 2024 The gRPC Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package io.grpc.stub; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
| import static io.grpc.stub.MetadataUtils.newAttachMetadataServerInterceptor; | ||
| import static io.grpc.stub.MetadataUtils.newCaptureMetadataInterceptor; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import io.grpc.CallOptions; | ||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.Metadata; | ||
| import io.grpc.MethodDescriptor; | ||
| import io.grpc.ServerCallHandler; | ||
| import io.grpc.ServerInterceptors; | ||
| import io.grpc.ServerMethodDefinition; | ||
| import io.grpc.ServerServiceDefinition; | ||
| import io.grpc.Status; | ||
| import io.grpc.Status.Code; | ||
| import io.grpc.StatusRuntimeException; | ||
| import io.grpc.StringMarshaller; | ||
| import io.grpc.inprocess.InProcessChannelBuilder; | ||
| import io.grpc.inprocess.InProcessServerBuilder; | ||
| import io.grpc.testing.GrpcCleanupRule; | ||
| import java.io.IOException; | ||
| import java.util.Iterator; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class MetadataUtilsTest { | ||
|
|
||
| @Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); | ||
|
|
||
| private static final String SERVER_NAME = "test"; | ||
| private static final Metadata.Key<String> FOO_KEY = | ||
| Metadata.Key.of("foo-key", Metadata.ASCII_STRING_MARSHALLER); | ||
|
|
||
| private final MethodDescriptor<String, String> echoMethod = | ||
| MethodDescriptor.newBuilder(StringMarshaller.INSTANCE, StringMarshaller.INSTANCE) | ||
| .setFullMethodName("test/echo") | ||
| .setType(MethodDescriptor.MethodType.UNARY) | ||
| .build(); | ||
|
|
||
| private final ServerCallHandler<String, String> echoCallHandler = | ||
| ServerCalls.asyncUnaryCall( | ||
| (req, respObserver) -> { | ||
| respObserver.onNext(req); | ||
| respObserver.onCompleted(); | ||
| }); | ||
|
|
||
| MethodDescriptor<String, String> echoServerStreamingMethod = | ||
| MethodDescriptor.newBuilder(StringMarshaller.INSTANCE, StringMarshaller.INSTANCE) | ||
| .setFullMethodName("test/echoStream") | ||
| .setType(MethodDescriptor.MethodType.SERVER_STREAMING) | ||
| .build(); | ||
|
|
||
| private final AtomicReference<Metadata> trailersCapture = new AtomicReference<>(); | ||
| private final AtomicReference<Metadata> headersCapture = new AtomicReference<>(); | ||
|
|
||
| @Test | ||
| public void shouldAttachHeadersToResponse() throws IOException { | ||
| Metadata extras = new Metadata(); | ||
| extras.put(FOO_KEY, "foo-value"); | ||
|
|
||
| ServerServiceDefinition serviceDef = | ||
| ServerInterceptors.intercept( | ||
| ServerServiceDefinition.builder("test").addMethod(echoMethod, echoCallHandler).build(), | ||
| ImmutableList.of(newAttachMetadataServerInterceptor(extras))); | ||
|
|
||
| grpcCleanup.register(newInProcessServerBuilder().addService(serviceDef).build().start()); | ||
| ManagedChannel channel = | ||
| grpcCleanup.register( | ||
| newInProcessChannelBuilder() | ||
| .intercept(newCaptureMetadataInterceptor(headersCapture, trailersCapture)) | ||
| .build()); | ||
|
|
||
| String response = | ||
| ClientCalls.blockingUnaryCall(channel, echoMethod, CallOptions.DEFAULT, "hello"); | ||
| assertThat(response).isEqualTo("hello"); | ||
| assertThat(trailersCapture.get() == null || !trailersCapture.get().containsKey(FOO_KEY)) | ||
| .isTrue(); | ||
| assertThat(headersCapture.get().get(FOO_KEY)).isEqualTo("foo-value"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldAttachTrailersWhenNoResponse() throws IOException { | ||
| Metadata extras = new Metadata(); | ||
| extras.put(FOO_KEY, "foo-value"); | ||
|
|
||
| ServerServiceDefinition serviceDef = | ||
| ServerInterceptors.intercept( | ||
| ServerServiceDefinition.builder("test") | ||
| .addMethod( | ||
| ServerMethodDefinition.create( | ||
| echoServerStreamingMethod, | ||
| ServerCalls.asyncUnaryCall( | ||
| (req, respObserver) -> respObserver.onCompleted()))) | ||
| .build(), | ||
| ImmutableList.of(newAttachMetadataServerInterceptor(extras))); | ||
| grpcCleanup.register(newInProcessServerBuilder().addService(serviceDef).build().start()); | ||
|
|
||
| ManagedChannel channel = | ||
| grpcCleanup.register( | ||
| newInProcessChannelBuilder() | ||
| .intercept(newCaptureMetadataInterceptor(headersCapture, trailersCapture)) | ||
| .build()); | ||
|
|
||
| Iterator<String> response = | ||
| ClientCalls.blockingServerStreamingCall( | ||
| channel, echoServerStreamingMethod, CallOptions.DEFAULT, "hello"); | ||
| assertThat(response.hasNext()).isFalse(); | ||
| assertThat(headersCapture.get() == null || !headersCapture.get().containsKey(FOO_KEY)).isTrue(); | ||
| assertThat(trailersCapture.get().get(FOO_KEY)).isEqualTo("foo-value"); | ||
| } | ||
|
|
||
| @Test | ||
| public void shouldAttachTrailersToErrorResponse() throws IOException { | ||
| Metadata extras = new Metadata(); | ||
| extras.put(FOO_KEY, "foo-value"); | ||
|
|
||
| ServerServiceDefinition serviceDef = | ||
| ServerInterceptors.intercept( | ||
| ServerServiceDefinition.builder("test") | ||
| .addMethod( | ||
| echoMethod, | ||
| ServerCalls.asyncUnaryCall( | ||
| (req, respObserver) -> | ||
| respObserver.onError(Status.INVALID_ARGUMENT.asRuntimeException()))) | ||
| .build(), | ||
| ImmutableList.of(newAttachMetadataServerInterceptor(extras))); | ||
| grpcCleanup.register(newInProcessServerBuilder().addService(serviceDef).build().start()); | ||
|
|
||
| ManagedChannel channel = | ||
| grpcCleanup.register( | ||
| newInProcessChannelBuilder() | ||
| .intercept(newCaptureMetadataInterceptor(headersCapture, trailersCapture)) | ||
| .build()); | ||
| try { | ||
| ClientCalls.blockingUnaryCall(channel, echoMethod, CallOptions.DEFAULT, "hello"); | ||
| fail(); | ||
| } catch (StatusRuntimeException e) { | ||
| assertThat(e.getStatus()).isNotNull(); | ||
| assertThat(e.getStatus().getCode()).isEqualTo(Code.INVALID_ARGUMENT); | ||
| } | ||
| assertThat(headersCapture.get() == null || !headersCapture.get().containsKey(FOO_KEY)).isTrue(); | ||
| assertThat(trailersCapture.get().get(FOO_KEY)).isEqualTo("foo-value"); | ||
| } | ||
|
|
||
| private static InProcessServerBuilder newInProcessServerBuilder() { | ||
| return InProcessServerBuilder.forName(SERVER_NAME).directExecutor(); | ||
| } | ||
|
|
||
| private static InProcessChannelBuilder newInProcessChannelBuilder() { | ||
| return InProcessChannelBuilder.forName(SERVER_NAME).directExecutor(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.