| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 #include "net/url_request/url_request_unittest.h" | 5 #include "net/url_request/url_request_unittest.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <windows.h> | 10 #include <windows.h> |
| (...skipping 1343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1354 req.set_upload(CreateSimpleUploadData(kData).get()); | 1354 req.set_upload(CreateSimpleUploadData(kData).get()); |
| 1355 req.SetExtraRequestHeaders( | 1355 req.SetExtraRequestHeaders( |
| 1356 "Content-Length: " + UintToString(sizeof(kData) - 1)); | 1356 "Content-Length: " + UintToString(sizeof(kData) - 1)); |
| 1357 req.Start(); | 1357 req.Start(); |
| 1358 MessageLoop::current()->Run(); | 1358 MessageLoop::current()->Run(); |
| 1359 EXPECT_EQ("POST", req.method()); | 1359 EXPECT_EQ("POST", req.method()); |
| 1360 EXPECT_EQ(kData, d.data_received()); | 1360 EXPECT_EQ(kData, d.data_received()); |
| 1361 } | 1361 } |
| 1362 | 1362 |
| 1363 // Custom URLRequestJobs for use with interceptor tests | 1363 // Custom URLRequestJobs for use with interceptor tests |
| 1364 class RestartTestJob : public URLRequestTestJob { | |
| 1365 public: | |
| 1366 explicit RestartTestJob(URLRequest* request) | |
| 1367 : URLRequestTestJob(request, true) {} | |
| 1368 protected: | |
| 1369 virtual void StartAsync() { | |
| 1370 this->NotifyRestartRequired(); | |
| 1371 } | |
| 1372 }; | |
| 1373 | |
| 1374 class CancelTestJob : public URLRequestTestJob { | 1364 class CancelTestJob : public URLRequestTestJob { |
| 1375 public: | 1365 public: |
| 1376 explicit CancelTestJob(URLRequest* request) | 1366 explicit CancelTestJob(URLRequest* request) |
| 1377 : URLRequestTestJob(request, true) {} | 1367 : URLRequestTestJob(request, true) {} |
| 1378 protected: | 1368 protected: |
| 1379 virtual void StartAsync() { | 1369 virtual void StartAsync() { |
| 1380 request_->Cancel(); | 1370 request_->Cancel(); |
| 1381 } | 1371 } |
| 1382 }; | 1372 }; |
| 1383 | 1373 |
| 1384 class CancelThenRestartTestJob : public URLRequestTestJob { | |
| 1385 public: | |
| 1386 explicit CancelThenRestartTestJob(URLRequest* request) | |
| 1387 : URLRequestTestJob(request, true) { | |
| 1388 } | |
| 1389 protected: | |
| 1390 virtual void StartAsync() { | |
| 1391 request_->Cancel(); | |
| 1392 this->NotifyRestartRequired(); | |
| 1393 } | |
| 1394 }; | |
| 1395 | |
| 1396 // An Interceptor for use with interceptor tests | 1374 // An Interceptor for use with interceptor tests |
| 1397 class TestInterceptor : URLRequest::Interceptor { | 1375 class TestInterceptor : URLRequest::Interceptor { |
| 1398 public: | 1376 public: |
| 1399 TestInterceptor() | 1377 TestInterceptor() |
| 1400 : intercept_main_request_(false), restart_main_request_(false), | 1378 : intercept_main_request_(false), cancel_main_request_(false), |
| 1401 cancel_main_request_(false), cancel_then_restart_main_request_(false), | |
| 1402 simulate_main_network_error_(false), | 1379 simulate_main_network_error_(false), |
| 1403 intercept_redirect_(false), cancel_redirect_request_(false), | 1380 intercept_redirect_(false), cancel_redirect_request_(false), |
| 1404 intercept_final_response_(false), cancel_final_request_(false), | 1381 intercept_final_response_(false), cancel_final_request_(false), |
| 1405 did_intercept_main_(false), did_restart_main_(false), | 1382 did_intercept_main_(false), did_cancel_main_(false), |
| 1406 did_cancel_main_(false), did_cancel_then_restart_main_(false), | |
| 1407 did_simulate_error_main_(false), | 1383 did_simulate_error_main_(false), |
| 1408 did_intercept_redirect_(false), did_cancel_redirect_(false), | 1384 did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1409 did_intercept_final_(false), did_cancel_final_(false) { | 1385 did_intercept_final_(false), did_cancel_final_(false) { |
| 1410 URLRequest::RegisterRequestInterceptor(this); | 1386 URLRequest::RegisterRequestInterceptor(this); |
| 1411 } | 1387 } |
| 1412 | 1388 |
| 1413 ~TestInterceptor() { | 1389 ~TestInterceptor() { |
| 1414 URLRequest::UnregisterRequestInterceptor(this); | 1390 URLRequest::UnregisterRequestInterceptor(this); |
| 1415 } | 1391 } |
| 1416 | 1392 |
| 1417 virtual URLRequestJob* MaybeIntercept(URLRequest* request) { | 1393 virtual URLRequestJob* MaybeIntercept(URLRequest* request) { |
| 1418 if (restart_main_request_) { | |
| 1419 restart_main_request_ = false; | |
| 1420 did_restart_main_ = true; | |
| 1421 return new RestartTestJob(request); | |
| 1422 } | |
| 1423 if (cancel_main_request_) { | 1394 if (cancel_main_request_) { |
| 1424 cancel_main_request_ = false; | 1395 cancel_main_request_ = false; |
| 1425 did_cancel_main_ = true; | 1396 did_cancel_main_ = true; |
| 1426 return new CancelTestJob(request); | 1397 return new CancelTestJob(request); |
| 1427 } | 1398 } |
| 1428 if (cancel_then_restart_main_request_) { | |
| 1429 cancel_then_restart_main_request_ = false; | |
| 1430 did_cancel_then_restart_main_ = true; | |
| 1431 return new CancelThenRestartTestJob(request); | |
| 1432 } | |
| 1433 if (simulate_main_network_error_) { | 1399 if (simulate_main_network_error_) { |
| 1434 simulate_main_network_error_ = false; | 1400 simulate_main_network_error_ = false; |
| 1435 did_simulate_error_main_ = true; | 1401 did_simulate_error_main_ = true; |
| 1436 // will error since the requeted url is not one of its canned urls | 1402 // will error since the requeted url is not one of its canned urls |
| 1437 return new URLRequestTestJob(request, true); | 1403 return new URLRequestTestJob(request, true); |
| 1438 } | 1404 } |
| 1439 if (!intercept_main_request_) | 1405 if (!intercept_main_request_) |
| 1440 return NULL; | 1406 return NULL; |
| 1441 intercept_main_request_ = false; | 1407 intercept_main_request_ = false; |
| 1442 did_intercept_main_ = true; | 1408 did_intercept_main_ = true; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1478 final_data_, | 1444 final_data_, |
| 1479 true); | 1445 true); |
| 1480 } | 1446 } |
| 1481 | 1447 |
| 1482 // Whether to intercept the main request, and if so the response to return. | 1448 // Whether to intercept the main request, and if so the response to return. |
| 1483 bool intercept_main_request_; | 1449 bool intercept_main_request_; |
| 1484 std::string main_headers_; | 1450 std::string main_headers_; |
| 1485 std::string main_data_; | 1451 std::string main_data_; |
| 1486 | 1452 |
| 1487 // Other actions we take at MaybeIntercept time | 1453 // Other actions we take at MaybeIntercept time |
| 1488 bool restart_main_request_; | |
| 1489 bool cancel_main_request_; | 1454 bool cancel_main_request_; |
| 1490 bool cancel_then_restart_main_request_; | |
| 1491 bool simulate_main_network_error_; | 1455 bool simulate_main_network_error_; |
| 1492 | 1456 |
| 1493 // Whether to intercept redirects, and if so the response to return. | 1457 // Whether to intercept redirects, and if so the response to return. |
| 1494 bool intercept_redirect_; | 1458 bool intercept_redirect_; |
| 1495 std::string redirect_headers_; | 1459 std::string redirect_headers_; |
| 1496 std::string redirect_data_; | 1460 std::string redirect_data_; |
| 1497 | 1461 |
| 1498 // Other actions we can take at MaybeInterceptRedirect time | 1462 // Other actions we can take at MaybeInterceptRedirect time |
| 1499 bool cancel_redirect_request_; | 1463 bool cancel_redirect_request_; |
| 1500 | 1464 |
| 1501 // Whether to intercept final response, and if so the response to return. | 1465 // Whether to intercept final response, and if so the response to return. |
| 1502 bool intercept_final_response_; | 1466 bool intercept_final_response_; |
| 1503 std::string final_headers_; | 1467 std::string final_headers_; |
| 1504 std::string final_data_; | 1468 std::string final_data_; |
| 1505 | 1469 |
| 1506 // Other actions we can take at MaybeInterceptResponse time | 1470 // Other actions we can take at MaybeInterceptResponse time |
| 1507 bool cancel_final_request_; | 1471 bool cancel_final_request_; |
| 1508 | 1472 |
| 1509 // If we did something or not | 1473 // If we did something or not |
| 1510 bool did_intercept_main_; | 1474 bool did_intercept_main_; |
| 1511 bool did_restart_main_; | |
| 1512 bool did_cancel_main_; | 1475 bool did_cancel_main_; |
| 1513 bool did_cancel_then_restart_main_; | |
| 1514 bool did_simulate_error_main_; | 1476 bool did_simulate_error_main_; |
| 1515 bool did_intercept_redirect_; | 1477 bool did_intercept_redirect_; |
| 1516 bool did_cancel_redirect_; | 1478 bool did_cancel_redirect_; |
| 1517 bool did_intercept_final_; | 1479 bool did_intercept_final_; |
| 1518 bool did_cancel_final_; | 1480 bool did_cancel_final_; |
| 1519 | 1481 |
| 1520 // Static getters for canned response header and data strings | 1482 // Static getters for canned response header and data strings |
| 1521 | 1483 |
| 1522 static std::string ok_data() { | 1484 static std::string ok_data() { |
| 1523 return URLRequestTestJob::test_data_1(); | 1485 return URLRequestTestJob::test_data_1(); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1664 EXPECT_TRUE(interceptor.did_intercept_final_); | 1626 EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1665 | 1627 |
| 1666 // Check we received one good response | 1628 // Check we received one good response |
| 1667 EXPECT_TRUE(req.status().is_success()); | 1629 EXPECT_TRUE(req.status().is_success()); |
| 1668 EXPECT_EQ(200, req.response_headers()->response_code()); | 1630 EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1669 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); | 1631 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1670 EXPECT_EQ(1, d.response_started_count()); | 1632 EXPECT_EQ(1, d.response_started_count()); |
| 1671 EXPECT_EQ(0, d.received_redirect_count()); | 1633 EXPECT_EQ(0, d.received_redirect_count()); |
| 1672 } | 1634 } |
| 1673 | 1635 |
| 1674 TEST_F(URLRequestTest, InterceptRestartRequired) { | |
| 1675 TestInterceptor interceptor; | |
| 1676 | |
| 1677 // restart the main request | |
| 1678 interceptor.restart_main_request_ = true; | |
| 1679 | |
| 1680 // then intercept the new main request and respond with an OK response | |
| 1681 interceptor.intercept_main_request_ = true; | |
| 1682 interceptor.main_headers_ = TestInterceptor::ok_headers(); | |
| 1683 interceptor.main_data_ = TestInterceptor::ok_data(); | |
| 1684 | |
| 1685 TestDelegate d; | |
| 1686 TestURLRequest req(GURL("http://test_intercept/foo"), &d); | |
| 1687 req.set_method("GET"); | |
| 1688 req.Start(); | |
| 1689 MessageLoop::current()->Run(); | |
| 1690 | |
| 1691 // Check the interceptor got called as expected | |
| 1692 EXPECT_TRUE(interceptor.did_restart_main_); | |
| 1693 EXPECT_TRUE(interceptor.did_intercept_main_); | |
| 1694 | |
| 1695 // Check we received one good response | |
| 1696 EXPECT_TRUE(req.status().is_success()); | |
| 1697 EXPECT_EQ(200, req.response_headers()->response_code()); | |
| 1698 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); | |
| 1699 EXPECT_EQ(1, d.response_started_count()); | |
| 1700 EXPECT_EQ(0, d.received_redirect_count()); | |
| 1701 } | |
| 1702 | |
| 1703 TEST_F(URLRequestTest, InterceptRespectsCancelMain) { | 1636 TEST_F(URLRequestTest, InterceptRespectsCancelMain) { |
| 1704 TestInterceptor interceptor; | 1637 TestInterceptor interceptor; |
| 1705 | 1638 |
| 1706 // intercept the main request and cancel from within the restarted job | 1639 // intercept the main request and cancel from within the restarted job |
| 1707 interceptor.cancel_main_request_ = true; | 1640 interceptor.cancel_main_request_ = true; |
| 1708 | 1641 |
| 1709 // setup to intercept final response and override it with an OK response | 1642 // setup to intercept final response and override it with an OK response |
| 1710 interceptor.intercept_final_response_ = true; | 1643 interceptor.intercept_final_response_ = true; |
| 1711 interceptor.final_headers_ = TestInterceptor::ok_headers(); | 1644 interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1712 interceptor.final_data_ = TestInterceptor::ok_data(); | 1645 interceptor.final_data_ = TestInterceptor::ok_data(); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1775 | 1708 |
| 1776 // Check the interceptor got called as expected | 1709 // Check the interceptor got called as expected |
| 1777 EXPECT_TRUE(interceptor.did_simulate_error_main_); | 1710 EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1778 EXPECT_TRUE(interceptor.did_cancel_final_); | 1711 EXPECT_TRUE(interceptor.did_cancel_final_); |
| 1779 | 1712 |
| 1780 // Check we see a canceled request | 1713 // Check we see a canceled request |
| 1781 EXPECT_FALSE(req.status().is_success()); | 1714 EXPECT_FALSE(req.status().is_success()); |
| 1782 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); | 1715 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1783 } | 1716 } |
| 1784 | 1717 |
| 1785 TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { | |
| 1786 TestInterceptor interceptor; | |
| 1787 | |
| 1788 // intercept the main request and cancel then restart from within that job | |
| 1789 interceptor.cancel_then_restart_main_request_ = true; | |
| 1790 | |
| 1791 // setup to intercept final response and override it with an OK response | |
| 1792 interceptor.intercept_final_response_ = true; | |
| 1793 interceptor.final_headers_ = TestInterceptor::ok_headers(); | |
| 1794 interceptor.final_data_ = TestInterceptor::ok_data(); | |
| 1795 | |
| 1796 TestDelegate d; | |
| 1797 TestURLRequest req(GURL("http://test_intercept/foo"), &d); | |
| 1798 req.set_method("GET"); | |
| 1799 req.Start(); | |
| 1800 MessageLoop::current()->Run(); | |
| 1801 | |
| 1802 // Check the interceptor got called as expected | |
| 1803 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); | |
| 1804 EXPECT_FALSE(interceptor.did_intercept_final_); | |
| 1805 | |
| 1806 // Check we see a canceled request | |
| 1807 EXPECT_FALSE(req.status().is_success()); | |
| 1808 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); | |
| 1809 } | |
| 1810 | |
| 1811 TEST_F(URLRequestTest, FTPGetTestAnonymous) { | 1718 TEST_F(URLRequestTest, FTPGetTestAnonymous) { |
| 1812 scoped_refptr<FTPTestServer> server = FTPTestServer::CreateServer(L""); | 1719 scoped_refptr<FTPTestServer> server = FTPTestServer::CreateServer(L""); |
| 1813 ASSERT_TRUE(NULL != server.get()); | 1720 ASSERT_TRUE(NULL != server.get()); |
| 1814 FilePath app_path; | 1721 FilePath app_path; |
| 1815 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); | 1722 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1816 app_path = app_path.AppendASCII("LICENSE"); | 1723 app_path = app_path.AppendASCII("LICENSE"); |
| 1817 TestDelegate d; | 1724 TestDelegate d; |
| 1818 { | 1725 { |
| 1819 TestURLRequest r(server->TestServerPage("/LICENSE"), &d); | 1726 TestURLRequest r(server->TestServerPage("/LICENSE"), &d); |
| 1820 r.Start(); | 1727 r.Start(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1899 | 1806 |
| 1900 int64 file_size = 0; | 1807 int64 file_size = 0; |
| 1901 file_util::GetFileSize(app_path, &file_size); | 1808 file_util::GetFileSize(app_path, &file_size); |
| 1902 | 1809 |
| 1903 EXPECT_FALSE(r.is_pending()); | 1810 EXPECT_FALSE(r.is_pending()); |
| 1904 EXPECT_EQ(1, d.response_started_count()); | 1811 EXPECT_EQ(1, d.response_started_count()); |
| 1905 EXPECT_FALSE(d.received_data_before_response()); | 1812 EXPECT_FALSE(d.received_data_before_response()); |
| 1906 EXPECT_EQ(d.bytes_received(), 0); | 1813 EXPECT_EQ(d.bytes_received(), 0); |
| 1907 } | 1814 } |
| 1908 } | 1815 } |
| OLD | NEW |