Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shlobj.h> | 9 #include <shlobj.h> |
| 10 #endif | 10 #endif |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 #include "net/proxy/proxy_service.h" | 65 #include "net/proxy/proxy_service.h" |
| 66 #include "net/socket/ssl_client_socket.h" | 66 #include "net/socket/ssl_client_socket.h" |
| 67 #include "net/ssl/ssl_cipher_suite_names.h" | 67 #include "net/ssl/ssl_cipher_suite_names.h" |
| 68 #include "net/ssl/ssl_connection_status_flags.h" | 68 #include "net/ssl/ssl_connection_status_flags.h" |
| 69 #include "net/test/cert_test_util.h" | 69 #include "net/test/cert_test_util.h" |
| 70 #include "net/test/spawned_test_server/spawned_test_server.h" | 70 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 71 #include "net/url_request/data_protocol_handler.h" | 71 #include "net/url_request/data_protocol_handler.h" |
| 72 #include "net/url_request/static_http_user_agent_settings.h" | 72 #include "net/url_request/static_http_user_agent_settings.h" |
| 73 #include "net/url_request/url_request.h" | 73 #include "net/url_request/url_request.h" |
| 74 #include "net/url_request/url_request_http_job.h" | 74 #include "net/url_request/url_request_http_job.h" |
| 75 #include "net/url_request/url_request_intercepting_job_factory.h" | |
| 76 #include "net/url_request/url_request_interceptor.h" | |
| 75 #include "net/url_request/url_request_job_factory_impl.h" | 77 #include "net/url_request/url_request_job_factory_impl.h" |
| 76 #include "net/url_request/url_request_redirect_job.h" | 78 #include "net/url_request/url_request_redirect_job.h" |
| 77 #include "net/url_request/url_request_test_job.h" | 79 #include "net/url_request/url_request_test_job.h" |
| 78 #include "net/url_request/url_request_test_util.h" | 80 #include "net/url_request/url_request_test_util.h" |
| 79 #include "testing/gtest/include/gtest/gtest.h" | 81 #include "testing/gtest/include/gtest/gtest.h" |
| 80 #include "testing/platform_test.h" | 82 #include "testing/platform_test.h" |
| 81 | 83 |
| 82 #if !defined(DISABLE_FILE_SUPPORT) | 84 #if !defined(DISABLE_FILE_SUPPORT) |
| 83 #include "net/base/filename_util.h" | 85 #include "net/base/filename_util.h" |
| 84 #include "net/url_request/file_protocol_handler.h" | 86 #include "net/url_request/file_protocol_handler.h" |
| (...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 628 return protocol_handler_; | 630 return protocol_handler_; |
| 629 } | 631 } |
| 630 | 632 |
| 631 protected: | 633 protected: |
| 632 CapturingNetLog net_log_; | 634 CapturingNetLog net_log_; |
| 633 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. | 635 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 634 URLRequestJobFactoryImpl job_factory_; | 636 URLRequestJobFactoryImpl job_factory_; |
| 635 TestURLRequestContext default_context_; | 637 TestURLRequestContext default_context_; |
| 636 }; | 638 }; |
| 637 | 639 |
| 640 | |
| 638 TEST_F(URLRequestTest, AboutBlankTest) { | 641 TEST_F(URLRequestTest, AboutBlankTest) { |
| 639 TestDelegate d; | 642 TestDelegate d; |
| 640 { | 643 { |
| 641 scoped_ptr<URLRequest> r(default_context_.CreateRequest( | 644 scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 642 GURL("about:blank"), DEFAULT_PRIORITY, &d, NULL)); | 645 GURL("about:blank"), DEFAULT_PRIORITY, &d, NULL)); |
| 643 | 646 |
| 644 r->Start(); | 647 r->Start(); |
| 645 EXPECT_TRUE(r->is_pending()); | 648 EXPECT_TRUE(r->is_pending()); |
| 646 | 649 |
| 647 base::RunLoop().Run(); | 650 base::RunLoop().Run(); |
| (...skipping 919 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1567 | 1570 |
| 1568 // Check the interceptor got called as expected | 1571 // Check the interceptor got called as expected |
| 1569 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); | 1572 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); |
| 1570 EXPECT_FALSE(interceptor.did_intercept_final_); | 1573 EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1571 | 1574 |
| 1572 // Check we see a canceled request | 1575 // Check we see a canceled request |
| 1573 EXPECT_FALSE(req->status().is_success()); | 1576 EXPECT_FALSE(req->status().is_success()); |
| 1574 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); | 1577 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1575 } | 1578 } |
| 1576 | 1579 |
| 1580 // An Interceptor for use with interceptor tests | |
| 1581 class MockURLRequestInterceptor : public URLRequestInterceptor { | |
| 1582 public: | |
| 1583 MockURLRequestInterceptor() | |
| 1584 : intercept_main_request_(false), restart_main_request_(false), | |
| 1585 cancel_main_request_(false), cancel_then_restart_main_request_(false), | |
| 1586 simulate_main_network_error_(false), | |
| 1587 intercept_redirect_(false), cancel_redirect_request_(false), | |
| 1588 intercept_final_response_(false), cancel_final_request_(false), | |
| 1589 use_url_request_http_job_(false), | |
| 1590 did_intercept_main_(false), did_restart_main_(false), | |
| 1591 did_cancel_main_(false), did_cancel_then_restart_main_(false), | |
| 1592 did_simulate_error_main_(false), | |
| 1593 did_intercept_redirect_(false), did_cancel_redirect_(false), | |
| 1594 did_intercept_final_(false), did_cancel_final_(false) { | |
| 1595 } | |
| 1596 | |
| 1597 ~MockURLRequestInterceptor() override { | |
| 1598 } | |
| 1599 | |
| 1600 virtual URLRequestJob* MaybeInterceptRequest( | |
| 1601 URLRequest* request, | |
| 1602 NetworkDelegate* network_delegate) const override { | |
| 1603 if (restart_main_request_) { | |
| 1604 restart_main_request_ = false; | |
| 1605 did_restart_main_ = true; | |
| 1606 return new RestartTestJob(request, network_delegate); | |
| 1607 } | |
| 1608 if (cancel_main_request_) { | |
| 1609 cancel_main_request_ = false; | |
| 1610 did_cancel_main_ = true; | |
| 1611 return new CancelTestJob(request, network_delegate); | |
| 1612 } | |
| 1613 if (cancel_then_restart_main_request_) { | |
| 1614 cancel_then_restart_main_request_ = false; | |
| 1615 did_cancel_then_restart_main_ = true; | |
| 1616 return new CancelThenRestartTestJob(request, network_delegate); | |
| 1617 } | |
| 1618 if (simulate_main_network_error_) { | |
| 1619 simulate_main_network_error_ = false; | |
| 1620 did_simulate_error_main_ = true; | |
| 1621 // will error since the requeted url is not one of its canned urls | |
| 1622 if (use_url_request_http_job_) { | |
| 1623 return URLRequestHttpJob::Factory(request, network_delegate, "http"); | |
| 1624 } | |
| 1625 return new URLRequestTestJob(request, network_delegate, true); | |
| 1626 } | |
| 1627 if (!intercept_main_request_) | |
| 1628 return NULL; | |
| 1629 intercept_main_request_ = false; | |
| 1630 did_intercept_main_ = true; | |
| 1631 URLRequestTestJob* job = new URLRequestTestJob(request, | |
| 1632 network_delegate, | |
| 1633 main_headers_, | |
| 1634 main_data_, | |
| 1635 true); | |
| 1636 job->set_load_timing_info(main_request_load_timing_info_); | |
| 1637 return job; | |
| 1638 } | |
| 1639 | |
| 1640 URLRequestJob* MaybeInterceptRedirect(URLRequest* request, | |
| 1641 NetworkDelegate* network_delegate, | |
| 1642 const GURL& location) const override { | |
| 1643 if (cancel_redirect_request_) { | |
| 1644 cancel_redirect_request_ = false; | |
| 1645 did_cancel_redirect_ = true; | |
| 1646 return new CancelTestJob(request, network_delegate); | |
| 1647 } | |
| 1648 if (!intercept_redirect_) | |
| 1649 return NULL; | |
| 1650 intercept_redirect_ = false; | |
| 1651 did_intercept_redirect_ = true; | |
| 1652 if (use_url_request_http_job_) { | |
| 1653 return URLRequestHttpJob::Factory(request, network_delegate, "http"); | |
| 1654 } | |
| 1655 return new URLRequestTestJob(request, | |
| 1656 network_delegate, | |
| 1657 redirect_headers_, | |
| 1658 redirect_data_, | |
| 1659 true); | |
| 1660 } | |
| 1661 | |
| 1662 URLRequestJob* MaybeInterceptResponse( | |
| 1663 URLRequest* request, | |
| 1664 NetworkDelegate* network_delegate) const override { | |
| 1665 if (cancel_final_request_) { | |
| 1666 cancel_final_request_ = false; | |
| 1667 did_cancel_final_ = true; | |
| 1668 return new CancelTestJob(request, network_delegate); | |
| 1669 } | |
| 1670 if (!intercept_final_response_) | |
| 1671 return NULL; | |
| 1672 intercept_final_response_ = false; | |
| 1673 did_intercept_final_ = true; | |
| 1674 if (use_url_request_http_job_) { | |
| 1675 return URLRequestHttpJob::Factory(request, network_delegate, "http"); | |
| 1676 } | |
| 1677 return new URLRequestTestJob(request, | |
| 1678 network_delegate, | |
| 1679 final_headers_, | |
| 1680 final_data_, | |
| 1681 true); | |
| 1682 } | |
| 1683 | |
| 1684 // Whether to intercept the main request, and if so the response to return and | |
| 1685 // the LoadTimingInfo to use. | |
| 1686 mutable bool intercept_main_request_; | |
| 1687 mutable std::string main_headers_; | |
| 1688 mutable std::string main_data_; | |
| 1689 mutable LoadTimingInfo main_request_load_timing_info_; | |
| 1690 | |
| 1691 // Other actions we take at MaybeIntercept time | |
| 1692 mutable bool restart_main_request_; | |
| 1693 mutable bool cancel_main_request_; | |
| 1694 mutable bool cancel_then_restart_main_request_; | |
| 1695 mutable bool simulate_main_network_error_; | |
| 1696 | |
| 1697 // Whether to intercept redirects, and if so the response to return. | |
| 1698 mutable bool intercept_redirect_; | |
| 1699 mutable std::string redirect_headers_; | |
| 1700 mutable std::string redirect_data_; | |
| 1701 | |
| 1702 // Other actions we can take at MaybeInterceptRedirect time | |
| 1703 mutable bool cancel_redirect_request_; | |
| 1704 | |
| 1705 // Whether to intercept final response, and if so the response to return. | |
| 1706 mutable bool intercept_final_response_; | |
| 1707 mutable std::string final_headers_; | |
| 1708 mutable std::string final_data_; | |
| 1709 | |
| 1710 // Other actions we can take at MaybeInterceptResponse time | |
| 1711 mutable bool cancel_final_request_; | |
| 1712 | |
| 1713 mutable bool use_url_request_http_job_; | |
| 1714 | |
| 1715 // If we did something or not | |
| 1716 mutable bool did_intercept_main_; | |
| 1717 mutable bool did_restart_main_; | |
| 1718 mutable bool did_cancel_main_; | |
| 1719 mutable bool did_cancel_then_restart_main_; | |
| 1720 mutable bool did_simulate_error_main_; | |
| 1721 mutable bool did_intercept_redirect_; | |
| 1722 mutable bool did_cancel_redirect_; | |
| 1723 mutable bool did_intercept_final_; | |
| 1724 mutable bool did_cancel_final_; | |
| 1725 | |
| 1726 // Static getters for canned response header and data strings | |
| 1727 | |
| 1728 static std::string ok_data() { | |
| 1729 return URLRequestTestJob::test_data_1(); | |
| 1730 } | |
| 1731 | |
| 1732 static std::string ok_headers() { | |
| 1733 return URLRequestTestJob::test_headers(); | |
| 1734 } | |
| 1735 | |
| 1736 static std::string redirect_data() { | |
| 1737 return std::string(); | |
| 1738 } | |
| 1739 | |
| 1740 static std::string redirect_headers() { | |
| 1741 return URLRequestTestJob::test_redirect_headers(); | |
| 1742 } | |
| 1743 | |
| 1744 static std::string error_data() { | |
| 1745 return std::string("ohhh nooooo mr. bill!"); | |
| 1746 } | |
| 1747 | |
| 1748 static std::string error_headers() { | |
| 1749 return URLRequestTestJob::test_error_headers(); | |
| 1750 } | |
| 1751 }; | |
| 1752 | |
| 1753 // Inherit PlatformTest since we require the autorelease pool on Mac OS X. | |
| 1754 class URLRequestInterceptorTest : public PlatformTest { | |
| 1755 public: | |
| 1756 URLRequestInterceptorTest() : default_context_(true) { | |
| 1757 default_context_.set_network_delegate(&default_network_delegate_); | |
| 1758 default_context_.set_net_log(&net_log_); | |
| 1759 job_factory_impl_ = new URLRequestJobFactoryImpl(); | |
| 1760 job_factory_.reset(job_factory_impl_); | |
| 1761 job_factory_impl_->SetProtocolHandler("data", new DataProtocolHandler); | |
| 1762 #if !defined(DISABLE_FILE_SUPPORT) | |
| 1763 job_factory_impl_->SetProtocolHandler( | |
| 1764 "file", new FileProtocolHandler(base::MessageLoopProxy::current())); | |
| 1765 #endif | |
| 1766 interceptor_ = new MockURLRequestInterceptor(); | |
| 1767 job_factory_.reset(new URLRequestInterceptingJobFactory( | |
| 1768 job_factory_.Pass(), make_scoped_ptr(interceptor_))); | |
| 1769 default_context_.set_job_factory(job_factory_.get()); | |
| 1770 default_context_.Init(); | |
| 1771 } | |
| 1772 ~URLRequestInterceptorTest() override { | |
| 1773 // URLRequestJobs may post clean-up tasks on destruction. | |
| 1774 base::RunLoop().RunUntilIdle(); | |
| 1775 } | |
| 1776 | |
| 1777 protected: | |
| 1778 CapturingNetLog net_log_; | |
| 1779 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. | |
| 1780 URLRequestJobFactoryImpl* job_factory_impl_; | |
| 1781 scoped_ptr<URLRequestJobFactory> job_factory_; | |
| 1782 TestURLRequestContext default_context_; | |
| 1783 MockURLRequestInterceptor* interceptor_; | |
| 1784 }; | |
| 1785 | |
| 1786 | |
| 1787 TEST_F(URLRequestInterceptorTest, Intercept) { | |
| 1788 // intercept the main request and respond with a simple response | |
| 1789 interceptor_->intercept_main_request_ = true; | |
| 1790 interceptor_->main_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 1791 interceptor_->main_data_ = MockURLRequestInterceptor::ok_data(); | |
| 1792 TestDelegate d; | |
| 1793 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 1794 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 1795 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); | |
| 1796 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); | |
| 1797 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); | |
| 1798 req->SetUserData(NULL, user_data0); | |
| 1799 req->SetUserData(&user_data1, user_data1); | |
| 1800 req->SetUserData(&user_data2, user_data2); | |
| 1801 req->set_method("GET"); | |
| 1802 req->Start(); | |
| 1803 base::RunLoop().Run(); | |
| 1804 | |
| 1805 // Make sure we can retrieve our specific user data | |
| 1806 EXPECT_EQ(user_data0, req->GetUserData(NULL)); | |
| 1807 EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); | |
| 1808 EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); | |
| 1809 | |
| 1810 // Check we got one good response | |
| 1811 EXPECT_TRUE(req->status().is_success()); | |
| 1812 EXPECT_EQ(200, req->response_headers()->response_code()); | |
| 1813 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); | |
| 1814 EXPECT_EQ(1, d.response_started_count()); | |
| 1815 EXPECT_EQ(0, d.received_redirect_count()); | |
| 1816 } | |
| 1817 | |
| 1818 TEST_F(URLRequestInterceptorTest, InterceptRedirect) { | |
| 1819 // intercept the main request and respond with a redirect | |
| 1820 interceptor_->intercept_main_request_ = true; | |
| 1821 interceptor_->main_headers_ = MockURLRequestInterceptor::redirect_headers(); | |
| 1822 interceptor_->main_data_ = MockURLRequestInterceptor::redirect_data(); | |
| 1823 | |
| 1824 // intercept that redirect and respond a final OK response | |
| 1825 interceptor_->intercept_redirect_ = true; | |
| 1826 interceptor_->redirect_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 1827 interceptor_->redirect_data_ = MockURLRequestInterceptor::ok_data(); | |
| 1828 | |
| 1829 TestDelegate d; | |
| 1830 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 1831 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 1832 req->set_method("GET"); | |
| 1833 req->Start(); | |
| 1834 base::RunLoop().Run(); | |
| 1835 | |
| 1836 // Check the interceptor got called as expected | |
| 1837 EXPECT_TRUE(interceptor_->did_intercept_main_); | |
| 1838 EXPECT_TRUE(interceptor_->did_intercept_redirect_); | |
| 1839 | |
| 1840 // Check we got one good response | |
| 1841 EXPECT_TRUE(req->status().is_success()); | |
| 1842 if (req->status().is_success()) { | |
| 1843 EXPECT_EQ(200, req->response_headers()->response_code()); | |
| 1844 } | |
| 1845 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); | |
| 1846 EXPECT_EQ(1, d.response_started_count()); | |
| 1847 EXPECT_EQ(0, d.received_redirect_count()); | |
| 1848 } | |
| 1849 | |
| 1850 TEST_F(URLRequestInterceptorTest, InterceptServerError) { | |
| 1851 // intercept the main request to generate a server error response | |
| 1852 interceptor_->intercept_main_request_ = true; | |
| 1853 interceptor_->main_headers_ = MockURLRequestInterceptor::error_headers(); | |
| 1854 interceptor_->main_data_ = MockURLRequestInterceptor::error_data(); | |
| 1855 | |
| 1856 // intercept that error and respond with an OK response | |
| 1857 interceptor_->intercept_final_response_ = true; | |
| 1858 interceptor_->final_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 1859 interceptor_->final_data_ = MockURLRequestInterceptor::ok_data(); | |
| 1860 | |
| 1861 TestDelegate d; | |
| 1862 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 1863 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 1864 req->set_method("GET"); | |
| 1865 req->Start(); | |
| 1866 base::RunLoop().Run(); | |
| 1867 | |
| 1868 // Check the interceptor got called as expected | |
| 1869 EXPECT_TRUE(interceptor_->did_intercept_main_); | |
| 1870 EXPECT_TRUE(interceptor_->did_intercept_final_); | |
| 1871 | |
| 1872 // Check we got one good response | |
| 1873 EXPECT_TRUE(req->status().is_success()); | |
| 1874 EXPECT_EQ(200, req->response_headers()->response_code()); | |
| 1875 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); | |
| 1876 EXPECT_EQ(1, d.response_started_count()); | |
| 1877 EXPECT_EQ(0, d.received_redirect_count()); | |
| 1878 } | |
| 1879 | |
| 1880 TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { | |
| 1881 // intercept the main request to simulate a network error | |
| 1882 interceptor_->simulate_main_network_error_ = true; | |
| 1883 | |
| 1884 // intercept that error and respond with an OK response | |
| 1885 interceptor_->intercept_final_response_ = true; | |
| 1886 interceptor_->final_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 1887 interceptor_->final_data_ = MockURLRequestInterceptor::ok_data(); | |
| 1888 | |
| 1889 TestDelegate d; | |
| 1890 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 1891 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 1892 req->set_method("GET"); | |
| 1893 req->Start(); | |
| 1894 base::RunLoop().Run(); | |
| 1895 | |
| 1896 // Check the interceptor got called as expected | |
| 1897 EXPECT_TRUE(interceptor_->did_simulate_error_main_); | |
| 1898 EXPECT_TRUE(interceptor_->did_intercept_final_); | |
| 1899 | |
| 1900 // Check we received one good response | |
| 1901 EXPECT_TRUE(req->status().is_success()); | |
| 1902 EXPECT_EQ(200, req->response_headers()->response_code()); | |
| 1903 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); | |
| 1904 EXPECT_EQ(1, d.response_started_count()); | |
| 1905 EXPECT_EQ(0, d.received_redirect_count()); | |
| 1906 } | |
| 1907 | |
| 1908 TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { | |
| 1909 // restart the main request | |
| 1910 interceptor_->restart_main_request_ = true; | |
| 1911 | |
| 1912 // then intercept the new main request and respond with an OK response | |
| 1913 interceptor_->intercept_main_request_ = true; | |
| 1914 interceptor_->main_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 1915 interceptor_->main_data_ = MockURLRequestInterceptor::ok_data(); | |
| 1916 | |
| 1917 TestDelegate d; | |
| 1918 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 1919 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 1920 req->set_method("GET"); | |
| 1921 req->Start(); | |
| 1922 base::RunLoop().Run(); | |
| 1923 | |
| 1924 // Check the interceptor got called as expected | |
| 1925 EXPECT_TRUE(interceptor_->did_restart_main_); | |
| 1926 EXPECT_TRUE(interceptor_->did_intercept_main_); | |
| 1927 | |
| 1928 // Check we received one good response | |
| 1929 EXPECT_TRUE(req->status().is_success()); | |
| 1930 if (req->status().is_success()) { | |
| 1931 EXPECT_EQ(200, req->response_headers()->response_code()); | |
| 1932 } | |
| 1933 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); | |
| 1934 EXPECT_EQ(1, d.response_started_count()); | |
| 1935 EXPECT_EQ(0, d.received_redirect_count()); | |
| 1936 } | |
| 1937 | |
| 1938 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { | |
| 1939 // intercept the main request and cancel from within the restarted job | |
| 1940 interceptor_->cancel_main_request_ = true; | |
| 1941 | |
| 1942 // setup to intercept final response and override it with an OK response | |
| 1943 interceptor_->intercept_final_response_ = true; | |
| 1944 interceptor_->final_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 1945 interceptor_->final_data_ = MockURLRequestInterceptor::ok_data(); | |
| 1946 | |
| 1947 TestDelegate d; | |
| 1948 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 1949 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 1950 req->set_method("GET"); | |
| 1951 req->Start(); | |
| 1952 base::RunLoop().Run(); | |
| 1953 | |
| 1954 // Check the interceptor got called as expected | |
| 1955 EXPECT_TRUE(interceptor_->did_cancel_main_); | |
| 1956 EXPECT_FALSE(interceptor_->did_intercept_final_); | |
| 1957 | |
| 1958 // Check we see a canceled request | |
| 1959 EXPECT_FALSE(req->status().is_success()); | |
| 1960 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); | |
| 1961 } | |
| 1962 | |
| 1963 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { | |
| 1964 // intercept the main request and respond with a redirect | |
| 1965 interceptor_->intercept_main_request_ = true; | |
| 1966 interceptor_->main_headers_ = MockURLRequestInterceptor::redirect_headers(); | |
| 1967 interceptor_->main_data_ = MockURLRequestInterceptor::redirect_data(); | |
| 1968 | |
| 1969 // intercept the redirect and cancel from within that job | |
| 1970 interceptor_->cancel_redirect_request_ = true; | |
| 1971 | |
| 1972 // setup to intercept final response and override it with an OK response | |
| 1973 interceptor_->intercept_final_response_ = true; | |
| 1974 interceptor_->final_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 1975 interceptor_->final_data_ = MockURLRequestInterceptor::ok_data(); | |
| 1976 | |
| 1977 TestDelegate d; | |
| 1978 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 1979 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 1980 req->set_method("GET"); | |
| 1981 req->Start(); | |
| 1982 base::RunLoop().Run(); | |
| 1983 | |
| 1984 // Check the interceptor got called as expected | |
| 1985 EXPECT_TRUE(interceptor_->did_intercept_main_); | |
| 1986 EXPECT_TRUE(interceptor_->did_cancel_redirect_); | |
| 1987 EXPECT_FALSE(interceptor_->did_intercept_final_); | |
| 1988 | |
| 1989 // Check we see a canceled request | |
| 1990 EXPECT_FALSE(req->status().is_success()); | |
| 1991 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); | |
| 1992 } | |
| 1993 | |
| 1994 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { | |
| 1995 // intercept the main request to simulate a network error | |
| 1996 interceptor_->simulate_main_network_error_ = true; | |
| 1997 | |
| 1998 // setup to intercept final response and cancel from within that job | |
| 1999 interceptor_->cancel_final_request_ = true; | |
| 2000 | |
| 2001 TestDelegate d; | |
| 2002 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 2003 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 2004 req->set_method("GET"); | |
| 2005 req->Start(); | |
| 2006 base::RunLoop().Run(); | |
| 2007 | |
| 2008 // Check the interceptor got called as expected | |
| 2009 EXPECT_TRUE(interceptor_->did_simulate_error_main_); | |
| 2010 EXPECT_TRUE(interceptor_->did_cancel_final_); | |
| 2011 | |
| 2012 // Check we see a canceled request | |
| 2013 EXPECT_FALSE(req->status().is_success()); | |
| 2014 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); | |
| 2015 } | |
| 2016 | |
| 2017 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { | |
| 2018 // intercept the main request and cancel then restart from within that job | |
| 2019 interceptor_->cancel_then_restart_main_request_ = true; | |
| 2020 | |
| 2021 // setup to intercept final response and override it with an OK response | |
| 2022 interceptor_->intercept_final_response_ = true; | |
| 2023 interceptor_->final_headers_ = TestInterceptor::ok_headers(); | |
| 2024 interceptor_->final_data_ = TestInterceptor::ok_data(); | |
| 2025 | |
| 2026 TestDelegate d; | |
| 2027 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 2028 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 2029 req->set_method("GET"); | |
| 2030 req->Start(); | |
| 2031 base::RunLoop().Run(); | |
| 2032 | |
| 2033 // Check the interceptor got called as expected | |
| 2034 EXPECT_TRUE(interceptor_->did_cancel_then_restart_main_); | |
| 2035 EXPECT_FALSE(interceptor_->did_intercept_final_); | |
| 2036 | |
| 2037 // Check we see a canceled request | |
| 2038 EXPECT_FALSE(req->status().is_success()); | |
| 2039 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); | |
| 2040 } | |
| 2041 | |
| 2042 | |
| 1577 LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing, | 2043 LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing, |
| 1578 URLRequestContext* context) { | 2044 URLRequestContext* context) { |
| 1579 TestInterceptor interceptor; | 2045 TestInterceptor interceptor; |
| 1580 interceptor.intercept_main_request_ = true; | 2046 interceptor.intercept_main_request_ = true; |
| 1581 interceptor.main_request_load_timing_info_ = job_load_timing; | 2047 interceptor.main_request_load_timing_info_ = job_load_timing; |
| 1582 TestDelegate d; | 2048 TestDelegate d; |
| 1583 scoped_ptr<URLRequest> req(context->CreateRequest( | 2049 scoped_ptr<URLRequest> req(context->CreateRequest( |
| 1584 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | 2050 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 2051 req->Start(); | |
| 2052 base::RunLoop().Run(); | |
| 2053 | |
| 2054 LoadTimingInfo resulting_load_timing; | |
| 2055 req->GetLoadTimingInfo(&resulting_load_timing); | |
| 2056 | |
| 2057 // None of these should be modified by the URLRequest. | |
| 2058 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); | |
| 2059 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); | |
| 2060 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); | |
| 2061 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); | |
| 2062 EXPECT_EQ(job_load_timing.receive_headers_end, | |
| 2063 resulting_load_timing.receive_headers_end); | |
| 2064 | |
| 2065 return resulting_load_timing; | |
| 2066 } | |
| 2067 | |
| 2068 // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not | |
| 2069 // reused. |connect_time_flags| is used to indicate if there should be dns | |
| 2070 // or SSL times, and |used_proxy| is used for proxy times. | |
| 2071 LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, | |
| 2072 int connect_time_flags, | |
| 2073 bool used_proxy) { | |
| 2074 LoadTimingInfo load_timing; | |
| 2075 load_timing.socket_log_id = 1; | |
| 2076 | |
| 2077 if (used_proxy) { | |
| 2078 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); | |
| 2079 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); | |
| 2080 } | |
| 2081 | |
| 2082 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; | |
| 2083 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { | |
| 2084 connect_timing.dns_start = now + base::TimeDelta::FromDays(3); | |
| 2085 connect_timing.dns_end = now + base::TimeDelta::FromDays(4); | |
| 2086 } | |
| 2087 connect_timing.connect_start = now + base::TimeDelta::FromDays(5); | |
| 2088 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { | |
| 2089 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); | |
| 2090 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); | |
| 2091 } | |
| 2092 connect_timing.connect_end = now + base::TimeDelta::FromDays(8); | |
| 2093 | |
| 2094 load_timing.send_start = now + base::TimeDelta::FromDays(9); | |
| 2095 load_timing.send_end = now + base::TimeDelta::FromDays(10); | |
| 2096 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); | |
| 2097 return load_timing; | |
| 2098 } | |
| 2099 | |
| 2100 // Same as above, but in the case of a reused socket. | |
| 2101 LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, | |
| 2102 bool used_proxy) { | |
| 2103 LoadTimingInfo load_timing; | |
| 2104 load_timing.socket_log_id = 1; | |
| 2105 load_timing.socket_reused = true; | |
| 2106 | |
| 2107 if (used_proxy) { | |
| 2108 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); | |
| 2109 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); | |
| 2110 } | |
| 2111 | |
| 2112 load_timing.send_start = now + base::TimeDelta::FromDays(9); | |
| 2113 load_timing.send_end = now + base::TimeDelta::FromDays(10); | |
| 2114 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); | |
| 2115 return load_timing; | |
| 2116 } | |
| 2117 | |
| 2118 // Basic test that the intercept + load timing tests work. | |
| 2119 TEST_F(URLRequestTest, InterceptLoadTiming) { | |
| 2120 base::TimeTicks now = base::TimeTicks::Now(); | |
| 2121 LoadTimingInfo job_load_timing = | |
| 2122 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); | |
| 2123 | |
| 2124 LoadTimingInfo load_timing_result = | |
| 2125 RunLoadTimingTest(job_load_timing, &default_context_); | |
| 2126 | |
| 2127 // Nothing should have been changed by the URLRequest. | |
| 2128 EXPECT_EQ(job_load_timing.proxy_resolve_start, | |
| 2129 load_timing_result.proxy_resolve_start); | |
| 2130 EXPECT_EQ(job_load_timing.proxy_resolve_end, | |
| 2131 load_timing_result.proxy_resolve_end); | |
| 2132 EXPECT_EQ(job_load_timing.connect_timing.dns_start, | |
| 2133 load_timing_result.connect_timing.dns_start); | |
| 2134 EXPECT_EQ(job_load_timing.connect_timing.dns_end, | |
| 2135 load_timing_result.connect_timing.dns_end); | |
| 2136 EXPECT_EQ(job_load_timing.connect_timing.connect_start, | |
| 2137 load_timing_result.connect_timing.connect_start); | |
| 2138 EXPECT_EQ(job_load_timing.connect_timing.connect_end, | |
| 2139 load_timing_result.connect_timing.connect_end); | |
| 2140 EXPECT_EQ(job_load_timing.connect_timing.ssl_start, | |
| 2141 load_timing_result.connect_timing.ssl_start); | |
| 2142 EXPECT_EQ(job_load_timing.connect_timing.ssl_end, | |
| 2143 load_timing_result.connect_timing.ssl_end); | |
| 2144 | |
| 2145 // Redundant sanity check. | |
| 2146 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); | |
| 2147 } | |
| 2148 | |
| 2149 // Another basic test, with proxy and SSL times, but no DNS times. | |
| 2150 TEST_F(URLRequestTest, InterceptLoadTimingProxy) { | |
| 2151 base::TimeTicks now = base::TimeTicks::Now(); | |
| 2152 LoadTimingInfo job_load_timing = | |
| 2153 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); | |
| 2154 | |
| 2155 LoadTimingInfo load_timing_result = | |
| 2156 RunLoadTimingTest(job_load_timing, &default_context_); | |
| 2157 | |
| 2158 // Nothing should have been changed by the URLRequest. | |
| 2159 EXPECT_EQ(job_load_timing.proxy_resolve_start, | |
| 2160 load_timing_result.proxy_resolve_start); | |
| 2161 EXPECT_EQ(job_load_timing.proxy_resolve_end, | |
| 2162 load_timing_result.proxy_resolve_end); | |
| 2163 EXPECT_EQ(job_load_timing.connect_timing.dns_start, | |
| 2164 load_timing_result.connect_timing.dns_start); | |
| 2165 EXPECT_EQ(job_load_timing.connect_timing.dns_end, | |
| 2166 load_timing_result.connect_timing.dns_end); | |
| 2167 EXPECT_EQ(job_load_timing.connect_timing.connect_start, | |
| 2168 load_timing_result.connect_timing.connect_start); | |
| 2169 EXPECT_EQ(job_load_timing.connect_timing.connect_end, | |
| 2170 load_timing_result.connect_timing.connect_end); | |
| 2171 EXPECT_EQ(job_load_timing.connect_timing.ssl_start, | |
| 2172 load_timing_result.connect_timing.ssl_start); | |
| 2173 EXPECT_EQ(job_load_timing.connect_timing.ssl_end, | |
| 2174 load_timing_result.connect_timing.ssl_end); | |
| 2175 | |
| 2176 // Redundant sanity check. | |
| 2177 TestLoadTimingNotReusedWithProxy(load_timing_result, | |
| 2178 CONNECT_TIMING_HAS_SSL_TIMES); | |
| 2179 } | |
| 2180 | |
| 2181 // Make sure that URLRequest correctly adjusts proxy times when they're before | |
| 2182 // |request_start|, due to already having a connected socket. This happens in | |
| 2183 // the case of reusing a SPDY session. The connected socket is not considered | |
| 2184 // reused in this test (May be a preconnect). | |
| 2185 // | |
| 2186 // To mix things up from the test above, assumes DNS times but no SSL times. | |
| 2187 TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) { | |
| 2188 base::TimeTicks now = base::TimeTicks::Now(); | |
| 2189 LoadTimingInfo job_load_timing = | |
| 2190 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); | |
| 2191 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); | |
| 2192 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); | |
| 2193 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); | |
| 2194 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); | |
| 2195 job_load_timing.connect_timing.connect_start = | |
| 2196 now - base::TimeDelta::FromDays(2); | |
| 2197 job_load_timing.connect_timing.connect_end = | |
| 2198 now - base::TimeDelta::FromDays(1); | |
| 2199 | |
| 2200 LoadTimingInfo load_timing_result = | |
| 2201 RunLoadTimingTest(job_load_timing, &default_context_); | |
| 2202 | |
| 2203 // Proxy times, connect times, and DNS times should all be replaced with | |
| 2204 // request_start. | |
| 2205 EXPECT_EQ(load_timing_result.request_start, | |
| 2206 load_timing_result.proxy_resolve_start); | |
| 2207 EXPECT_EQ(load_timing_result.request_start, | |
| 2208 load_timing_result.proxy_resolve_end); | |
| 2209 EXPECT_EQ(load_timing_result.request_start, | |
| 2210 load_timing_result.connect_timing.dns_start); | |
| 2211 EXPECT_EQ(load_timing_result.request_start, | |
| 2212 load_timing_result.connect_timing.dns_end); | |
| 2213 EXPECT_EQ(load_timing_result.request_start, | |
| 2214 load_timing_result.connect_timing.connect_start); | |
| 2215 EXPECT_EQ(load_timing_result.request_start, | |
| 2216 load_timing_result.connect_timing.connect_end); | |
| 2217 | |
| 2218 // Other times should have been left null. | |
| 2219 TestLoadTimingNotReusedWithProxy(load_timing_result, | |
| 2220 CONNECT_TIMING_HAS_DNS_TIMES); | |
| 2221 } | |
| 2222 | |
| 2223 // Same as above, but in the reused case. | |
| 2224 TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) { | |
| 2225 base::TimeTicks now = base::TimeTicks::Now(); | |
| 2226 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); | |
| 2227 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); | |
| 2228 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); | |
| 2229 | |
| 2230 LoadTimingInfo load_timing_result = | |
| 2231 RunLoadTimingTest(job_load_timing, &default_context_); | |
| 2232 | |
| 2233 // Proxy times and connect times should all be replaced with request_start. | |
| 2234 EXPECT_EQ(load_timing_result.request_start, | |
| 2235 load_timing_result.proxy_resolve_start); | |
| 2236 EXPECT_EQ(load_timing_result.request_start, | |
| 2237 load_timing_result.proxy_resolve_end); | |
| 2238 | |
| 2239 // Other times should have been left null. | |
| 2240 TestLoadTimingReusedWithProxy(load_timing_result); | |
| 2241 } | |
| 2242 | |
| 2243 // Make sure that URLRequest correctly adjusts connect times when they're before | |
| 2244 // |request_start|, due to reusing a connected socket. The connected socket is | |
| 2245 // not considered reused in this test (May be a preconnect). | |
| 2246 // | |
| 2247 // To mix things up, the request has SSL times, but no DNS times. | |
| 2248 TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) { | |
| 2249 base::TimeTicks now = base::TimeTicks::Now(); | |
| 2250 LoadTimingInfo job_load_timing = | |
| 2251 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); | |
| 2252 job_load_timing.connect_timing.connect_start = | |
| 2253 now - base::TimeDelta::FromDays(1); | |
| 2254 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); | |
| 2255 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); | |
| 2256 job_load_timing.connect_timing.connect_end = | |
| 2257 now - base::TimeDelta::FromDays(4); | |
| 2258 | |
| 2259 LoadTimingInfo load_timing_result = | |
| 2260 RunLoadTimingTest(job_load_timing, &default_context_); | |
| 2261 | |
| 2262 // Connect times, and SSL times should be replaced with request_start. | |
| 2263 EXPECT_EQ(load_timing_result.request_start, | |
| 2264 load_timing_result.connect_timing.connect_start); | |
| 2265 EXPECT_EQ(load_timing_result.request_start, | |
| 2266 load_timing_result.connect_timing.ssl_start); | |
| 2267 EXPECT_EQ(load_timing_result.request_start, | |
| 2268 load_timing_result.connect_timing.ssl_end); | |
| 2269 EXPECT_EQ(load_timing_result.request_start, | |
| 2270 load_timing_result.connect_timing.connect_end); | |
| 2271 | |
| 2272 // Other times should have been left null. | |
| 2273 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); | |
| 2274 } | |
| 2275 | |
| 2276 // Make sure that URLRequest correctly adjusts connect times when they're before | |
| 2277 // |request_start|, due to reusing a connected socket in the case that there | |
| 2278 // are also proxy times. The connected socket is not considered reused in this | |
| 2279 // test (May be a preconnect). | |
| 2280 // | |
| 2281 // In this test, there are no SSL or DNS times. | |
| 2282 TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) { | |
| 2283 base::TimeTicks now = base::TimeTicks::Now(); | |
| 2284 LoadTimingInfo job_load_timing = | |
| 2285 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); | |
| 2286 job_load_timing.connect_timing.connect_start = | |
| 2287 now - base::TimeDelta::FromDays(1); | |
| 2288 job_load_timing.connect_timing.connect_end = | |
| 2289 now - base::TimeDelta::FromDays(2); | |
| 2290 | |
| 2291 LoadTimingInfo load_timing_result = | |
| 2292 RunLoadTimingTest(job_load_timing, &default_context_); | |
| 2293 | |
| 2294 // Connect times should be replaced with proxy_resolve_end. | |
| 2295 EXPECT_EQ(load_timing_result.proxy_resolve_end, | |
| 2296 load_timing_result.connect_timing.connect_start); | |
| 2297 EXPECT_EQ(load_timing_result.proxy_resolve_end, | |
| 2298 load_timing_result.connect_timing.connect_end); | |
| 2299 | |
| 2300 // Other times should have been left null. | |
| 2301 TestLoadTimingNotReusedWithProxy(load_timing_result, | |
| 2302 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
| 2303 } | |
| 2304 | |
| 2305 LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( | |
| 2306 const LoadTimingInfo& job_load_timing, | |
| 2307 URLRequestContext* context, | |
| 2308 MockURLRequestInterceptor* interceptor) { | |
| 2309 interceptor->intercept_main_request_ = true; | |
| 2310 interceptor->main_request_load_timing_info_ = job_load_timing; | |
| 2311 TestDelegate d; | |
| 2312 scoped_ptr<URLRequest> req(context->CreateRequest( | |
| 2313 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 1585 req->Start(); | 2314 req->Start(); |
| 1586 base::RunLoop().Run(); | 2315 base::RunLoop().Run(); |
| 1587 | 2316 |
| 1588 LoadTimingInfo resulting_load_timing; | 2317 LoadTimingInfo resulting_load_timing; |
| 1589 req->GetLoadTimingInfo(&resulting_load_timing); | 2318 req->GetLoadTimingInfo(&resulting_load_timing); |
| 1590 | 2319 |
| 1591 // None of these should be modified by the URLRequest. | 2320 // None of these should be modified by the URLRequest. |
| 1592 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); | 2321 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1593 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); | 2322 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1594 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); | 2323 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1595 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); | 2324 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1596 EXPECT_EQ(job_load_timing.receive_headers_end, | 2325 EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1597 resulting_load_timing.receive_headers_end); | 2326 resulting_load_timing.receive_headers_end); |
| 1598 | 2327 |
| 1599 return resulting_load_timing; | 2328 return resulting_load_timing; |
| 1600 } | 2329 } |
| 1601 | 2330 |
| 1602 // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not | |
| 1603 // reused. |connect_time_flags| is used to indicate if there should be dns | |
| 1604 // or SSL times, and |used_proxy| is used for proxy times. | |
| 1605 LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, | |
| 1606 int connect_time_flags, | |
| 1607 bool used_proxy) { | |
| 1608 LoadTimingInfo load_timing; | |
| 1609 load_timing.socket_log_id = 1; | |
| 1610 | |
| 1611 if (used_proxy) { | |
| 1612 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); | |
| 1613 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); | |
| 1614 } | |
| 1615 | |
| 1616 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; | |
| 1617 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { | |
| 1618 connect_timing.dns_start = now + base::TimeDelta::FromDays(3); | |
| 1619 connect_timing.dns_end = now + base::TimeDelta::FromDays(4); | |
| 1620 } | |
| 1621 connect_timing.connect_start = now + base::TimeDelta::FromDays(5); | |
| 1622 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { | |
| 1623 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); | |
| 1624 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); | |
| 1625 } | |
| 1626 connect_timing.connect_end = now + base::TimeDelta::FromDays(8); | |
| 1627 | |
| 1628 load_timing.send_start = now + base::TimeDelta::FromDays(9); | |
| 1629 load_timing.send_end = now + base::TimeDelta::FromDays(10); | |
| 1630 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); | |
| 1631 return load_timing; | |
| 1632 } | |
| 1633 | |
| 1634 // Same as above, but in the case of a reused socket. | |
| 1635 LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, | |
| 1636 bool used_proxy) { | |
| 1637 LoadTimingInfo load_timing; | |
| 1638 load_timing.socket_log_id = 1; | |
| 1639 load_timing.socket_reused = true; | |
| 1640 | |
| 1641 if (used_proxy) { | |
| 1642 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); | |
| 1643 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); | |
| 1644 } | |
| 1645 | |
| 1646 load_timing.send_start = now + base::TimeDelta::FromDays(9); | |
| 1647 load_timing.send_end = now + base::TimeDelta::FromDays(10); | |
| 1648 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); | |
| 1649 return load_timing; | |
| 1650 } | |
| 1651 | |
| 1652 // Basic test that the intercept + load timing tests work. | 2331 // Basic test that the intercept + load timing tests work. |
| 1653 TEST_F(URLRequestTest, InterceptLoadTiming) { | 2332 TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
| 1654 base::TimeTicks now = base::TimeTicks::Now(); | 2333 base::TimeTicks now = base::TimeTicks::Now(); |
| 1655 LoadTimingInfo job_load_timing = | 2334 LoadTimingInfo job_load_timing = |
| 1656 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); | 2335 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1657 | 2336 |
| 1658 LoadTimingInfo load_timing_result = | 2337 LoadTimingInfo load_timing_result = |
| 1659 RunLoadTimingTest(job_load_timing, &default_context_); | 2338 RunURLRequestInterceptorLoadTimingTest( |
| 2339 job_load_timing, &default_context_, interceptor_); | |
| 1660 | 2340 |
| 1661 // Nothing should have been changed by the URLRequest. | 2341 // Nothing should have been changed by the URLRequest. |
| 1662 EXPECT_EQ(job_load_timing.proxy_resolve_start, | 2342 EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1663 load_timing_result.proxy_resolve_start); | 2343 load_timing_result.proxy_resolve_start); |
| 1664 EXPECT_EQ(job_load_timing.proxy_resolve_end, | 2344 EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1665 load_timing_result.proxy_resolve_end); | 2345 load_timing_result.proxy_resolve_end); |
| 1666 EXPECT_EQ(job_load_timing.connect_timing.dns_start, | 2346 EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1667 load_timing_result.connect_timing.dns_start); | 2347 load_timing_result.connect_timing.dns_start); |
| 1668 EXPECT_EQ(job_load_timing.connect_timing.dns_end, | 2348 EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1669 load_timing_result.connect_timing.dns_end); | 2349 load_timing_result.connect_timing.dns_end); |
| 1670 EXPECT_EQ(job_load_timing.connect_timing.connect_start, | 2350 EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1671 load_timing_result.connect_timing.connect_start); | 2351 load_timing_result.connect_timing.connect_start); |
| 1672 EXPECT_EQ(job_load_timing.connect_timing.connect_end, | 2352 EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1673 load_timing_result.connect_timing.connect_end); | 2353 load_timing_result.connect_timing.connect_end); |
| 1674 EXPECT_EQ(job_load_timing.connect_timing.ssl_start, | 2354 EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1675 load_timing_result.connect_timing.ssl_start); | 2355 load_timing_result.connect_timing.ssl_start); |
| 1676 EXPECT_EQ(job_load_timing.connect_timing.ssl_end, | 2356 EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1677 load_timing_result.connect_timing.ssl_end); | 2357 load_timing_result.connect_timing.ssl_end); |
| 1678 | 2358 |
| 1679 // Redundant sanity check. | 2359 // Redundant sanity check. |
| 1680 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); | 2360 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1681 } | 2361 } |
| 1682 | 2362 |
| 1683 // Another basic test, with proxy and SSL times, but no DNS times. | 2363 // Another basic test, with proxy and SSL times, but no DNS times. |
| 1684 TEST_F(URLRequestTest, InterceptLoadTimingProxy) { | 2364 TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
| 1685 base::TimeTicks now = base::TimeTicks::Now(); | 2365 base::TimeTicks now = base::TimeTicks::Now(); |
| 1686 LoadTimingInfo job_load_timing = | 2366 LoadTimingInfo job_load_timing = |
| 1687 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); | 2367 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1688 | 2368 |
| 1689 LoadTimingInfo load_timing_result = | 2369 LoadTimingInfo load_timing_result = |
| 1690 RunLoadTimingTest(job_load_timing, &default_context_); | 2370 RunURLRequestInterceptorLoadTimingTest( |
| 2371 job_load_timing, &default_context_, interceptor_); | |
| 1691 | 2372 |
| 1692 // Nothing should have been changed by the URLRequest. | 2373 // Nothing should have been changed by the URLRequest. |
| 1693 EXPECT_EQ(job_load_timing.proxy_resolve_start, | 2374 EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1694 load_timing_result.proxy_resolve_start); | 2375 load_timing_result.proxy_resolve_start); |
| 1695 EXPECT_EQ(job_load_timing.proxy_resolve_end, | 2376 EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1696 load_timing_result.proxy_resolve_end); | 2377 load_timing_result.proxy_resolve_end); |
| 1697 EXPECT_EQ(job_load_timing.connect_timing.dns_start, | 2378 EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1698 load_timing_result.connect_timing.dns_start); | 2379 load_timing_result.connect_timing.dns_start); |
| 1699 EXPECT_EQ(job_load_timing.connect_timing.dns_end, | 2380 EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1700 load_timing_result.connect_timing.dns_end); | 2381 load_timing_result.connect_timing.dns_end); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1711 TestLoadTimingNotReusedWithProxy(load_timing_result, | 2392 TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1712 CONNECT_TIMING_HAS_SSL_TIMES); | 2393 CONNECT_TIMING_HAS_SSL_TIMES); |
| 1713 } | 2394 } |
| 1714 | 2395 |
| 1715 // Make sure that URLRequest correctly adjusts proxy times when they're before | 2396 // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1716 // |request_start|, due to already having a connected socket. This happens in | 2397 // |request_start|, due to already having a connected socket. This happens in |
| 1717 // the case of reusing a SPDY session. The connected socket is not considered | 2398 // the case of reusing a SPDY session. The connected socket is not considered |
| 1718 // reused in this test (May be a preconnect). | 2399 // reused in this test (May be a preconnect). |
| 1719 // | 2400 // |
| 1720 // To mix things up from the test above, assumes DNS times but no SSL times. | 2401 // To mix things up from the test above, assumes DNS times but no SSL times. |
| 1721 TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) { | 2402 TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
| 1722 base::TimeTicks now = base::TimeTicks::Now(); | 2403 base::TimeTicks now = base::TimeTicks::Now(); |
| 1723 LoadTimingInfo job_load_timing = | 2404 LoadTimingInfo job_load_timing = |
| 1724 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); | 2405 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 1725 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); | 2406 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 1726 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); | 2407 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 1727 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); | 2408 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 1728 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); | 2409 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 1729 job_load_timing.connect_timing.connect_start = | 2410 job_load_timing.connect_timing.connect_start = |
| 1730 now - base::TimeDelta::FromDays(2); | 2411 now - base::TimeDelta::FromDays(2); |
| 1731 job_load_timing.connect_timing.connect_end = | 2412 job_load_timing.connect_timing.connect_end = |
| 1732 now - base::TimeDelta::FromDays(1); | 2413 now - base::TimeDelta::FromDays(1); |
| 1733 | 2414 |
| 1734 LoadTimingInfo load_timing_result = | 2415 LoadTimingInfo load_timing_result = |
| 1735 RunLoadTimingTest(job_load_timing, &default_context_); | 2416 RunURLRequestInterceptorLoadTimingTest( |
| 2417 job_load_timing, &default_context_, interceptor_); | |
| 1736 | 2418 |
| 1737 // Proxy times, connect times, and DNS times should all be replaced with | 2419 // Proxy times, connect times, and DNS times should all be replaced with |
| 1738 // request_start. | 2420 // request_start. |
| 1739 EXPECT_EQ(load_timing_result.request_start, | 2421 EXPECT_EQ(load_timing_result.request_start, |
| 1740 load_timing_result.proxy_resolve_start); | 2422 load_timing_result.proxy_resolve_start); |
| 1741 EXPECT_EQ(load_timing_result.request_start, | 2423 EXPECT_EQ(load_timing_result.request_start, |
| 1742 load_timing_result.proxy_resolve_end); | 2424 load_timing_result.proxy_resolve_end); |
| 1743 EXPECT_EQ(load_timing_result.request_start, | 2425 EXPECT_EQ(load_timing_result.request_start, |
| 1744 load_timing_result.connect_timing.dns_start); | 2426 load_timing_result.connect_timing.dns_start); |
| 1745 EXPECT_EQ(load_timing_result.request_start, | 2427 EXPECT_EQ(load_timing_result.request_start, |
| 1746 load_timing_result.connect_timing.dns_end); | 2428 load_timing_result.connect_timing.dns_end); |
| 1747 EXPECT_EQ(load_timing_result.request_start, | 2429 EXPECT_EQ(load_timing_result.request_start, |
| 1748 load_timing_result.connect_timing.connect_start); | 2430 load_timing_result.connect_timing.connect_start); |
| 1749 EXPECT_EQ(load_timing_result.request_start, | 2431 EXPECT_EQ(load_timing_result.request_start, |
| 1750 load_timing_result.connect_timing.connect_end); | 2432 load_timing_result.connect_timing.connect_end); |
| 1751 | 2433 |
| 1752 // Other times should have been left null. | 2434 // Other times should have been left null. |
| 1753 TestLoadTimingNotReusedWithProxy(load_timing_result, | 2435 TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1754 CONNECT_TIMING_HAS_DNS_TIMES); | 2436 CONNECT_TIMING_HAS_DNS_TIMES); |
| 1755 } | 2437 } |
| 1756 | 2438 |
| 1757 // Same as above, but in the reused case. | 2439 // Same as above, but in the reused case. |
| 1758 TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) { | 2440 TEST_F(URLRequestInterceptorTest, |
| 2441 InterceptLoadTimingEarlyProxyResolutionReused) { | |
| 1759 base::TimeTicks now = base::TimeTicks::Now(); | 2442 base::TimeTicks now = base::TimeTicks::Now(); |
| 1760 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); | 2443 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 1761 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); | 2444 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 1762 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); | 2445 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 1763 | 2446 |
| 1764 LoadTimingInfo load_timing_result = | 2447 LoadTimingInfo load_timing_result = |
| 1765 RunLoadTimingTest(job_load_timing, &default_context_); | 2448 RunURLRequestInterceptorLoadTimingTest( |
| 2449 job_load_timing, &default_context_, interceptor_); | |
| 1766 | 2450 |
| 1767 // Proxy times and connect times should all be replaced with request_start. | 2451 // Proxy times and connect times should all be replaced with request_start. |
| 1768 EXPECT_EQ(load_timing_result.request_start, | 2452 EXPECT_EQ(load_timing_result.request_start, |
| 1769 load_timing_result.proxy_resolve_start); | 2453 load_timing_result.proxy_resolve_start); |
| 1770 EXPECT_EQ(load_timing_result.request_start, | 2454 EXPECT_EQ(load_timing_result.request_start, |
| 1771 load_timing_result.proxy_resolve_end); | 2455 load_timing_result.proxy_resolve_end); |
| 1772 | 2456 |
| 1773 // Other times should have been left null. | 2457 // Other times should have been left null. |
| 1774 TestLoadTimingReusedWithProxy(load_timing_result); | 2458 TestLoadTimingReusedWithProxy(load_timing_result); |
| 1775 } | 2459 } |
| 1776 | 2460 |
| 1777 // Make sure that URLRequest correctly adjusts connect times when they're before | 2461 // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1778 // |request_start|, due to reusing a connected socket. The connected socket is | 2462 // |request_start|, due to reusing a connected socket. The connected socket is |
| 1779 // not considered reused in this test (May be a preconnect). | 2463 // not considered reused in this test (May be a preconnect). |
| 1780 // | 2464 // |
| 1781 // To mix things up, the request has SSL times, but no DNS times. | 2465 // To mix things up, the request has SSL times, but no DNS times. |
| 1782 TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) { | 2466 TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
| 1783 base::TimeTicks now = base::TimeTicks::Now(); | 2467 base::TimeTicks now = base::TimeTicks::Now(); |
| 1784 LoadTimingInfo job_load_timing = | 2468 LoadTimingInfo job_load_timing = |
| 1785 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); | 2469 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 1786 job_load_timing.connect_timing.connect_start = | 2470 job_load_timing.connect_timing.connect_start = |
| 1787 now - base::TimeDelta::FromDays(1); | 2471 now - base::TimeDelta::FromDays(1); |
| 1788 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); | 2472 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 1789 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); | 2473 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 1790 job_load_timing.connect_timing.connect_end = | 2474 job_load_timing.connect_timing.connect_end = |
| 1791 now - base::TimeDelta::FromDays(4); | 2475 now - base::TimeDelta::FromDays(4); |
| 1792 | 2476 |
| 1793 LoadTimingInfo load_timing_result = | 2477 LoadTimingInfo load_timing_result = |
| 1794 RunLoadTimingTest(job_load_timing, &default_context_); | 2478 RunURLRequestInterceptorLoadTimingTest( |
| 2479 job_load_timing, &default_context_, interceptor_); | |
| 1795 | 2480 |
| 1796 // Connect times, and SSL times should be replaced with request_start. | 2481 // Connect times, and SSL times should be replaced with request_start. |
| 1797 EXPECT_EQ(load_timing_result.request_start, | 2482 EXPECT_EQ(load_timing_result.request_start, |
| 1798 load_timing_result.connect_timing.connect_start); | 2483 load_timing_result.connect_timing.connect_start); |
| 1799 EXPECT_EQ(load_timing_result.request_start, | 2484 EXPECT_EQ(load_timing_result.request_start, |
| 1800 load_timing_result.connect_timing.ssl_start); | 2485 load_timing_result.connect_timing.ssl_start); |
| 1801 EXPECT_EQ(load_timing_result.request_start, | 2486 EXPECT_EQ(load_timing_result.request_start, |
| 1802 load_timing_result.connect_timing.ssl_end); | 2487 load_timing_result.connect_timing.ssl_end); |
| 1803 EXPECT_EQ(load_timing_result.request_start, | 2488 EXPECT_EQ(load_timing_result.request_start, |
| 1804 load_timing_result.connect_timing.connect_end); | 2489 load_timing_result.connect_timing.connect_end); |
| 1805 | 2490 |
| 1806 // Other times should have been left null. | 2491 // Other times should have been left null. |
| 1807 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); | 2492 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 1808 } | 2493 } |
| 1809 | 2494 |
| 1810 // Make sure that URLRequest correctly adjusts connect times when they're before | 2495 // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1811 // |request_start|, due to reusing a connected socket in the case that there | 2496 // |request_start|, due to reusing a connected socket in the case that there |
| 1812 // are also proxy times. The connected socket is not considered reused in this | 2497 // are also proxy times. The connected socket is not considered reused in this |
| 1813 // test (May be a preconnect). | 2498 // test (May be a preconnect). |
| 1814 // | 2499 // |
| 1815 // In this test, there are no SSL or DNS times. | 2500 // In this test, there are no SSL or DNS times. |
| 1816 TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) { | 2501 TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
| 1817 base::TimeTicks now = base::TimeTicks::Now(); | 2502 base::TimeTicks now = base::TimeTicks::Now(); |
| 1818 LoadTimingInfo job_load_timing = | 2503 LoadTimingInfo job_load_timing = |
| 1819 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); | 2504 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 1820 job_load_timing.connect_timing.connect_start = | 2505 job_load_timing.connect_timing.connect_start = |
| 1821 now - base::TimeDelta::FromDays(1); | 2506 now - base::TimeDelta::FromDays(1); |
| 1822 job_load_timing.connect_timing.connect_end = | 2507 job_load_timing.connect_timing.connect_end = |
| 1823 now - base::TimeDelta::FromDays(2); | 2508 now - base::TimeDelta::FromDays(2); |
| 1824 | 2509 |
| 1825 LoadTimingInfo load_timing_result = | 2510 LoadTimingInfo load_timing_result = |
| 1826 RunLoadTimingTest(job_load_timing, &default_context_); | 2511 RunURLRequestInterceptorLoadTimingTest( |
| 2512 job_load_timing, &default_context_, interceptor_); | |
| 1827 | 2513 |
| 1828 // Connect times should be replaced with proxy_resolve_end. | 2514 // Connect times should be replaced with proxy_resolve_end. |
| 1829 EXPECT_EQ(load_timing_result.proxy_resolve_end, | 2515 EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1830 load_timing_result.connect_timing.connect_start); | 2516 load_timing_result.connect_timing.connect_start); |
| 1831 EXPECT_EQ(load_timing_result.proxy_resolve_end, | 2517 EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1832 load_timing_result.connect_timing.connect_end); | 2518 load_timing_result.connect_timing.connect_end); |
| 1833 | 2519 |
| 1834 // Other times should have been left null. | 2520 // Other times should have been left null. |
| 1835 TestLoadTimingNotReusedWithProxy(load_timing_result, | 2521 TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1836 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | 2522 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| (...skipping 4602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6439 scoped_ptr<URLRequest> req(context.CreateRequest( | 7125 scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6440 GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL)); | 7126 GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL)); |
| 6441 req->Start(); | 7127 req->Start(); |
| 6442 base::RunLoop().Run(); | 7128 base::RunLoop().Run(); |
| 6443 | 7129 |
| 6444 EXPECT_TRUE(d.request_failed()); | 7130 EXPECT_TRUE(d.request_failed()); |
| 6445 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); | 7131 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6446 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); | 7132 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
| 6447 } | 7133 } |
| 6448 | 7134 |
| 7135 class URLRequestInterceptorTestHTTP : public URLRequestInterceptorTest { | |
| 7136 public: | |
| 7137 URLRequestInterceptorTestHTTP() | |
| 7138 : test_server_(base::FilePath(FILE_PATH_LITERAL( | |
| 7139 "net/data/url_request_unittest"))) { | |
| 7140 } | |
| 7141 | |
| 7142 protected: | |
| 7143 // Requests |redirect_url|, which must return a HTTP 3xx redirect. | |
| 7144 // |request_method| is the method to use for the initial request. | |
| 7145 // |redirect_method| is the method that is expected to be used for the second | |
| 7146 // request, after redirection. | |
| 7147 // If |include_data| is true, data is uploaded with the request. The | |
| 7148 // response body is expected to match it exactly, if and only if | |
| 7149 // |request_method| == |redirect_method|. | |
| 7150 void HTTPRedirectMethodTest(const GURL& redirect_url, | |
|
mmenke
2014/11/05 20:52:49
Per other comment, I'd like to see this test fixtu
| |
| 7151 const std::string& request_method, | |
| 7152 const std::string& redirect_method, | |
| 7153 bool include_data) { | |
| 7154 static const char kData[] = "hello world"; | |
| 7155 TestDelegate d; | |
| 7156 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 7157 redirect_url, DEFAULT_PRIORITY, &d, NULL)); | |
| 7158 req->set_method(request_method); | |
| 7159 if (include_data) { | |
| 7160 req->set_upload(CreateSimpleUploadData(kData)); | |
| 7161 HttpRequestHeaders headers; | |
| 7162 headers.SetHeader(HttpRequestHeaders::kContentLength, | |
| 7163 base::UintToString(arraysize(kData) - 1)); | |
| 7164 req->SetExtraRequestHeaders(headers); | |
| 7165 } | |
| 7166 req->Start(); | |
| 7167 base::RunLoop().Run(); | |
| 7168 EXPECT_EQ(redirect_method, req->method()); | |
| 7169 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); | |
| 7170 EXPECT_EQ(OK, req->status().error()); | |
| 7171 if (include_data) { | |
| 7172 if (request_method == redirect_method) { | |
| 7173 EXPECT_EQ(kData, d.data_received()); | |
| 7174 } else { | |
| 7175 EXPECT_NE(kData, d.data_received()); | |
| 7176 } | |
| 7177 } | |
| 7178 if (HasFailure()) | |
| 7179 LOG(WARNING) << "Request method was: " << request_method; | |
| 7180 } | |
| 7181 | |
| 7182 void HTTPUploadDataOperationTest(const std::string& method) { | |
| 7183 const int kMsgSize = 20000; // multiple of 10 | |
| 7184 const int kIterations = 50; | |
| 7185 char* uploadBytes = new char[kMsgSize+1]; | |
| 7186 char* ptr = uploadBytes; | |
| 7187 char marker = 'a'; | |
| 7188 for (int idx = 0; idx < kMsgSize/10; idx++) { | |
| 7189 memcpy(ptr, "----------", 10); | |
| 7190 ptr += 10; | |
| 7191 if (idx % 100 == 0) { | |
| 7192 ptr--; | |
| 7193 *ptr++ = marker; | |
| 7194 if (++marker > 'z') | |
| 7195 marker = 'a'; | |
| 7196 } | |
| 7197 } | |
| 7198 uploadBytes[kMsgSize] = '\0'; | |
| 7199 | |
| 7200 for (int i = 0; i < kIterations; ++i) { | |
| 7201 TestDelegate d; | |
| 7202 scoped_ptr<URLRequest> r(default_context_.CreateRequest( | |
| 7203 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); | |
| 7204 r->set_method(method.c_str()); | |
| 7205 | |
| 7206 r->set_upload(CreateSimpleUploadData(uploadBytes)); | |
| 7207 | |
| 7208 r->Start(); | |
| 7209 EXPECT_TRUE(r->is_pending()); | |
| 7210 | |
| 7211 base::RunLoop().Run(); | |
| 7212 | |
| 7213 ASSERT_EQ(1, d.response_started_count()) | |
| 7214 << "request failed: " << r->status().status() | |
| 7215 << ", os error: " << r->status().error(); | |
| 7216 | |
| 7217 EXPECT_FALSE(d.received_data_before_response()); | |
| 7218 EXPECT_EQ(uploadBytes, d.data_received()); | |
| 7219 } | |
| 7220 delete[] uploadBytes; | |
| 7221 } | |
| 7222 | |
| 7223 void AddChunksToUpload(URLRequest* r) { | |
| 7224 r->AppendChunkToUpload("a", 1, false); | |
| 7225 r->AppendChunkToUpload("bcd", 3, false); | |
| 7226 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); | |
| 7227 r->AppendChunkToUpload("\r\n\r\n", 4, false); | |
| 7228 r->AppendChunkToUpload("0", 1, false); | |
| 7229 r->AppendChunkToUpload("2323", 4, true); | |
| 7230 } | |
| 7231 | |
| 7232 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { | |
| 7233 // This should match the chunks sent by AddChunksToUpload(). | |
| 7234 const std::string expected_data = | |
| 7235 "abcdthis is a longer chunk than before.\r\n\r\n02323"; | |
| 7236 | |
| 7237 ASSERT_EQ(1, d->response_started_count()) | |
| 7238 << "request failed: " << r->status().status() | |
| 7239 << ", os error: " << r->status().error(); | |
| 7240 | |
| 7241 EXPECT_FALSE(d->received_data_before_response()); | |
| 7242 | |
| 7243 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); | |
| 7244 EXPECT_EQ(expected_data, d->data_received()); | |
| 7245 } | |
| 7246 | |
| 7247 bool DoManyCookiesRequest(int num_cookies) { | |
| 7248 TestDelegate d; | |
| 7249 scoped_ptr<URLRequest> r(default_context_.CreateRequest( | |
| 7250 test_server_.GetURL("set-many-cookies?" + | |
| 7251 base::IntToString(num_cookies)), | |
| 7252 DEFAULT_PRIORITY, &d, NULL)); | |
| 7253 | |
| 7254 r->Start(); | |
| 7255 EXPECT_TRUE(r->is_pending()); | |
| 7256 | |
| 7257 base::RunLoop().Run(); | |
| 7258 | |
| 7259 bool is_success = r->status().is_success(); | |
| 7260 | |
| 7261 if (!is_success) { | |
| 7262 EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); | |
| 7263 // The test server appears to be unable to handle subsequent requests | |
| 7264 // after this error is triggered. Force it to restart. | |
| 7265 EXPECT_TRUE(test_server_.Stop()); | |
| 7266 EXPECT_TRUE(test_server_.Start()); | |
| 7267 } | |
| 7268 | |
| 7269 return is_success; | |
| 7270 } | |
| 7271 | |
| 7272 LocalHttpTestServer test_server_; | |
| 7273 }; | |
| 7274 | |
| 7275 TEST_F(URLRequestInterceptorTestHTTP, | |
| 7276 NetworkDelegateNotificationOnRedirectIntercept) { | |
|
mmenke
2014/11/05 20:52:49
Should probably intercept the request and replace
| |
| 7277 interceptor_->intercept_redirect_ = true; | |
| 7278 interceptor_->redirect_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 7279 interceptor_->redirect_data_ = MockURLRequestInterceptor::ok_data(); | |
| 7280 | |
| 7281 ASSERT_TRUE(test_server_.Start()); | |
| 7282 | |
| 7283 TestDelegate d; | |
| 7284 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 7285 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, | |
| 7286 &d, NULL)); | |
| 7287 req->Start(); | |
| 7288 base::RunLoop().Run(); | |
| 7289 | |
| 7290 EXPECT_TRUE(interceptor_->did_intercept_redirect_); | |
| 7291 // Check we got one good response | |
| 7292 EXPECT_TRUE(req->status().is_success()); | |
| 7293 if (req->status().is_success()) { | |
| 7294 EXPECT_EQ(200, req->response_headers()->response_code()); | |
| 7295 } | |
| 7296 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); | |
| 7297 EXPECT_EQ(1, d.response_started_count()); | |
| 7298 EXPECT_EQ(0, d.received_redirect_count()); | |
| 7299 | |
| 7300 EXPECT_EQ(1, default_network_delegate_.created_requests()); | |
| 7301 EXPECT_EQ(1, default_network_delegate_.before_send_headers_count()); | |
| 7302 EXPECT_EQ(1, default_network_delegate_.headers_received_count()); | |
| 7303 } | |
| 7304 | |
| 7305 TEST_F(URLRequestInterceptorTestHTTP, | |
| 7306 NetworkDelegateNotificationOnErrorIntercept) { | |
| 7307 // Intercept that error and respond with an OK response. | |
| 7308 interceptor_->intercept_final_response_ = true; | |
| 7309 interceptor_->final_headers_ = MockURLRequestInterceptor::ok_headers(); | |
| 7310 interceptor_->final_data_ = MockURLRequestInterceptor::ok_data(); | |
| 7311 default_network_delegate_.set_can_be_intercepted_on_error(true); | |
| 7312 | |
| 7313 ASSERT_TRUE(test_server_.Start()); | |
| 7314 | |
| 7315 TestDelegate d; | |
| 7316 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 7317 test_server_.GetURL("files/two-content-lengths.html"), DEFAULT_PRIORITY, | |
| 7318 &d, NULL)); | |
| 7319 req->set_method("GET"); | |
| 7320 req->Start(); | |
| 7321 base::RunLoop().Run(); | |
| 7322 | |
| 7323 EXPECT_TRUE(interceptor_->did_intercept_final_); | |
| 7324 | |
| 7325 // Check we received one good response. | |
| 7326 EXPECT_TRUE(req->status().is_success()); | |
| 7327 EXPECT_EQ(200, req->response_headers()->response_code()); | |
| 7328 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); | |
| 7329 EXPECT_EQ(1, d.response_started_count()); | |
| 7330 EXPECT_EQ(0, d.received_redirect_count()); | |
| 7331 | |
| 7332 EXPECT_EQ(1, default_network_delegate_.created_requests()); | |
| 7333 EXPECT_EQ(1, default_network_delegate_.before_send_headers_count()); | |
| 7334 EXPECT_EQ(0, default_network_delegate_.headers_received_count()); | |
| 7335 } | |
| 7336 | |
| 7337 TEST_F(URLRequestInterceptorTestHTTP, | |
| 7338 NetworkDelegateNotificationOnResponseIntercept) { | |
| 7339 // Intercept that error and respond with an OK response. | |
| 7340 interceptor_->intercept_final_response_ = true; | |
| 7341 | |
| 7342 // Intercept with a real URLRequestHttpJob. | |
| 7343 interceptor_->use_url_request_http_job_ = true; | |
| 7344 | |
| 7345 ASSERT_TRUE(test_server_.Start()); | |
| 7346 | |
| 7347 TestDelegate d; | |
| 7348 scoped_ptr<URLRequest> req(default_context_.CreateRequest( | |
| 7349 test_server_.GetURL("files/simple.html"), DEFAULT_PRIORITY, | |
| 7350 &d, NULL)); | |
| 7351 req->set_method("GET"); | |
| 7352 req->Start(); | |
| 7353 base::RunLoop().Run(); | |
| 7354 | |
| 7355 EXPECT_TRUE(interceptor_->did_intercept_final_); | |
| 7356 | |
| 7357 // Check we received one good response. | |
| 7358 EXPECT_TRUE(req->status().is_success()); | |
| 7359 EXPECT_EQ(200, req->response_headers()->response_code()); | |
| 7360 EXPECT_EQ("hello", d.data_received()); | |
| 7361 EXPECT_EQ(1, d.response_started_count()); | |
| 7362 EXPECT_EQ(0, d.received_redirect_count()); | |
| 7363 | |
| 7364 EXPECT_EQ(1, default_network_delegate_.created_requests()); | |
| 7365 EXPECT_EQ(2, default_network_delegate_.before_send_headers_count()); | |
| 7366 EXPECT_EQ(2, default_network_delegate_.headers_received_count()); | |
| 7367 } | |
| 7368 | |
| 7369 | |
| 6449 class HTTPSRequestTest : public testing::Test { | 7370 class HTTPSRequestTest : public testing::Test { |
| 6450 public: | 7371 public: |
| 6451 HTTPSRequestTest() : default_context_(true) { | 7372 HTTPSRequestTest() : default_context_(true) { |
| 6452 default_context_.set_network_delegate(&default_network_delegate_); | 7373 default_context_.set_network_delegate(&default_network_delegate_); |
| 6453 default_context_.Init(); | 7374 default_context_.Init(); |
| 6454 } | 7375 } |
| 6455 ~HTTPSRequestTest() override {} | 7376 ~HTTPSRequestTest() override {} |
| 6456 | 7377 |
| 6457 protected: | 7378 protected: |
| 6458 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. | 7379 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| (...skipping 1828 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 8287 | 9208 |
| 8288 EXPECT_FALSE(r->is_pending()); | 9209 EXPECT_FALSE(r->is_pending()); |
| 8289 EXPECT_EQ(1, d->response_started_count()); | 9210 EXPECT_EQ(1, d->response_started_count()); |
| 8290 EXPECT_FALSE(d->received_data_before_response()); | 9211 EXPECT_FALSE(d->received_data_before_response()); |
| 8291 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); | 9212 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 8292 } | 9213 } |
| 8293 } | 9214 } |
| 8294 #endif // !defined(DISABLE_FTP_SUPPORT) | 9215 #endif // !defined(DISABLE_FTP_SUPPORT) |
| 8295 | 9216 |
| 8296 } // namespace net | 9217 } // namespace net |
| OLD | NEW |