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);
}
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));
}
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());
}
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));
}
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);
}
});
}
Aggregations