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