Search in sources :

Example 1 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class CacheAdapterTest method put_httpsGet.

@Test
public void put_httpsGet() throws Exception {
    final URL serverUrl = configureHttpsServer(new MockResponse());
    assertEquals("https", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            try {
                assertTrue(connection instanceof HttpsURLConnection);
                assertEquals(toUri(serverUrl), uri);
                assertEquals(serverUrl, connection.getURL());
                HttpsURLConnection cacheHttpsUrlConnection = (HttpsURLConnection) connection;
                HttpsURLConnection realHttpsUrlConnection = (HttpsURLConnection) CacheAdapterTest.this.connection;
                assertEquals(realHttpsUrlConnection.getCipherSuite(), cacheHttpsUrlConnection.getCipherSuite());
                assertEquals(realHttpsUrlConnection.getPeerPrincipal(), cacheHttpsUrlConnection.getPeerPrincipal());
                assertArrayEquals(realHttpsUrlConnection.getLocalCertificates(), cacheHttpsUrlConnection.getLocalCertificates());
                assertArrayEquals(realHttpsUrlConnection.getServerCertificates(), cacheHttpsUrlConnection.getServerCertificates());
                assertEquals(realHttpsUrlConnection.getLocalPrincipal(), cacheHttpsUrlConnection.getLocalPrincipal());
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    connection = new OkUrlFactory(client).open(serverUrl);
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 2 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class CacheAdapterTest method get_httpGet.

@Test
public void get_httpGet() throws Exception {
    final URL serverUrl = configureServer(new MockResponse());
    assertEquals("http", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 3 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class CacheAdapterTest method get_httpsGet.

@Test
public void get_httpsGet() throws Exception {
    final URL serverUrl = configureHttpsServer(new MockResponse());
    assertEquals("https", serverUrl.getProtocol());
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String method, Map<String, List<String>> headers) throws IOException {
            try {
                assertEquals("https", uri.getScheme());
                assertEquals(toUri(serverUrl), uri);
                assertEquals("GET", method);
                assertTrue("Arbitrary standard header not present", headers.containsKey("User-Agent"));
                assertEquals(Collections.singletonList("value1"), headers.get("key1"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    client = client.newBuilder().sslSocketFactory(sslClient.socketFactory, sslClient.trustManager).hostnameVerifier(hostnameVerifier).build();
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key1", "value1");
    executeGet(connection);
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) URI(java.net.URI) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 4 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class DisconnectTest method interruptWritingRequestBody.

@Test
public void interruptWritingRequestBody() throws Exception {
    // 2 MiB
    int requestBodySize = 2 * 1024 * 1024;
    server.enqueue(new MockResponse().throttleBody(64 * 1024, 125, // 500 Kbps
    TimeUnit.MILLISECONDS));
    server.start();
    HttpURLConnection connection = new OkUrlFactory(client).open(server.url("/").url());
    disconnectLater(connection, 500);
    connection.setDoOutput(true);
    connection.setFixedLengthStreamingMode(requestBodySize);
    OutputStream requestBody = connection.getOutputStream();
    byte[] buffer = new byte[1024];
    try {
        for (int i = 0; i < requestBodySize; i += buffer.length) {
            requestBody.write(buffer);
            requestBody.flush();
        }
        fail("Expected connection to be closed");
    } catch (IOException expected) {
    }
    connection.disconnect();
}
Also used : OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with OkUrlFactory

use of okhttp3.OkUrlFactory 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)

Aggregations

OkUrlFactory (okhttp3.OkUrlFactory)10 IOException (java.io.IOException)9 MockResponse (okhttp3.mockwebserver.MockResponse)8 Test (org.junit.Test)8 HttpURLConnection (java.net.HttpURLConnection)7 URI (java.net.URI)6 ResponseCache (java.net.ResponseCache)5 URL (java.net.URL)5 AbstractResponseCache (okhttp3.AbstractResponseCache)5 URLConnection (java.net.URLConnection)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 OkHttpClient (okhttp3.OkHttpClient)3 OutputStream (java.io.OutputStream)2 Map (java.util.Map)2 Buffer (okio.Buffer)2 Before (org.junit.Before)2 InputStream (java.io.InputStream)1 CookieManager (java.net.CookieManager)1 URISyntaxException (java.net.URISyntaxException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1