mirror of
https://github.com/square/okhttp.git
synced 2026-01-17 08:42:25 +03:00
Merge pull request #2403 from square/dr-Mar132016-interceptor-null-connection
Ensure network interceptors always have access to the underlying connection.
This commit is contained in:
@@ -26,6 +26,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import okhttp3.mockwebserver.MockResponse;
|
||||
import okhttp3.mockwebserver.MockWebServer;
|
||||
import okhttp3.mockwebserver.RecordedRequest;
|
||||
import okhttp3.mockwebserver.SocketPolicy;
|
||||
import okio.Buffer;
|
||||
import okio.BufferedSink;
|
||||
import okio.ForwardingSink;
|
||||
@@ -630,6 +631,31 @@ public final class InterceptorTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test public void networkInterceptorReturnsConnectionOnEmptyBody() throws Exception {
|
||||
server.enqueue(new MockResponse()
|
||||
.setSocketPolicy(SocketPolicy.DISCONNECT_AT_END)
|
||||
.addHeader("Connection", "Close"));
|
||||
|
||||
Interceptor interceptor = new Interceptor() {
|
||||
@Override public Response intercept(Chain chain) throws IOException {
|
||||
Response response = chain.proceed(chain.request());
|
||||
assertNotNull(chain.connection());
|
||||
return response;
|
||||
}
|
||||
};
|
||||
|
||||
client = client.newBuilder()
|
||||
.addNetworkInterceptor(interceptor)
|
||||
.build();
|
||||
|
||||
Request request = new Request.Builder()
|
||||
.url(server.url("/"))
|
||||
.build();
|
||||
|
||||
Response response = client.newCall(request).execute();
|
||||
response.body().close();
|
||||
}
|
||||
|
||||
private RequestBody uppercase(final RequestBody original) {
|
||||
return new RequestBody() {
|
||||
@Override public MediaType contentType() {
|
||||
|
||||
@@ -560,7 +560,8 @@ public final class HttpEngine {
|
||||
httpStream.writeRequestHeaders(networkRequest);
|
||||
networkResponse = readNetworkResponse();
|
||||
} else if (!callerWritesRequestBody) {
|
||||
networkResponse = new NetworkInterceptorChain(0, networkRequest).proceed(networkRequest);
|
||||
networkResponse = new NetworkInterceptorChain(0, networkRequest,
|
||||
streamAllocation.connection()).proceed(networkRequest);
|
||||
} else {
|
||||
// Emit the request body's buffer so that everything is in requestBodyOut.
|
||||
if (bufferedRequestBody != null && bufferedRequestBody.buffer().size() > 0) {
|
||||
@@ -638,15 +639,17 @@ public final class HttpEngine {
|
||||
class NetworkInterceptorChain implements Interceptor.Chain {
|
||||
private final int index;
|
||||
private final Request request;
|
||||
private final Connection connection;
|
||||
private int calls;
|
||||
|
||||
NetworkInterceptorChain(int index, Request request) {
|
||||
NetworkInterceptorChain(int index, Request request, Connection connection) {
|
||||
this.index = index;
|
||||
this.request = request;
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
@Override public Connection connection() {
|
||||
return streamAllocation.connection();
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override public Request request() {
|
||||
@@ -676,7 +679,7 @@ public final class HttpEngine {
|
||||
|
||||
if (index < client.networkInterceptors().size()) {
|
||||
// There's another interceptor in the chain. Call that.
|
||||
NetworkInterceptorChain chain = new NetworkInterceptorChain(index + 1, request);
|
||||
NetworkInterceptorChain chain = new NetworkInterceptorChain(index + 1, request, connection);
|
||||
Interceptor interceptor = client.networkInterceptors().get(index);
|
||||
Response interceptedResponse = interceptor.intercept(chain);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user