Search in sources :

Example 41 with Header

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

the class CallTest method expect100ContinueTimesOutWithoutContinue.

@Test
public void expect100ContinueTimesOutWithoutContinue() throws Exception {
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.NO_RESPONSE));
    client = client.newBuilder().readTimeout(500, TimeUnit.MILLISECONDS).build();
    Request request = new Request.Builder().url(server.url("/")).header("Expect", "100-continue").post(RequestBody.create(MediaType.parse("text/plain"), "abc")).build();
    Call call = client.newCall(request);
    try {
        call.execute();
        fail();
    } catch (SocketTimeoutException expected) {
    }
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("", recordedRequest.getBody().readUtf8());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) SocketTimeoutException(java.net.SocketTimeoutException) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 42 with Header

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

the class CacheTest method varyMatchesRemovedRequestHeaderField.

@Test
public void varyMatchesRemovedRequestHeaderField() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").addHeader("Vary: Foo").setBody("A"));
    server.enqueue(new MockResponse().setBody("B"));
    Request request = new Request.Builder().url(server.url("/")).header("Foo", "bar").build();
    Response fooresponse = client.newCall(request).execute();
    assertEquals("A", fooresponse.body().string());
    assertEquals("B", get(server.url("/")).body().string());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 43 with Header

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

the class CacheTest method conditionalHitUpdatesCache.

@Test
public void conditionalHitUpdatesCache() throws Exception {
    server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(0, TimeUnit.SECONDS)).addHeader("Cache-Control: max-age=0").setBody("A"));
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=30").addHeader("Allow: GET, HEAD").setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED));
    server.enqueue(new MockResponse().setBody("B"));
    // A cache miss writes the cache.
    long t0 = System.currentTimeMillis();
    Response response1 = get(server.url("/a"));
    assertEquals("A", response1.body().string());
    assertEquals(null, response1.header("Allow"));
    assertEquals(0, response1.receivedResponseAtMillis() - t0, 250.0);
    // A conditional cache hit updates the cache.
    // Make sure t0 and t1 are distinct.
    Thread.sleep(500);
    long t1 = System.currentTimeMillis();
    Response response2 = get(server.url("/a"));
    assertEquals(HttpURLConnection.HTTP_OK, response2.code());
    assertEquals("A", response2.body().string());
    assertEquals("GET, HEAD", response2.header("Allow"));
    assertEquals(0, response2.receivedResponseAtMillis() - t1, 250.0);
    // A full cache hit reads the cache.
    // Make sure t1 and t2 are distinct.
    Thread.sleep(500);
    long t2 = System.currentTimeMillis();
    Response response3 = get(server.url("/a"));
    assertEquals("A", response3.body().string());
    assertEquals("GET, HEAD", response3.header("Allow"));
    assertEquals(0, response3.receivedResponseAtMillis() - t1, 250.0);
    assertEquals(2, server.getRequestCount());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 44 with Header

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

the class CacheTest method requestMaxAge.

@Test
public void requestMaxAge() throws IOException {
    server.enqueue(new MockResponse().setBody("A").addHeader("Last-Modified: " + formatDate(-2, TimeUnit.HOURS)).addHeader("Date: " + formatDate(-1, TimeUnit.MINUTES)).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)));
    server.enqueue(new MockResponse().setBody("B"));
    assertEquals("A", get(server.url("/")).body().string());
    Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "max-age=30").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 45 with Header

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

the class CacheTest method requestOnlyIfCachedWithUnhelpfulResponseCached.

@Test
public void requestOnlyIfCachedWithUnhelpfulResponseCached() throws IOException {
    server.enqueue(new MockResponse().setBody("A"));
    assertEquals("A", get(server.url("/")).body().string());
    Request request = new Request.Builder().url(server.url("/")).header("Cache-Control", "only-if-cached").build();
    Response response = client.newCall(request).execute();
    assertTrue(response.body().source().exhausted());
    assertEquals(504, response.code());
    assertEquals(2, cache.requestCount());
    assertEquals(1, cache.networkCount());
    assertEquals(0, cache.hitCount());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)89 MockResponse (okhttp3.mockwebserver.MockResponse)82 Request (okhttp3.Request)75 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)56 Response (okhttp3.Response)54 IOException (java.io.IOException)53 RequestBody (okhttp3.RequestBody)24 OkHttpClient (okhttp3.OkHttpClient)22 Call (okhttp3.Call)20 Callback (okhttp3.Callback)19 Interceptor (okhttp3.Interceptor)14 ResponseBody (okhttp3.ResponseBody)12 FormBody (okhttp3.FormBody)11 Headers (okhttp3.Headers)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 List (java.util.List)9 JSONObject (org.json.JSONObject)8 File (java.io.File)6 HashMap (java.util.HashMap)6