Search in sources :

Example 6 with Dns

use of okhttp3.Dns in project okhttp by square.

the class HttpOverHttp2Test method setUp.

@Before
public void setUp() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    cache = new Cache(tempDir.getRoot(), Integer.MAX_VALUE);
    client = defaultClient().newBuilder().protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1)).dns(new SingleInetAddressDns()).sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
}
Also used : SingleInetAddressDns(okhttp3.internal.SingleInetAddressDns) Cache(okhttp3.Cache) Before(org.junit.Before)

Example 7 with Dns

use of okhttp3.Dns in project okhttp by square.

the class URLConnectionTest method streamedBodyIsNotRetried.

@Test
public void streamedBodyIsNotRetried() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST));
    urlFactory = new OkUrlFactory(defaultClient().newBuilder().dns(new DoubleInetAddressDns()).build());
    HttpURLConnection connection = urlFactory.open(server.url("/").url());
    connection.setDoOutput(true);
    connection.setChunkedStreamingMode(100);
    OutputStream os = connection.getOutputStream();
    os.write("OutputStream is no fun.".getBytes("UTF-8"));
    os.close();
    try {
        connection.getResponseCode();
        fail();
    } catch (IOException expected) {
    }
    assertEquals(1, server.getRequestCount());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OkHttpURLConnection(okhttp3.internal.huc.OkHttpURLConnection) DoubleInetAddressDns(okhttp3.internal.DoubleInetAddressDns) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with Dns

use of okhttp3.Dns in project okhttp by square.

the class URLConnectionTest method sameConnectionRedirectAndReuse.

/**
   * Retry redirects if the socket is closed.
   * https://code.google.com/p/android/issues/detail?id=41576
   */
@Test
public void sameConnectionRedirectAndReuse() throws Exception {
    urlFactory.setClient(urlFactory.client().newBuilder().dns(new SingleInetAddressDns()).build());
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).setSocketPolicy(SHUTDOWN_INPUT_AT_END).addHeader("Location: /foo"));
    server.enqueue(new MockResponse().setBody("This is the new page!"));
    assertContent("This is the new page!", urlFactory.open(server.url("/").url()));
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(0, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SingleInetAddressDns(okhttp3.internal.SingleInetAddressDns) Test(org.junit.Test)

Example 9 with Dns

use of okhttp3.Dns in project okhttp by square.

the class CallTest method networkDropsOnConditionalGet.

@Test
public void networkDropsOnConditionalGet() throws IOException {
    client = client.newBuilder().cache(cache).build();
    // Seed the cache.
    server.enqueue(new MockResponse().addHeader("ETag: v1").setBody("A"));
    executeSynchronously("/").assertCode(200).assertBody("A");
    // Attempt conditional cache validation and a DNS miss.
    client.connectionPool().evictAll();
    client = client.newBuilder().dns(new FakeDns()).build();
    executeSynchronously("/").assertFailure(UnknownHostException.class);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 10 with Dns

use of okhttp3.Dns in project okhttp by square.

the class CallTest method noRecoveryFromTlsHandshakeFailureWhenTlsFallbackIsDisabled.

@Test
public void noRecoveryFromTlsHandshakeFailureWhenTlsFallbackIsDisabled() throws Exception {
    client = client.newBuilder().connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT)).hostnameVerifier(new RecordingHostnameVerifier()).dns(new SingleInetAddressDns()).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build();
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (SSLProtocolException expected) {
    // RI response to the FAIL_HANDSHAKE
    } catch (SSLHandshakeException expected) {
    // Android's response to the FAIL_HANDSHAKE
    }
}
Also used : SSLProtocolException(javax.net.ssl.SSLProtocolException) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) SingleInetAddressDns(okhttp3.internal.SingleInetAddressDns) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) 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