Search in sources :

Example 31 with Callback

use of okhttp3.Callback in project okhttp by square.

the class CallTest method asyncCallEngineInitialized.

/** https://github.com/square/okhttp/issues/1801 */
@Test
public void asyncCallEngineInitialized() throws Exception {
    OkHttpClient c = defaultClient().newBuilder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            throw new IOException();
        }
    }).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    c.newCall(request).enqueue(callback);
    RecordedResponse response = callback.await(request.url());
    assertEquals(request, response.request);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) Test(org.junit.Test)

Example 32 with Callback

use of okhttp3.Callback in project glide by bumptech.

the class VolleyStreamFetcherServerTest method testCallsLoadFailedIfStatusCodeIs500.

@Test
public void testCallsLoadFailedIfStatusCodeIs500() throws Exception {
    mockWebServer.enqueue(new MockResponse().setResponseCode(500).setBody("error"));
    getFetcher().loadData(Priority.NORMAL, callback);
    waitForResponseLatch.await();
    verify(callback).onLoadFailed(isA(VolleyError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 33 with Callback

use of okhttp3.Callback in project glide by bumptech.

the class VolleyStreamFetcherServerTest method testHandlesRedirect302s.

@Test
public void testHandlesRedirect302s() throws Exception {
    String expected = "fakedata";
    mockWebServer.enqueue(new MockResponse().setResponseCode(302).setHeader("Location", mockWebServer.url("/redirect").toString()));
    mockWebServer.enqueue(new MockResponse().setResponseCode(200).setBody(expected));
    getFetcher().loadData(Priority.LOW, callback);
    waitForResponseLatch.await();
    verify(callback).onDataReady(streamCaptor.capture());
    assertStreamOf(expected, streamCaptor.getValue());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 34 with Callback

use of okhttp3.Callback in project glide by bumptech.

the class VolleyStreamFetcherServerTest method testCallsLoadFailedIfStatusCodeIs400.

@Test
public void testCallsLoadFailedIfStatusCodeIs400() throws Exception {
    mockWebServer.enqueue(new MockResponse().setResponseCode(400).setBody("error"));
    getFetcher().loadData(Priority.LOW, callback);
    waitForResponseLatch.await();
    verify(callback).onLoadFailed(isA(VolleyError.class));
}
Also used : VolleyError(com.android.volley.VolleyError) MockResponse(okhttp3.mockwebserver.MockResponse) Test(org.junit.Test)

Example 35 with Callback

use of okhttp3.Callback in project glide by bumptech.

the class OkHttpStreamFetcher method loadData.

@Override
public void loadData(Priority priority, final DataCallback<? super InputStream> callback) {
    Request.Builder requestBuilder = new Request.Builder().url(url.toStringUrl());
    for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) {
        String key = headerEntry.getKey();
        requestBuilder.addHeader(key, headerEntry.getValue());
    }
    Request request = requestBuilder.build();
    call = client.newCall(request);
    call.enqueue(new okhttp3.Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "OkHttp failed to obtain result", e);
            }
            callback.onLoadFailed(e);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            responseBody = response.body();
            if (response.isSuccessful()) {
                long contentLength = responseBody.contentLength();
                stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength);
            } else if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "OkHttp got error response: " + response.code() + ", " + response.message());
            }
            callback.onDataReady(stream);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Request(okhttp3.Request) IOException(java.io.IOException) Map(java.util.Map)

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