Skip to content

Commit 36bd68a

Browse files
authored
New API, Chain.call() returns the in-flight call. (square#3557)
Interceptors could use this to detect if the call has been canceled, or cancel the call, or do other things.
1 parent 16f43b6 commit 36bd68a

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

okhttp-tests/src/test/java/okhttp3/InterceptorTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.SynchronousQueue;
2525
import java.util.concurrent.ThreadPoolExecutor;
2626
import java.util.concurrent.TimeUnit;
27+
import java.util.concurrent.atomic.AtomicReference;
2728
import okhttp3.mockwebserver.MockResponse;
2829
import okhttp3.mockwebserver.MockWebServer;
2930
import okhttp3.mockwebserver.RecordedRequest;
@@ -41,9 +42,11 @@
4142

4243
import static okhttp3.TestUtil.defaultClient;
4344
import static org.junit.Assert.assertEquals;
45+
import static org.junit.Assert.assertFalse;
4446
import static org.junit.Assert.assertNotNull;
4547
import static org.junit.Assert.assertNull;
4648
import static org.junit.Assert.assertSame;
49+
import static org.junit.Assert.assertTrue;
4750
import static org.junit.Assert.fail;
4851

4952
public final class InterceptorTest {
@@ -820,12 +823,46 @@ private void interceptorThrowsRuntimeExceptionAsynchronous(boolean network) thro
820823
Call call = client.newCall(request1);
821824

822825
try {
823-
Response response = call.execute(); // we want this call to throw a SocketTimeoutException
826+
call.execute(); // we want this call to throw a SocketTimeoutException
824827
fail();
825828
} catch (SocketTimeoutException expected) {
826829
}
827830
}
828831

832+
@Test public void chainCanCancelCall() throws Exception {
833+
final AtomicReference<Call> callRef = new AtomicReference<>();
834+
835+
Interceptor interceptor = new Interceptor() {
836+
@Override public Response intercept(Chain chain) throws IOException {
837+
Call call = chain.call();
838+
callRef.set(call);
839+
840+
assertFalse(call.isCanceled());
841+
call.cancel();
842+
assertTrue(call.isCanceled());
843+
844+
return chain.proceed(chain.request());
845+
}
846+
};
847+
848+
client = client.newBuilder()
849+
.addInterceptor(interceptor)
850+
.build();
851+
852+
Request request = new Request.Builder()
853+
.url(server.url("/"))
854+
.build();
855+
Call call = client.newCall(request);
856+
857+
try {
858+
call.execute();
859+
fail();
860+
} catch (IOException expected) {
861+
}
862+
863+
assertSame(call, callRef.get());
864+
}
865+
829866
private RequestBody uppercase(final RequestBody original) {
830867
return new RequestBody() {
831868
@Override public MediaType contentType() {

okhttp/src/main/java/okhttp3/Interceptor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ interface Chain {
3838
*/
3939
@Nullable Connection connection();
4040

41+
Call call();
42+
4143
int connectTimeoutMillis();
4244

4345
Chain withConnectTimeout(int timeout, TimeUnit unit);

okhttp/src/main/java/okhttp3/internal/http/RealInterceptorChain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public HttpCodec httpStream() {
105105
return httpCodec;
106106
}
107107

108-
public Call call() {
108+
@Override public Call call() {
109109
return call;
110110
}
111111

0 commit comments

Comments
 (0)