Chromium Code Reviews| Index: components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java |
| diff --git a/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java |
| index f4eda23dd0e2cccae535abcbc63f92c8865d29c4..20768b986bb704b35727821a4b61b8e751e6090b 100644 |
| --- a/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java |
| +++ b/components/cronet/android/test/javatests/src/org/chromium/cronet_test_apk/urlconnection/CronetHttpURLConnectionTest.java |
| @@ -10,6 +10,7 @@ import org.chromium.base.test.util.Feature; |
| import org.chromium.cronet_test_apk.CronetTestActivity; |
| import org.chromium.cronet_test_apk.CronetTestBase; |
| import org.chromium.cronet_test_apk.NativeTestServer; |
| +import org.chromium.net.UrlRequestContextConfig; |
| import org.chromium.net.UrlRequestException; |
| import java.io.ByteArrayOutputStream; |
| @@ -40,7 +41,14 @@ public class CronetHttpURLConnectionTest extends CronetTestBase { |
| @Override |
| protected void setUp() throws Exception { |
| super.setUp(); |
| - mActivity = launchCronetTestApp(); |
| + UrlRequestContextConfig config = new UrlRequestContextConfig(); |
| + config.setStoragePath(prepareTestStorage()); |
| + config.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, |
| + 1000 * 1024); |
| + String[] commandLineArgs = { |
| + CronetTestActivity.CONFIG_KEY, config.toString()}; |
| + mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, |
| + commandLineArgs); |
| assertTrue(NativeTestServer.startNativeTestServer( |
| getInstrumentation().getTargetContext())); |
| } |
| @@ -602,6 +610,59 @@ public class CronetHttpURLConnectionTest extends CronetTestBase { |
| connection.disconnect(); |
| } |
| + /** |
| + * Helper method to make a request with cache enabled or disabled, and check |
| + * whether the request is successful. |
| + * @param requestUrl request url. |
| + * @param useCaches whether cache should be used. |
| + * @param success whether request is expected to be successful. |
| + */ |
| + private void checkRequestCaching(String requestUrl, boolean useCaches, |
| + boolean success) throws Exception { |
| + URL url = new URL(requestUrl); |
| + HttpURLConnection connection = |
| + (HttpURLConnection) url.openConnection(); |
| + connection.setUseCaches(useCaches); |
| + if (success) { |
| + assertEquals(200, connection.getResponseCode()); |
| + assertEquals("this is a cacheable file\n", |
| + getResponseAsString(connection)); |
| + } else { |
| + try { |
| + connection.getResponseCode(); |
| + fail(); |
| + } catch (IOException e) { |
| + // Expected. |
| + } |
| + } |
| + connection.disconnect(); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @OnlyRunCronetHttpURLConnection |
| + // Strangely, the default implementation fails to return a cached response. |
| + // If the server is shut down, the request just fails with a connection |
| + // refused error. Therefore, this test and the next only runs Cronet. |
| + public void testSetUseCaches() throws Exception { |
| + String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| + checkRequestCaching(url, true /** useCaches */, true /** success */); |
|
mef
2015/02/04 20:39:42
I find the use of comments interesting, is it reco
xunjieli
2015/02/04 21:27:05
Done.
|
| + // Shut down the server, we should be able to receive a cached response. |
| + NativeTestServer.shutdownNativeTestServer(); |
| + checkRequestCaching(url, true /** useCaches */, true /** success */); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"Cronet"}) |
| + @OnlyRunCronetHttpURLConnection |
| + public void testSetUseCachesFalse() throws Exception { |
| + String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| + checkRequestCaching(url, true /** useCaches */, true /** success */); |
| + NativeTestServer.shutdownNativeTestServer(); |
| + // Disables caching. No cached response is received. |
| + checkRequestCaching(url, false /** useCaches */, false /** success */); |
| + } |
| + |
| private void checkExceptionsAreThrown(HttpURLConnection connection) |
| throws Exception { |
| try { |