Search in sources :

Example 86 with URLConnection

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

the class UrlConnectionCacheTest 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(urlFactory.open(url)));
    URLConnection connection = urlFactory.open(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 87 with URLConnection

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

the class UrlConnectionCacheTest method varyMultipleFieldValuesWithNoMatch.

@Test
public void varyMultipleFieldValuesWithNoMatch() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    URL url = server.url("/").url();
    URLConnection connection1 = urlFactory.open(url);
    connection1.addRequestProperty("Accept-Language", "fr-CA, fr-FR");
    connection1.addRequestProperty("Accept-Language", "en-US");
    assertEquals("A", readAscii(connection1));
    URLConnection connection2 = urlFactory.open(url);
    connection2.addRequestProperty("Accept-Language", "fr-CA");
    connection2.addRequestProperty("Accept-Language", "en-US");
    assertEquals("B", readAscii(connection2));
}
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 88 with URLConnection

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

the class UrlConnectionCacheTest method setIfModifiedSince.

/**
   * Confirm that {@link URLConnection#setIfModifiedSince} causes an If-Modified-Since header with a
   * GMT timestamp.
   *
   * https://code.google.com/p/android/issues/detail?id=66135
   */
@Test
public void setIfModifiedSince() throws Exception {
    server.enqueue(new MockResponse().setBody("A"));
    URL url = server.url("/").url();
    URLConnection connection = urlFactory.open(url);
    connection.setIfModifiedSince(1393666200000L);
    assertEquals("A", readAscii(connection));
    RecordedRequest request = server.takeRequest();
    String ifModifiedSinceHeader = request.getHeader("If-Modified-Since");
    assertEquals("Sat, 01 Mar 2014 09:30:00 GMT", ifModifiedSinceHeader);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) 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 89 with URLConnection

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

the class UrlConnectionCacheTest method getHeadersReturnsCachedHopByHopHeaders.

@Test
public void getHeadersReturnsCachedHopByHopHeaders() throws Exception {
    server.enqueue(new MockResponse().addHeader("Transfer-Encoding: identity").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Transfer-Encoding: none").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    URLConnection connection1 = urlFactory.open(server.url("/").url());
    assertEquals("A", readAscii(connection1));
    assertEquals("identity", connection1.getHeaderField("Transfer-Encoding"));
    URLConnection connection2 = urlFactory.open(server.url("/").url());
    assertEquals("A", readAscii(connection2));
    assertEquals("identity", connection2.getHeaderField("Transfer-Encoding"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 90 with URLConnection

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

the class UrlConnectionCacheTest method defaultExpirationDateFullyCachedForLessThan24Hours.

@Test
public void defaultExpirationDateFullyCachedForLessThan24Hours() throws Exception {
    //      last modified: 105 seconds ago
    //             served:   5 seconds ago
    //   default lifetime: (105 - 5) / 10 = 10 seconds
    //            expires:  10 seconds from served date = 5 seconds from now
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-105, TimeUnit.SECONDS)).addHeader("Date: " + formatDate(-5, TimeUnit.SECONDS)).setBody("A"));
    URL url = server.url("/").url();
    assertEquals("A", readAscii(urlFactory.open(url)));
    URLConnection connection = urlFactory.open(url);
    assertEquals("A", readAscii(connection));
    assertNull(connection.getHeaderField("Warning"));
}
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)

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