use of io.grpc.ProxyDetector in project grpc-java by grpc.
the class ManagedChannelImplTest method nameResolverArgsPropagation.
@Test
public void nameResolverArgsPropagation() {
final AtomicReference<NameResolver.Args> capturedArgs = new AtomicReference<>();
final NameResolver noopResolver = new NameResolver() {
@Override
public String getServiceAuthority() {
return "fake-authority";
}
@Override
public void start(Listener2 listener) {
}
@Override
public void shutdown() {
}
};
ProxyDetector neverProxy = new ProxyDetector() {
@Override
public ProxiedSocketAddress proxyFor(SocketAddress targetAddress) {
return null;
}
};
NameResolver.Factory factory = new NameResolver.Factory() {
@Override
public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) {
capturedArgs.set(args);
return noopResolver;
}
@Override
public String getDefaultScheme() {
return "fakescheme";
}
};
channelBuilder.nameResolverFactory(factory).proxyDetector(neverProxy);
createChannel();
NameResolver.Args args = capturedArgs.get();
assertThat(args).isNotNull();
assertThat(args.getDefaultPort()).isEqualTo(DEFAULT_PORT);
assertThat(args.getProxyDetector()).isSameInstanceAs(neverProxy);
verify(offloadExecutor, never()).execute(any(Runnable.class));
args.getOffloadExecutor().execute(new Runnable() {
@Override
public void run() {
}
});
verify(offloadExecutor, times(1)).execute(any(Runnable.class));
}
use of io.grpc.ProxyDetector in project grpc-java by grpc.
the class DnsNameResolverTest method doNotResolveWhenProxyDetected.
@Test
public void doNotResolveWhenProxyDetected() throws Exception {
final String name = "foo.googleapis.com";
final int port = 81;
final InetSocketAddress proxyAddress = new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 1000);
ProxyDetector alwaysDetectProxy = new ProxyDetector() {
@Override
public HttpConnectProxiedSocketAddress proxyFor(SocketAddress targetAddress) {
return HttpConnectProxiedSocketAddress.newBuilder().setTargetAddress((InetSocketAddress) targetAddress).setProxyAddress(proxyAddress).setUsername("username").setPassword("password").build();
}
};
DnsNameResolver resolver = newResolver(name, port, alwaysDetectProxy, Stopwatch.createUnstarted());
AddressResolver mockAddressResolver = mock(AddressResolver.class);
when(mockAddressResolver.resolveAddress(anyString())).thenThrow(new AssertionError());
resolver.setAddressResolver(mockAddressResolver);
resolver.start(mockListener);
assertEquals(1, fakeExecutor.runDueTasks());
verify(mockListener).onResult(resultCaptor.capture());
List<EquivalentAddressGroup> result = resultCaptor.getValue().getAddresses();
assertThat(result).hasSize(1);
EquivalentAddressGroup eag = result.get(0);
assertThat(eag.getAddresses()).hasSize(1);
HttpConnectProxiedSocketAddress socketAddress = (HttpConnectProxiedSocketAddress) eag.getAddresses().get(0);
assertSame(proxyAddress, socketAddress.getProxyAddress());
assertEquals("username", socketAddress.getUsername());
assertEquals("password", socketAddress.getPassword());
assertTrue(socketAddress.getTargetAddress().isUnresolved());
}
Aggregations