| 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 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(URL); |
| 28 | 28 |
| 29 // Make sure that the URL is set as expected. | 29 // Make sure that the URL is set as expected. |
| 30 assertEquals(URL, testFramework.getUrl()); | 30 assertEquals(URL, testFramework.getUrl()); |
| 31 assertEquals(200, testFramework.getHttpStatusCode()); | 31 assertEquals(200, testFramework.getHttpStatusCode()); |
| 32 } | 32 } |
| 33 | 33 |
| 34 @SmallTest | 34 @SmallTest |
| 35 @Feature({"Cronet"}) | 35 @Feature({"Cronet"}) |
| 36 public void testInvalidUrl() throws Exception { | 36 public void testInvalidUrl() throws Exception { |
| 37 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl("127
.0.0.1:8000"); | 37 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
("127.0.0.1:8000"); |
| 38 | 38 |
| 39 // The load should fail. | 39 // The load should fail. |
| 40 assertEquals(0, testFramework.getHttpStatusCode()); | 40 assertEquals(0, testFramework.getHttpStatusCode()); |
| 41 } | 41 } |
| 42 | 42 |
| 43 @SmallTest | 43 @SmallTest |
| 44 @Feature({"Cronet"}) | 44 @Feature({"Cronet"}) |
| 45 public void testPostData() throws Exception { | 45 public void testPostData() throws Exception { |
| 46 String[] commandLineArgs = {CronetTestFramework.POST_DATA_KEY, "test"}; | 46 String[] commandLineArgs = {CronetTestFramework.POST_DATA_KEY, "test", |
| 47 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar
yInitType.LEGACY}; |
| 47 CronetTestFramework testFramework = | 48 CronetTestFramework testFramework = |
| 48 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi
neArgs); | 49 startCronetTestFrameworkWithUrlAndCommandLineArgs(URL, commandLi
neArgs); |
| 49 | 50 |
| 50 // Make sure that the URL is set as expected. | 51 // Make sure that the URL is set as expected. |
| 51 assertEquals(URL, testFramework.getUrl()); | 52 assertEquals(URL, testFramework.getUrl()); |
| 52 assertEquals(200, testFramework.getHttpStatusCode()); | 53 assertEquals(200, testFramework.getHttpStatusCode()); |
| 53 } | 54 } |
| 54 | 55 |
| 55 @SmallTest | 56 @SmallTest |
| 56 @Feature({"Cronet"}) | 57 @Feature({"Cronet"}) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 87 | 88 |
| 88 @Override | 89 @Override |
| 89 public void onResponseStarted(HttpUrlRequest request) { | 90 public void onResponseStarted(HttpUrlRequest request) { |
| 90 throw new NullPointerException(THROW_TAG); | 91 throw new NullPointerException(THROW_TAG); |
| 91 } | 92 } |
| 92 } | 93 } |
| 93 | 94 |
| 94 @SmallTest | 95 @SmallTest |
| 95 @Feature({"Cronet"}) | 96 @Feature({"Cronet"}) |
| 96 public void testCalledByNativeException() throws Exception { | 97 public void testCalledByNativeException() throws Exception { |
| 97 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL)
; | 98 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(URL); |
| 98 | 99 |
| 99 HashMap<String, String> headers = new HashMap<String, String>(); | 100 HashMap<String, String> headers = new HashMap<String, String>(); |
| 100 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); | 101 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); |
| 101 | 102 |
| 102 // Create request with bad listener to trigger an exception. | 103 // Create request with bad listener to trigger an exception. |
| 103 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | 104 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 104 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 105 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 105 request.start(); | 106 request.start(); |
| 106 listener.blockForComplete(); | 107 listener.blockForComplete(); |
| 107 assertTrue(request.isCanceled()); | 108 assertTrue(request.isCanceled()); |
| 108 assertNotNull(request.getException()); | 109 assertNotNull(request.getException()); |
| 109 assertEquals(BadHttpUrlRequestListener.THROW_TAG, | 110 assertEquals(BadHttpUrlRequestListener.THROW_TAG, |
| 110 request.getException().getCause().getMessage()); | 111 request.getException().getCause().getMessage()); |
| 111 } | 112 } |
| 112 | 113 |
| 113 @SmallTest | 114 @SmallTest |
| 114 @Feature({"Cronet"}) | 115 @Feature({"Cronet"}) |
| 115 public void testSetUploadDataWithNullContentType() throws Exception { | 116 public void testSetUploadDataWithNullContentType() throws Exception { |
| 116 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL)
; | 117 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(URL); |
| 117 | 118 |
| 118 HashMap<String, String> headers = new HashMap<String, String>(); | 119 HashMap<String, String> headers = new HashMap<String, String>(); |
| 119 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); | 120 BadHttpUrlRequestListener listener = new BadHttpUrlRequestListener(); |
| 120 | 121 |
| 121 // Create request. | 122 // Create request. |
| 122 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | 123 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 123 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 124 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 124 byte[] uploadData = new byte[] {1, 2, 3}; | 125 byte[] uploadData = new byte[] {1, 2, 3}; |
| 125 try { | 126 try { |
| 126 request.setUploadData(null, uploadData); | 127 request.setUploadData(null, uploadData); |
| 127 fail("setUploadData should throw on null content type"); | 128 fail("setUploadData should throw on null content type"); |
| 128 } catch (NullPointerException e) { | 129 } catch (NullPointerException e) { |
| 129 // Nothing to do here. | 130 // Nothing to do here. |
| 130 } | 131 } |
| 131 } | 132 } |
| 132 | 133 |
| 133 @SmallTest | 134 @SmallTest |
| 134 @Feature({"Cronet"}) | 135 @Feature({"Cronet"}) |
| 135 public void testLegacyLoadUrl() throws Exception { | 136 public void testLegacyLoadUrl() throws Exception { |
| 136 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | 137 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
| 137 builder.enableLegacyMode(true); | 138 builder.enableLegacyMode(true); |
| 138 // TODO(mef) fix tests so that library isn't loaded for legacy stack | |
| 139 | 139 |
| 140 CronetTestFramework testFramework = | 140 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(URL); |
| 141 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(URL, build
er); | |
| 142 | 141 |
| 143 // Make sure that the URL is set as expected. | 142 // Make sure that the URL is set as expected. |
| 144 assertEquals(URL, testFramework.getUrl()); | 143 assertEquals(URL, testFramework.getUrl()); |
| 145 assertEquals(200, testFramework.getHttpStatusCode()); | 144 assertEquals(200, testFramework.getHttpStatusCode()); |
| 146 } | 145 } |
| 147 | 146 |
| 148 @SmallTest | 147 @SmallTest |
| 149 @Feature({"Cronet"}) | 148 @Feature({"Cronet"}) |
| 150 public void testRequestHead() throws Exception { | 149 public void testRequestHead() throws Exception { |
| 151 CronetTestFramework testFramework = startCronetTestFrameworkWithUrl(URL)
; | 150 CronetTestFramework testFramework = startCronetTestFrameworkForLegacyApi
(URL); |
| 152 | 151 |
| 153 HashMap<String, String> headers = new HashMap<String, String>(); | 152 HashMap<String, String> headers = new HashMap<String, String>(); |
| 154 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 153 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 155 | 154 |
| 156 // Create request. | 155 // Create request. |
| 157 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( | 156 HttpUrlRequest request = testFramework.mRequestFactory.createRequest( |
| 158 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 157 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 159 request.setHttpMethod("HEAD"); | 158 request.setHttpMethod("HEAD"); |
| 160 request.start(); | 159 request.start(); |
| 161 listener.blockForComplete(); | 160 listener.blockForComplete(); |
| 162 assertEquals(200, listener.mHttpStatusCode); | 161 assertEquals(200, listener.mHttpStatusCode); |
| 163 // HEAD requests do not get any response data and Content-Length must be | 162 // HEAD requests do not get any response data and Content-Length must be |
| 164 // ignored. | 163 // ignored. |
| 165 assertEquals(0, listener.mResponseAsBytes.length); | 164 assertEquals(0, listener.mResponseAsBytes.length); |
| 166 } | 165 } |
| 167 } | 166 } |
| OLD | NEW |