use of mockwebserver3.Dispatcher in project okhttp by square.
the class InterceptorTest method networkInterceptorReturnsNull.
@Test
public void networkInterceptorReturnsNull() throws Exception {
server.enqueue(new MockResponse());
Interceptor interceptor = chain -> {
chain.proceed(chain.request());
return null;
};
client = client.newBuilder().addNetworkInterceptor(interceptor).build();
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
Request request = new Request.Builder().url(server.url("/")).build();
try {
client.newCall(request).execute();
fail();
} catch (NullPointerException expected) {
assertThat(expected.getMessage()).isEqualTo(("interceptor " + interceptor + " returned null"));
}
}
use of mockwebserver3.Dispatcher in project okhttp by square.
the class InterceptorTest method interceptorThrowsRuntimeExceptionAsynchronous.
/**
* When an interceptor throws an unexpected exception, asynchronous calls are canceled. The
* exception goes to the uncaught exception handler.
*/
private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) throws Exception {
RuntimeException boom = new RuntimeException("boom!");
addInterceptor(network, chain -> {
throw boom;
});
ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
Request request = new Request.Builder().url(server.url("/")).build();
Call call = client.newCall(request);
call.enqueue(callback);
RecordedResponse recordedResponse = callback.await(server.url("/"));
assertThat(recordedResponse.failure).hasMessage("canceled due to java.lang.RuntimeException: boom!");
assertSuppressed(recordedResponse.failure, throwables -> {
assertThat(throwables).contains(boom);
return Unit.INSTANCE;
});
assertThat(call.isCanceled()).isTrue();
assertThat(executor.takeException()).isEqualTo(boom);
}
use of mockwebserver3.Dispatcher in project edx-app-android by edx.
the class HttpBaseTestCase method setUp.
@Override
public void setUp() throws Exception {
server = new MockWebServer();
server.setDispatcher(new MockResponseDispatcher());
server.start();
okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(new RoboExecutorService())).addInterceptor(new JsonMergePatchInterceptor()).addInterceptor(new OnlyIfCachedStrippingInterceptor()).build();
super.setUp();
}
Aggregations