Search in sources :

Example 66 with URLConnection

use of java.net.URLConnection 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 67 with URLConnection

use of java.net.URLConnection 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 68 with URLConnection

use of java.net.URLConnection 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 69 with URLConnection

use of java.net.URLConnection in project okhttp by square.

the class MockWebServerTest method delayResponse.

/** Delay the response body by sleeping 1s. */
@Test
public void delayResponse() throws IOException {
    server.enqueue(new MockResponse().setBody("ABCDEF").setBodyDelay(1, SECONDS));
    long startNanos = System.nanoTime();
    URLConnection connection = server.url("/").url().openConnection();
    InputStream in = connection.getInputStream();
    assertEquals('A', in.read());
    long elapsedNanos = System.nanoTime() - startNanos;
    long elapsedMillis = NANOSECONDS.toMillis(elapsedNanos);
    assertTrue(Util.format("Request + Response: %sms", elapsedMillis), elapsedMillis >= 1000);
    in.close();
}
Also used : InputStream(java.io.InputStream) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Example 70 with URLConnection

use of java.net.URLConnection in project okhttp by square.

the class MockWebServerTest method redirect.

@Test
public void redirect() throws Exception {
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_TEMP).addHeader("Location: " + server.url("/new-path")).setBody("This page has moved!"));
    server.enqueue(new MockResponse().setBody("This is the new location!"));
    URLConnection connection = server.url("/").url().openConnection();
    InputStream in = connection.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    assertEquals("This is the new location!", reader.readLine());
    RecordedRequest first = server.takeRequest();
    assertEquals("GET / HTTP/1.1", first.getRequestLine());
    RecordedRequest redirect = server.takeRequest();
    assertEquals("GET /new-path HTTP/1.1", redirect.getRequestLine());
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) Test(org.junit.Test)

Aggregations

URLConnection (java.net.URLConnection)950 URL (java.net.URL)654 IOException (java.io.IOException)378 HttpURLConnection (java.net.HttpURLConnection)310 InputStream (java.io.InputStream)279 InputStreamReader (java.io.InputStreamReader)227 BufferedReader (java.io.BufferedReader)208 Test (org.junit.Test)177 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)145 File (java.io.File)95 MalformedURLException (java.net.MalformedURLException)91 MockResponse (okhttp3.mockwebserver.MockResponse)76 BufferedInputStream (java.io.BufferedInputStream)57 JarURLConnection (java.net.JarURLConnection)57 OutputStream (java.io.OutputStream)55 FileOutputStream (java.io.FileOutputStream)52 FileInputStream (java.io.FileInputStream)47 ArrayList (java.util.ArrayList)47 GZIPInputStream (java.util.zip.GZIPInputStream)37 URI (java.net.URI)35