Search in sources :

Example 16 with Header

use of okhttp3.internal.http2.Header 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 17 with Header

use of okhttp3.internal.http2.Header in project okhttp by square.

the class CacheTest method getHeadersReturnsNetworkEndToEndHeaders.

@Test
public void getHeadersReturnsNetworkEndToEndHeaders() throws Exception {
    server.enqueue(new MockResponse().addHeader("Allow: GET, HEAD").addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Allow: GET, HEAD, PUT").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    Response response1 = get(server.url("/"));
    assertEquals("A", response1.body().string());
    assertEquals("GET, HEAD", response1.header("Allow"));
    Response response2 = get(server.url("/"));
    assertEquals("A", response2.body().string());
    assertEquals("GET, HEAD, PUT", response2.header("Allow"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 18 with Header

use of okhttp3.internal.http2.Header in project okhttp by square.

the class CacheTest 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));
    Response response1 = get(server.url("/"));
    assertEquals("A", response1.body().string());
    assertEquals("identity", response1.header("Transfer-Encoding"));
    Response response2 = get(server.url("/"));
    assertEquals("A", response2.body().string());
    assertEquals("identity", response2.header("Transfer-Encoding"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 19 with Header

use of okhttp3.internal.http2.Header in project okhttp by square.

the class CacheTest 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"));
    HttpUrl url = server.url("/");
    assertEquals("A", get(url).body().string());
    Request request = new Request.Builder().url(url).header("Cache-Control", "no-cache").build();
    Response response = client.newCall(request).execute();
    assertEquals("B", response.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 20 with Header

use of okhttp3.internal.http2.Header in project okhttp by square.

the class CacheTest method varyMatchesUnchangedRequestHeaderField.

@Test
public void varyMatchesUnchangedRequestHeaderField() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Accept-Language").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    HttpUrl url = server.url("/");
    Request request = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
    Response response1 = client.newCall(request).execute();
    assertEquals("A", response1.body().string());
    Request request1 = new Request.Builder().url(url).header("Accept-Language", "fr-CA").build();
    Response response2 = client.newCall(request1).execute();
    assertEquals("A", response2.body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)88 MockResponse (okhttp3.mockwebserver.MockResponse)82 Request (okhttp3.Request)70 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)56 Response (okhttp3.Response)52 IOException (java.io.IOException)50 RequestBody (okhttp3.RequestBody)24 Call (okhttp3.Call)20 OkHttpClient (okhttp3.OkHttpClient)20 Callback (okhttp3.Callback)19 Interceptor (okhttp3.Interceptor)12 FormBody (okhttp3.FormBody)11 ResponseBody (okhttp3.ResponseBody)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 Headers (okhttp3.Headers)10 List (java.util.List)9 JSONObject (org.json.JSONObject)7 File (java.io.File)6 HashMap (java.util.HashMap)6