Search in sources :

Example 11 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class ResponseCacheTest method setUp.

@Before
public void setUp() throws Exception {
    server.setProtocolNegotiationEnabled(false);
    cache = AndroidShimResponseCache.create(cacheRule.getRoot(), 10 * 1024 * 1024);
    urlFactory = new OkUrlFactory(new OkHttpClient());
    AndroidInternal.setResponseCache(urlFactory, cache);
    cookieManager = new CookieManager();
}
Also used : OkUrlFactory(okhttp3.OkUrlFactory) OkHttpClient(okhttp3.OkHttpClient) CookieManager(java.net.CookieManager) Before(org.junit.Before)

Example 12 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class DisconnectTest method interruptReadingResponseBody.

@Test
public void interruptReadingResponseBody() throws Exception {
    // 2 MiB
    int responseBodySize = 2 * 1024 * 1024;
    server.enqueue(new MockResponse().setBody(new Buffer().write(new byte[responseBodySize])).throttleBody(64 * 1024, 125, // 500 Kbps
    TimeUnit.MILLISECONDS));
    server.start();
    HttpURLConnection connection = new OkUrlFactory(client).open(server.url("/").url());
    disconnectLater(connection, 500);
    InputStream responseBody = connection.getInputStream();
    byte[] buffer = new byte[1024];
    try {
        while (responseBody.read(buffer) != -1) {
        }
        fail("Expected connection to be closed");
    } catch (IOException expected) {
    }
    responseBody.close();
}
Also used : Buffer(okio.Buffer) OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 13 with OkUrlFactory

use of okhttp3.OkUrlFactory in project okhttp by square.

the class ThreadInterruptTest 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();
    interruptLater(500);
    HttpURLConnection connection = new OkUrlFactory(client).open(server.url("/").url());
    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 thread to be interrupted");
    } catch (InterruptedIOException expected) {
    }
    connection.disconnect();
}
Also used : OkUrlFactory(okhttp3.OkUrlFactory) MockResponse(okhttp3.mockwebserver.MockResponse) InterruptedIOException(java.io.InterruptedIOException) HttpURLConnection(java.net.HttpURLConnection) OutputStream(java.io.OutputStream) 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