Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't NPE if disconnecting before connecting. #1235

Merged
merged 1 commit into from
Dec 26, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.squareup.okhttp.internal.http;

import com.squareup.okhttp.Cache;
import com.squareup.okhttp.ConnectionPool;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.OkUrlFactory;
import com.squareup.okhttp.Protocol;
Expand Down Expand Up @@ -426,6 +427,26 @@ public boolean verify(String hostname, SSLSession session) {
assertContains(requestB.getHeaders(), "cookie: c=oreo");
}

/** https://github.com/square/okhttp/issues/1191 */
@Test public void disconnectWithStreamNotEstablished() throws Exception {
ConnectionPool connectionPool = new ConnectionPool(5, 5000);
client.client().setConnectionPool(connectionPool);

server.enqueue(new MockResponse().setBody("abc"));
server.play();

// Disconnect before the stream is created. A connection is still established!
HttpURLConnection connection1 = client.open(server.getUrl("/"));
connection1.connect();
connection1.disconnect();

// That connection is pooled, and it works.
assertEquals(1, connectionPool.getSpdyConnectionCount());
HttpURLConnection connection2 = client.open(server.getUrl("/"));
assertContent("abc", connection2, 3);
assertEquals(0, server.takeRequest().getSequenceNumber());
}

<T> void assertContains(Collection<T> collection, T value) {
assertTrue(collection.toString(), collection.contains(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static Response.Builder readNameValueBlock(List<Header> headerBlock,
}

@Override public void disconnect(HttpEngine engine) throws IOException {
stream.close(ErrorCode.CANCEL);
if (stream != null) stream.close(ErrorCode.CANCEL);
}

@Override public boolean canReuseConnection() {
Expand Down