Search in sources :

Example 21 with Callback

use of okhttp3.Callback in project okhttp by square.

the class CallTest method post_Async.

@Test
public void post_Async() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));
    Request request = new Request.Builder().url(server.url("/")).post(RequestBody.create(MediaType.parse("text/plain"), "def")).build();
    client.newCall(request).enqueue(callback);
    callback.await(request.url()).assertCode(200).assertBody("abc");
    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals("def", recordedRequest.getBody().readUtf8());
    assertEquals("3", recordedRequest.getHeader("Content-Length"));
    assertEquals("text/plain; charset=utf-8", recordedRequest.getHeader("Content-Type"));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 22 with Callback

use of okhttp3.Callback in project okhttp by square.

the class CallTest method get_Async.

@Test
public void get_Async() throws Exception {
    server.enqueue(new MockResponse().setBody("abc").addHeader("Content-Type: text/plain"));
    Request request = new Request.Builder().url(server.url("/")).header("User-Agent", "AsyncApiTest").build();
    client.newCall(request).enqueue(callback);
    callback.await(request.url()).assertCode(200).assertHeader("Content-Type", "text/plain").assertBody("abc");
    assertEquals("AsyncApiTest", server.takeRequest().getHeader("User-Agent"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 23 with Callback

use of okhttp3.Callback in project okhttp by square.

the class CallTest method recoverFromTlsHandshakeFailure_Async.

@Test
public void recoverFromTlsHandshakeFailure_Async() throws Exception {
    server.useHttps(sslClient.socketFactory, false);
    server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.FAIL_HANDSHAKE));
    server.enqueue(new MockResponse().setBody("abc"));
    client = client.newBuilder().hostnameVerifier(new RecordingHostnameVerifier()).connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS)).sslSocketFactory(suppressTlsFallbackClientSocketFactory(), sslClient.trustManager).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);
    callback.await(request.url()).assertBody("abc");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 24 with Callback

use of okhttp3.Callback in project okhttp by square.

the class CallTest method connectionReuseWhenResponseBodyConsumed_Async.

@Test
public void connectionReuseWhenResponseBodyConsumed_Async() throws Exception {
    server.enqueue(new MockResponse().setBody("abc"));
    server.enqueue(new MockResponse().setBody("def"));
    Request request = new Request.Builder().url(server.url("/a")).build();
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            throw new AssertionError();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            InputStream bytes = response.body().byteStream();
            assertEquals('a', bytes.read());
            assertEquals('b', bytes.read());
            assertEquals('c', bytes.read());
            // This request will share a connection with 'A' cause it's all done.
            client.newCall(new Request.Builder().url(server.url("/b")).build()).enqueue(callback);
        }
    });
    callback.await(server.url("/b")).assertCode(200).assertBody("def");
    // New connection.
    assertEquals(0, server.takeRequest().getSequenceNumber());
    // Connection reuse!
    assertEquals(1, server.takeRequest().getSequenceNumber());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) InputStream(java.io.InputStream) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 25 with Callback

use of okhttp3.Callback in project okhttp by square.

the class CallTest method conditionalCacheMiss_Async.

@Test
public void conditionalCacheMiss_Async() throws Exception {
    server.enqueue(new MockResponse().setBody("A").addHeader("ETag: v1"));
    server.enqueue(new MockResponse().setBody("B"));
    client = client.newBuilder().cache(cache).build();
    Request request1 = new Request.Builder().url(server.url("/")).build();
    client.newCall(request1).enqueue(callback);
    callback.await(request1.url()).assertCode(200).assertBody("A");
    assertNull(server.takeRequest().getHeader("If-None-Match"));
    Request request2 = new Request.Builder().url(server.url("/")).build();
    client.newCall(request2).enqueue(callback);
    callback.await(request2.url()).assertCode(200).assertBody("B");
    assertEquals("v1", server.takeRequest().getHeader("If-None-Match"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Aggregations

ResponseBody (okhttp3.ResponseBody)178 DateTimeRfc1123 (com.microsoft.rest.DateTimeRfc1123)166 DateTime (org.joda.time.DateTime)166 ServiceCall (com.microsoft.rest.ServiceCall)140 IOException (java.io.IOException)68 Test (org.junit.Test)60 MockResponse (okhttp3.mockwebserver.MockResponse)58 List (java.util.List)54 PagedList (com.microsoft.azure.PagedList)52 ServiceResponseWithHeaders (com.microsoft.rest.ServiceResponseWithHeaders)52 Call (okhttp3.Call)49 Request (okhttp3.Request)49 Response (okhttp3.Response)48 Callback (okhttp3.Callback)41 RequestBody (okhttp3.RequestBody)28 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)27 CountDownLatch (java.util.concurrent.CountDownLatch)16 OkHttpClient (okhttp3.OkHttpClient)15 Call (retrofit2.Call)15 Callback (retrofit2.Callback)14