MCPcopy Create free account
hub / github.com/grpc/grpc-java / MetadataUtilsTest

Class MetadataUtilsTest

stub/src/test/java/io/grpc/stub/MetadataUtilsTest.java:48–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

46import org.junit.runners.JUnit4;
47
48@RunWith(JUnit4.class)
49public class MetadataUtilsTest {
50
51 @Rule public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
52
53 private static final String SERVER_NAME = "test";
54 private static final Metadata.Key<String> FOO_KEY =
55 Metadata.Key.of("foo-key", Metadata.ASCII_STRING_MARSHALLER);
56
57 private final MethodDescriptor<String, String> echoMethod =
58 MethodDescriptor.newBuilder(StringMarshaller.INSTANCE, StringMarshaller.INSTANCE)
59 .setFullMethodName("test/echo")
60 .setType(MethodDescriptor.MethodType.UNARY)
61 .build();
62
63 private final ServerCallHandler<String, String> echoCallHandler =
64 ServerCalls.asyncUnaryCall(
65 (req, respObserver) -> {
66 respObserver.onNext(req);
67 respObserver.onCompleted();
68 });
69
70 MethodDescriptor<String, String> echoServerStreamingMethod =
71 MethodDescriptor.newBuilder(StringMarshaller.INSTANCE, StringMarshaller.INSTANCE)
72 .setFullMethodName("test/echoStream")
73 .setType(MethodDescriptor.MethodType.SERVER_STREAMING)
74 .build();
75
76 private final AtomicReference<Metadata> trailersCapture = new AtomicReference<>();
77 private final AtomicReference<Metadata> headersCapture = new AtomicReference<>();
78
79 @Test
80 public void shouldAttachHeadersToResponse() throws IOException {
81 Metadata extras = new Metadata();
82 extras.put(FOO_KEY, "foo-value");
83
84 ServerServiceDefinition serviceDef =
85 ServerInterceptors.intercept(
86 ServerServiceDefinition.builder("test").addMethod(echoMethod, echoCallHandler).build(),
87 ImmutableList.of(newAttachMetadataServerInterceptor(extras)));
88
89 grpcCleanup.register(newInProcessServerBuilder().addService(serviceDef).build().start());
90 ManagedChannel channel =
91 grpcCleanup.register(
92 newInProcessChannelBuilder()
93 .intercept(newCaptureMetadataInterceptor(headersCapture, trailersCapture))
94 .build());
95
96 String response =
97 ClientCalls.blockingUnaryCall(channel, echoMethod, CallOptions.DEFAULT, "hello");
98 assertThat(response).isEqualTo("hello");
99 assertThat(trailersCapture.get() == null || !trailersCapture.get().containsKey(FOO_KEY))
100 .isTrue();
101 assertThat(headersCapture.get().get(FOO_KEY)).isEqualTo("foo-value");
102 }
103
104 @Test
105 public void shouldAttachTrailersWhenNoResponse() throws IOException {

Callers

nothing calls this directly

Calls 8

newBuilderMethod · 0.95
asyncUnaryCallMethod · 0.95
setTypeMethod · 0.80
setFullMethodNameMethod · 0.80
buildMethod · 0.65
onNextMethod · 0.65
onCompletedMethod · 0.65
ofMethod · 0.45

Tested by

no test coverage detected