Search in sources :

Example 41 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ResponseCacheTest method requestMinFresh.

@Test
public void requestMinFresh() throws IOException {
    server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=60").addHeader("Date: " + formatDate(0, TimeUnit.MINUTES)));
    server.enqueue(new MockResponse().setBody("B"));
    assertEquals("A", readAscii(openConnection(server.url("/").url())));
    URLConnection connection = openConnection(server.url("/").url());
    connection.addRequestProperty("Cache-Control", "min-fresh=120");
    assertEquals("B", readAscii(connection));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 42 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ResponseCacheTest method otherStacks_cacheMissWithVaryAsterisk.

// Other stacks (e.g. older versions of OkHttp bundled inside Android apps) can interact with the
// default ResponseCache. We can't keep the Vary case working, because we can't get to the Vary
// request headers after connect().
@Test
public void otherStacks_cacheMissWithVaryAsterisk() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: *").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    // Set the cache as the shared cache.
    ResponseCache.setDefault(cache);
    // Use the platform's HTTP stack.
    URLConnection connection = server.url("/").url().openConnection();
    assertFalse(connection instanceof OkHttpURLConnection);
    assertEquals("A", readAscii(connection));
    URLConnection connection2 = server.url("/").url().openConnection();
    assertFalse(connection2 instanceof OkHttpURLConnection);
    assertEquals("B", readAscii(connection2));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 43 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ResponseCacheTest method responseCacheReturnsNullOutputStream.

/** Don't explode if the cache returns a null body. http://b/3373699 */
@Test
public void responseCacheReturnsNullOutputStream() throws Exception {
    final AtomicBoolean aborted = new AtomicBoolean();
    setInternalCache(new CacheAdapter(new AbstractResponseCache() {

        @Override
        public CacheRequest put(URI uri, URLConnection connection) {
            return new CacheRequest() {

                @Override
                public void abort() {
                    aborted.set(true);
                }

                @Override
                public OutputStream getBody() throws IOException {
                    return null;
                }
            };
        }
    }));
    server.enqueue(new MockResponse().setBody("abcdef"));
    HttpURLConnection connection = openConnection(server.url("/").url());
    assertEquals("abc", readAscii(connection, 3));
    connection.getInputStream().close();
    // The best behavior is ambiguous, but RI 6 doesn't abort here
    assertFalse(aborted.get());
}
Also used : AbstractResponseCache(okhttp3.AbstractResponseCache) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) CacheRequest(java.net.CacheRequest) URI(java.net.URI) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 44 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HttpOverHttp2Test method connectionTimeout.

@Test
public void connectionTimeout() throws Exception {
    server.enqueue(new MockResponse().setBody("A").setBodyDelay(1, SECONDS));
    OkHttpClient client1 = client.newBuilder().readTimeout(2000, MILLISECONDS).build();
    Call call1 = client1.newCall(new Request.Builder().url(server.url("/")).build());
    OkHttpClient client2 = client.newBuilder().readTimeout(200, MILLISECONDS).build();
    Call call2 = client2.newCall(new Request.Builder().url(server.url("/")).build());
    Response response1 = call1.execute();
    assertEquals("A", response1.body().string());
    try {
        call2.execute();
        fail();
    } catch (IOException expected) {
    }
    // Confirm that the connection was reused.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException) Test(org.junit.Test)

Example 45 with Connection

use of okhttp3.Connection in project okhttp by square.

the class HttpOverHttp2Test method concurrentHttp2ConnectionsDeduplicated.

/**
   * We don't know if the connection will support HTTP/2 until after we've connected. When multiple
   * connections are requested concurrently OkHttp will pessimistically connect multiple times, then
   * close any unnecessary connections. This test confirms that behavior works as intended.
   *
   * <p>This test uses proxy tunnels to get a hook while a connection is being established.
   */
@Test
public void concurrentHttp2ConnectionsDeduplicated() throws Exception {
    server.useHttps(sslClient.socketFactory, true);
    // Force a fresh connection pool for the test.
    client.connectionPool().evictAll();
    final QueueDispatcher queueDispatcher = new QueueDispatcher();
    queueDispatcher.enqueueResponse(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    queueDispatcher.enqueueResponse(new MockResponse().setSocketPolicy(SocketPolicy.UPGRADE_TO_SSL_AT_END).clearHeaders());
    queueDispatcher.enqueueResponse(new MockResponse().setBody("call2 response"));
    queueDispatcher.enqueueResponse(new MockResponse().setBody("call1 response"));
    // We use a re-entrant dispatcher to initiate one HTTPS connection while the other is in flight.
    server.setDispatcher(new Dispatcher() {

        int requestCount;

        @Override
        public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
            MockResponse result = queueDispatcher.dispatch(request);
            requestCount++;
            if (requestCount == 1) {
                // Before handling call1's CONNECT we do all of call2. This part re-entrant!
                try {
                    Call call2 = client.newCall(new Request.Builder().url("https://android.com/call2").build());
                    Response response2 = call2.execute();
                    assertEquals("call2 response", response2.body().string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return result;
        }

        @Override
        public MockResponse peek() {
            return queueDispatcher.peek();
        }

        @Override
        public void shutdown() {
            queueDispatcher.shutdown();
        }
    });
    client = client.newBuilder().proxy(server.toProxyAddress()).build();
    Call call1 = client.newCall(new Request.Builder().url("https://android.com/call1").build());
    Response response2 = call1.execute();
    assertEquals("call1 response", response2.body().string());
    RecordedRequest call1Connect = server.takeRequest();
    assertEquals("CONNECT", call1Connect.getMethod());
    assertEquals(0, call1Connect.getSequenceNumber());
    RecordedRequest call2Connect = server.takeRequest();
    assertEquals("CONNECT", call2Connect.getMethod());
    assertEquals(0, call2Connect.getSequenceNumber());
    RecordedRequest call2Get = server.takeRequest();
    assertEquals("GET", call2Get.getMethod());
    assertEquals("/call2", call2Get.getPath());
    assertEquals(0, call2Get.getSequenceNumber());
    RecordedRequest call1Get = server.takeRequest();
    assertEquals("GET", call1Get.getMethod());
    assertEquals("/call1", call1Get.getPath());
    assertEquals(1, call1Get.getSequenceNumber());
    assertEquals(1, client.connectionPool().connectionCount());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Call(okhttp3.Call) Request(okhttp3.Request) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) QueueDispatcher(okhttp3.mockwebserver.QueueDispatcher) IOException(java.io.IOException) QueueDispatcher(okhttp3.mockwebserver.QueueDispatcher) Dispatcher(okhttp3.mockwebserver.Dispatcher) MockResponse(okhttp3.mockwebserver.MockResponse) Response(okhttp3.Response) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)79 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)43 Connection (com.trilead.ssh2.Connection)40 InputStream (java.io.InputStream)40 Request (okhttp3.Request)38 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)36 URL (java.net.URL)32 Session (com.trilead.ssh2.Session)31 InFrame (okhttp3.internal.http2.MockHttp2Peer.InFrame)28 Buffer (okio.Buffer)26 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 ResponseBody (okhttp3.ResponseBody)14