From 36bd68aa3e93affb12504cd40454e64c6812019c Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Wed, 30 Aug 2017 14:05:13 -0400 Subject: [PATCH] New API, Chain.call() returns the in-flight call. (#3557) Interceptors could use this to detect if the call has been canceled, or cancel the call, or do other things. --- .../test/java/okhttp3/InterceptorTest.java | 39 ++++++++++++++++++- okhttp/src/main/java/okhttp3/Interceptor.java | 2 + .../internal/http/RealInterceptorChain.java | 2 +- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/okhttp-tests/src/test/java/okhttp3/InterceptorTest.java b/okhttp-tests/src/test/java/okhttp3/InterceptorTest.java index 5d77f1c5f223..e01a360b9b54 100644 --- a/okhttp-tests/src/test/java/okhttp3/InterceptorTest.java +++ b/okhttp-tests/src/test/java/okhttp3/InterceptorTest.java @@ -24,6 +24,7 @@ import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; import okhttp3.mockwebserver.RecordedRequest; @@ -41,9 +42,11 @@ import static okhttp3.TestUtil.defaultClient; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; public final class InterceptorTest { @@ -820,12 +823,46 @@ private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) thro Call call = client.newCall(request1); try { - Response response = call.execute(); // we want this call to throw a SocketTimeoutException + call.execute(); // we want this call to throw a SocketTimeoutException fail(); } catch (SocketTimeoutException expected) { } } + @Test public void chainCanCancelCall() throws Exception { + final AtomicReference callRef = new AtomicReference<>(); + + Interceptor interceptor = new Interceptor() { + @Override public Response intercept(Chain chain) throws IOException { + Call call = chain.call(); + callRef.set(call); + + assertFalse(call.isCanceled()); + call.cancel(); + assertTrue(call.isCanceled()); + + return chain.proceed(chain.request()); + } + }; + + client = client.newBuilder() + .addInterceptor(interceptor) + .build(); + + Request request = new Request.Builder() + .url(server.url("/")) + .build(); + Call call = client.newCall(request); + + try { + call.execute(); + fail(); + } catch (IOException expected) { + } + + assertSame(call, callRef.get()); + } + private RequestBody uppercase(final RequestBody original) { return new RequestBody() { @Override public MediaType contentType() { diff --git a/okhttp/src/main/java/okhttp3/Interceptor.java b/okhttp/src/main/java/okhttp3/Interceptor.java index 3272e4188227..100559269734 100644 --- a/okhttp/src/main/java/okhttp3/Interceptor.java +++ b/okhttp/src/main/java/okhttp3/Interceptor.java @@ -38,6 +38,8 @@ interface Chain { */ @Nullable Connection connection(); + Call call(); + int connectTimeoutMillis(); Chain withConnectTimeout(int timeout, TimeUnit unit); diff --git a/okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.java b/okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.java index 373c08b2123a..52f9880892c9 100644 --- a/okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.java +++ b/okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.java @@ -105,7 +105,7 @@ public HttpCodec httpStream() { return httpCodec; } - public Call call() { + @Override public Call call() { return call; }