Search in sources :

Example 6 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)

Example 7 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class URLEncodingTest method backdoorUrlToUri.

private URI backdoorUrlToUri(URL url) throws Exception {
    final AtomicReference<URI> uriReference = new AtomicReference<>();
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    Internal.instance.setCache(builder, new InternalCache() {

        @Override
        public Response get(Request request) throws IOException {
            uriReference.set(request.url().uri());
            throw new UnsupportedOperationException();
        }

        @Override
        public CacheRequest put(Response response) throws IOException {
            return null;
        }

        @Override
        public void remove(Request request) throws IOException {
        }

        @Override
        public void update(Response cached, Response network) {
        }

        @Override
        public void trackConditionalCacheHit() {
        }

        @Override
        public void trackResponse(CacheStrategy cacheStrategy) {
        }
    });
    try {
        HttpURLConnection connection = new OkUrlFactory(builder.build()).open(url);
        connection.getResponseCode();
    } catch (Exception expected) {
        if (expected.getCause() instanceof URISyntaxException) {
            expected.printStackTrace();
        }
    }
    return uriReference.get();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) CacheRequest(okhttp3.internal.cache.CacheRequest) InternalCache(okhttp3.internal.cache.InternalCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Response(okhttp3.Response) OkUrlFactory(okhttp3.OkUrlFactory) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(okhttp3.internal.cache.CacheRequest) CacheStrategy(okhttp3.internal.cache.CacheStrategy)

Example 8 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class HttpResponseCacheTest method setUp.

@Before
public void setUp() throws Exception {
    cacheDir = cacheRule.getRoot();
    urlFactory = new OkUrlFactory(new OkHttpClient());
}
Also used : OkUrlFactory(okhttp3.OkUrlFactory) OkHttpClient(okhttp3.OkHttpClient) Before(org.junit.Before)

Example 9 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class CacheAdapterTest method put_httpPost.

@Test
public void put_httpPost() throws Exception {
    final String statusLine = "HTTP/1.1 200 Fantastic";
    final URL serverUrl = configureServer(new MockResponse().setStatus(statusLine).addHeader("A", "c"));
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            try {
                assertTrue(connection instanceof HttpURLConnection);
                assertFalse(connection instanceof HttpsURLConnection);
                assertEquals(0, connection.getContentLength());
                HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
                assertEquals("POST", httpUrlConnection.getRequestMethod());
                assertTrue(httpUrlConnection.getDoInput());
                assertTrue(httpUrlConnection.getDoOutput());
                assertEquals("Fantastic", httpUrlConnection.getResponseMessage());
                assertEquals(toUri(serverUrl), uri);
                assertEquals(serverUrl, connection.getURL());
                assertEquals("value", connection.getRequestProperty("key"));
                // Check retrieval by string key.
                assertEquals(statusLine, httpUrlConnection.getHeaderField(null));
                assertEquals("c", httpUrlConnection.getHeaderField("A"));
                // The RI and OkHttp supports case-insensitive matching for this method.
                assertEquals("c", httpUrlConnection.getHeaderField("a"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    executePost(connection);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) HttpURLConnection(java.net.HttpURLConnection) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 10 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class CacheAdapterTest method put_httpGet.

@Test
public void put_httpGet() throws Exception {
    final String statusLine = "HTTP/1.1 200 Fantastic";
    final byte[] response = "ResponseString".getBytes(StandardCharsets.UTF_8);
    final URL serverUrl = configureServer(new MockResponse().setStatus(statusLine).addHeader("A", "c").setBody(new Buffer().write(response)));
    ResponseCache responseCache = new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) throws IOException {
            try {
                assertTrue(connection instanceof HttpURLConnection);
                assertFalse(connection instanceof HttpsURLConnection);
                assertEquals(response.length, connection.getContentLength());
                HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
                assertEquals("GET", httpUrlConnection.getRequestMethod());
                assertTrue(httpUrlConnection.getDoInput());
                assertFalse(httpUrlConnection.getDoOutput());
                assertEquals("Fantastic", httpUrlConnection.getResponseMessage());
                assertEquals(toUri(serverUrl), uri);
                assertEquals(serverUrl, connection.getURL());
                assertEquals("value", connection.getRequestProperty("key"));
                // Check retrieval by string key.
                assertEquals(statusLine, httpUrlConnection.getHeaderField(null));
                assertEquals("c", httpUrlConnection.getHeaderField("A"));
                // The RI and OkHttp supports case-insensitive matching for this method.
                assertEquals("c", httpUrlConnection.getHeaderField("a"));
                return null;
            } catch (Throwable t) {
                throw new IOException("unexpected cache failure", t);
            }
        }
    };
    setInternalCache(new CacheAdapter(responseCache));
    connection = new OkUrlFactory(client).open(serverUrl);
    connection.setRequestProperty("key", "value");
    executeGet(connection);
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) AbstractResponseCache(okhttp3.AbstractResponseCache) OkUrlFactory(okhttp3.OkUrlFactory) HttpURLConnection(java.net.HttpURLConnection) ResponseCache(java.net.ResponseCache) AbstractResponseCache(okhttp3.AbstractResponseCache) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Aggregations

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