| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/http/http_stream_factory_impl_request.h" | 5 #include "net/http/http_stream_factory_impl_request.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "net/http/http_stream_factory_impl.h" |
| 10 #include "net/http/http_stream_factory_impl_job.h" | 11 #include "net/http/http_stream_factory_impl_job.h" |
| 12 #include "net/http/http_stream_factory_impl_job_controller.h" |
| 13 #include "net/http/http_stream_factory_test_util.h" |
| 11 #include "net/proxy/proxy_info.h" | 14 #include "net/proxy/proxy_info.h" |
| 12 #include "net/proxy/proxy_service.h" | 15 #include "net/proxy/proxy_service.h" |
| 13 #include "net/spdy/spdy_test_util_common.h" | 16 #include "net/spdy/spdy_test_util_common.h" |
| 14 #include "net/ssl/ssl_failure_state.h" | 17 #include "net/ssl/ssl_failure_state.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 19 |
| 20 using testing::_; |
| 21 |
| 17 namespace net { | 22 namespace net { |
| 18 | 23 |
| 19 class HttpStreamFactoryImplRequestTest | 24 class HttpStreamFactoryImplRequestTest |
| 20 : public ::testing::Test, | 25 : public ::testing::Test, |
| 21 public ::testing::WithParamInterface<NextProto> {}; | 26 public ::testing::WithParamInterface<NextProto> {}; |
| 22 | 27 |
| 23 INSTANTIATE_TEST_CASE_P(NextProto, | 28 INSTANTIATE_TEST_CASE_P(NextProto, |
| 24 HttpStreamFactoryImplRequestTest, | 29 HttpStreamFactoryImplRequestTest, |
| 25 testing::Values(kProtoSPDY31, | 30 testing::Values(kProtoSPDY31, |
| 26 kProtoHTTP2)); | 31 kProtoHTTP2)); |
| 27 | 32 |
| 28 namespace { | |
| 29 | |
| 30 class DoNothingRequestDelegate : public HttpStreamRequest::Delegate { | |
| 31 public: | |
| 32 DoNothingRequestDelegate() {} | |
| 33 | |
| 34 ~DoNothingRequestDelegate() override {} | |
| 35 | |
| 36 // HttpStreamRequest::Delegate | |
| 37 void OnStreamReady(const SSLConfig& used_ssl_config, | |
| 38 const ProxyInfo& used_proxy_info, | |
| 39 HttpStream* stream) override {} | |
| 40 void OnBidirectionalStreamImplReady( | |
| 41 const SSLConfig& used_ssl_config, | |
| 42 const ProxyInfo& used_proxy_info, | |
| 43 BidirectionalStreamImpl* stream) override {} | |
| 44 | |
| 45 void OnWebSocketHandshakeStreamReady( | |
| 46 const SSLConfig& used_ssl_config, | |
| 47 const ProxyInfo& used_proxy_info, | |
| 48 WebSocketHandshakeStreamBase* stream) override {} | |
| 49 void OnStreamFailed(int status, | |
| 50 const SSLConfig& used_ssl_config, | |
| 51 SSLFailureState ssl_failure_state) override {} | |
| 52 void OnCertificateError(int status, | |
| 53 const SSLConfig& used_ssl_config, | |
| 54 const SSLInfo& ssl_info) override {} | |
| 55 void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response, | |
| 56 const SSLConfig& used_ssl_config, | |
| 57 const ProxyInfo& used_proxy_info, | |
| 58 HttpAuthController* auth_controller) override {} | |
| 59 void OnNeedsClientAuth(const SSLConfig& used_ssl_config, | |
| 60 SSLCertRequestInfo* cert_info) override {} | |
| 61 void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info, | |
| 62 const SSLConfig& used_ssl_config, | |
| 63 const ProxyInfo& used_proxy_info, | |
| 64 HttpStream* stream) override {} | |
| 65 void OnQuicBroken() override {} | |
| 66 }; | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 // Make sure that Request passes on its priority updates to its jobs. | 33 // Make sure that Request passes on its priority updates to its jobs. |
| 71 TEST_P(HttpStreamFactoryImplRequestTest, SetPriority) { | 34 TEST_P(HttpStreamFactoryImplRequestTest, SetPriority) { |
| 72 SpdySessionDependencies session_deps(GetParam(), | 35 SpdySessionDependencies session_deps(GetParam(), |
| 73 ProxyService::CreateDirect()); | 36 ProxyService::CreateDirect()); |
| 74 | |
| 75 std::unique_ptr<HttpNetworkSession> session = | 37 std::unique_ptr<HttpNetworkSession> session = |
| 76 SpdySessionDependencies::SpdyCreateSession(&session_deps); | 38 SpdySessionDependencies::SpdyCreateSession(&session_deps); |
| 77 HttpStreamFactoryImpl* factory = | 39 HttpStreamFactoryImpl* factory = |
| 78 static_cast<HttpStreamFactoryImpl*>(session->http_stream_factory()); | 40 static_cast<HttpStreamFactoryImpl*>(session->http_stream_factory()); |
| 79 | 41 MockHttpStreamRequestDelegate request_delegate; |
| 80 DoNothingRequestDelegate request_delegate; | 42 TestJobFactory job_factory; |
| 81 HttpStreamFactoryImpl::Request request( | 43 HttpStreamFactoryImpl::JobController* job_controller = |
| 82 GURL(), factory, &request_delegate, NULL, BoundNetLog(), | 44 new HttpStreamFactoryImpl::JobController(factory, &request_delegate, |
| 83 HttpStreamFactoryImpl::Request::HTTP_STREAM); | 45 session.get(), &job_factory); |
| 46 factory->job_controller_set_.insert(base::WrapUnique(job_controller)); |
| 84 | 47 |
| 85 HttpRequestInfo request_info; | 48 HttpRequestInfo request_info; |
| 49 std::unique_ptr<HttpStreamFactoryImpl::Request> request( |
| 50 job_controller->Start(request_info, &request_delegate, nullptr, |
| 51 BoundNetLog(), HttpStreamRequest::HTTP_STREAM, |
| 52 DEFAULT_PRIORITY, SSLConfig(), SSLConfig())); |
| 53 EXPECT_TRUE(job_controller->main_job()); |
| 54 EXPECT_EQ(DEFAULT_PRIORITY, job_controller->main_job()->priority()); |
| 86 | 55 |
| 87 HostPortPair server = HostPortPair::FromURL(request_info.url); | 56 request->SetPriority(MEDIUM); |
| 88 GURL original_url = factory->ApplyHostMappingRules(request_info.url, &server); | 57 EXPECT_EQ(MEDIUM, job_controller->main_job()->priority()); |
| 89 | 58 |
| 90 HttpStreamFactoryImpl::Job* job = new HttpStreamFactoryImpl::Job( | 59 EXPECT_CALL(request_delegate, OnStreamFailed(_, _, SSL_FAILURE_NONE)) |
| 91 factory, session.get(), request_info, DEFAULT_PRIORITY, SSLConfig(), | 60 .Times(1); |
| 92 SSLConfig(), server, original_url, NULL); | 61 job_controller->OnStreamFailed(job_factory.main_job(), ERR_FAILED, |
| 93 request.AttachJob(job); | 62 SSLConfig(), SSL_FAILURE_NONE); |
| 94 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); | |
| 95 | 63 |
| 96 request.SetPriority(MEDIUM); | 64 request->SetPriority(IDLE); |
| 97 EXPECT_EQ(MEDIUM, job->priority()); | 65 EXPECT_EQ(IDLE, job_controller->main_job()->priority()); |
| 98 | |
| 99 // Make |job| the bound job. | |
| 100 request.OnStreamFailed(job, ERR_FAILED, SSLConfig(), SSL_FAILURE_NONE); | |
| 101 | |
| 102 request.SetPriority(IDLE); | |
| 103 EXPECT_EQ(IDLE, job->priority()); | |
| 104 } | 66 } |
| 105 | 67 |
| 106 TEST_P(HttpStreamFactoryImplRequestTest, DelayMainJob) { | 68 TEST_P(HttpStreamFactoryImplRequestTest, DelayMainJob) { |
| 107 SpdySessionDependencies session_deps(GetParam(), | 69 SpdySessionDependencies session_deps(GetParam(), |
| 108 ProxyService::CreateDirect()); | 70 ProxyService::CreateDirect()); |
| 109 | 71 |
| 110 std::unique_ptr<HttpNetworkSession> session = | 72 std::unique_ptr<HttpNetworkSession> session = |
| 111 SpdySessionDependencies::SpdyCreateSession(&session_deps); | 73 SpdySessionDependencies::SpdyCreateSession(&session_deps); |
| 112 | 74 |
| 113 StaticSocketDataProvider socket_data; | 75 StaticSocketDataProvider socket_data; |
| 114 socket_data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); | 76 socket_data.set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); |
| 115 session_deps.socket_factory->AddSocketDataProvider(&socket_data); | 77 session_deps.socket_factory->AddSocketDataProvider(&socket_data); |
| 116 | 78 |
| 117 HttpStreamFactoryImpl* factory = | 79 HttpStreamFactoryImpl* factory = |
| 118 static_cast<HttpStreamFactoryImpl*>(session->http_stream_factory()); | 80 static_cast<HttpStreamFactoryImpl*>(session->http_stream_factory()); |
| 81 MockHttpStreamRequestDelegate request_delegate; |
| 82 HttpStreamFactoryImpl::JobFactory* job_factory = |
| 83 HttpStreamFactoryImplPeer::GetDefaultJobFactory(factory); |
| 84 HttpStreamFactoryImpl::JobController* job_controller = |
| 85 new HttpStreamFactoryImpl::JobController(factory, &request_delegate, |
| 86 session.get(), job_factory); |
| 87 factory->job_controller_set_.insert(base::WrapUnique(job_controller)); |
| 119 | 88 |
| 120 DoNothingRequestDelegate request_delegate; | |
| 121 HttpRequestInfo request_info; | 89 HttpRequestInfo request_info; |
| 122 request_info.method = "GET"; | 90 request_info.method = "GET"; |
| 123 request_info.url = GURL("http://www.google.com"); | 91 request_info.url = GURL("http://www.google.com"); |
| 124 | 92 |
| 125 HttpStreamFactoryImpl::Request request( | 93 HttpStreamFactoryImpl::Request request( |
| 126 request_info.url, factory, &request_delegate, NULL, BoundNetLog(), | 94 request_info.url, job_controller, &request_delegate, nullptr, |
| 127 HttpStreamFactoryImpl::Request::HTTP_STREAM); | 95 BoundNetLog(), HttpStreamFactoryImpl::Request::HTTP_STREAM); |
| 96 job_controller->request_ = &request; |
| 128 | 97 |
| 129 HostPortPair server = HostPortPair::FromURL(request_info.url); | 98 HostPortPair server = HostPortPair::FromURL(request_info.url); |
| 130 GURL original_url = factory->ApplyHostMappingRules(request_info.url, &server); | 99 GURL original_url = |
| 100 job_controller->ApplyHostMappingRules(request_info.url, &server); |
| 131 | 101 |
| 132 HttpStreamFactoryImpl::Job* job = new HttpStreamFactoryImpl::Job( | 102 HttpStreamFactoryImpl::Job* job = new HttpStreamFactoryImpl::Job( |
| 133 factory, session.get(), request_info, DEFAULT_PRIORITY, SSLConfig(), | 103 job_controller, HttpStreamFactoryImpl::MAIN, session.get(), request_info, |
| 134 SSLConfig(), server, original_url, NULL); | 104 DEFAULT_PRIORITY, SSLConfig(), SSLConfig(), server, original_url, |
| 135 request.AttachJob(job); | 105 nullptr); |
| 106 job_controller->main_job_.reset(job); |
| 107 job_controller->AttachJob(job); |
| 136 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); | 108 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 137 | 109 |
| 138 AlternativeService alternative_service(net::NPN_HTTP_2, server); | 110 AlternativeService alternative_service(net::NPN_HTTP_2, server); |
| 139 HttpStreamFactoryImpl::Job* alternative_job = new HttpStreamFactoryImpl::Job( | 111 HttpStreamFactoryImpl::Job* alternative_job = new HttpStreamFactoryImpl::Job( |
| 140 factory, session.get(), request_info, DEFAULT_PRIORITY, SSLConfig(), | 112 job_controller, HttpStreamFactoryImpl::ALTERNATIVE, session.get(), |
| 141 SSLConfig(), server, original_url, alternative_service, NULL); | 113 request_info, DEFAULT_PRIORITY, SSLConfig(), SSLConfig(), server, |
| 142 request.AttachJob(alternative_job); | 114 original_url, alternative_service, nullptr); |
| 115 job_controller->alternative_job_.reset(alternative_job); |
| 116 job_controller->AttachJob(alternative_job); |
| 143 | 117 |
| 144 job->WaitFor(alternative_job); | 118 job->WaitFor(alternative_job); |
| 145 EXPECT_EQ(HttpStreamFactoryImpl::Job::STATE_NONE, job->next_state_); | 119 EXPECT_EQ(HttpStreamFactoryImpl::Job::STATE_NONE, job->next_state_); |
| 146 | 120 |
| 147 // Test |alternative_job| resuming the |job| after delay. | 121 // Test |alternative_job| resuming the |job| after delay. |
| 148 int wait_time = 1; | 122 int wait_time = 1; |
| 149 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(wait_time); | 123 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(wait_time); |
| 150 job->Resume(alternative_job, delay); | 124 job->Resume(alternative_job, delay); |
| 151 | 125 |
| 152 // Verify |job| has |wait_time_| and there is no |blocking_job_| | 126 // Verify |job| has |wait_time_| and there is no |blocking_job_| |
| 153 EXPECT_EQ(delay, job->wait_time_); | 127 EXPECT_EQ(delay, job->wait_time_); |
| 154 EXPECT_TRUE(!job->blocking_job_); | 128 EXPECT_TRUE(!job->blocking_job_); |
| 155 | 129 |
| 156 // Start the |job| and verify |job|'s |wait_time_| is cleared. | 130 // Start the |job| and verify |job|'s |wait_time_| is cleared. |
| 157 job->Start(&request); | 131 job->Start(request.stream_type()); |
| 158 | 132 |
| 159 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(wait_time + 1)); | 133 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(wait_time + 1)); |
| 160 base::RunLoop().RunUntilIdle(); | 134 base::RunLoop().RunUntilIdle(); |
| 161 | 135 |
| 162 EXPECT_NE(delay, job->wait_time_); | 136 EXPECT_NE(delay, job->wait_time_); |
| 163 EXPECT_TRUE(job->wait_time_.is_zero()); | 137 EXPECT_TRUE(job->wait_time_.is_zero()); |
| 164 EXPECT_EQ(HttpStreamFactoryImpl::Job::STATE_INIT_CONNECTION_COMPLETE, | 138 EXPECT_EQ(HttpStreamFactoryImpl::Job::STATE_INIT_CONNECTION_COMPLETE, |
| 165 job->next_state_); | 139 job->next_state_); |
| 166 } | 140 } |
| 167 | 141 |
| 168 } // namespace net | 142 } // namespace net |
| OLD | NEW |