Search in sources :

Example 86 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CallTest method uploadBodySmallChunkedEncoding.

@Test
public void uploadBodySmallChunkedEncoding() throws Exception {
    upload(true, 1048576, 256);
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals(1048576, recordedRequest.getBodySize());
    assertFalse(recordedRequest.getChunkSizes().isEmpty());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 87 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CallTest method canceledBeforeIOSignalsOnFailure.

/**
   * This test puts a request in front of one that is to be canceled, so that it is canceled before
   * I/O takes place.
   */
@Test
public void canceledBeforeIOSignalsOnFailure() throws Exception {
    // Force requests to be executed serially.
    okhttp3.Dispatcher dispatcher = new okhttp3.Dispatcher(client.dispatcher().executorService());
    dispatcher.setMaxRequests(1);
    client = client.newBuilder().dispatcher(dispatcher).build();
    Request requestA = new Request.Builder().url(server.url("/a")).build();
    Request requestB = new Request.Builder().url(server.url("/b")).build();
    final Call callA = client.newCall(requestA);
    final Call callB = client.newCall(requestB);
    server.setDispatcher(new Dispatcher() {

        char nextResponse = 'A';

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            callB.cancel();
            return new MockResponse().setBody(Character.toString(nextResponse++));
        }
    });
    callA.enqueue(callback);
    callB.enqueue(callback);
    assertEquals("/a", server.takeRequest().getPath());
    callback.await(requestA.url()).assertBody("A");
    // At this point we know the callback is ready, and that it will receive a cancel failure.
    callback.await(requestB.url()).assertFailure("Canceled", "Socket closed");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Dispatcher(okhttp3.mockwebserver.Dispatcher) Test(org.junit.Test)

Example 88 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CacheTest method redirectToCachedResult.

@Test
public void redirectToCachedResult() throws Exception {
    server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60").setBody("ABC"));
    server.enqueue(new MockResponse().setResponseCode(HttpURLConnection.HTTP_MOVED_PERM).addHeader("Location: /foo"));
    server.enqueue(new MockResponse().setBody("DEF"));
    Request request1 = new Request.Builder().url(server.url("/foo")).build();
    Response response1 = client.newCall(request1).execute();
    assertEquals("ABC", response1.body().string());
    RecordedRequest recordedRequest1 = server.takeRequest();
    assertEquals("GET /foo HTTP/1.1", recordedRequest1.getRequestLine());
    assertEquals(0, recordedRequest1.getSequenceNumber());
    Request request2 = new Request.Builder().url(server.url("/bar")).build();
    Response response2 = client.newCall(request2).execute();
    assertEquals("ABC", response2.body().string());
    RecordedRequest recordedRequest2 = server.takeRequest();
    assertEquals("GET /bar HTTP/1.1", recordedRequest2.getRequestLine());
    assertEquals(1, recordedRequest2.getSequenceNumber());
    // an unrelated request should reuse the pooled connection
    Request request3 = new Request.Builder().url(server.url("/baz")).build();
    Response response3 = client.newCall(request3).execute();
    assertEquals("DEF", response3.body().string());
    RecordedRequest recordedRequest3 = server.takeRequest();
    assertEquals("GET /baz HTTP/1.1", recordedRequest3.getRequestLine());
    assertEquals(2, recordedRequest3.getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 89 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest in project okhttp by square.

the class CacheTest method pragmaNoCacheAndExpirationDateInTheFuture.

@Test
public void pragmaNoCacheAndExpirationDateInTheFuture() throws Exception {
    String lastModifiedDate = formatDate(-2, TimeUnit.HOURS);
    RecordedRequest conditionalRequest = assertConditionallyCached(new MockResponse().addHeader("Last-Modified: " + lastModifiedDate).addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).addHeader("Pragma: no-cache"));
    assertEquals(lastModifiedDate, conditionalRequest.getHeader("If-Modified-Since"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 90 with RecordedRequest

use of okhttp3.mockwebserver.RecordedRequest 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)

Aggregations

RecordedRequest (okhttp3.mockwebserver.RecordedRequest)206 MockResponse (okhttp3.mockwebserver.MockResponse)188 Test (org.junit.Test)168 HttpURLConnection (java.net.HttpURLConnection)18 MockWebServer (okhttp3.mockwebserver.MockWebServer)14 IOException (java.io.IOException)13 URL (java.net.URL)13 OutputStream (java.io.OutputStream)12 Response (okhttp3.Response)12 OkHttpURLConnection (okhttp3.internal.huc.OkHttpURLConnection)12 AbstractTest (org.openstack4j.api.AbstractTest)12 Test (org.testng.annotations.Test)12 Call (okhttp3.Call)11 Buffer (okio.Buffer)11 Request (okhttp3.Request)8 RecordingOkAuthenticator (okhttp3.internal.RecordingOkAuthenticator)7 Dispatcher (okhttp3.mockwebserver.Dispatcher)6 ByteString (okio.ByteString)6 CookieManager (java.net.CookieManager)5 URLConnection (java.net.URLConnection)5