Search in sources :

Example 51 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method networkInterceptorReturnsNull.

@Test
public void networkInterceptorReturnsNull() throws Exception {
    server.enqueue(new MockResponse());
    Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            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) {
        assertEquals("interceptor " + interceptor + " returned null", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 52 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method applicationInterceptorsCanMakeMultipleRequestsToServer.

@Test
public void applicationInterceptorsCanMakeMultipleRequestsToServer() throws Exception {
    server.enqueue(new MockResponse().setBody("a"));
    server.enqueue(new MockResponse().setBody("b"));
    client = client.newBuilder().addInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response1 = chain.proceed(chain.request());
            response1.body().close();
            return chain.proceed(chain.request());
        }
    }).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    assertEquals(response.body().string(), "b");
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException) Test(org.junit.Test)

Example 53 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method interceptorThrowsRuntimeExceptionAsynchronous.

/**
   * When an interceptor throws an unexpected exception, asynchronous callers are left hanging. The
   * exception goes to the uncaught exception handler.
   */
private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) throws Exception {
    addInterceptor(network, new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            throw new RuntimeException("boom!");
        }
    });
    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);
    assertEquals("boom!", executor.takeException().getMessage());
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException)

Example 54 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method networkInterceptorsCannotCallProceedMultipleTimes.

@Test
public void networkInterceptorsCannotCallProceedMultipleTimes() throws Exception {
    server.enqueue(new MockResponse());
    server.enqueue(new MockResponse());
    Interceptor interceptor = new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            chain.proceed(chain.request());
            return chain.proceed(chain.request());
        }
    };
    client = client.newBuilder().addNetworkInterceptor(interceptor).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (IllegalStateException expected) {
        assertEquals("network interceptor " + interceptor + " must call proceed() exactly once", expected.getMessage());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Test(org.junit.Test)

Example 55 with Interceptor

use of okhttp3.Interceptor in project okhttp by square.

the class InterceptorTest method rewriteResponseFromServer.

private void rewriteResponseFromServer(boolean network) throws Exception {
    server.enqueue(new MockResponse().addHeader("Original-Header: foo").setBody("abc"));
    addInterceptor(network, new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder().body(uppercase(originalResponse.body())).addHeader("OkHttp-Intercepted", "yep").build();
        }
    });
    Request request = new Request.Builder().url(server.url("/")).build();
    Response response = client.newCall(request).execute();
    assertEquals("ABC", response.body().string());
    assertEquals("yep", response.header("OkHttp-Intercepted"));
    assertEquals("foo", response.header("Original-Header"));
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockResponse(okhttp3.mockwebserver.MockResponse) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)35 Interceptor (okhttp3.Interceptor)33 MockResponse (okhttp3.mockwebserver.MockResponse)29 Request (okhttp3.Request)26 Test (org.junit.Test)25 Response (okhttp3.Response)24 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)24 OkHttpClient (okhttp3.OkHttpClient)20 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)10 File (java.io.File)8 Provides (dagger.Provides)6 Singleton (javax.inject.Singleton)6 Cache (okhttp3.Cache)4 Retrofit (retrofit2.Retrofit)4 InterruptedIOException (java.io.InterruptedIOException)3 Call (okhttp3.Call)3 HttpUrl (okhttp3.HttpUrl)3 NonNull (android.support.annotation.NonNull)2 ANResponse (com.androidnetworking.common.ANResponse)2 ResponseProgressBody (com.androidnetworking.internal.ResponseProgressBody)2