Search in sources :

Example 26 with Dns

use of okhttp3.Dns in project okhttp by square.

the class CallTest method recoverWhenRetryOnConnectionFailureIsTrue.

@Test
public void recoverWhenRetryOnConnectionFailureIsTrue() throws Exception {
    server.enqueue(new MockResponse().setBody("seed connection pool"));
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST));
    server.enqueue(new MockResponse().setBody("retry success"));
    client = client.newBuilder().dns(new DoubleInetAddressDns()).build();
    assertTrue(client.retryOnConnectionFailure());
    executeSynchronously("/").assertBody("seed connection pool");
    executeSynchronously("/").assertBody("retry success");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) DoubleInetAddressDns(okhttp3.internal.DoubleInetAddressDns) Test(org.junit.Test)

Example 27 with Dns

use of okhttp3.Dns in project okhttp by square.

the class CallTest method invalidHost.

// This may fail in DNS lookup, which we don't have timeouts for.
@Ignore
@Test
public void invalidHost() throws Exception {
    Request request = new Request.Builder().url(HttpUrl.parse("http://1234.1.1.1/")).build();
    executeSynchronously(request).assertFailure(UnknownHostException.class);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with Dns

use of okhttp3.Dns in project okhttp by square.

the class CallTest method customDns.

@Test
public void customDns() throws Exception {
    // Configure a DNS that returns our local MockWebServer for android.com.
    FakeDns dns = new FakeDns();
    dns.set("android.com", Dns.SYSTEM.lookup(server.url("/").host()));
    client = client.newBuilder().dns(dns).build();
    server.enqueue(new MockResponse());
    Request request = new Request.Builder().url(server.url("/").newBuilder().host("android.com").build()).build();
    executeSynchronously(request).assertCode(200);
    dns.assertRequests("android.com");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 29 with Dns

use of okhttp3.Dns in project okhttp by square.

the class URLConnectionTest method unexpectedExceptionAsync.

/** Confirm that runtime exceptions thrown inside of OkHttp propagate to the caller. */
@Test
public void unexpectedExceptionAsync() throws Exception {
    urlFactory.setClient(urlFactory.client().newBuilder().dns(new Dns() {

        @Override
        public List<InetAddress> lookup(String hostname) {
            throw new RuntimeException("boom!");
        }
    }).build());
    server.enqueue(new MockResponse());
    HttpURLConnection connection = urlFactory.open(server.url("/").url());
    try {
        // Force the async implementation.
        connection.connect();
        fail();
    } catch (RuntimeException expected) {
        assertEquals("boom!", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) DoubleInetAddressDns(okhttp3.internal.DoubleInetAddressDns) SingleInetAddressDns(okhttp3.internal.SingleInetAddressDns) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 30 with Dns

use of okhttp3.Dns in project okhttp by square.

the class URLConnectionTest method connectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache.

/** Tolerate bad https proxy response when using HttpResponseCache. Android bug 6754912. */
@Test
public void connectViaHttpProxyToHttpsUsingBadProxyAndHttpResponseCache() throws Exception {
    initResponseCache();
    server.useHttps(sslClient.socketFactory, true);
    // The inclusion of a body in the response to a CONNECT is key to reproducing b/6754912.
    MockResponse badProxyResponse = new MockResponse().setSocketPolicy(UPGRADE_TO_SSL_AT_END).setBody("bogus proxy connect response content");
    server.enqueue(badProxyResponse);
    server.enqueue(new MockResponse().setBody("response"));
    // Configure a single IP address for the host and a single configuration, so we only need one
    // failure to fail permanently.
    urlFactory.setClient(urlFactory.client().newBuilder().dns(new SingleInetAddressDns()).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).connectionSpecs(Util.immutableList(ConnectionSpec.MODERN_TLS)).hostnameVerifier(new RecordingHostnameVerifier()).proxy(server.toProxyAddress()).build());
    URL url = new URL("https://android.com/foo");
    connection = urlFactory.open(url);
    assertContent("response", connection);
    RecordedRequest connect = server.takeRequest();
    assertEquals("CONNECT android.com:443 HTTP/1.1", connect.getRequestLine());
    assertEquals("android.com:443", connect.getHeader("Host"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SingleInetAddressDns(okhttp3.internal.SingleInetAddressDns) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)23 MockResponse (okhttp3.mockwebserver.MockResponse)13 OkHttpClient (okhttp3.OkHttpClient)12 DoubleInetAddressDns (okhttp3.internal.DoubleInetAddressDns)10 Test (org.junit.jupiter.api.Test)9 IOException (java.io.IOException)8 Request (okhttp3.Request)8 SingleInetAddressDns (okhttp3.internal.SingleInetAddressDns)8 InetAddress (java.net.InetAddress)7 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)7 MockResponse (mockwebserver3.MockResponse)6 Response (okhttp3.Response)6 HttpURLConnection (java.net.HttpURLConnection)5 InetSocketAddress (java.net.InetSocketAddress)5 UnknownHostException (java.net.UnknownHostException)5 List (java.util.List)4 X509TrustManager (javax.net.ssl.X509TrustManager)4 Dns (okhttp3.Dns)4 Interceptor (okhttp3.Interceptor)4 HandshakeCertificates (okhttp3.tls.HandshakeCertificates)4