Search in sources :

Example 6 with Callback

use of okhttp3.Callback in project zipkin by openzipkin.

the class HttpCallTest method executionException_conversionException.

@Test
public void executionException_conversionException() throws Exception {
    mws.enqueue(new MockResponse());
    http.newCall(request, b -> {
        throw new IllegalArgumentException("eeek");
    }).submit(callback);
    try {
        callback.get();
        failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
    } catch (IllegalArgumentException expected) {
        assertThat(expected).isInstanceOf(IllegalArgumentException.class);
    }
}
Also used : Request(okhttp3.Request) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) OkHttpClient(okhttp3.OkHttpClient) Rule(org.junit.Rule) After(org.junit.After) MockWebServer(okhttp3.mockwebserver.MockWebServer) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) MockResponse(okhttp3.mockwebserver.MockResponse) CallbackCaptor(zipkin.internal.CallbackCaptor) Assertions.failBecauseExceptionWasNotThrown(org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 7 with Callback

use of okhttp3.Callback in project zipkin by openzipkin.

the class HttpBulkSpanIndexerTest method doesntWriteSpanId.

@Test
public void doesntWriteSpanId() throws Exception {
    es.enqueue(new MockResponse());
    indexer.add("test_zipkin_http-2016-10-01", TestObjects.LOTS_OF_SPANS[0], (Long) null);
    indexer.execute(callback);
    callback.get();
    RecordedRequest request = es.takeRequest();
    assertThat(request.getBody().readByteString().utf8()).doesNotContain("\"_id\"");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 8 with Callback

use of okhttp3.Callback in project okhttp by square.

the class InterceptorTest method asyncInterceptors.

private void asyncInterceptors(boolean network) throws Exception {
    server.enqueue(new MockResponse());
    addInterceptor(network, new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder().addHeader("OkHttp-Intercepted", "yep").build();
        }
    });
    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);
    callback.await(request.url()).assertCode(200).assertHeader("OkHttp-Intercepted", "yep");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException)

Example 9 with Callback

use of okhttp3.Callback in project okhttp by square.

the class RealWebSocket method connect.

public void connect(OkHttpClient client) {
    client = client.newBuilder().protocols(ONLY_HTTP1).build();
    final int pingIntervalMillis = client.pingIntervalMillis();
    final Request request = originalRequest.newBuilder().header("Upgrade", "websocket").header("Connection", "Upgrade").header("Sec-WebSocket-Key", key).header("Sec-WebSocket-Version", "13").build();
    call = Internal.instance.newWebSocketCall(client, request);
    call.enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) {
            try {
                checkResponse(response);
            } catch (ProtocolException e) {
                failWebSocket(e, response);
                closeQuietly(response);
                return;
            }
            // Promote the HTTP streams into web socket streams.
            StreamAllocation streamAllocation = Internal.instance.streamAllocation(call);
            // Prevent connection pooling!
            streamAllocation.noNewStreams();
            Streams streams = streamAllocation.connection().newWebSocketStreams(streamAllocation);
            // Process all web socket messages.
            try {
                listener.onOpen(RealWebSocket.this, response);
                String name = "OkHttp WebSocket " + request.url().redact();
                initReaderAndWriter(name, pingIntervalMillis, streams);
                streamAllocation.connection().socket().setSoTimeout(0);
                loopReader();
            } catch (Exception e) {
                failWebSocket(e, null);
            }
        }

        @Override
        public void onFailure(Call call, IOException e) {
            failWebSocket(e, null);
        }
    });
}
Also used : Response(okhttp3.Response) StreamAllocation(okhttp3.internal.connection.StreamAllocation) Call(okhttp3.Call) ProtocolException(java.net.ProtocolException) Callback(okhttp3.Callback) Request(okhttp3.Request) ByteString(okio.ByteString) IOException(java.io.IOException) IOException(java.io.IOException) ProtocolException(java.net.ProtocolException)

Example 10 with Callback

use of okhttp3.Callback in project okhttp by square.

the class AsynchronousGet method run.

public void run() throws Exception {
    Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build();
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try (ResponseBody responseBody = response.body()) {
                if (!response.isSuccessful())
                    throw new IOException("Unexpected code " + response);
                Headers responseHeaders = response.headers();
                for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }
                System.out.println(responseBody.string());
            }
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Headers(okhttp3.Headers) Request(okhttp3.Request) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

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