Search in sources :

Example 46 with Dispatcher

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"));
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Okio(okio.Okio) BeforeEach(org.junit.jupiter.api.BeforeEach) Socket(java.net.Socket) Source(okio.Source) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) Sink(okio.Sink) MockWebServer(mockwebserver3.MockWebServer) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) SocketPolicy(mockwebserver3.SocketPolicy) BufferedSink(okio.BufferedSink) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Locale(java.util.Locale) SocketTimeoutException(java.net.SocketTimeoutException) Duration(java.time.Duration) ForwardingSource(okio.ForwardingSource) Tag(org.junit.jupiter.api.Tag) Buffer(okio.Buffer) SynchronousQueue(java.util.concurrent.SynchronousQueue) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Unit(kotlin.Unit) GzipSink(okio.GzipSink) ForwardingSink(okio.ForwardingSink) MockResponse(mockwebserver3.MockResponse) TestUtil.assertSuppressed(okhttp3.TestUtil.assertSuppressed) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 47 with Dispatcher

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);
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest)

Example 48 with Dispatcher

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();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) JsonMergePatchInterceptor(org.edx.mobile.http.interceptor.JsonMergePatchInterceptor) OnlyIfCachedStrippingInterceptor(org.edx.mobile.http.interceptor.OnlyIfCachedStrippingInterceptor) MockWebServer(okhttp3.mockwebserver.MockWebServer) Dispatcher(okhttp3.Dispatcher) RoboExecutorService(org.robolectric.util.concurrent.RoboExecutorService)

Aggregations

Dispatcher (okhttp3.Dispatcher)43 OkHttpClient (okhttp3.OkHttpClient)28 Before (org.junit.Before)14 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)10 IOException (java.io.IOException)9 Interceptor (okhttp3.Interceptor)7 Test (org.junit.Test)7 Call (okhttp3.Call)6 RecordedRequest (mockwebserver3.RecordedRequest)5 ApolloHttpCache (com.apollographql.apollo.cache.http.ApolloHttpCache)3 CustomTypeValue (com.apollographql.apollo.response.CustomTypeValue)3 ParseException (java.text.ParseException)3 MockResponse (mockwebserver3.MockResponse)3 ConnectionPool (okhttp3.ConnectionPool)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 Buffer (okio.Buffer)3 NonNull (android.support.annotation.NonNull)2 IdFieldCacheKeyResolver (com.apollographql.apollo.IdFieldCacheKeyResolver)2 Response (com.apollographql.apollo.api.Response)2