Don't throw needlessly when returning the CacheResponse.
We were throwing under the premise that we'd always have
response headers before we had a cached body. But when the
user calls connect() we'll have queried the cache but we
won't necessarily have response headers from the server yet.
That's because connect() doesn't require a response to be ready.
Bug: http://b/5920659
Cherry-picked from: I9d69b44b5be3d0e29f14d8b83781f6e65c67582a
Change-Id: Ia403b42ddc9783fd1456164f4b7350da3ce80566
diff --git a/luni/src/main/java/libcore/net/http/HttpEngine.java b/luni/src/main/java/libcore/net/http/HttpEngine.java
index 08ff1ae..25f28f5 100644
--- a/luni/src/main/java/libcore/net/http/HttpEngine.java
+++ b/luni/src/main/java/libcore/net/http/HttpEngine.java
@@ -408,9 +408,6 @@
}
public final CacheResponse getCacheResponse() {
- if (responseHeaders == null) {
- throw new IllegalStateException();
- }
return cacheResponse;
}
diff --git a/luni/src/test/java/libcore/java/net/URLConnectionTest.java b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
index acd3c13..1305a81 100644
--- a/luni/src/test/java/libcore/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/libcore/java/net/URLConnectionTest.java
@@ -1895,6 +1895,57 @@
assertEquals(-1, in.read());
}
+ public void testInspectSslBeforeConnect() throws Exception {
+ TestSSLContext testSSLContext = TestSSLContext.create();
+ server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
+ server.enqueue(new MockResponse());
+ server.play();
+
+ HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
+ connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
+ assertNotNull(connection.getHostnameVerifier());
+ try {
+ connection.getLocalCertificates();
+ fail();
+ } catch (IllegalStateException expected) {
+ }
+ try {
+ connection.getServerCertificates();
+ fail();
+ } catch (IllegalStateException expected) {
+ }
+ try {
+ connection.getCipherSuite();
+ fail();
+ } catch (IllegalStateException expected) {
+ }
+ try {
+ connection.getPeerPrincipal();
+ fail();
+ } catch (IllegalStateException expected) {
+ }
+ }
+
+ /**
+ * Test that we can inspect the SSL session after connect().
+ * http://code.google.com/p/android/issues/detail?id=24431
+ */
+ public void testInspectSslAfterConnect() throws Exception {
+ TestSSLContext testSSLContext = TestSSLContext.create();
+ server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
+ server.enqueue(new MockResponse());
+ server.play();
+
+ HttpsURLConnection connection = (HttpsURLConnection) server.getUrl("/").openConnection();
+ connection.setSSLSocketFactory(testSSLContext.clientContext.getSocketFactory());
+ connection.connect();
+ assertNotNull(connection.getHostnameVerifier());
+ assertNull(connection.getLocalCertificates());
+ assertNotNull(connection.getServerCertificates());
+ assertNotNull(connection.getCipherSuite());
+ assertNotNull(connection.getPeerPrincipal());
+ }
+
/**
* Returns a gzipped copy of {@code bytes}.
*/