From 0e199f608909793892b7883614c0d20a94c27916 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Thu, 25 Dec 2014 21:05:33 -0500 Subject: [PATCH] Don't NPE if disconnecting before connecting. Closes https://github.com/square/okhttp/issues/1191 --- .../internal/http/HttpOverSpdyTest.java | 21 +++++++++++++++++++ .../okhttp/internal/http/SpdyTransport.java | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java b/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java index 2c275e02822c..670361c60380 100644 --- a/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java +++ b/okhttp-tests/src/test/java/com/squareup/okhttp/internal/http/HttpOverSpdyTest.java @@ -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; @@ -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()); + } + void assertContains(Collection collection, T value) { assertTrue(collection.toString(), collection.contains(value)); } diff --git a/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java b/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java index 0561ac38ea4d..61b6610810f8 100644 --- a/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java +++ b/okhttp/src/main/java/com/squareup/okhttp/internal/http/SpdyTransport.java @@ -212,7 +212,7 @@ public static Response.Builder readNameValueBlock(List
headerBlock, } @Override public void disconnect(HttpEngine engine) throws IOException { - stream.close(ErrorCode.CANCEL); + if (stream != null) stream.close(ErrorCode.CANCEL); } @Override public boolean canReuseConnection() {