Search in sources :

Example 6 with Address

use of okhttp3.Address in project okhttp by square.

the class RouteSelectorTest method proxySelectorDirectConnectionsAreSkipped.

@Test
public void proxySelectorDirectConnectionsAreSkipped() throws Exception {
    Address address = httpAddress();
    proxySelector.proxies.add(NO_PROXY);
    RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
    proxySelector.assertRequests(address.url().uri());
    // Only the origin server will be attempted.
    assertTrue(routeSelector.hasNext());
    dns.set(uriHost, dns.allocate(1));
    assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
    dns.assertRequests(uriHost);
    assertFalse(routeSelector.hasNext());
}
Also used : SocketAddress(java.net.SocketAddress) Address(okhttp3.Address) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 7 with Address

use of okhttp3.Address in project okhttp by square.

the class RouteSelectorTest method singleRouteReturnsFailedRoute.

@Test
public void singleRouteReturnsFailedRoute() throws Exception {
    Address address = httpAddress();
    RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
    assertTrue(routeSelector.hasNext());
    dns.set(uriHost, dns.allocate(1));
    Route route = routeSelector.next();
    routeDatabase.failed(route);
    routeSelector = new RouteSelector(address, routeDatabase);
    assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
    assertFalse(routeSelector.hasNext());
    try {
        routeSelector.next();
        fail();
    } catch (NoSuchElementException expected) {
    }
}
Also used : SocketAddress(java.net.SocketAddress) Address(okhttp3.Address) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) Route(okhttp3.Route) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test)

Example 8 with Address

use of okhttp3.Address in project okhttp by square.

the class RouteSelectorTest method proxySelectorReturnsNoProxies.

@Test
public void proxySelectorReturnsNoProxies() throws Exception {
    Address address = httpAddress();
    RouteSelector routeSelector = new RouteSelector(address, routeDatabase);
    assertTrue(routeSelector.hasNext());
    dns.set(uriHost, dns.allocate(2));
    assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 0), uriPort);
    assertRoute(routeSelector.next(), address, NO_PROXY, dns.lookup(uriHost, 1), uriPort);
    assertFalse(routeSelector.hasNext());
    dns.assertRequests(uriHost);
    proxySelector.assertRequests(address.url().uri());
}
Also used : SocketAddress(java.net.SocketAddress) Address(okhttp3.Address) InetAddress(java.net.InetAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 9 with Address

use of okhttp3.Address in project okhttp by square.

the class URLConnectionTest method redirectWithProxySelector.

@Test
public void redirectWithProxySelector() throws Exception {
    final List<URI> proxySelectionRequests = new ArrayList<>();
    urlFactory.setClient(urlFactory.client().newBuilder().proxySelector(new ProxySelector() {

        @Override
        public List<Proxy> select(URI uri) {
            proxySelectionRequests.add(uri);
            MockWebServer proxyServer = (uri.getPort() == server.getPort()) ? server : server2;
            return Arrays.asList(proxyServer.toProxyAddress());
        }

        @Override
        public void connectFailed(URI uri, SocketAddress address, IOException failure) {
            throw new AssertionError();
        }
    }).build());
    server2.enqueue(new MockResponse().setBody("This is the 2nd server!"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server2.url("/b").url().toString()).setBody("This page has moved!"));
    assertContent("This is the 2nd server!", urlFactory.open(server.url("/a").url()));
    assertEquals(Arrays.asList(server.url("/").url().toURI(), server2.url("/").url().toURI()), proxySelectionRequests);
}
Also used : ProxySelector(java.net.ProxySelector) MockResponse(okhttp3.mockwebserver.MockResponse) ArrayList(java.util.ArrayList) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) URI(java.net.URI) Test(org.junit.Test)

Example 10 with Address

use of okhttp3.Address in project okhttp by square.

the class URLConnectionTest method testConnectViaSocketFactory.

public void testConnectViaSocketFactory(boolean useHttps) throws IOException {
    SocketFactory uselessSocketFactory = new SocketFactory() {

        public Socket createSocket() {
            throw new IllegalArgumentException("useless");
        }

        public Socket createSocket(InetAddress host, int port) {
            return null;
        }

        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) {
            return null;
        }

        public Socket createSocket(String host, int port) {
            return null;
        }

        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) {
            return null;
        }
    };
    if (useHttps) {
        server.useHttps(sslClient.socketFactory, false);
        urlFactory.setClient(urlFactory.client().newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(new RecordingHostnameVerifier()).build());
    }
    server.enqueue(new MockResponse().setStatus("HTTP/1.1 200 OK"));
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(uselessSocketFactory).build());
    connection = urlFactory.open(server.url("/").url());
    try {
        connection.getResponseCode();
        fail();
    } catch (IllegalArgumentException expected) {
    }
    urlFactory.setClient(urlFactory.client().newBuilder().socketFactory(SocketFactory.getDefault()).build());
    connection = urlFactory.open(server.url("/").url());
    assertEquals(200, connection.getResponseCode());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) SocketFactory(javax.net.SocketFactory) ServerSocketFactory(javax.net.ServerSocketFactory) InetAddress(java.net.InetAddress)

Aggregations

Test (org.junit.Test)92 Address (org.orcid.jaxb.model.record_v2.Address)84 Keyword (org.orcid.jaxb.model.record_v2.Keyword)44 PersonExternalIdentifier (org.orcid.jaxb.model.record_v2.PersonExternalIdentifier)44 ResearcherUrl (org.orcid.jaxb.model.record_v2.ResearcherUrl)44 OtherName (org.orcid.jaxb.model.record_v2.OtherName)43 Addresses (org.orcid.jaxb.model.record_v2.Addresses)42 Email (org.orcid.jaxb.model.record_v2.Email)41 Biography (org.orcid.jaxb.model.record_v2.Biography)34 OtherNames (org.orcid.jaxb.model.record_v2.OtherNames)34 Emails (org.orcid.jaxb.model.record_v2.Emails)32 Keywords (org.orcid.jaxb.model.record_v2.Keywords)32 PersonExternalIdentifiers (org.orcid.jaxb.model.record_v2.PersonExternalIdentifiers)32 ResearcherUrls (org.orcid.jaxb.model.record_v2.ResearcherUrls)32 Name (org.orcid.jaxb.model.record_v2.Name)30 Person (org.orcid.jaxb.model.record_v2.Person)30 DBUnitTest (org.orcid.test.DBUnitTest)20 Response (javax.ws.rs.core.Response)18 EducationSummary (org.orcid.jaxb.model.record.summary_v2.EducationSummary)18 EmploymentSummary (org.orcid.jaxb.model.record.summary_v2.EmploymentSummary)18