| 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.os.ConditionVariable; | 7 import android.os.ConditionVariable; |
| 8 import android.test.MoreAsserts; |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
| 9 | 10 |
| 10 import org.chromium.base.test.util.Feature; | 11 import org.chromium.base.test.util.Feature; |
| 11 import org.chromium.net.TestUrlRequestCallback.FailureType; | 12 import org.chromium.net.TestUrlRequestCallback.FailureType; |
| 12 import org.chromium.net.TestUrlRequestCallback.ResponseStep; | 13 import org.chromium.net.TestUrlRequestCallback.ResponseStep; |
| 13 import org.chromium.net.test.FailurePhase; | 14 import org.chromium.net.test.FailurePhase; |
| 14 | 15 |
| 15 import java.nio.ByteBuffer; | 16 import java.nio.ByteBuffer; |
| 17 import java.util.AbstractMap; |
| 16 import java.util.ArrayList; | 18 import java.util.ArrayList; |
| 19 import java.util.Arrays; |
| 17 import java.util.List; | 20 import java.util.List; |
| 18 import java.util.Map; | 21 import java.util.Map; |
| 19 import java.util.concurrent.ExecutorService; | 22 import java.util.concurrent.ExecutorService; |
| 20 import java.util.concurrent.Executors; | 23 import java.util.concurrent.Executors; |
| 21 import java.util.regex.Matcher; | 24 import java.util.regex.Matcher; |
| 22 import java.util.regex.Pattern; | 25 import java.util.regex.Pattern; |
| 23 | 26 |
| 24 /** | 27 /** |
| 25 * Test functionality of CronetUrlRequest. | 28 * Test functionality of CronetUrlRequest. |
| 26 */ | 29 */ |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 @SmallTest | 121 @SmallTest |
| 119 @Feature({"Cronet"}) | 122 @Feature({"Cronet"}) |
| 120 public void testSimpleGet() throws Exception { | 123 public void testSimpleGet() throws Exception { |
| 121 String url = NativeTestServer.getEchoMethodURL(); | 124 String url = NativeTestServer.getEchoMethodURL(); |
| 122 TestUrlRequestCallback callback = startAndWaitForComplete(url); | 125 TestUrlRequestCallback callback = startAndWaitForComplete(url); |
| 123 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 126 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 124 // Default method is 'GET'. | 127 // Default method is 'GET'. |
| 125 assertEquals("GET", callback.mResponseAsString); | 128 assertEquals("GET", callback.mResponseAsString); |
| 126 assertEquals(0, callback.mRedirectCount); | 129 assertEquals(0, callback.mRedirectCount); |
| 127 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); | 130 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); |
| 128 assertEquals(String.format("UrlResponseInfo[%s]: urlChain = [%s], httpSt
atus = 200 OK, " | 131 UrlResponseInfo urlResponseInfo = createUrlResponseInfo(new String[] {ur
l}, "OK", 200, 86, |
| 129 + "headers = [Connection=close, Content-Len
gth=3, " | 132 "Connection", "close", "Content-Length", "3", "Content-Type", "t
ext/plain"); |
| 130 + "Content-Type=text/plain], wasCached = fa
lse, " | 133 assertResponseEquals(urlResponseInfo, callback.mResponseInfo); |
| 131 + "negotiatedProtocol = unknown, proxyServe
r= :0, " | |
| 132 + "receivedBytesCount = 86", | |
| 133 url, url), | |
| 134 callback.mResponseInfo.toString()); | |
| 135 checkResponseInfo(callback.mResponseInfo, NativeTestServer.getEchoMethod
URL(), 200, "OK"); | 134 checkResponseInfo(callback.mResponseInfo, NativeTestServer.getEchoMethod
URL(), 200, "OK"); |
| 136 } | 135 } |
| 137 | 136 |
| 137 UrlResponseInfo createUrlResponseInfo( |
| 138 String[] urls, String message, int statusCode, int receivedBytes, St
ring... headers) { |
| 139 ArrayList<Map.Entry<String, String>> headersList = new ArrayList<>(); |
| 140 for (int i = 0; i < headers.length; i += 2) { |
| 141 headersList.add(new AbstractMap.SimpleImmutableEntry<String, String>
( |
| 142 headers[i], headers[i + 1])); |
| 143 } |
| 144 UrlResponseInfo unknown = new UrlResponseInfo( |
| 145 Arrays.asList(urls), statusCode, message, headersList, false, "u
nknown", ":0"); |
| 146 unknown.setReceivedBytesCount(receivedBytes); |
| 147 return unknown; |
| 148 } |
| 149 |
| 150 void assertResponseEquals(UrlResponseInfo expected, UrlResponseInfo actual)
{ |
| 151 assertEquals(expected.getAllHeaders(), actual.getAllHeaders()); |
| 152 assertEquals(expected.getAllHeadersAsList(), actual.getAllHeadersAsList(
)); |
| 153 assertEquals(expected.getHttpStatusCode(), actual.getHttpStatusCode()); |
| 154 assertEquals(expected.getHttpStatusText(), actual.getHttpStatusText()); |
| 155 assertEquals(expected.getUrlChain(), actual.getUrlChain()); |
| 156 assertEquals(expected.getUrl(), actual.getUrl()); |
| 157 // Transferred bytes and proxy server are not supported in pure java |
| 158 if (!(mTestFramework.mCronetEngine instanceof JavaCronetEngine)) { |
| 159 assertEquals(expected.getReceivedBytesCount(), actual.getReceivedByt
esCount()); |
| 160 assertEquals(expected.getProxyServer(), actual.getProxyServer()); |
| 161 // This is a place where behavior intentionally differs between nati
ve and java |
| 162 assertEquals(expected.getNegotiatedProtocol(), actual.getNegotiatedP
rotocol()); |
| 163 } |
| 164 } |
| 165 |
| 138 /** | 166 /** |
| 139 * Tests a redirect by running it step-by-step. Also tests that delaying a | 167 * Tests a redirect by running it step-by-step. Also tests that delaying a |
| 140 * request works as expected. To make sure there are no unexpected pending | 168 * request works as expected. To make sure there are no unexpected pending |
| 141 * messages, does a GET between UrlRequest.Callback callbacks. | 169 * messages, does a GET between UrlRequest.Callback callbacks. |
| 142 */ | 170 */ |
| 143 @SmallTest | 171 @SmallTest |
| 144 @Feature({"Cronet"}) | 172 @Feature({"Cronet"}) |
| 145 public void testRedirectAsync() throws Exception { | 173 public void testRedirectAsync() throws Exception { |
| 146 // Start the request and wait to see the redirect. | 174 // Start the request and wait to see the redirect. |
| 147 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 175 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 148 callback.setAutoAdvance(false); | 176 callback.setAutoAdvance(false); |
| 149 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
RedirectURL(), | 177 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
RedirectURL(), |
| 150 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 178 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 151 UrlRequest urlRequest = builder.build(); | 179 UrlRequest urlRequest = builder.build(); |
| 152 urlRequest.start(); | 180 urlRequest.start(); |
| 153 callback.waitForNextStep(); | 181 callback.waitForNextStep(); |
| 154 | 182 |
| 155 // Check the redirect. | 183 // Check the redirect. |
| 156 assertEquals(ResponseStep.ON_RECEIVED_REDIRECT, callback.mResponseStep); | 184 assertEquals(ResponseStep.ON_RECEIVED_REDIRECT, callback.mResponseStep); |
| 157 assertEquals(1, callback.mRedirectResponseInfoList.size()); | 185 assertEquals(1, callback.mRedirectResponseInfoList.size()); |
| 158 checkResponseInfo(callback.mRedirectResponseInfoList.get(0), | 186 checkResponseInfo(callback.mRedirectResponseInfoList.get(0), |
| 159 NativeTestServer.getRedirectURL(), 302, "Found"); | 187 NativeTestServer.getRedirectURL(), 302, "Found"); |
| 160 assertEquals(1, callback.mRedirectResponseInfoList.get(0).getUrlChain().
size()); | 188 assertEquals(1, callback.mRedirectResponseInfoList.get(0).getUrlChain().
size()); |
| 161 assertEquals(NativeTestServer.getSuccessURL(), callback.mRedirectUrlList
.get(0)); | 189 assertEquals(NativeTestServer.getSuccessURL(), callback.mRedirectUrlList
.get(0)); |
| 162 checkResponseInfoHeader( | 190 checkResponseInfoHeader( |
| 163 callback.mRedirectResponseInfoList.get(0), "redirect-header", "h
eader-value"); | 191 callback.mRedirectResponseInfoList.get(0), "redirect-header", "h
eader-value"); |
| 164 | 192 |
| 165 assertEquals(String.format("UrlResponseInfo[%s]: urlChain = [%s], httpSt
atus = 302 Found, " | 193 UrlResponseInfo expected = |
| 166 + "headers = [Location=/success.txt, " | 194 createUrlResponseInfo(new String[] {NativeTestServer.getRedirect
URL()}, "Found", |
| 167 + "redirect-header=header-value], wasCached
= false, " | 195 302, 74, "Location", "/success.txt", "redirect-header",
"header-value"); |
| 168 + "negotiatedProtocol = unknown, proxyServe
r= :0, " | 196 assertResponseEquals(expected, callback.mRedirectResponseInfoList.get(0)
); |
| 169 + "receivedBytesCount = 74", | |
| 170 NativeTestServer.getRedirectURL(), NativeTestServer
.getRedirectURL()), | |
| 171 callback.mRedirectResponseInfoList.get(0).toString()); | |
| 172 | 197 |
| 173 // Wait for an unrelated request to finish. The request should not | 198 // Wait for an unrelated request to finish. The request should not |
| 174 // advance until followRedirect is invoked. | 199 // advance until followRedirect is invoked. |
| 175 testSimpleGet(); | 200 testSimpleGet(); |
| 176 assertEquals(ResponseStep.ON_RECEIVED_REDIRECT, callback.mResponseStep); | 201 assertEquals(ResponseStep.ON_RECEIVED_REDIRECT, callback.mResponseStep); |
| 177 assertEquals(1, callback.mRedirectResponseInfoList.size()); | 202 assertEquals(1, callback.mRedirectResponseInfoList.size()); |
| 178 | 203 |
| 179 // Follow the redirect and wait for the next set of headers. | 204 // Follow the redirect and wait for the next set of headers. |
| 180 urlRequest.followRedirect(); | 205 urlRequest.followRedirect(); |
| 181 callback.waitForNextStep(); | 206 callback.waitForNextStep(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 206 } | 231 } |
| 207 // Should not receive any messages while waiting for another get, | 232 // Should not receive any messages while waiting for another get, |
| 208 // as the next read has not been started. | 233 // as the next read has not been started. |
| 209 testSimpleGet(); | 234 testSimpleGet(); |
| 210 assertEquals(response, callback.mResponseAsString); | 235 assertEquals(response, callback.mResponseAsString); |
| 211 assertEquals(step, callback.mResponseStep); | 236 assertEquals(step, callback.mResponseStep); |
| 212 } | 237 } |
| 213 assertEquals(ResponseStep.ON_SUCCEEDED, callback.mResponseStep); | 238 assertEquals(ResponseStep.ON_SUCCEEDED, callback.mResponseStep); |
| 214 assertEquals(NativeTestServer.SUCCESS_BODY, callback.mResponseAsString); | 239 assertEquals(NativeTestServer.SUCCESS_BODY, callback.mResponseAsString); |
| 215 | 240 |
| 216 assertEquals(String.format("UrlResponseInfo[%s]: urlChain = [%s, %s], ht
tpStatus = 200 OK, " | 241 UrlResponseInfo urlResponseInfo = createUrlResponseInfo( |
| 217 + "headers = [Content-Type=text/plain, " | 242 new String[] {NativeTestServer.getRedirectURL(), NativeTestServe
r.getSuccessURL()}, |
| 218 + "Access-Control-Allow-Origin=*, header-na
me=header-value, " | 243 "OK", 200, 260, "Content-Type", "text/plain", "Access-Control-Al
low-Origin", "*", |
| 219 + "multi-header-name=header-value1, " | 244 "header-name", "header-value", "multi-header-name", "header-valu
e1", |
| 220 + "multi-header-name=header-value2], wasCac
hed = false, " | 245 "multi-header-name", "header-value2"); |
| 221 + "negotiatedProtocol = unknown, proxyServe
r= :0, " | |
| 222 + "receivedBytesCount = 260", | |
| 223 NativeTestServer.getSuccessURL(), NativeTestServer.
getRedirectURL(), | |
| 224 NativeTestServer.getSuccessURL()), | |
| 225 callback.mResponseInfo.toString()); | |
| 226 | 246 |
| 247 assertResponseEquals(urlResponseInfo, callback.mResponseInfo); |
| 227 // Make sure there are no other pending messages, which would trigger | 248 // Make sure there are no other pending messages, which would trigger |
| 228 // asserts in TestUrlRequestCallback. | 249 // asserts in TestUrlRequestCallback. |
| 229 testSimpleGet(); | 250 testSimpleGet(); |
| 230 } | 251 } |
| 231 | 252 |
| 232 @SmallTest | 253 @SmallTest |
| 233 @Feature({"Cronet"}) | 254 @Feature({"Cronet"}) |
| 234 public void testNotFound() throws Exception { | 255 public void testNotFound() throws Exception { |
| 235 String url = NativeTestServer.getFileURL("/notfound.html"); | 256 String url = NativeTestServer.getFileURL("/notfound.html"); |
| 236 TestUrlRequestCallback callback = startAndWaitForComplete(url); | 257 TestUrlRequestCallback callback = startAndWaitForComplete(url); |
| 237 checkResponseInfo(callback.mResponseInfo, url, 404, "Not Found"); | 258 checkResponseInfo(callback.mResponseInfo, url, 404, "Not Found"); |
| 238 assertEquals("<!DOCTYPE html>\n<html>\n<head>\n<title>Not found</title>\
n" | 259 assertEquals("<!DOCTYPE html>\n<html>\n<head>\n<title>Not found</title>\
n" |
| 239 + "<p>Test page loaded.</p>\n</head>\n</html>\n", | 260 + "<p>Test page loaded.</p>\n</head>\n</html>\n", |
| 240 callback.mResponseAsString); | 261 callback.mResponseAsString); |
| 241 assertEquals(0, callback.mRedirectCount); | 262 assertEquals(0, callback.mRedirectCount); |
| 242 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); | 263 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); |
| 243 } | 264 } |
| 244 | 265 |
| 245 // Checks that UrlRequest.Callback.onFailed is only called once in the case | 266 // Checks that UrlRequest.Callback.onFailed is only called once in the case |
| 246 // of ERR_CONTENT_LENGTH_MISMATCH, which has an unusual failure path. | 267 // of ERR_CONTENT_LENGTH_MISMATCH, which has an unusual failure path. |
| 247 // See http://crbug.com/468803. | 268 // See http://crbug.com/468803. |
| 248 @SmallTest | 269 @SmallTest |
| 249 @Feature({"Cronet"}) | 270 @Feature({"Cronet"}) |
| 271 @OnlyRunNativeCronet // No canonical exception to assert on |
| 250 public void testContentLengthMismatchFailsOnce() throws Exception { | 272 public void testContentLengthMismatchFailsOnce() throws Exception { |
| 251 String url = NativeTestServer.getFileURL( | 273 String url = NativeTestServer.getFileURL( |
| 252 "/content_length_mismatch.html"); | 274 "/content_length_mismatch.html"); |
| 253 TestUrlRequestCallback callback = startAndWaitForComplete(url); | 275 TestUrlRequestCallback callback = startAndWaitForComplete(url); |
| 254 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 276 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 255 // The entire response body will be read before the error is returned. | 277 // The entire response body will be read before the error is returned. |
| 256 // This is because the network stack returns data as it's read from the | 278 // This is because the network stack returns data as it's read from the |
| 257 // socket, and the socket close message which tiggers the error will | 279 // socket, and the socket close message which triggers the error will |
| 258 // only be passed along after all data has been read. | 280 // only be passed along after all data has been read. |
| 259 assertEquals("Response that lies about content length.", callback.mRespo
nseAsString); | 281 assertEquals("Response that lies about content length.", callback.mRespo
nseAsString); |
| 260 assertNotNull(callback.mError); | 282 assertNotNull(callback.mError); |
| 261 assertEquals("Exception in CronetUrlRequest: net::ERR_CONTENT_LENGTH_MIS
MATCH", | 283 assertEquals("Exception in CronetUrlRequest: net::ERR_CONTENT_LENGTH_MIS
MATCH", |
| 262 callback.mError.getMessage()); | 284 callback.mError.getMessage()); |
| 263 // Wait for a couple round trips to make sure there are no pending | 285 // Wait for a couple round trips to make sure there are no pending |
| 264 // onFailed messages. This test relies on checks in | 286 // onFailed messages. This test relies on checks in |
| 265 // TestUrlRequestCallback catching a second onFailed call. | 287 // TestUrlRequestCallback catching a second onFailed call. |
| 266 testSimpleGet(); | 288 testSimpleGet(); |
| 267 } | 289 } |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 assertEquals("header-value2", multiHeader.get(1)); | 449 assertEquals("header-value2", multiHeader.get(1)); |
| 428 } | 450 } |
| 429 | 451 |
| 430 @SmallTest | 452 @SmallTest |
| 431 @Feature({"Cronet"}) | 453 @Feature({"Cronet"}) |
| 432 public void testResponseHeadersList() throws Exception { | 454 public void testResponseHeadersList() throws Exception { |
| 433 TestUrlRequestCallback callback = startAndWaitForComplete(NativeTestServ
er.getSuccessURL()); | 455 TestUrlRequestCallback callback = startAndWaitForComplete(NativeTestServ
er.getSuccessURL()); |
| 434 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 456 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 435 List<Map.Entry<String, String>> responseHeaders = | 457 List<Map.Entry<String, String>> responseHeaders = |
| 436 callback.mResponseInfo.getAllHeadersAsList(); | 458 callback.mResponseInfo.getAllHeadersAsList(); |
| 437 assertEquals(5, responseHeaders.size()); | 459 |
| 438 assertEquals("Content-Type", responseHeaders.get(0).getKey()); | 460 MoreAsserts.assertContentsInOrder(responseHeaders, |
| 439 assertEquals("text/plain", responseHeaders.get(0).getValue()); | 461 new AbstractMap.SimpleEntry<>("Content-Type", "text/plain"), |
| 440 assertEquals("Access-Control-Allow-Origin", responseHeaders.get(1).getKe
y()); | 462 new AbstractMap.SimpleEntry<>("Access-Control-Allow-Origin", "*"
), |
| 441 assertEquals("*", responseHeaders.get(1).getValue()); | 463 new AbstractMap.SimpleEntry<>("header-name", "header-value"), |
| 442 assertEquals("header-name", responseHeaders.get(2).getKey()); | 464 new AbstractMap.SimpleEntry<>("multi-header-name", "header-value
1"), |
| 443 assertEquals("header-value", responseHeaders.get(2).getValue()); | 465 new AbstractMap.SimpleEntry<>("multi-header-name", "header-value
2")); |
| 444 assertEquals("multi-header-name", responseHeaders.get(3).getKey()); | |
| 445 assertEquals("header-value1", responseHeaders.get(3).getValue()); | |
| 446 assertEquals("multi-header-name", responseHeaders.get(4).getKey()); | |
| 447 assertEquals("header-value2", responseHeaders.get(4).getValue()); | |
| 448 } | 466 } |
| 449 | 467 |
| 450 @SmallTest | 468 @SmallTest |
| 451 @Feature({"Cronet"}) | 469 @Feature({"Cronet"}) |
| 452 public void testMockMultiRedirect() throws Exception { | 470 public void testMockMultiRedirect() throws Exception { |
| 453 TestUrlRequestCallback callback = | 471 TestUrlRequestCallback callback = |
| 454 startAndWaitForComplete(NativeTestServer.getMultiRedirectURL()); | 472 startAndWaitForComplete(NativeTestServer.getMultiRedirectURL()); |
| 455 UrlResponseInfo mResponseInfo = callback.mResponseInfo; | 473 UrlResponseInfo mResponseInfo = callback.mResponseInfo; |
| 456 assertEquals(2, callback.mRedirectCount); | 474 assertEquals(2, callback.mRedirectCount); |
| 457 assertEquals(200, mResponseInfo.getHttpStatusCode()); | 475 assertEquals(200, mResponseInfo.getHttpStatusCode()); |
| 458 assertEquals(2, callback.mRedirectResponseInfoList.size()); | 476 assertEquals(2, callback.mRedirectResponseInfoList.size()); |
| 459 | 477 |
| 460 // Check first redirect (multiredirect.html -> redirect.html) | 478 // Check first redirect (multiredirect.html -> redirect.html) |
| 479 UrlResponseInfo firstExpectedResponseInfo = createUrlResponseInfo( |
| 480 new String[] {NativeTestServer.getMultiRedirectURL()}, "Found",
302, 77, "Location", |
| 481 "/redirect.html", "redirect-header0", "header-value"); |
| 461 UrlResponseInfo firstRedirectResponseInfo = callback.mRedirectResponseIn
foList.get(0); | 482 UrlResponseInfo firstRedirectResponseInfo = callback.mRedirectResponseIn
foList.get(0); |
| 462 assertEquals(1, firstRedirectResponseInfo.getUrlChain().size()); | 483 assertResponseEquals(firstExpectedResponseInfo, firstRedirectResponseInf
o); |
| 463 assertEquals(NativeTestServer.getMultiRedirectURL(), | |
| 464 firstRedirectResponseInfo.getUrlChain().get(0)); | |
| 465 checkResponseInfo( | |
| 466 firstRedirectResponseInfo, NativeTestServer.getMultiRedirectURL(
), 302, "Found"); | |
| 467 checkResponseInfoHeader(firstRedirectResponseInfo, | |
| 468 "redirect-header0", "header-value"); | |
| 469 assertEquals(77, firstRedirectResponseInfo.getReceivedBytesCount()); | |
| 470 | 484 |
| 471 // Check second redirect (redirect.html -> success.txt) | 485 // Check second redirect (redirect.html -> success.txt) |
| 472 UrlResponseInfo secondRedirectResponseInfo = callback.mRedirectResponseI
nfoList.get(1); | 486 UrlResponseInfo secondExpectedResponseInfo = createUrlResponseInfo( |
| 473 assertEquals(2, secondRedirectResponseInfo.getUrlChain().size()); | 487 new String[] {NativeTestServer.getMultiRedirectURL(), |
| 474 assertEquals(NativeTestServer.getMultiRedirectURL(), | 488 NativeTestServer.getRedirectURL(), NativeTestServer.getS
uccessURL()}, |
| 475 secondRedirectResponseInfo.getUrlChain().get(0)); | 489 "OK", 200, 337, "Content-Type", "text/plain", "Access-Control-Al
low-Origin", "*", |
| 476 assertEquals( | 490 "header-name", "header-value", "multi-header-name", "header-valu
e1", |
| 477 NativeTestServer.getRedirectURL(), secondRedirectResponseInfo.ge
tUrlChain().get(1)); | 491 "multi-header-name", "header-value2"); |
| 478 checkResponseInfo( | |
| 479 secondRedirectResponseInfo, NativeTestServer.getRedirectURL(), 3
02, "Found"); | |
| 480 checkResponseInfoHeader(secondRedirectResponseInfo, | |
| 481 "redirect-header", "header-value"); | |
| 482 assertEquals(151, secondRedirectResponseInfo.getReceivedBytesCount()); | |
| 483 | 492 |
| 484 // Check final response (success.txt). | 493 assertResponseEquals(secondExpectedResponseInfo, mResponseInfo); |
| 485 assertEquals(NativeTestServer.getSuccessURL(), mResponseInfo.getUrl()); | |
| 486 assertEquals(3, mResponseInfo.getUrlChain().size()); | |
| 487 assertEquals(NativeTestServer.getMultiRedirectURL(), mResponseInfo.getUr
lChain().get(0)); | |
| 488 assertEquals(NativeTestServer.getRedirectURL(), mResponseInfo.getUrlChai
n().get(1)); | |
| 489 assertEquals(NativeTestServer.getSuccessURL(), mResponseInfo.getUrlChain
().get(2)); | |
| 490 assertTrue(callback.mHttpResponseDataLength != 0); | 494 assertTrue(callback.mHttpResponseDataLength != 0); |
| 491 assertEquals(2, callback.mRedirectCount); | 495 assertEquals(2, callback.mRedirectCount); |
| 492 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); | 496 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); |
| 493 assertEquals(337, mResponseInfo.getReceivedBytesCount()); | |
| 494 } | 497 } |
| 495 | 498 |
| 496 @SmallTest | 499 @SmallTest |
| 497 @Feature({"Cronet"}) | 500 @Feature({"Cronet"}) |
| 498 public void testMockNotFound() throws Exception { | 501 public void testMockNotFound() throws Exception { |
| 499 TestUrlRequestCallback callback = | 502 TestUrlRequestCallback callback = |
| 500 startAndWaitForComplete(NativeTestServer.getNotFoundURL()); | 503 startAndWaitForComplete(NativeTestServer.getNotFoundURL()); |
| 501 assertEquals(404, callback.mResponseInfo.getHttpStatusCode()); | 504 UrlResponseInfo expected = createUrlResponseInfo( |
| 502 assertEquals(121, callback.mResponseInfo.getReceivedBytesCount()); | 505 new String[] {NativeTestServer.getNotFoundURL()}, "Not Found", 4
04, 121); |
| 506 assertResponseEquals(expected, callback.mResponseInfo); |
| 503 assertTrue(callback.mHttpResponseDataLength != 0); | 507 assertTrue(callback.mHttpResponseDataLength != 0); |
| 504 assertEquals(0, callback.mRedirectCount); | 508 assertEquals(0, callback.mRedirectCount); |
| 505 assertFalse(callback.mOnErrorCalled); | 509 assertFalse(callback.mOnErrorCalled); |
| 506 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); | 510 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); |
| 507 } | 511 } |
| 508 | 512 |
| 509 @SmallTest | 513 @SmallTest |
| 510 @Feature({"Cronet"}) | 514 @Feature({"Cronet"}) |
| 515 @OnlyRunNativeCronet // No canonical exception to assert on |
| 511 public void testMockStartAsyncError() throws Exception { | 516 public void testMockStartAsyncError() throws Exception { |
| 512 final int arbitraryNetError = -3; | 517 final int arbitraryNetError = -3; |
| 513 TestUrlRequestCallback callback = | 518 TestUrlRequestCallback callback = |
| 514 startAndWaitForComplete(MockUrlRequestJobFactory.getMockUrlWithF
ailure( | 519 startAndWaitForComplete(MockUrlRequestJobFactory.getMockUrlWithF
ailure( |
| 515 FailurePhase.START, arbitraryNetError)); | 520 FailurePhase.START, arbitraryNetError)); |
| 516 assertNull(callback.mResponseInfo); | 521 assertNull(callback.mResponseInfo); |
| 517 assertNotNull(callback.mError); | 522 assertNotNull(callback.mError); |
| 518 assertEquals(arbitraryNetError, callback.mError.netError()); | 523 assertEquals(arbitraryNetError, callback.mError.netError()); |
| 519 assertEquals(0, callback.mRedirectCount); | 524 assertEquals(0, callback.mRedirectCount); |
| 520 assertTrue(callback.mOnErrorCalled); | 525 assertTrue(callback.mOnErrorCalled); |
| 521 assertEquals(callback.mResponseStep, ResponseStep.NOTHING); | 526 assertEquals(callback.mResponseStep, ResponseStep.NOTHING); |
| 522 } | 527 } |
| 523 | 528 |
| 524 @SmallTest | 529 @SmallTest |
| 525 @Feature({"Cronet"}) | 530 @Feature({"Cronet"}) |
| 531 @OnlyRunNativeCronet // No canonical exception to assert on |
| 526 public void testMockReadDataSyncError() throws Exception { | 532 public void testMockReadDataSyncError() throws Exception { |
| 527 final int arbitraryNetError = -4; | 533 final int arbitraryNetError = -4; |
| 528 TestUrlRequestCallback callback = | 534 TestUrlRequestCallback callback = |
| 529 startAndWaitForComplete(MockUrlRequestJobFactory.getMockUrlWithF
ailure( | 535 startAndWaitForComplete(MockUrlRequestJobFactory.getMockUrlWithF
ailure( |
| 530 FailurePhase.READ_SYNC, arbitraryNetError)); | 536 FailurePhase.READ_SYNC, arbitraryNetError)); |
| 531 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 537 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 532 assertEquals(0, callback.mResponseInfo.getReceivedBytesCount()); | 538 assertEquals(0, callback.mResponseInfo.getReceivedBytesCount()); |
| 533 assertNotNull(callback.mError); | 539 assertNotNull(callback.mError); |
| 534 assertEquals(arbitraryNetError, callback.mError.netError()); | 540 assertEquals(arbitraryNetError, callback.mError.netError()); |
| 535 assertEquals(0, callback.mRedirectCount); | 541 assertEquals(0, callback.mRedirectCount); |
| 536 assertTrue(callback.mOnErrorCalled); | 542 assertTrue(callback.mOnErrorCalled); |
| 537 assertEquals(callback.mResponseStep, ResponseStep.ON_RESPONSE_STARTED); | 543 assertEquals(callback.mResponseStep, ResponseStep.ON_RESPONSE_STARTED); |
| 538 } | 544 } |
| 539 | 545 |
| 540 @SmallTest | 546 @SmallTest |
| 541 @Feature({"Cronet"}) | 547 @Feature({"Cronet"}) |
| 548 @OnlyRunNativeCronet // No canonical exception to assert on |
| 542 public void testMockReadDataAsyncError() throws Exception { | 549 public void testMockReadDataAsyncError() throws Exception { |
| 543 final int arbitraryNetError = -5; | 550 final int arbitraryNetError = -5; |
| 544 TestUrlRequestCallback callback = | 551 TestUrlRequestCallback callback = |
| 545 startAndWaitForComplete(MockUrlRequestJobFactory.getMockUrlWithF
ailure( | 552 startAndWaitForComplete(MockUrlRequestJobFactory.getMockUrlWithF
ailure( |
| 546 FailurePhase.READ_ASYNC, arbitraryNetError)); | 553 FailurePhase.READ_ASYNC, arbitraryNetError)); |
| 547 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 554 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 548 assertEquals(0, callback.mResponseInfo.getReceivedBytesCount()); | 555 assertEquals(0, callback.mResponseInfo.getReceivedBytesCount()); |
| 549 assertNotNull(callback.mError); | 556 assertNotNull(callback.mError); |
| 550 assertEquals(arbitraryNetError, callback.mError.netError()); | 557 assertEquals(arbitraryNetError, callback.mError.netError()); |
| 551 assertEquals(0, callback.mRedirectCount); | 558 assertEquals(0, callback.mRedirectCount); |
| 552 assertTrue(callback.mOnErrorCalled); | 559 assertTrue(callback.mOnErrorCalled); |
| 553 assertEquals(callback.mResponseStep, ResponseStep.ON_RESPONSE_STARTED); | 560 assertEquals(callback.mResponseStep, ResponseStep.ON_RESPONSE_STARTED); |
| 554 } | 561 } |
| 555 | 562 |
| 556 /** | 563 /** |
| 557 * Tests that an SSL cert error will be reported via {@link UrlRequest#onFai
led}. | 564 * Tests that an SSL cert error will be reported via {@link UrlRequest#onFai
led}. |
| 558 */ | 565 */ |
| 559 @SmallTest | 566 @SmallTest |
| 560 @Feature({"Cronet"}) | 567 @Feature({"Cronet"}) |
| 568 @OnlyRunNativeCronet // No canonical exception to use yet |
| 561 public void testMockSSLCertificateError() throws Exception { | 569 public void testMockSSLCertificateError() throws Exception { |
| 562 TestUrlRequestCallback callback = startAndWaitForComplete( | 570 TestUrlRequestCallback callback = startAndWaitForComplete( |
| 563 MockUrlRequestJobFactory.getMockUrlForSSLCertificateError()); | 571 MockUrlRequestJobFactory.getMockUrlForSSLCertificateError()); |
| 564 assertNull(callback.mResponseInfo); | 572 assertNull(callback.mResponseInfo); |
| 565 assertNotNull(callback.mError); | 573 assertNotNull(callback.mError); |
| 566 assertTrue(callback.mOnErrorCalled); | 574 assertTrue(callback.mOnErrorCalled); |
| 567 assertEquals(-201, callback.mError.netError()); | 575 assertEquals(-201, callback.mError.netError()); |
| 568 assertEquals("Exception in CronetUrlRequest: net::ERR_CERT_DATE_INVALID"
, | 576 assertEquals("Exception in CronetUrlRequest: net::ERR_CERT_DATE_INVALID"
, |
| 569 callback.mError.getMessage()); | 577 callback.mError.getMessage()); |
| 570 assertEquals(callback.mResponseStep, ResponseStep.NOTHING); | 578 assertEquals(callback.mResponseStep, ResponseStep.NOTHING); |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 795 final UrlRequest urlRequest = | 803 final UrlRequest urlRequest = |
| 796 new UrlRequest.Builder(NativeTestServer.getRedirectURL(), callba
ck, | 804 new UrlRequest.Builder(NativeTestServer.getRedirectURL(), callba
ck, |
| 797 callback.getExecutor(), mTestFramework.mCr
onetEngine) | 805 callback.getExecutor(), mTestFramework.mCr
onetEngine) |
| 798 .build(); | 806 .build(); |
| 799 | 807 |
| 800 // Try to read before starting request. | 808 // Try to read before starting request. |
| 801 try { | 809 try { |
| 802 callback.startNextRead(urlRequest); | 810 callback.startNextRead(urlRequest); |
| 803 fail("Exception not thrown"); | 811 fail("Exception not thrown"); |
| 804 } catch (IllegalStateException e) { | 812 } catch (IllegalStateException e) { |
| 805 assertEquals("Unexpected read attempt.", | |
| 806 e.getMessage()); | |
| 807 } | 813 } |
| 808 | 814 |
| 809 // Verify reading right after start throws an assertion. Both must be | 815 // Verify reading right after start throws an assertion. Both must be |
| 810 // invoked on the Executor thread, to prevent receiving data until after | 816 // invoked on the Executor thread, to prevent receiving data until after |
| 811 // startNextRead has been invoked. | 817 // startNextRead has been invoked. |
| 812 Runnable startAndRead = new Runnable() { | 818 Runnable startAndRead = new Runnable() { |
| 813 @Override | 819 @Override |
| 814 public void run() { | 820 public void run() { |
| 815 urlRequest.start(); | 821 urlRequest.start(); |
| 816 try { | 822 try { |
| 817 callback.startNextRead(urlRequest); | 823 callback.startNextRead(urlRequest); |
| 818 fail("Exception not thrown"); | 824 fail("Exception not thrown"); |
| 819 } catch (IllegalStateException e) { | 825 } catch (IllegalStateException e) { |
| 820 assertEquals("Unexpected read attempt.", | |
| 821 e.getMessage()); | |
| 822 } | 826 } |
| 823 } | 827 } |
| 824 }; | 828 }; |
| 825 callback.getExecutor().execute(startAndRead); | 829 callback.getExecutor().execute(startAndRead); |
| 826 callback.waitForNextStep(); | 830 callback.waitForNextStep(); |
| 827 | 831 |
| 828 assertEquals(callback.mResponseStep, ResponseStep.ON_RECEIVED_REDIRECT); | 832 assertEquals(callback.mResponseStep, ResponseStep.ON_RECEIVED_REDIRECT); |
| 829 // Try to read after the redirect. | 833 // Try to read after the redirect. |
| 830 try { | 834 try { |
| 831 callback.startNextRead(urlRequest); | 835 callback.startNextRead(urlRequest); |
| 832 fail("Exception not thrown"); | 836 fail("Exception not thrown"); |
| 833 } catch (IllegalStateException e) { | 837 } catch (IllegalStateException e) { |
| 834 assertEquals("Unexpected read attempt.", | |
| 835 e.getMessage()); | |
| 836 } | 838 } |
| 837 urlRequest.followRedirect(); | 839 urlRequest.followRedirect(); |
| 838 callback.waitForNextStep(); | 840 callback.waitForNextStep(); |
| 839 | 841 |
| 840 assertEquals(callback.mResponseStep, ResponseStep.ON_RESPONSE_STARTED); | 842 assertEquals(callback.mResponseStep, ResponseStep.ON_RESPONSE_STARTED); |
| 841 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 843 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 842 | 844 |
| 843 while (!callback.isDone()) { | 845 while (!callback.isDone()) { |
| 844 Runnable readTwice = new Runnable() { | 846 Runnable readTwice = new Runnable() { |
| 845 @Override | 847 @Override |
| 846 public void run() { | 848 public void run() { |
| 847 callback.startNextRead(urlRequest); | 849 callback.startNextRead(urlRequest); |
| 848 // Try to read again before the last read completes. | 850 // Try to read again before the last read completes. |
| 849 try { | 851 try { |
| 850 callback.startNextRead(urlRequest); | 852 callback.startNextRead(urlRequest); |
| 851 fail("Exception not thrown"); | 853 fail("Exception not thrown"); |
| 852 } catch (IllegalStateException e) { | 854 } catch (IllegalStateException e) { |
| 853 assertEquals("Unexpected read attempt.", | |
| 854 e.getMessage()); | |
| 855 } | 855 } |
| 856 } | 856 } |
| 857 }; | 857 }; |
| 858 callback.getExecutor().execute(readTwice); | 858 callback.getExecutor().execute(readTwice); |
| 859 callback.waitForNextStep(); | 859 callback.waitForNextStep(); |
| 860 } | 860 } |
| 861 | 861 |
| 862 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); | 862 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); |
| 863 assertEquals(NativeTestServer.SUCCESS_BODY, callback.mResponseAsString); | 863 assertEquals(NativeTestServer.SUCCESS_BODY, callback.mResponseAsString); |
| 864 | 864 |
| 865 // Try to read after request is complete. | 865 // Try to read after request is complete. |
| 866 try { | 866 try { |
| 867 callback.startNextRead(urlRequest); | 867 callback.startNextRead(urlRequest); |
| 868 fail("Exception not thrown"); | 868 fail("Exception not thrown"); |
| 869 } catch (IllegalStateException e) { | 869 } catch (IllegalStateException e) { |
| 870 assertEquals("Unexpected read attempt.", | |
| 871 e.getMessage()); | |
| 872 } | 870 } |
| 873 } | 871 } |
| 874 | 872 |
| 875 @SmallTest | 873 @SmallTest |
| 876 @Feature({"Cronet"}) | 874 @Feature({"Cronet"}) |
| 877 public void testUnexpectedFollowRedirects() throws Exception { | 875 public void testUnexpectedFollowRedirects() throws Exception { |
| 878 final TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 876 final TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 879 callback.setAutoAdvance(false); | 877 callback.setAutoAdvance(false); |
| 880 final UrlRequest urlRequest = | 878 final UrlRequest urlRequest = |
| 881 new UrlRequest.Builder(NativeTestServer.getRedirectURL(), callba
ck, | 879 new UrlRequest.Builder(NativeTestServer.getRedirectURL(), callba
ck, |
| 882 callback.getExecutor(), mTestFramework.mCr
onetEngine) | 880 callback.getExecutor(), mTestFramework.mCr
onetEngine) |
| 883 .build(); | 881 .build(); |
| 884 | 882 |
| 885 // Try to follow a redirect before starting the request. | 883 // Try to follow a redirect before starting the request. |
| 886 try { | 884 try { |
| 887 urlRequest.followRedirect(); | 885 urlRequest.followRedirect(); |
| 888 fail("Exception not thrown"); | 886 fail("Exception not thrown"); |
| 889 } catch (IllegalStateException e) { | 887 } catch (IllegalStateException e) { |
| 890 assertEquals("No redirect to follow.", | |
| 891 e.getMessage()); | |
| 892 } | 888 } |
| 893 | 889 |
| 894 // Try to follow a redirect just after starting the request. Has to be | 890 // Try to follow a redirect just after starting the request. Has to be |
| 895 // done on the executor thread to avoid a race. | 891 // done on the executor thread to avoid a race. |
| 896 Runnable startAndRead = new Runnable() { | 892 Runnable startAndRead = new Runnable() { |
| 897 @Override | 893 @Override |
| 898 public void run() { | 894 public void run() { |
| 899 urlRequest.start(); | 895 urlRequest.start(); |
| 900 try { | 896 try { |
| 901 urlRequest.followRedirect(); | 897 urlRequest.followRedirect(); |
| 902 fail("Exception not thrown"); | 898 fail("Exception not thrown"); |
| 903 } catch (IllegalStateException e) { | 899 } catch (IllegalStateException e) { |
| 904 assertEquals("No redirect to follow.", | |
| 905 e.getMessage()); | |
| 906 } | 900 } |
| 907 } | 901 } |
| 908 }; | 902 }; |
| 909 callback.getExecutor().execute(startAndRead); | 903 callback.getExecutor().execute(startAndRead); |
| 910 callback.waitForNextStep(); | 904 callback.waitForNextStep(); |
| 911 | 905 |
| 912 assertEquals(callback.mResponseStep, ResponseStep.ON_RECEIVED_REDIRECT); | 906 assertEquals(callback.mResponseStep, ResponseStep.ON_RECEIVED_REDIRECT); |
| 913 // Try to follow the redirect twice. Second attempt should fail. | 907 // Try to follow the redirect twice. Second attempt should fail. |
| 914 Runnable followRedirectTwice = new Runnable() { | 908 Runnable followRedirectTwice = new Runnable() { |
| 915 @Override | 909 @Override |
| 916 public void run() { | 910 public void run() { |
| 917 urlRequest.followRedirect(); | 911 urlRequest.followRedirect(); |
| 918 try { | 912 try { |
| 919 urlRequest.followRedirect(); | 913 urlRequest.followRedirect(); |
| 920 fail("Exception not thrown"); | 914 fail("Exception not thrown"); |
| 921 } catch (IllegalStateException e) { | 915 } catch (IllegalStateException e) { |
| 922 assertEquals("No redirect to follow.", | |
| 923 e.getMessage()); | |
| 924 } | 916 } |
| 925 } | 917 } |
| 926 }; | 918 }; |
| 927 callback.getExecutor().execute(followRedirectTwice); | 919 callback.getExecutor().execute(followRedirectTwice); |
| 928 callback.waitForNextStep(); | 920 callback.waitForNextStep(); |
| 929 | 921 |
| 930 assertEquals(callback.mResponseStep, ResponseStep.ON_RESPONSE_STARTED); | 922 assertEquals(callback.mResponseStep, ResponseStep.ON_RESPONSE_STARTED); |
| 931 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 923 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 932 | 924 |
| 933 while (!callback.isDone()) { | 925 while (!callback.isDone()) { |
| 934 try { | 926 try { |
| 935 urlRequest.followRedirect(); | 927 urlRequest.followRedirect(); |
| 936 fail("Exception not thrown"); | 928 fail("Exception not thrown"); |
| 937 } catch (IllegalStateException e) { | 929 } catch (IllegalStateException e) { |
| 938 assertEquals("No redirect to follow.", | |
| 939 e.getMessage()); | |
| 940 } | 930 } |
| 941 callback.startNextRead(urlRequest); | 931 callback.startNextRead(urlRequest); |
| 942 callback.waitForNextStep(); | 932 callback.waitForNextStep(); |
| 943 } | 933 } |
| 944 | 934 |
| 945 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); | 935 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); |
| 946 assertEquals(NativeTestServer.SUCCESS_BODY, callback.mResponseAsString); | 936 assertEquals(NativeTestServer.SUCCESS_BODY, callback.mResponseAsString); |
| 947 | 937 |
| 948 // Try to follow redirect after request is complete. | 938 // Try to follow redirect after request is complete. |
| 949 try { | 939 try { |
| 950 urlRequest.followRedirect(); | 940 urlRequest.followRedirect(); |
| 951 fail("Exception not thrown"); | 941 fail("Exception not thrown"); |
| 952 } catch (IllegalStateException e) { | 942 } catch (IllegalStateException e) { |
| 953 assertEquals("No redirect to follow.", | |
| 954 e.getMessage()); | |
| 955 } | 943 } |
| 956 } | 944 } |
| 957 | 945 |
| 958 @SmallTest | 946 @SmallTest |
| 959 @Feature({"Cronet"}) | 947 @Feature({"Cronet"}) |
| 960 public void testUploadSetDataProvider() throws Exception { | 948 public void testUploadSetDataProvider() throws Exception { |
| 961 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 949 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 962 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), | 950 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), |
| 963 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 951 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 964 | 952 |
| 965 try { | 953 try { |
| 966 builder.setUploadDataProvider(null, callback.getExecutor()); | 954 builder.setUploadDataProvider(null, callback.getExecutor()); |
| 967 fail("Exception not thrown"); | 955 fail("Exception not thrown"); |
| 968 } catch (NullPointerException e) { | 956 } catch (NullPointerException e) { |
| 969 assertEquals("Invalid UploadDataProvider.", e.getMessage()); | 957 assertEquals("Invalid UploadDataProvider.", e.getMessage()); |
| 970 } | 958 } |
| 971 | 959 |
| 972 TestUploadDataProvider dataProvider = new TestUploadDataProvider( | 960 TestUploadDataProvider dataProvider = new TestUploadDataProvider( |
| 973 TestUploadDataProvider.SuccessCallbackMode.SYNC, callback.getExe
cutor()); | 961 TestUploadDataProvider.SuccessCallbackMode.SYNC, callback.getExe
cutor()); |
| 974 builder.setUploadDataProvider(dataProvider, callback.getExecutor()); | 962 builder.setUploadDataProvider(dataProvider, callback.getExecutor()); |
| 975 try { | 963 try { |
| 976 builder.build().start(); | 964 builder.build().start(); |
| 977 fail("Exception not thrown"); | 965 fail("Exception not thrown"); |
| 978 } catch (IllegalArgumentException e) { | 966 } catch (IllegalArgumentException e) { |
| 979 assertEquals("Requests with upload data must have a Content-Type.",
e.getMessage()); | |
| 980 } | 967 } |
| 981 } | 968 } |
| 982 | 969 |
| 983 @SmallTest | 970 @SmallTest |
| 984 @Feature({"Cronet"}) | 971 @Feature({"Cronet"}) |
| 985 public void testUploadEmptyBodySync() throws Exception { | 972 public void testUploadEmptyBodySync() throws Exception { |
| 986 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 973 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 987 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), | 974 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), |
| 988 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 975 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 989 | 976 |
| (...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1369 | 1356 |
| 1370 // 2 read call for the first two data chunks, and 1 for final chunk. | 1357 // 2 read call for the first two data chunks, and 1 for final chunk. |
| 1371 assertEquals(3, dataProvider.getNumReadCalls()); | 1358 assertEquals(3, dataProvider.getNumReadCalls()); |
| 1372 assertEquals("hello there!", callback.mResponseAsString); | 1359 assertEquals("hello there!", callback.mResponseAsString); |
| 1373 } | 1360 } |
| 1374 | 1361 |
| 1375 // Test where an upload fails without ever initializing the | 1362 // Test where an upload fails without ever initializing the |
| 1376 // UploadDataStream, because it can't connect to the server. | 1363 // UploadDataStream, because it can't connect to the server. |
| 1377 @SmallTest | 1364 @SmallTest |
| 1378 @Feature({"Cronet"}) | 1365 @Feature({"Cronet"}) |
| 1366 @OnlyRunNativeCronet // No canonical exception to assert on |
| 1379 public void testUploadFailsWithoutInitializingStream() throws Exception { | 1367 public void testUploadFailsWithoutInitializingStream() throws Exception { |
| 1380 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1368 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1381 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), | 1369 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), |
| 1382 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 1370 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 1383 // Shut down the test server, so connecting to it fails. Note that | 1371 // Shut down the test server, so connecting to it fails. Note that |
| 1384 // calling shutdown again during teardown is safe. | 1372 // calling shutdown again during teardown is safe. |
| 1385 NativeTestServer.shutdownNativeTestServer(); | 1373 NativeTestServer.shutdownNativeTestServer(); |
| 1386 | 1374 |
| 1387 TestUploadDataProvider dataProvider = new TestUploadDataProvider( | 1375 TestUploadDataProvider dataProvider = new TestUploadDataProvider( |
| 1388 TestUploadDataProvider.SuccessCallbackMode.SYNC, callback.getExe
cutor()); | 1376 TestUploadDataProvider.SuccessCallbackMode.SYNC, callback.getExe
cutor()); |
| 1389 dataProvider.addRead("test".getBytes()); | 1377 dataProvider.addRead("test".getBytes()); |
| 1390 builder.setUploadDataProvider(dataProvider, callback.getExecutor()); | 1378 builder.setUploadDataProvider(dataProvider, callback.getExecutor()); |
| 1391 builder.addHeader("Content-Type", "useless/string"); | 1379 builder.addHeader("Content-Type", "useless/string"); |
| 1392 builder.build().start(); | 1380 builder.build().start(); |
| 1393 callback.blockForDone(); | 1381 callback.blockForDone(); |
| 1394 | 1382 |
| 1395 assertNull(callback.mResponseInfo); | 1383 assertNull(callback.mResponseInfo); |
| 1396 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED
", | 1384 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED
", |
| 1397 callback.mError.getMessage()); | 1385 callback.mError.getMessage()); |
| 1398 } | 1386 } |
| 1399 | 1387 |
| 1400 private void throwOrCancel(FailureType failureType, ResponseStep failureStep
, | 1388 private void throwOrCancel(FailureType failureType, ResponseStep failureStep
, |
| 1401 boolean expectResponseInfo, boolean expectError) { | 1389 boolean expectResponseInfo, boolean expectError) { |
| 1390 System.out.println("Testing " + failureType + " during " + failureStep); |
| 1402 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1391 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1403 callback.setFailure(failureType, failureStep); | 1392 callback.setFailure(failureType, failureStep); |
| 1404 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
RedirectURL(), | 1393 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
RedirectURL(), |
| 1405 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 1394 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 1406 UrlRequest urlRequest = builder.build(); | 1395 UrlRequest urlRequest = builder.build(); |
| 1407 urlRequest.start(); | 1396 urlRequest.start(); |
| 1408 callback.blockForDone(); | 1397 callback.blockForDone(); |
| 1409 assertEquals(1, callback.mRedirectCount); | 1398 assertEquals(1, callback.mRedirectCount); |
| 1410 assertEquals(callback.mResponseStep, failureStep); | |
| 1411 assertTrue(urlRequest.isDone()); | 1399 assertTrue(urlRequest.isDone()); |
| 1412 assertEquals(expectResponseInfo, callback.mResponseInfo != null); | 1400 assertEquals(expectResponseInfo, callback.mResponseInfo != null); |
| 1413 assertEquals(expectError, callback.mError != null); | 1401 assertEquals(expectError, callback.mError != null); |
| 1414 assertEquals(expectError, callback.mOnErrorCalled); | 1402 assertEquals(expectError, callback.mOnErrorCalled); |
| 1415 assertEquals(failureType == FailureType.CANCEL_SYNC | 1403 assertEquals(failureType == FailureType.CANCEL_SYNC |
| 1416 || failureType == FailureType.CANCEL_ASYNC | 1404 || failureType == FailureType.CANCEL_ASYNC |
| 1417 || failureType == FailureType.CANCEL_ASYNC_WITHOUT_PAUSE
, | 1405 || failureType == FailureType.CANCEL_ASYNC_WITHOUT_PAUSE
, |
| 1418 callback.mOnCanceledCalled); | 1406 callback.mOnCanceledCalled); |
| 1419 } | 1407 } |
| 1420 | 1408 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1462 assertEquals(1, callback.mRedirectCount); | 1450 assertEquals(1, callback.mRedirectCount); |
| 1463 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); | 1451 assertEquals(callback.mResponseStep, ResponseStep.ON_SUCCEEDED); |
| 1464 assertTrue(urlRequest.isDone()); | 1452 assertTrue(urlRequest.isDone()); |
| 1465 assertNotNull(callback.mResponseInfo); | 1453 assertNotNull(callback.mResponseInfo); |
| 1466 assertNull(callback.mError); | 1454 assertNull(callback.mError); |
| 1467 assertFalse(callback.mOnErrorCalled); | 1455 assertFalse(callback.mOnErrorCalled); |
| 1468 } | 1456 } |
| 1469 | 1457 |
| 1470 @SmallTest | 1458 @SmallTest |
| 1471 @Feature({"Cronet"}) | 1459 @Feature({"Cronet"}) |
| 1460 @OnlyRunNativeCronet // No destroyed callback for tests |
| 1472 public void testExecutorShutdown() { | 1461 public void testExecutorShutdown() { |
| 1473 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1462 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1474 | 1463 |
| 1475 callback.setAutoAdvance(false); | 1464 callback.setAutoAdvance(false); |
| 1476 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), | 1465 UrlRequest.Builder builder = new UrlRequest.Builder(NativeTestServer.get
EchoBodyURL(), |
| 1477 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 1466 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 1478 CronetUrlRequest urlRequest = (CronetUrlRequest) builder.build(); | 1467 CronetUrlRequest urlRequest = (CronetUrlRequest) builder.build(); |
| 1479 urlRequest.start(); | 1468 urlRequest.start(); |
| 1480 callback.waitForNextStep(); | 1469 callback.waitForNextStep(); |
| 1481 assertFalse(callback.isDone()); | 1470 assertFalse(callback.isDone()); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1559 int end) { | 1548 int end) { |
| 1560 // Use a duplicate to avoid modifying byteBuffer. | 1549 // Use a duplicate to avoid modifying byteBuffer. |
| 1561 ByteBuffer duplicate = byteBuffer.duplicate(); | 1550 ByteBuffer duplicate = byteBuffer.duplicate(); |
| 1562 duplicate.position(start); | 1551 duplicate.position(start); |
| 1563 duplicate.limit(end); | 1552 duplicate.limit(end); |
| 1564 byte[] contents = new byte[duplicate.remaining()]; | 1553 byte[] contents = new byte[duplicate.remaining()]; |
| 1565 duplicate.get(contents); | 1554 duplicate.get(contents); |
| 1566 return new String(contents); | 1555 return new String(contents); |
| 1567 } | 1556 } |
| 1568 } | 1557 } |
| OLD | NEW |