| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.test.suitebuilder.annotation.SmallTest; | |
| 9 | |
| 10 import org.chromium.base.PathUtils; | |
| 11 import org.chromium.base.test.util.Feature; | |
| 12 import org.chromium.net.test.EmbeddedTestServer; | |
| 13 | |
| 14 import java.io.File; | |
| 15 import java.util.HashMap; | |
| 16 | |
| 17 /** | |
| 18 * Test for deprecated {@link HttpUrlRequest} API. | |
| 19 */ | |
| 20 @SuppressWarnings("deprecation") | |
| 21 public class CronetUrlTest extends CronetTestBase { | |
| 22 private EmbeddedTestServer mTestServer; | |
| 23 private String mUrl; | |
| 24 | |
| 25 @Override | |
| 26 protected void setUp() throws Exception { | |
| 27 super.setUp(); | |
| 28 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext(
)); | |
| 29 mUrl = mTestServer.getURL("/echo?status=200"); | |
| 30 } | |
| 31 | |
| 32 @Override | |
| 33 protected void tearDown() throws Exception { | |
| 34 mTestServer.stopAndDestroyServer(); | |
| 35 super.tearDown(); | |
| 36 } | |
| 37 | |
| 38 @SmallTest | |
| 39 @Feature({"Cronet"}) | |
| 40 public void testLoadUrl() throws Exception { | |
| 41 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(mUrl); | |
| 42 | |
| 43 // Make sure that the URL is set as expected. | |
| 44 assertEquals(mUrl, testFramework.getUrl()); | |
| 45 assertEquals(200, testFramework.getHttpStatusCode()); | |
| 46 } | |
| 47 | |
| 48 @SmallTest | |
| 49 @Feature({"Cronet"}) | |
| 50 public void testInvalidUrl() throws Exception { | |
| 51 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
("127.0.0.1:8000"); | |
| 52 | |
| 53 // The load should fail. | |
| 54 assertEquals(0, testFramework.getHttpStatusCode()); | |
| 55 } | |
| 56 | |
| 57 @SmallTest | |
| 58 @Feature({"Cronet"}) | |
| 59 public void testPostData() throws Exception { | |
| 60 String[] commandLineArgs = {CronetTestFramework.POST_DATA_KEY, "test", | |
| 61 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar
yInitType.LEGACY}; | |
| 62 CronetTestFramework testFramework = | |
| 63 startCronetTestFrameworkWithUrlAndCommandLineArgs(mUrl, commandL
ineArgs); | |
| 64 | |
| 65 // Make sure that the URL is set as expected. | |
| 66 assertEquals(mUrl, testFramework.getUrl()); | |
| 67 assertEquals(200, testFramework.getHttpStatusCode()); | |
| 68 } | |
| 69 | |
| 70 @SmallTest | |
| 71 @Feature({"Cronet"}) | |
| 72 @OnlyRunNativeCronet // No NetLog from HttpURLConnection | |
| 73 public void testNetLog() throws Exception { | |
| 74 Context context = getContext(); | |
| 75 File directory = new File(PathUtils.getDataDirectory()); | |
| 76 File file = File.createTempFile("cronet", "json", directory); | |
| 77 HttpUrlRequestFactory factory = HttpUrlRequestFactory.createFactory( | |
| 78 context, new CronetEngine.Builder(null /*context*/).setLibraryNa
me("cronet_tests")); | |
| 79 // Start NetLog immediately after the request context is created to make | |
| 80 // sure that the call won't crash the app even when the native request | |
| 81 // context is not fully initialized. See crbug.com/470196. | |
| 82 factory.startNetLogToFile(file.getPath(), false); | |
| 83 // Starts a request. | |
| 84 HashMap<String, String> headers = new HashMap<String, String>(); | |
| 85 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | |
| 86 HttpUrlRequest request = factory.createRequest( | |
| 87 mUrl, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener)
; | |
| 88 request.start(); | |
| 89 listener.blockForComplete(); | |
| 90 factory.stopNetLog(); | |
| 91 assertTrue(file.exists()); | |
| 92 assertTrue(file.length() != 0); | |
| 93 assertTrue(file.delete()); | |
| 94 assertTrue(!file.exists()); | |
| 95 } | |
| 96 | |
| 97 static class BadHttpUrlRequestListener extends TestHttpUrlRequestListener { | |
| 98 static final String THROW_TAG = "BadListener"; | |
| 99 | |
| 100 public BadHttpUrlRequestListener() { | |
| 101 } | |
| 102 | |
| 103 @Override | |
| 104 public void onResponseStarted(HttpUrlRequest request) { | |
| 105 throw new NullPointerException(THROW_TAG); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 @SmallTest | |
| 110 @Feature({"Cronet"}) | |
| 111 public void testCalledByNativeException() throws Exception { | |
| 112 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(mUrl); | |
| 113 | |
| 114 HashMap<String, String> headers = new HashMap<String, String>(); | |
| 115 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); | |
| 116 | |
| 117 // Create request with bad listener to trigger an exception. | |
| 118 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | |
| 119 mUrl, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener)
; | |
| 120 request.start(); | |
| 121 listener.blockForComplete(); | |
| 122 assertTrue(request.isCanceled()); | |
| 123 assertNotNull(request.getException()); | |
| 124 assertEquals(BadHttpUrlRequestListener.THROW_TAG, | |
| 125 request.getException().getCause().getMessage()); | |
| 126 } | |
| 127 | |
| 128 @SmallTest | |
| 129 @Feature({"Cronet"}) | |
| 130 public void testSetUploadDataWithNullContentType() throws Exception { | |
| 131 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(mUrl); | |
| 132 | |
| 133 HashMap<String, String> headers = new HashMap<String, String>(); | |
| 134 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); | |
| 135 | |
| 136 // Create request. | |
| 137 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | |
| 138 mUrl, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener)
; | |
| 139 byte[] uploadData = new byte[] {1, 2, 3}; | |
| 140 try { | |
| 141 request.setUploadData(null, uploadData); | |
| 142 fail("setUploadData should throw on null content type"); | |
| 143 } catch (NullPointerException e) { | |
| 144 // Nothing to do here. | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 @SmallTest | |
| 149 @Feature({"Cronet"}) | |
| 150 public void testLegacyLoadUrl() throws Exception { | |
| 151 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | |
| 152 builder.enableLegacyMode(true); | |
| 153 | |
| 154 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(mUrl); | |
| 155 | |
| 156 // Make sure that the URL is set as expected. | |
| 157 assertEquals(mUrl, testFramework.getUrl()); | |
| 158 assertEquals(200, testFramework.getHttpStatusCode()); | |
| 159 } | |
| 160 | |
| 161 @SmallTest | |
| 162 @Feature({"Cronet"}) | |
| 163 public void testRequestHead() throws Exception { | |
| 164 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(mUrl); | |
| 165 | |
| 166 HashMap<String, String> headers = new HashMap<String, String>(); | |
| 167 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | |
| 168 | |
| 169 // Create request. | |
| 170 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | |
| 171 mUrl, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener)
; | |
| 172 request.setHttpMethod("HEAD"); | |
| 173 request.start(); | |
| 174 listener.blockForComplete(); | |
| 175 assertEquals(200, listener.mHttpStatusCode); | |
| 176 // HEAD requests do not get any response data and Content-Length must be | |
| 177 // ignored. | |
| 178 assertEquals(0, listener.mResponseAsBytes.length); | |
| 179 } | |
| 180 } | |
| OLD | NEW |