@Test public void singleRoute() throws Exception { Address address = new Address(uriHost, uriPort, socketFactory, null, null, authenticator, null, protocols); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 1); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); dns.assertRequests(uriHost); assertFalse(routeSelector.hasNext()); try { routeSelector.next("GET"); fail(); } catch (NoSuchElementException expected) { } }
@Test public void singleRouteReturnsFailedRoute() throws Exception { Address address = new Address(uriHost, uriPort, socketFactory, null, null, authenticator, null, protocols); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 1); Connection connection = routeSelector.next("GET"); RouteDatabase routeDatabase = new RouteDatabase(); routeDatabase.failed(connection.getRoute()); routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, routeDatabase); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); assertFalse(routeSelector.hasNext()); try { routeSelector.next("GET"); fail(); } catch (NoSuchElementException expected) { } }
@Test public void explicitProxyTriesThatProxiesAddressesOnly() throws Exception { Address address = new Address(uriHost, uriPort, socketFactory, null, null, authenticator, proxyA, protocols); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 2); assertConnection(routeSelector.next("GET"), address, proxyA, dns.inetAddresses[0], proxyAPort, false); assertConnection(routeSelector.next("GET"), address, proxyA, dns.inetAddresses[1], proxyAPort, false); assertFalse(routeSelector.hasNext()); dns.assertRequests(proxyAHost); proxySelector.assertRequests(); // No proxy selector requests! }
@Test public void explicitDirectProxy() throws Exception { Address address = new Address(uriHost, uriPort, socketFactory, null, null, authenticator, NO_PROXY, protocols); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 2); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[1], uriPort, false); assertFalse(routeSelector.hasNext()); dns.assertRequests(uri.getHost()); proxySelector.assertRequests(); // No proxy selector requests! }
@Test public void proxySelectorReturnsNull() throws Exception { Address address = new Address(uriHost, uriPort, socketFactory, null, null, authenticator, null, protocols); proxySelector.proxies = null; RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); proxySelector.assertRequests(uri); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 1); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); dns.assertRequests(uriHost); assertFalse(routeSelector.hasNext()); }
@Test public void proxySelectorReturnsNoProxies() throws Exception { Address address = new Address(uriHost, uriPort, socketFactory, null, null, authenticator, null, protocols); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 2); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[1], uriPort, false); assertFalse(routeSelector.hasNext()); dns.assertRequests(uri.getHost()); proxySelector.assertRequests(uri); }
@Test public void proxySelectorDirectConnectionsAreSkipped() throws Exception { Address address = new Address(uriHost, uriPort, socketFactory, null, null, authenticator, null, protocols); proxySelector.proxies.add(NO_PROXY); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); proxySelector.assertRequests(uri); // Only the origin server will be attempted. assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 1); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); dns.assertRequests(uriHost); assertFalse(routeSelector.hasNext()); }
@Test public void singleRoute() throws Exception { Address address = new Address(uriHost, uriPort, null, null, authenticator, null, transports); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 1); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); dns.assertRequests(uriHost); assertFalse(routeSelector.hasNext()); try { routeSelector.next("GET"); fail(); } catch (NoSuchElementException expected) { } }
@Test public void singleRouteReturnsFailedRoute() throws Exception { Address address = new Address(uriHost, uriPort, null, null, authenticator, null, transports); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 1); Connection connection = routeSelector.next("GET"); RouteDatabase routeDatabase = new RouteDatabase(); routeDatabase.failed(connection.getRoute(), new IOException()); routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, routeDatabase); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); assertFalse(routeSelector.hasNext()); try { routeSelector.next("GET"); fail(); } catch (NoSuchElementException expected) { } }
@Test public void explicitProxyTriesThatProxiesAddressesOnly() throws Exception { Address address = new Address(uriHost, uriPort, null, null, authenticator, proxyA, transports); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 2); assertConnection(routeSelector.next("GET"), address, proxyA, dns.inetAddresses[0], proxyAPort, false); assertConnection(routeSelector.next("GET"), address, proxyA, dns.inetAddresses[1], proxyAPort, false); assertFalse(routeSelector.hasNext()); dns.assertRequests(proxyAHost); proxySelector.assertRequests(); // No proxy selector requests! }
@Test public void explicitDirectProxy() throws Exception { Address address = new Address(uriHost, uriPort, null, null, authenticator, NO_PROXY, transports); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 2); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[1], uriPort, false); assertFalse(routeSelector.hasNext()); dns.assertRequests(uri.getHost()); proxySelector.assertRequests(); // No proxy selector requests! }
@Test public void proxySelectorReturnsNull() throws Exception { Address address = new Address(uriHost, uriPort, null, null, authenticator, null, transports); proxySelector.proxies = null; RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); proxySelector.assertRequests(uri); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 1); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); dns.assertRequests(uriHost); assertFalse(routeSelector.hasNext()); }
@Test public void proxySelectorReturnsNoProxies() throws Exception { Address address = new Address(uriHost, uriPort, null, null, authenticator, null, transports); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 2); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[1], uriPort, false); assertFalse(routeSelector.hasNext()); dns.assertRequests(uri.getHost()); proxySelector.assertRequests(uri); }
@Test public void proxySelectorDirectConnectionsAreSkipped() throws Exception { Address address = new Address(uriHost, uriPort, null, null, authenticator, null, transports); proxySelector.proxies.add(NO_PROXY); RouteSelector routeSelector = new RouteSelector(address, uri, proxySelector, pool, dns, new RouteDatabase()); proxySelector.assertRequests(uri); // Only the origin server will be attempted. assertTrue(routeSelector.hasNext()); dns.inetAddresses = makeFakeAddresses(255, 1); assertConnection(routeSelector.next("GET"), address, NO_PROXY, dns.inetAddresses[0], uriPort, false); dns.assertRequests(uriHost); assertFalse(routeSelector.hasNext()); }
public RouteSelector(Address address, URI uri, ProxySelector proxySelector, ConnectionPool pool, Dns dns, RouteDatabase routeDatabase) { this.address = address; this.uri = uri; this.proxySelector = proxySelector; this.pool = pool; this.dns = dns; this.routeDatabase = routeDatabase; this.postponedRoutes = new LinkedList<Route>(); resetNextProxy(uri, address.getProxy()); }