Example usage for com.squareup.okhttp Interceptor Interceptor

List of usage examples for com.squareup.okhttp Interceptor Interceptor

Introduction

In this page you can find the example usage for com.squareup.okhttp Interceptor Interceptor.

Prototype

Interceptor

Source Link

Usage

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

private void multipleInterceptors(List<Interceptor> interceptors) throws Exception {
    server.enqueue(new MockResponse());

    interceptors.add(new Interceptor() {
        @Override/*from w w  w  . j  ava2s.c  o m*/
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Response originalResponse = chain
                    .proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", "Android") // 1. Added first.
                            .build());
            return originalResponse.newBuilder().addHeader("Response-Interceptor", "Donut") // 4. Added last.
                    .build();
        }
    });
    interceptors.add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            Response originalResponse = chain
                    .proceed(originalRequest.newBuilder().addHeader("Request-Interceptor", "Bob") // 2. Added second.
                            .build());
            return originalResponse.newBuilder().addHeader("Response-Interceptor", "Cupcake") // 3. Added third.
                    .build();
        }
    });

    Request request = new Request.Builder().url(server.url("/")).build();

    Response response = client.newCall(request).execute();
    assertEquals(Arrays.asList("Cupcake", "Donut"), response.headers("Response-Interceptor"));

    RecordedRequest recordedRequest = server.takeRequest();
    assertEquals(Arrays.asList("Android", "Bob"), recordedRequest.getHeaders().values("Request-Interceptor"));
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

private void asyncInterceptors(List<Interceptor> interceptors) throws Exception {
    server.enqueue(new MockResponse());

    interceptors.add(new Interceptor() {
        @Override//from  w  ww .java2s  . c  o m
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder().addHeader("OkHttp-Intercepted", "yep").build();
        }
    });

    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);

    callback.await(request.httpUrl()).assertCode(200).assertHeader("OkHttp-Intercepted", "yep");
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

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

    client.interceptors().add(new Interceptor() {
        @Override//from  w  w  w . java2s.c o m
        public Response intercept(Chain chain) throws IOException {
            chain.proceed(chain.request());
            return chain.proceed(chain.request());
        }
    });

    Request request = new Request.Builder().url(server.url("/")).build();

    Response response = client.newCall(request).execute();
    assertEquals(response.body().string(), "b");
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

/** Make sure interceptors can interact with the OkHttp client. */
@Test//from www .j a v a 2s.  c  o m
public void interceptorMakesAnUnrelatedRequest() throws Exception {
    server.enqueue(new MockResponse().setBody("a")); // Fetched by interceptor.
    server.enqueue(new MockResponse().setBody("b")); // Fetched directly.

    client.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            if (chain.request().url().getPath().equals("/b")) {
                Request requestA = new Request.Builder().url(server.url("/a")).build();
                Response responseA = client.newCall(requestA).execute();
                assertEquals("a", responseA.body().string());
            }

            return chain.proceed(chain.request());
        }
    });

    Request requestB = new Request.Builder().url(server.url("/b")).build();
    Response responseB = client.newCall(requestB).execute();
    assertEquals("b", responseB.body().string());
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

/** Make sure interceptors can interact with the OkHttp client asynchronously. */
@Test//w w  w  . j a  v a  2 s  . c  o  m
public void interceptorMakesAnUnrelatedAsyncRequest() throws Exception {
    server.enqueue(new MockResponse().setBody("a")); // Fetched by interceptor.
    server.enqueue(new MockResponse().setBody("b")); // Fetched directly.

    client.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            if (chain.request().url().getPath().equals("/b")) {
                Request requestA = new Request.Builder().url(server.url("/a")).build();

                try {
                    RecordingCallback callbackA = new RecordingCallback();
                    client.newCall(requestA).enqueue(callbackA);
                    callbackA.await(requestA.httpUrl()).assertBody("a");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            return chain.proceed(chain.request());
        }
    });

    Request requestB = new Request.Builder().url(server.url("/b")).build();
    RecordingCallback callbackB = new RecordingCallback();
    client.newCall(requestB).enqueue(callbackB);
    callbackB.await(requestB.httpUrl()).assertBody("b");
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

/**
 * When an interceptor throws an unexpected exception, synchronous callers can catch it and deal
 * with it./*  w w  w  . j av  a2s  . com*/
 *
 * TODO(jwilson): test that resources are not leaked when this happens.
 */
private void interceptorThrowsRuntimeExceptionSynchronous(List<Interceptor> interceptors) throws Exception {
    interceptors.add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            throw new RuntimeException("boom!");
        }
    });

    Request request = new Request.Builder().url(server.url("/")).build();

    try {
        client.newCall(request).execute();
        fail();
    } catch (RuntimeException expected) {
        assertEquals("boom!", expected.getMessage());
    }
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Test
public void networkInterceptorModifiedRequestIsReturned() throws IOException {
    server.enqueue(new MockResponse());

    Interceptor modifyHeaderInterceptor = new Interceptor() {
        @Override/*ww  w. jav  a2 s .com*/
        public Response intercept(Chain chain) throws IOException {
            return chain
                    .proceed(chain.request().newBuilder().header("User-Agent", "intercepted request").build());
        }
    };

    client.networkInterceptors().add(modifyHeaderInterceptor);

    Request request = new Request.Builder().url(server.url("/")).header("User-Agent", "user request").build();

    Response response = client.newCall(request).execute();
    assertNotNull(response.request().header("User-Agent"));
    assertEquals("user request", response.request().header("User-Agent"));
    assertEquals("intercepted request", response.networkResponse().request().header("User-Agent"));
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

/**
 * When an interceptor throws an unexpected exception, asynchronous callers are left hanging. The
 * exception goes to the uncaught exception handler.
 *
 * TODO(jwilson): test that resources are not leaked when this happens.
 *///from ww w. j  av a2  s . c om
private void interceptorThrowsRuntimeExceptionAsynchronous(List<Interceptor> interceptors) throws Exception {
    interceptors.add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            throw new RuntimeException("boom!");
        }
    });

    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client.setDispatcher(new Dispatcher(executor));

    Request request = new Request.Builder().url(server.url("/")).build();
    client.newCall(request).enqueue(callback);

    assertEquals("boom!", executor.takeException().getMessage());
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Ignore
@Test/* w  w  w.j a  v a2 s  .c o m*/
public void applicationInterceptorReturnsNull() 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.interceptors().add(interceptor);

    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client.setDispatcher(new Dispatcher(executor));

    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (NullPointerException expected) {
        assertEquals("application interceptor " + interceptor + " returned null", expected.getMessage());
    }
}

From source file:co.paralleluniverse.fibers.okhttp.test.InterceptorTest.java

License:Apache License

@Ignore
@Test/*w  ww  .  j a  v a  2  s .c  om*/
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.networkInterceptors().add(interceptor);

    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client.setDispatcher(new Dispatcher(executor));

    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (NullPointerException expected) {
        assertEquals("network interceptor " + interceptor + " returned null", expected.getMessage());
    }
}