ClientCallImpl extends ClientCall<ReqT, RespT> implements Context.CancellationListener { @VisibleForTesting static void prepareHeaders(Metadata headers, CallOptions callOptions, String userAgent, DecompressorRegistry decompressorRegistry, Compressor compressor) { headers.removeAll(USER_AGENT_KEY); if (userAgent != null) { headers.put(USER_AGENT_KEY, userAgent); } headers.removeAll(MESSAGE_ENCODING_KEY); if (compressor != Codec.Identity.NONE) { headers.put(MESSAGE_ENCODING_KEY, compressor.getMessageEncoding()); } headers.removeAll(MESSAGE_ACCEPT_ENCODING_KEY); if (!decompressorRegistry.getAdvertisedMessageEncodings().isEmpty()) { String acceptEncoding = ACCEPT_ENCODING_JOINER.join(decompressorRegistry.getAdvertisedMessageEncodings()); headers.put(MESSAGE_ACCEPT_ENCODING_KEY, acceptEncoding); } } ClientCallImpl(MethodDescriptor<ReqT, RespT> method, Executor executor,
      CallOptions callOptions, ClientTransportProvider clientTransportProvider,
      ScheduledExecutorService deadlineCancellationExecutor); @Override void cancelled(Context context); @Override void start(final Listener<RespT> observer, Metadata headers); @Override void request(int numMessages); @Override void cancel(@Nullable String message, @Nullable Throwable cause); @Override void halfClose(); @Override void sendMessage(ReqT message); @Override void setMessageCompression(boolean enabled); @Override boolean isReady();  }
@Test public void prepareHeaders_userAgentAdded() { Metadata m = new Metadata(); ClientCallImpl.prepareHeaders(m, CallOptions.DEFAULT, "user agent", decompressorRegistry, Codec.Identity.NONE); assertEquals(m.get(GrpcUtil.USER_AGENT_KEY), "user agent"); }
@Test public void prepareHeaders_ignoreIdentityEncoding() { Metadata m = new Metadata(); ClientCallImpl.prepareHeaders(m, CallOptions.DEFAULT, "user agent", decompressorRegistry, Codec.Identity.NONE); assertNull(m.get(GrpcUtil.MESSAGE_ENCODING_KEY)); }
@Test public void prepareHeaders_acceptedEncodingsAdded() { Metadata m = new Metadata(); DecompressorRegistry customRegistry = DecompressorRegistry.newEmptyInstance(); customRegistry.register(new Decompressor() { @Override public String getMessageEncoding() { return "a"; } @Override public InputStream decompress(InputStream is) throws IOException { return null; } }, true); customRegistry.register(new Decompressor() { @Override public String getMessageEncoding() { return "b"; } @Override public InputStream decompress(InputStream is) throws IOException { return null; } }, true); customRegistry.register(new Decompressor() { @Override public String getMessageEncoding() { return "c"; } @Override public InputStream decompress(InputStream is) throws IOException { return null; } }, false); ClientCallImpl.prepareHeaders(m, CallOptions.DEFAULT, "user agent", customRegistry, Codec.Identity.NONE); Iterable<String> acceptedEncodings = ACCEPT_ENCODING_SPLITER.split(m.get(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY)); assertEquals(ImmutableSet.of("b", "a"), ImmutableSet.copyOf(acceptedEncodings)); }
@Test public void prepareHeaders_removeReservedHeaders() { Metadata m = new Metadata(); m.put(GrpcUtil.USER_AGENT_KEY, "user agent"); m.put(GrpcUtil.MESSAGE_ENCODING_KEY, "gzip"); m.put(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY, "gzip"); ClientCallImpl.prepareHeaders(m, CallOptions.DEFAULT, null, DecompressorRegistry.newEmptyInstance(), Codec.Identity.NONE); assertNull(m.get(GrpcUtil.USER_AGENT_KEY)); assertNull(m.get(GrpcUtil.MESSAGE_ENCODING_KEY)); assertNull(m.get(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY)); }