Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 | 9 |
| 10 import org.chromium.base.PathUtils; | 10 import org.chromium.base.PathUtils; |
| 11 import org.chromium.base.test.util.Feature; | 11 import org.chromium.base.test.util.Feature; |
| 12 | 12 |
| 13 import java.io.File; | 13 import java.io.File; |
| 14 import java.util.HashMap; | 14 import java.util.HashMap; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Test for deprecated {@link HttpUrlRequest} API. | 17 * Test for deprecated {@link HttpUrlRequest} API. |
| 18 */ | 18 */ |
| 19 @SuppressWarnings("deprecation") | 19 @SuppressWarnings("deprecation") |
| 20 public class CronetUrlTest extends CronetTestBase { | 20 public class CronetUrlTest extends CronetTestBase { |
| 21 // URL used for base tests. | 21 // URL used for base tests. |
| 22 private static final String URL = "http://127.0.0.1:8000"; | 22 private static final String URL = "http://127.0.0.1:8000"; |
| 23 | 23 |
| 24 @SmallTest | 24 @SmallTest |
| 25 @Feature({"Cronet"}) | 25 @Feature({"Cronet"}) |
| 26 public void testLoadUrl() throws Exception { | 26 public void testLoadUrl() throws Exception { |
| 27 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL) ; | 27 String[] commandLineArgs = { |
| 28 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.LEGACY}; | |
| 29 CronetTestFramework testFramework = | |
| 30 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi neArgs); | |
| 28 | 31 |
| 29 // Make sure that the URL is set as expected. | 32 // Make sure that the URL is set as expected. |
| 30 assertEquals(URL, testFramework.getUrl()); | 33 assertEquals(URL, testFramework.getUrl()); |
| 31 assertEquals(200, testFramework.getHttpStatusCode()); | 34 assertEquals(200, testFramework.getHttpStatusCode()); |
| 32 } | 35 } |
| 33 | 36 |
| 34 @SmallTest | 37 @SmallTest |
| 35 @Feature({"Cronet"}) | 38 @Feature({"Cronet"}) |
| 36 public void testInvalidUrl() throws Exception { | 39 public void testInvalidUrl() throws Exception { |
| 37 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl("127 .0.0.1:8000"); | 40 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl("127 .0.0.1:8000"); |
| 38 | 41 |
| 39 // The load should fail. | 42 // The load should fail. |
| 40 assertEquals(0, testFramework.getHttpStatusCode()); | 43 assertEquals(0, testFramework.getHttpStatusCode()); |
| 41 } | 44 } |
| 42 | 45 |
| 43 @SmallTest | 46 @SmallTest |
| 44 @Feature({"Cronet"}) | 47 @Feature({"Cronet"}) |
| 45 public void testPostData() throws Exception { | 48 public void testPostData() throws Exception { |
| 46 String[] commandLineArgs = {CronetTestFramework.POST_DATA_KEY, "test"}; | 49 String[] commandLineArgs = {CronetTestFramework.POST_DATA_KEY, "test", |
| 50 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.LEGACY}; | |
| 47 CronetTestFramework testFramework = | 51 CronetTestFramework testFramework = |
| 48 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi neArgs); | 52 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi neArgs); |
| 49 | 53 |
| 50 // Make sure that the URL is set as expected. | 54 // Make sure that the URL is set as expected. |
| 51 assertEquals(URL, testFramework.getUrl()); | 55 assertEquals(URL, testFramework.getUrl()); |
| 52 assertEquals(200, testFramework.getHttpStatusCode()); | 56 assertEquals(200, testFramework.getHttpStatusCode()); |
| 53 } | 57 } |
| 54 | 58 |
| 55 @SmallTest | 59 @SmallTest |
| 56 @Feature({"Cronet"}) | 60 @Feature({"Cronet"}) |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 87 | 91 |
| 88 @Override | 92 @Override |
| 89 public void onResponseStarted(HttpUrlRequest request) { | 93 public void onResponseStarted(HttpUrlRequest request) { |
| 90 throw new NullPointerException(THROW_TAG); | 94 throw new NullPointerException(THROW_TAG); |
| 91 } | 95 } |
| 92 } | 96 } |
| 93 | 97 |
| 94 @SmallTest | 98 @SmallTest |
| 95 @Feature({"Cronet"}) | 99 @Feature({"Cronet"}) |
| 96 public void testCalledByNativeException() throws Exception { | 100 public void testCalledByNativeException() throws Exception { |
| 97 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL) ; | 101 String[] commandLineArgs = { |
| 102 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.LEGACY}; | |
| 103 CronetTestFramework testFramework = | |
| 104 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi neArgs); | |
| 98 | 105 |
| 99 HashMap<String, String> headers = new HashMap<String, String>(); | 106 HashMap<String, String> headers = new HashMap<String, String>(); |
| 100 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); | 107 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); |
| 101 | 108 |
| 102 // Create request with bad listener to trigger an exception. | 109 // Create request with bad listener to trigger an exception. |
| 103 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | 110 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 104 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 111 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 105 request.start(); | 112 request.start(); |
| 106 listener.blockForComplete(); | 113 listener.blockForComplete(); |
| 107 assertTrue(request.isCanceled()); | 114 assertTrue(request.isCanceled()); |
| 108 assertNotNull(request.getException()); | 115 assertNotNull(request.getException()); |
| 109 assertEquals(BadHttpUrlRequestListener.THROW_TAG, | 116 assertEquals(BadHttpUrlRequestListener.THROW_TAG, |
| 110 request.getException().getCause().getMessage()); | 117 request.getException().getCause().getMessage()); |
| 111 } | 118 } |
| 112 | 119 |
| 113 @SmallTest | 120 @SmallTest |
| 114 @Feature({"Cronet"}) | 121 @Feature({"Cronet"}) |
| 115 public void testSetUploadDataWithNullContentType() throws Exception { | 122 public void testSetUploadDataWithNullContentType() throws Exception { |
| 116 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL) ; | 123 String[] commandLineArgs = { |
| 124 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.LEGACY}; | |
| 125 CronetTestFramework testFramework = | |
|
mef
2015/11/24 20:38:33
Can this construct be extracted into a method like
xunjieli
2015/11/24 21:28:06
Done.
| |
| 126 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi neArgs); | |
| 117 | 127 |
| 118 HashMap<String, String> headers = new HashMap<String, String>(); | 128 HashMap<String, String> headers = new HashMap<String, String>(); |
| 119 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); | 129 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); |
| 120 | 130 |
| 121 // Create request. | 131 // Create request. |
| 122 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | 132 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 123 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 133 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 124 byte[] uploadData = new byte[] {1, 2, 3}; | 134 byte[] uploadData = new byte[] {1, 2, 3}; |
| 125 try { | 135 try { |
| 126 request.setUploadData(null, uploadData); | 136 request.setUploadData(null, uploadData); |
| 127 fail("setUploadData should throw on null content type"); | 137 fail("setUploadData should throw on null content type"); |
| 128 } catch (NullPointerException e) { | 138 } catch (NullPointerException e) { |
| 129 // Nothing to do here. | 139 // Nothing to do here. |
| 130 } | 140 } |
| 131 } | 141 } |
| 132 | 142 |
| 133 @SmallTest | 143 @SmallTest |
| 134 @Feature({"Cronet"}) | 144 @Feature({"Cronet"}) |
| 135 public void testLegacyLoadUrl() throws Exception { | 145 public void testLegacyLoadUrl() throws Exception { |
| 136 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | 146 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
| 137 builder.enableLegacyMode(true); | 147 builder.enableLegacyMode(true); |
| 138 // TODO(mef) fix tests so that library isn't loaded for legacy stack | |
| 139 | 148 |
| 149 String[] commandLineArgs = { | |
| 150 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.LEGACY}; | |
| 140 CronetTestFramework testFramework = | 151 CronetTestFramework testFramework = |
| 141 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(URL, build er); | 152 new CronetTestFramework(URL, commandLineArgs, getContext(), buil der); |
| 142 | 153 |
| 143 // Make sure that the URL is set as expected. | 154 // Make sure that the URL is set as expected. |
| 144 assertEquals(URL, testFramework.getUrl()); | 155 assertEquals(URL, testFramework.getUrl()); |
| 145 assertEquals(200, testFramework.getHttpStatusCode()); | 156 assertEquals(200, testFramework.getHttpStatusCode()); |
| 146 } | 157 } |
| 147 | 158 |
| 148 @SmallTest | 159 @SmallTest |
| 149 @Feature({"Cronet"}) | 160 @Feature({"Cronet"}) |
| 150 public void testRequestHead() throws Exception { | 161 public void testRequestHead() throws Exception { |
| 151 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL) ; | 162 String[] commandLineArgs = { |
| 163 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.LEGACY}; | |
| 164 CronetTestFramework testFramework = | |
| 165 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi neArgs); | |
| 152 | 166 |
| 153 HashMap<String, String> headers = new HashMap<String, String>(); | 167 HashMap<String, String> headers = new HashMap<String, String>(); |
| 154 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 168 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 155 | 169 |
| 156 // Create request. | 170 // Create request. |
| 157 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | 171 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 158 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 172 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 159 request.setHttpMethod("HEAD"); | 173 request.setHttpMethod("HEAD"); |
| 160 request.start(); | 174 request.start(); |
| 161 listener.blockForComplete(); | 175 listener.blockForComplete(); |
| 162 assertEquals(200, listener.mHttpStatusCode); | 176 assertEquals(200, listener.mHttpStatusCode); |
| 163 // HEAD requests do not get any response data and Content-Length must be | 177 // HEAD requests do not get any response data and Content-Length must be |
| 164 // ignored. | 178 // ignored. |
| 165 assertEquals(0, listener.mResponseAsBytes.length); | 179 assertEquals(0, listener.mResponseAsBytes.length); |
| 166 } | 180 } |
| 167 } | 181 } |
| OLD | NEW |