Address some test failures in the HTTP client.
Change-Id: I44783aa7cadd51ed6b9e7aabc94144c60252c095
http://b/3180373
diff --git a/luni/src/main/java/libcore/net/http/HttpEngine.java b/luni/src/main/java/libcore/net/http/HttpEngine.java
index ecf6656..14c6c71 100644
--- a/luni/src/main/java/libcore/net/http/HttpEngine.java
+++ b/luni/src/main/java/libcore/net/http/HttpEngine.java
@@ -26,7 +26,6 @@
import java.net.CacheResponse;
import java.net.CookieHandler;
import java.net.HttpURLConnection;
-import java.net.ProtocolException;
import java.net.Proxy;
import java.net.ResponseCache;
import java.net.URI;
@@ -460,7 +459,7 @@
* Releases this connection so that it may be either reused or closed.
*/
public final void releaseSocket(boolean reusable) {
- if (released) {
+ if (released || connection == null) {
return;
}
released = true;
diff --git a/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java b/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java
index 74a1fb7..4b61829 100644
--- a/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java
+++ b/luni/src/main/java/libcore/net/http/HttpURLConnectionImpl.java
@@ -187,7 +187,15 @@
@Override public final OutputStream getOutputStream() throws IOException {
connect();
- return httpEngine.getRequestBody();
+
+ OutputStream result = httpEngine.getRequestBody();
+ if (result == null) {
+ throw new ProtocolException("method does not support a request body: " + method);
+ } else if (httpEngine.hasResponse()) {
+ throw new ProtocolException("cannot write request body after response has been read");
+ }
+
+ return result;
}
@Override public final Permission getPermission() throws IOException {
diff --git a/luni/src/main/java/libcore/net/http/HttpsURLConnectionImpl.java b/luni/src/main/java/libcore/net/http/HttpsURLConnectionImpl.java
index 46f74f4..dedf035 100644
--- a/luni/src/main/java/libcore/net/http/HttpsURLConnectionImpl.java
+++ b/luni/src/main/java/libcore/net/http/HttpsURLConnectionImpl.java
@@ -382,7 +382,8 @@
}
public SecureCacheResponse getCacheResponse() {
- return (SecureCacheResponse) httpEngine.getCacheResponse();
+ HttpsEngine engine = (HttpsEngine) httpEngine;
+ return engine != null ? (SecureCacheResponse) engine.getCacheResponse() : null;
}
public SSLSocket getSSLSocket() {
diff --git a/luni/src/test/java/libcore/java/net/HttpResponseCacheTest.java b/luni/src/test/java/libcore/java/net/HttpResponseCacheTest.java
index 5d62dc6..505cb1d 100644
--- a/luni/src/test/java/libcore/java/net/HttpResponseCacheTest.java
+++ b/luni/src/test/java/libcore/java/net/HttpResponseCacheTest.java
@@ -1118,6 +1118,48 @@
assertEquals("B", readAscii(server.getUrl("/bar").openConnection()));
}
+ public void testUseCachesFalseDoesNotWriteToCache() throws Exception {
+ server.enqueue(new MockResponse()
+ .addHeader("Cache-Control: max-age=60")
+ .setBody("A").setBody("A"));
+ server.enqueue(new MockResponse().setBody("B"));
+ server.play();
+
+ URLConnection connection = server.getUrl("/").openConnection();
+ connection.setUseCaches(false);
+ assertEquals("A", readAscii(connection));
+ assertEquals("B", readAscii(server.getUrl("/").openConnection()));
+ }
+
+ public void testUseCachesFalseDoesNotReadFromCache() throws Exception {
+ server.enqueue(new MockResponse()
+ .addHeader("Cache-Control: max-age=60")
+ .setBody("A").setBody("A"));
+ server.enqueue(new MockResponse().setBody("B"));
+ server.play();
+
+ assertEquals("A", readAscii(server.getUrl("/").openConnection()));
+ URLConnection connection = server.getUrl("/").openConnection();
+ connection.setUseCaches(false);
+ assertEquals("B", readAscii(connection));
+ }
+
+ public void testDefaultUseCachesSetsInitialValueOnly() throws Exception {
+ URL url = new URL("http://localhost/");
+ URLConnection c1 = url.openConnection();
+ URLConnection c2 = url.openConnection();
+ assertTrue(c1.getDefaultUseCaches());
+ c1.setDefaultUseCaches(false);
+ try {
+ assertTrue(c1.getUseCaches());
+ assertTrue(c2.getUseCaches());
+ URLConnection c3 = url.openConnection();
+ assertFalse(c3.getUseCaches());
+ } finally {
+ c1.setDefaultUseCaches(true);
+ }
+ }
+
/**
* @param delta the offset from the current date to use. Negative
* values yield dates in the past; positive values yield dates in the
diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java
index 216af26..c516f67 100644
--- a/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java
+++ b/luni/src/test/java/org/apache/harmony/luni/tests/internal/net/www/protocol/https/HttpsURLConnectionTest.java
@@ -973,7 +973,7 @@
log("Authentication required...");
// send Authentication Request
os.write(respAuthenticationRequired.getBytes());
- // read response
+ // read request
num = is.read(buff);
if (num == -1) {
// this connection was closed,
@@ -990,8 +990,8 @@
log("Got authenticated request:\n" + message);
log("------------------");
// check provided authorization credentials
- assertTrue("Received message does not contain authorization credentials",
- message.toLowerCase().indexOf("proxy-authorization:") > 0);
+ assertTrue("no proxy-authorization credentials: " + message,
+ message.toLowerCase().indexOf("proxy-authorization:") != -1);
}
assertTrue(message.startsWith("CONNECT"));
diff --git a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
index dd6a0f0..f2d64cd 100644
--- a/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
+++ b/luni/src/test/java/org/apache/harmony/luni/tests/java/net/URLConnectionTest.java
@@ -71,24 +71,8 @@
private JarURLConnection jarURLCon;
- private URL jarURL;
-
private URLConnection gifURLCon;
- private URL gifURL;
-
- public boolean isGetCalled;
-
- public boolean isPutCalled;
-
- private Map<String, List<String>> mockHeaderMap;
-
- private InputStream mockIs = new MockInputStream();
-
- public boolean isCacheWriteCalled;
-
- public boolean isAbortCalled;
-
/**
* {@link java.net.URLConnection#addRequestProperty(String, String)}
*/
@@ -220,107 +204,8 @@
}
}
- class MockCachedResponseCache extends ResponseCache {
-
- public CacheResponse get(URI arg0, String arg1, Map arg2)
- throws IOException {
- if (null == arg0 || null == arg1 || null == arg2) {
- throw new NullPointerException();
- }
- isGetCalled = true;
- return new MockCacheResponse();
- }
-
- public CacheRequest put(URI arg0, URLConnection arg1)
- throws IOException {
- if (null == arg0 || null == arg1) {
- throw new NullPointerException();
- }
- isPutCalled = true;
- return new MockCacheRequest();
- }
- }
-
- class MockNonCachedResponseCache extends ResponseCache {
-
- public CacheResponse get(URI arg0, String arg1, Map arg2)
- throws IOException {
- isGetCalled = true;
- return null;
- }
-
- public CacheRequest put(URI arg0, URLConnection arg1)
- throws IOException {
- isPutCalled = true;
- return new MockCacheRequest();
- }
- }
-
- class MockCacheRequest extends CacheRequest {
-
- public OutputStream getBody() throws IOException {
- isCacheWriteCalled = true;
- return new MockOutputStream();
- }
-
- public void abort() {
- isAbortCalled = true;
- }
-
- }
-
- class MockInputStream extends InputStream {
-
- public int read() throws IOException {
- return 4711;
- }
-
- public int read(byte[] arg0, int arg1, int arg2) throws IOException {
- return 1;
- }
-
- public int read(byte[] arg0) throws IOException {
- return 1;
- }
-
- }
-
- class MockOutputStream extends OutputStream {
-
- public void write(int b) throws IOException {
- isCacheWriteCalled = true;
- }
-
- public void write(byte[] b, int off, int len) throws IOException {
- isCacheWriteCalled = true;
- }
-
- public void write(byte[] b) throws IOException {
- isCacheWriteCalled = true;
- }
- }
-
- class MockCacheResponse extends CacheResponse {
-
- public Map<String, List<String>> getHeaders() throws IOException {
- return mockHeaderMap;
- }
-
- public InputStream getBody() throws IOException {
- return mockIs;
- }
- }
-
-
private static int port;
- static String getContentType(String fileName) throws IOException {
- String resourceName = "org/apache/harmony/luni/tests/" + fileName;
- URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
- assertNotNull("Cannot find test resource " + resourceName, url);
- return url.openConnection().getContentType();
- }
-
URL url;
URL url2;
@@ -349,10 +234,7 @@
fileURLCon = fileURL.openConnection();
jarURLCon = openJarURLConnection();
- jarURL = jarURLCon.getURL();
-
gifURLCon = openGifURLConnection();
- gifURL = gifURLCon.getURL();
}
@Override
@@ -679,7 +561,7 @@
public void test_getDate() {
// should be greater than 930000000000L which represents the past
assertTrue("getDate gave wrong date: " + uc.getDate(),
- uc.getDate() > 930000000000L);
+ uc.getDate() > 930000000000L);
}
/**
@@ -722,65 +604,6 @@
/**
* @throws IOException
- * {@link java.net.URLConnection#getDefaultUseCaches()}
- */
- public void test_getDefaultUseCaches_CachedRC() throws IOException {
- boolean oldSetting = uc.getDefaultUseCaches();
-
- ResponseCache old = ResponseCache.getDefault();
- ResponseCache rc = new MockCachedResponseCache();
- ResponseCache.setDefault(rc);
-
- // Recreate the connection so that we get the cache from ResponseCache.
- uc2 = url2.openConnection();
-
- uc2.setUseCaches(true);
-
- uc.setDefaultUseCaches(false);
-
- // uc unaffected
- assertTrue(uc.getUseCaches());
- // uc2 unaffected
- assertTrue(uc2.getUseCaches());
-
- //test get
- assertFalse("getDefaultUseCaches should have returned false", uc
- .getDefaultUseCaches());
-
- // subsequent connections should have default value
- URL url3 = new URL("http://localhost:" + port + "/test2");
- URLConnection uc3 = url3.openConnection();
- assertFalse(uc3.getUseCaches());
-
- // test if uc does not cache but uc2 does
- isGetCalled = false;
- isPutCalled = false;
-
- // test uc
- uc.setDoOutput(true);
- assertFalse(isGetCalled);
- uc.connect();
- assertFalse(isGetCalled);
- assertFalse(isPutCalled);
- OutputStream os = uc.getOutputStream();
- assertFalse(isPutCalled);
- assertFalse(isGetCalled);
-
- os.close();
-
- //uc2 should be unaffected
- uc2.setDoOutput(true);
- assertFalse(isGetCalled);
- uc2.connect();
- assertTrue(isGetCalled);
- assertFalse(isPutCalled);
-
- uc.setDefaultUseCaches(oldSetting);
- ResponseCache.setDefault(null);
- }
-
- /**
- * @throws IOException
* {@link java.net.URLConnection#getDoInput()}
*/
public void test_getDoInput() throws IOException {
diff --git a/luni/src/test/java/tests/api/javax/net/ssl/HttpsURLConnectionTest.java b/luni/src/test/java/tests/api/javax/net/ssl/HttpsURLConnectionTest.java
index 3c49fc4..e2ebdbd 100644
--- a/luni/src/test/java/tests/api/javax/net/ssl/HttpsURLConnectionTest.java
+++ b/luni/src/test/java/tests/api/javax/net/ssl/HttpsURLConnectionTest.java
@@ -46,71 +46,45 @@
/**
* javax.net.ssl.HttpsURLConnection#HttpsURLConnection(java_net_URL)
*/
- public final void test_Constructor() {
- try {
- MyHttpsURLConnection huc = new MyHttpsURLConnection(new URL("https://www.fortify.net/"));
- } catch (Exception e) {
- fail("Unexpected exception: " + e.toString());
- }
- try {
- MyHttpsURLConnection huc = new MyHttpsURLConnection(null);
- } catch (Exception e) {
- fail("Unexpected exception " + e.toString());
- }
+ public final void test_Constructor() throws Exception {
+ new MyHttpsURLConnection(new URL("https://www.fortify.net/"));
+ new MyHttpsURLConnection(null);
}
/**
* javax.net.ssl.HttpsURLConnection#getCipherSuite()
*/
- public final void test_getCipherSuite() {
+ public final void test_getCipherSuite() throws Exception {
+ URL url = new URL("https://localhost:55555");
+ HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
try {
- URL url = new URL("https://localhost:55555");
- HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
- try {
- connection.getCipherSuite();
- fail("IllegalStateException wasn't thrown");
- } catch (IllegalStateException ise) {
- //expected
- }
- } catch (Exception e) {
- fail("Unexpected exception " + e + " for exception case");
+ connection.getCipherSuite();
+ fail("IllegalStateException wasn't thrown");
+ } catch (IllegalStateException expected) {
}
- try {
- HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"));
- assertEquals("CipherSuite", con.getCipherSuite());
- } catch (Exception e) {
- fail("Unexpected exception " + e);
- }
+ HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"));
+ assertEquals("CipherSuite", con.getCipherSuite());
}
/**
* javax.net.ssl.HttpsURLConnection#getLocalCertificates()
*/
- public final void test_getLocalCertificates() {
+ public final void test_getLocalCertificates() throws Exception {
+ URL url = new URL("https://localhost:55555");
+ HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
try {
- URL url = new URL("https://localhost:55555");
- HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
- try {
- connection.getLocalCertificates();
- fail("IllegalStateException wasn't thrown");
- } catch (IllegalStateException ise) {
- //expected
- }
- } catch (Exception e) {
- fail("Unexpected exception " + e + " for exception case");
+ connection.getLocalCertificates();
+ fail("IllegalStateException wasn't thrown");
+ } catch (IllegalStateException expected) {
}
- try {
- HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.508");
- assertNull(con.getLocalCertificates());
- con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.509");
- Certificate[] cert = con.getLocalCertificates();
- assertNotNull(cert);
- assertEquals(1, cert.length);
- } catch (Exception e) {
- fail("Unexpected exception " + e);
- }
+ HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.508");
+ assertNull(con.getLocalCertificates());
+ con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.509");
+ Certificate[] cert = con.getLocalCertificates();
+ assertNotNull(cert);
+ assertEquals(1, cert.length);
}
/**
@@ -148,96 +122,67 @@
/**
* javax.net.ssl.HttpsURLConnection#getLocalPrincipal()
*/
- public final void test_getLocalPrincipal() {
+ public final void test_getLocalPrincipal() throws Exception {
+ URL url = new URL("https://localhost:55555");
+ HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
try {
- URL url = new URL("https://localhost:55555");
- HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
- try {
- connection.getLocalPrincipal();
- fail("IllegalStateException wasn't thrown");
- } catch (IllegalStateException ise) {
- //expected
- }
- } catch (Exception e) {
- fail("Unexpected exception " + e + " for exception case");
+ connection.getLocalPrincipal();
+ fail("IllegalStateException wasn't thrown");
+ } catch (IllegalStateException expected) {
}
- try {
- HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.508");
- assertNull(con.getLocalPrincipal());
- con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.509");
- assertNotNull("Local principal is null", con.getLocalPrincipal());
- } catch (Exception e) {
- fail("Unexpected exception " + e);
- }
+ HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.508");
+ assertNull(con.getLocalPrincipal());
+ con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.509");
+ assertNotNull("Local principal is null", con.getLocalPrincipal());
}
/**
* javax.net.ssl.HttpsURLConnection#getPeerPrincipal()
*/
public final void test_getPeerPrincipal() throws Exception {
+ URL url = new URL("https://localhost:55555");
+ HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
try {
- URL url = new URL("https://localhost:55555");
- HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
- try {
- connection.getPeerPrincipal();
- fail("IllegalStateException wasn't thrown");
- } catch (IllegalStateException ise) {
- //expected
- }
- } catch (Exception e) {
- fail("Unexpected exception " + e + " for exception case");
+ connection.getPeerPrincipal();
+ fail("IllegalStateException wasn't thrown");
+ } catch (IllegalStateException expected) {
}
HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.508");
try {
Principal p = con.getPeerPrincipal();
fail("SSLPeerUnverifiedException wasn't thrown");
- } catch (SSLPeerUnverifiedException e) {
- //expected
+ } catch (SSLPeerUnverifiedException expected) {
}
con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.509");
- try {
- Principal p = con.getPeerPrincipal();
- assertNotNull(p);
- } catch (Exception e) {
- fail("Unexpected exception " + e);
- }
+ Principal p = con.getPeerPrincipal();
+ assertNotNull(p);
}
/**
* javax.net.ssl.HttpsURLConnection#getServerCertificates()
*/
public final void test_getServerCertificates() throws Exception {
+ URL url = new URL("https://localhost:55555");
+ HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
try {
- URL url = new URL("https://localhost:55555");
- HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
- try {
- connection.getServerCertificates();
- fail("IllegalStateException wasn't thrown");
- } catch (IllegalStateException ise) {
- //expected
- }
- } catch (Exception e) {
- fail("Unexpected exception " + e + " for exception case");
+ connection.getServerCertificates();
+ fail("IllegalStateException wasn't thrown");
+ } catch (IllegalStateException expected) {
}
HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.508");
try {
- Certificate[] cert = con.getServerCertificates();
+ con.getServerCertificates();
fail("SSLPeerUnverifiedException wasn't thrown");
- } catch (SSLPeerUnverifiedException e) {
- //expected
+ } catch (SSLPeerUnverifiedException expected) {
}
con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.509");
- try {
- Certificate[] cert = con.getServerCertificates();
- assertNotNull(cert);
- assertEquals(1, cert.length);
- } catch (Exception e) {
- fail("Unexpected exception " + e);
- }
+ Certificate[] cert = con.getServerCertificates();
+ assertNotNull(cert);
+ assertEquals(1, cert.length);
}
/**
@@ -258,16 +203,13 @@
try {
HttpsURLConnection.setDefaultHostnameVerifier(null);
fail("No expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
+ } catch (IllegalArgumentException expected) {
}
HostnameVerifier def = HttpsURLConnection.getDefaultHostnameVerifier();
try {
myHostnameVerifier hnv = new myHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
assertEquals(hnv, HttpsURLConnection.getDefaultHostnameVerifier());
- } catch (Exception e) {
- fail("Unexpected exception " + e);
} finally {
HttpsURLConnection.setDefaultHostnameVerifier(def);
}
@@ -281,14 +223,10 @@
try {
con.setHostnameVerifier(null);
fail("No expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
+ } catch (IllegalArgumentException expected) {
}
- try {
- myHostnameVerifier hnv = new myHostnameVerifier();
- con.setHostnameVerifier(hnv);
- } catch (Exception e) {
- fail("Unexpected exception " + e);
- }
+ myHostnameVerifier hnv = new myHostnameVerifier();
+ con.setHostnameVerifier(hnv);
}
/**
@@ -298,15 +236,11 @@
try {
HttpsURLConnection.setDefaultSSLSocketFactory(null);
fail("No expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
+ } catch (IllegalArgumentException expected) {
}
- try {
- SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory
- .getDefault();
- HttpsURLConnection.setDefaultSSLSocketFactory(ssf);
- } catch (Exception e) {
- fail("Unexpected exception " + e);
- }
+ SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory
+ .getDefault();
+ HttpsURLConnection.setDefaultSSLSocketFactory(ssf);
}
/**
@@ -317,15 +251,11 @@
try {
con.setSSLSocketFactory(null);
fail("No expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
+ } catch (IllegalArgumentException expected) {
}
- try {
- SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory
- .getDefault();
- con.setSSLSocketFactory(ssf);
- } catch (Exception e) {
- fail("Unexpected exception " + e);
- }
+ SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory
+ .getDefault();
+ con.setSSLSocketFactory(ssf);
}
}
@@ -353,32 +283,30 @@
* @see javax.net.ssl.HttpsURLConnection#getLocalCertificates()
*/
public Certificate[] getLocalCertificates() {
- Certificate cert = null;
try {
CertificateFactory cf = CertificateFactory.getInstance(typeDone);
byte[] barr = TestUtils.getX509Certificate_v1();
ByteArrayInputStream bis = new ByteArrayInputStream(barr);
- cert = cf.generateCertificate(bis);
+ Certificate cert = cf.generateCertificate(bis);
+ return new Certificate[] { cert };
} catch (CertificateException se) {
- cert = null;
+ return null;
}
- return cert == null ? null : new Certificate[]{cert};
}
/*
* @see javax.net.ssl.HttpsURLConnection#getServerCertificates()
*/
public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
- Certificate cert = null;
try {
CertificateFactory cf = CertificateFactory.getInstance(typeDone);
byte[] barr = TestUtils.getX509Certificate_v3();
ByteArrayInputStream bis = new ByteArrayInputStream(barr);
- cert = cf.generateCertificate(bis);
+ Certificate cert = cf.generateCertificate(bis);
+ return new Certificate[] { cert };
} catch (CertificateException se) {
throw new SSLPeerUnverifiedException("No server's end-entity certificate");
}
- return cert == null ? null : new Certificate[]{cert};
}
/*