Search in sources :

Example 6 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ResponseCacheTest method requestCacheControlNoCache.

@Test
public void requestCacheControlNoCache() throws Exception {
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-120, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(0, TimeUnit.SECONDS)).addHeader("Cache-Control: max-age=60").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    URL url = server.url("/").url();
    assertEquals("A", readAscii(openConnection(url)));
    URLConnection connection = openConnection(url);
    connection.setRequestProperty("Cache-Control", "no-cache");
    assertEquals("B", readAscii(connection));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 7 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ResponseCacheTest method requestOnlyIfCachedWithConditionalResponseCached.

@Test
public void requestOnlyIfCachedWithConditionalResponseCached() throws IOException {
    server.enqueue(new MockResponse().setBody("A").addHeader("Cache-Control: max-age=30").addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES)));
    assertEquals("A", readAscii(openConnection(server.url("/").url())));
    HttpURLConnection connection = openConnection(server.url("/").url());
    connection.addRequestProperty("Cache-Control", "only-if-cached");
    assertGatewayTimeout(connection);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 8 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ResponseCacheTest method responseCacheReturnsNullStatusLine.

/**
   * Fail if a badly-behaved cache returns a null status line header.
   * https://code.google.com/p/android/issues/detail?id=160522
   */
@Test
public void responseCacheReturnsNullStatusLine() throws Exception {
    String cachedContentString = "Hello";
    final byte[] cachedContent = cachedContentString.getBytes(StandardCharsets.US_ASCII);
    setInternalCache(new CacheAdapter(new AbstractResponseCache() {

        @Override
        public CacheResponse get(URI uri, String requestMethod, Map<String, List<String>> requestHeaders) throws IOException {
            return new CacheResponse() {

                @Override
                public Map<String, List<String>> getHeaders() throws IOException {
                    String contentType = "text/plain";
                    Map<String, List<String>> headers = new LinkedHashMap<>();
                    headers.put("Content-Length", Arrays.asList(Integer.toString(cachedContent.length)));
                    headers.put("Content-Type", Arrays.asList(contentType));
                    headers.put("Expires", Arrays.asList(formatDate(-1, TimeUnit.HOURS)));
                    headers.put("Cache-Control", Arrays.asList("max-age=60"));
                    // unusable because OkHttp only caches responses with cacheable response codes.
                    return headers;
                }

                @Override
                public InputStream getBody() throws IOException {
                    return new ByteArrayInputStream(cachedContent);
                }
            };
        }
    }));
    HttpURLConnection connection = openConnection(server.url("/").url());
    // should be made.
    try {
        connection.getResponseCode();
        fail();
    } catch (ProtocolException expected) {
    }
}
Also used : ProtocolException(java.net.ProtocolException) URI(java.net.URI) LinkedHashMap(java.util.LinkedHashMap) AbstractResponseCache(okhttp3.AbstractResponseCache) CacheResponse(java.net.CacheResponse) SecureCacheResponse(java.net.SecureCacheResponse) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 9 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ResponseCacheTest method otherStacks_cacheMissWithVaryAcceptEncoding.

// 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(). Accept-Encoding has special behavior so we test it explicitly.
@Test
public void otherStacks_cacheMissWithVaryAcceptEncoding() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Encoding").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 10 with Connection

use of okhttp3.Connection in project okhttp by square.

the class ResponseCacheTest method useCachesFalseDoesNotWriteToCache.

@Test
public void useCachesFalseDoesNotWriteToCache() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    URLConnection connection = openConnection(server.url("/").url());
    connection.setUseCaches(false);
    assertEquals("A", readAscii(connection));
    assertEquals("B", readAscii(openConnection(server.url("/").url())));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)226 MockResponse (okhttp3.mockwebserver.MockResponse)215 HttpURLConnection (java.net.HttpURLConnection)106 IOException (java.io.IOException)73 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)67 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)59 URLConnection (java.net.URLConnection)53 Response (okhttp3.Response)41 InputStream (java.io.InputStream)39 Connection (com.trilead.ssh2.Connection)36 Request (okhttp3.Request)36 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)25 OutputStream (java.io.OutputStream)19 InterruptedIOException (java.io.InterruptedIOException)15 Call (okhttp3.Call)14 BufferedSink (okio.BufferedSink)14