Search in sources :

Example 1 with ProxyDetector

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));
}
Also used : PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) ProxyDetector(io.grpc.ProxyDetector) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) NameResolver(io.grpc.NameResolver) Test(org.junit.Test)

Example 2 with ProxyDetector

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());
}
Also used : AddressResolver(io.grpc.internal.DnsNameResolver.AddressResolver) InetSocketAddress(java.net.InetSocketAddress) ProxyDetector(io.grpc.ProxyDetector) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) HttpConnectProxiedSocketAddress(io.grpc.HttpConnectProxiedSocketAddress) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) HttpConnectProxiedSocketAddress(io.grpc.HttpConnectProxiedSocketAddress) Test(org.junit.Test)

Aggregations

ProxyDetector (io.grpc.ProxyDetector)2 SocketAddress (java.net.SocketAddress)2 Test (org.junit.Test)2 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)1 HttpConnectProxiedSocketAddress (io.grpc.HttpConnectProxiedSocketAddress)1 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)1 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)1 NameResolver (io.grpc.NameResolver)1 ProxiedSocketAddress (io.grpc.ProxiedSocketAddress)1 AddressResolver (io.grpc.internal.DnsNameResolver.AddressResolver)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1