forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insert custom headers via HeaderInterceptor.
- Loading branch information
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/google/api/gax/grpc/HeaderInterceptor.java
This file contains 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,37 @@ | ||
package com.google.api.gax.grpc; | ||
|
||
import io.grpc.CallOptions; | ||
import io.grpc.Channel; | ||
import io.grpc.ClientCall; | ||
import io.grpc.ClientInterceptor; | ||
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; | ||
import io.grpc.Metadata; | ||
import io.grpc.MethodDescriptor; | ||
|
||
/** | ||
* An intercepter to handle custom header. | ||
*/ | ||
public class HeaderInterceptor implements ClientInterceptor { | ||
private static final Metadata.Key<String> HEADER_KEY = | ||
Metadata.Key.of("x-google-apis-agent", Metadata.ASCII_STRING_MARSHALLER); | ||
private final String header; | ||
|
||
public HeaderInterceptor(String header) { | ||
this.header = header; | ||
} | ||
|
||
@Override | ||
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( | ||
MethodDescriptor<ReqT, RespT> method, | ||
CallOptions callOptions, | ||
Channel next) { | ||
ClientCall<ReqT, RespT> call = next.newCall(method, callOptions); | ||
return new SimpleForwardingClientCall<ReqT, RespT>(call) { | ||
@Override | ||
public void start(ClientCall.Listener<RespT> responseListener, Metadata headers) { | ||
headers.put(HEADER_KEY, header); | ||
super.start(responseListener, headers); | ||
} | ||
}; | ||
} | ||
} |
This file contains 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
69 changes: 69 additions & 0 deletions
69
src/test/java/com/google/api/gax/grpc/HeaderInterceptorTest.java
This file contains 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,69 @@ | ||
package com.google.api.gax.grpc; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.same; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import io.grpc.CallOptions; | ||
import io.grpc.Channel; | ||
import io.grpc.ClientCall; | ||
import io.grpc.ClientInterceptors; | ||
import io.grpc.Metadata; | ||
import io.grpc.MethodDescriptor; | ||
|
||
/** | ||
* Tests for {@link HeaderInterceptor}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class HeaderInterceptorTest { | ||
|
||
@Mock | ||
private Channel channel; | ||
|
||
@Mock | ||
private ClientCall<String, Integer> call; | ||
|
||
@Mock | ||
private MethodDescriptor<String, Integer> method; | ||
|
||
/** | ||
* Sets up mocks. | ||
*/ | ||
@Before public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
when(channel.newCall( | ||
Mockito.<MethodDescriptor<String, Integer>>any(), any(CallOptions.class))) | ||
.thenReturn(call); | ||
} | ||
|
||
@Test | ||
public void testInterceptor() { | ||
final Metadata.Key<String> headerKey = | ||
Metadata.Key.of("x-google-apis-agent", Metadata.ASCII_STRING_MARSHALLER); | ||
String data = "abcd"; | ||
HeaderInterceptor interceptor = new HeaderInterceptor(data); | ||
Channel intercepted = ClientInterceptors.intercept(channel, interceptor); | ||
@SuppressWarnings("unchecked") | ||
ClientCall.Listener<Integer> listener = mock(ClientCall.Listener.class); | ||
ClientCall<String, Integer> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT); | ||
// start() on the intercepted call will eventually reach the call created by the real channel | ||
interceptedCall.start(listener, new Metadata()); | ||
// The headers passed to the real channel call will contain the information inserted by the | ||
// interceptor. | ||
ArgumentCaptor<Metadata> captor = ArgumentCaptor.forClass(Metadata.class); | ||
verify(call).start(same(listener), captor.capture()); | ||
assertEquals(data, captor.getValue().get(headerKey)); | ||
} | ||
} |