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