[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/url_request/url_request_test_util.h" |
| 6 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame^] | 7 | #include <utility> |
| 8 | |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 9 | #include "base/compiler_specific.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 10 | #include "base/location.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
| 14 | #include "base/thread_task_runner_handle.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 15 | #include "base/threading/thread.h" |
[email protected] | bf470dfc | 2012-05-15 21:13:45 | [diff] [blame] | 16 | #include "base/threading/worker_pool.h" |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 17 | #include "net/base/host_port_pair.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 18 | #include "net/cert/cert_verifier.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 19 | #include "net/dns/mock_host_resolver.h" |
[email protected] | 57cb0f7 | 2011-01-28 06:33:58 | [diff] [blame] | 20 | #include "net/http/http_network_session.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 21 | #include "net/http/http_response_headers.h" |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 22 | #include "net/http/http_server_properties_impl.h" |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 23 | #include "net/http/transport_security_state.h" |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 24 | #include "net/ssl/channel_id_service.h" |
| 25 | #include "net/ssl/default_channel_id_store.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 26 | #include "net/url_request/static_http_user_agent_settings.h" |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 27 | #include "net/url_request/url_request_job.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 28 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 0140bd3 | 2012-03-21 01:47:12 | [diff] [blame] | 29 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 30 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 31 | namespace net { |
| 32 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 33 | namespace { |
| 34 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 35 | // These constants put the NetworkDelegate events of TestNetworkDelegate |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 36 | // into an order. They are used in conjunction with |
| 37 | // |TestNetworkDelegate::next_states_| to check that we do not send |
| 38 | // events in the wrong order. |
| 39 | const int kStageBeforeURLRequest = 1 << 0; |
| 40 | const int kStageBeforeSendHeaders = 1 << 1; |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 41 | const int kStageSendHeaders = 1 << 2; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 42 | const int kStageHeadersReceived = 1 << 3; |
| 43 | const int kStageAuthRequired = 1 << 4; |
| 44 | const int kStageBeforeRedirect = 1 << 5; |
| 45 | const int kStageResponseStarted = 1 << 6; |
| 46 | const int kStageCompletedSuccess = 1 << 7; |
| 47 | const int kStageCompletedError = 1 << 8; |
| 48 | const int kStageURLRequestDestroyed = 1 << 9; |
| 49 | const int kStageDestruction = 1 << 10; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 50 | |
| 51 | } // namespace |
| 52 | |
[email protected] | 529623e | 2011-02-23 17:37:16 | [diff] [blame] | 53 | TestURLRequestContext::TestURLRequestContext() |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 54 | : initialized_(false), |
[email protected] | 4549925 | 2013-01-23 17:12:56 | [diff] [blame] | 55 | client_socket_factory_(NULL), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 56 | context_storage_(this) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 57 | Init(); |
| 58 | } |
| 59 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 60 | TestURLRequestContext::TestURLRequestContext(bool delay_initialization) |
| 61 | : initialized_(false), |
[email protected] | 4549925 | 2013-01-23 17:12:56 | [diff] [blame] | 62 | client_socket_factory_(NULL), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 63 | context_storage_(this) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 64 | if (!delay_initialization) |
| 65 | Init(); |
| 66 | } |
| 67 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 68 | TestURLRequestContext::~TestURLRequestContext() { |
| 69 | DCHECK(initialized_); |
[email protected] | 42455949 | 2014-07-22 00:27:40 | [diff] [blame] | 70 | AssertNoURLRequests(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 71 | } |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 72 | |
| 73 | void TestURLRequestContext::Init() { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 74 | DCHECK(!initialized_); |
| 75 | initialized_ = true; |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 76 | |
| 77 | if (!host_resolver()) |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 78 | context_storage_.set_host_resolver( |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 79 | scoped_ptr<HostResolver>(new MockCachingHostResolver())); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 80 | if (!proxy_service()) |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 81 | context_storage_.set_proxy_service(ProxyService::CreateDirect()); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 82 | if (!cert_verifier()) |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 83 | context_storage_.set_cert_verifier(CertVerifier::CreateDefault()); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 84 | if (!transport_security_state()) |
rdsmith | 82957ad | 2015-09-16 19:42:03 | [diff] [blame] | 85 | context_storage_.set_transport_security_state( |
| 86 | make_scoped_ptr(new TransportSecurityState())); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 87 | if (!ssl_config_service()) |
rdsmith | 82957ad | 2015-09-16 19:42:03 | [diff] [blame] | 88 | context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults()); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 89 | if (!http_auth_handler_factory()) { |
| 90 | context_storage_.set_http_auth_handler_factory( |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 91 | HttpAuthHandlerFactory::CreateDefault(host_resolver())); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 92 | } |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 93 | if (!http_server_properties()) { |
| 94 | context_storage_.set_http_server_properties( |
[email protected] | 30d4c02 | 2013-07-18 22:58:16 | [diff] [blame] | 95 | scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl())); |
[email protected] | 17291a02 | 2011-10-10 07:32:53 | [diff] [blame] | 96 | } |
[email protected] | 4549925 | 2013-01-23 17:12:56 | [diff] [blame] | 97 | if (http_transaction_factory()) { |
| 98 | // Make sure we haven't been passed an object we're not going to use. |
| 99 | EXPECT_FALSE(client_socket_factory_); |
| 100 | } else { |
| 101 | HttpNetworkSession::Params params; |
[email protected] | 067ce8b0 | 2014-06-02 22:20:02 | [diff] [blame] | 102 | if (http_network_session_params_) |
| 103 | params = *http_network_session_params_; |
[email protected] | 4549925 | 2013-01-23 17:12:56 | [diff] [blame] | 104 | params.client_socket_factory = client_socket_factory(); |
| 105 | params.host_resolver = host_resolver(); |
| 106 | params.cert_verifier = cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 107 | params.transport_security_state = transport_security_state(); |
[email protected] | 4549925 | 2013-01-23 17:12:56 | [diff] [blame] | 108 | params.proxy_service = proxy_service(); |
| 109 | params.ssl_config_service = ssl_config_service(); |
| 110 | params.http_auth_handler_factory = http_auth_handler_factory(); |
| 111 | params.network_delegate = network_delegate(); |
| 112 | params.http_server_properties = http_server_properties(); |
| 113 | params.net_log = net_log(); |
mmenke | e65e7af | 2015-10-13 17:16:42 | [diff] [blame] | 114 | context_storage_.set_http_network_session( |
| 115 | make_scoped_ptr(new HttpNetworkSession(params))); |
| 116 | context_storage_.set_http_transaction_factory(make_scoped_ptr( |
| 117 | new HttpCache(context_storage_.http_network_session(), |
| 118 | HttpCache::DefaultBackend::InMemory(0), false))); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 119 | } |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 120 | // In-memory cookie store. |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 121 | if (!cookie_store()) |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 122 | context_storage_.set_cookie_store(new CookieMonster(NULL, NULL)); |
[email protected] | 6b8a3c74 | 2014-07-25 00:25:35 | [diff] [blame] | 123 | // In-memory Channel ID service. |
| 124 | if (!channel_id_service()) { |
mattm | 368d9462 | 2015-01-21 13:54:00 | [diff] [blame] | 125 | context_storage_.set_channel_id_service(make_scoped_ptr( |
| 126 | new ChannelIDService(new DefaultChannelIDStore(NULL), |
| 127 | base::WorkerPool::GetTaskRunner(true)))); |
[email protected] | eb7974c | 2012-01-25 17:38:48 | [diff] [blame] | 128 | } |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 129 | if (!http_user_agent_settings()) { |
rdsmith | 12c553e | 2015-09-30 22:44:21 | [diff] [blame] | 130 | context_storage_.set_http_user_agent_settings(make_scoped_ptr( |
| 131 | new StaticHttpUserAgentSettings("en-us,fr", std::string()))); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 132 | } |
rdsmith | 82957ad | 2015-09-16 19:42:03 | [diff] [blame] | 133 | if (!job_factory()) { |
| 134 | context_storage_.set_job_factory( |
rdsmith | 12c553e | 2015-09-30 22:44:21 | [diff] [blame] | 135 | make_scoped_ptr(new URLRequestJobFactoryImpl())); |
rdsmith | 82957ad | 2015-09-16 19:42:03 | [diff] [blame] | 136 | } |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 4473860 | 2012-03-02 22:56:01 | [diff] [blame] | 139 | TestURLRequestContextGetter::TestURLRequestContextGetter( |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 140 | const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) |
| 141 | : network_task_runner_(network_task_runner) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 142 | DCHECK(network_task_runner_.get()); |
[email protected] | 4473860 | 2012-03-02 22:56:01 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 145 | TestURLRequestContextGetter::TestURLRequestContextGetter( |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 146 | const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 147 | scoped_ptr<TestURLRequestContext> context) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame^] | 148 | : network_task_runner_(network_task_runner), context_(std::move(context)) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 149 | DCHECK(network_task_runner_.get()); |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 150 | } |
| 151 | |
[email protected] | 4473860 | 2012-03-02 22:56:01 | [diff] [blame] | 152 | TestURLRequestContextGetter::~TestURLRequestContextGetter() {} |
| 153 | |
| 154 | TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 155 | if (!context_.get()) |
| 156 | context_.reset(new TestURLRequestContext); |
[email protected] | 4473860 | 2012-03-02 22:56:01 | [diff] [blame] | 157 | return context_.get(); |
| 158 | } |
| 159 | |
[email protected] | 4969b012 | 2012-06-16 01:58:28 | [diff] [blame] | 160 | scoped_refptr<base::SingleThreadTaskRunner> |
| 161 | TestURLRequestContextGetter::GetNetworkTaskRunner() const { |
| 162 | return network_task_runner_; |
[email protected] | 4473860 | 2012-03-02 22:56:01 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 165 | TestDelegate::TestDelegate() |
| 166 | : cancel_in_rr_(false), |
| 167 | cancel_in_rs_(false), |
| 168 | cancel_in_rd_(false), |
| 169 | cancel_in_rd_pending_(false), |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 170 | quit_on_complete_(true), |
| 171 | quit_on_redirect_(false), |
mmenke | 8d457fbf | 2015-06-08 19:56:35 | [diff] [blame] | 172 | quit_on_auth_required_(false), |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 173 | quit_on_before_network_start_(false), |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 174 | allow_certificate_errors_(false), |
| 175 | response_started_count_(0), |
| 176 | received_bytes_count_(0), |
| 177 | received_redirect_count_(0), |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 178 | received_before_network_start_count_(0), |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 179 | received_data_before_response_(false), |
| 180 | request_failed_(false), |
| 181 | have_certificate_errors_(false), |
[email protected] | 46d117e | 2012-01-18 01:53:14 | [diff] [blame] | 182 | certificate_errors_are_fatal_(false), |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 183 | auth_required_(false), |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 184 | have_full_request_headers_(false), |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 185 | buf_(new IOBuffer(kBufferSize)) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TestDelegate::~TestDelegate() {} |
| 189 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 190 | void TestDelegate::ClearFullRequestHeaders() { |
| 191 | full_request_headers_.Clear(); |
| 192 | have_full_request_headers_ = false; |
| 193 | } |
| 194 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 195 | void TestDelegate::OnReceivedRedirect(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 196 | const RedirectInfo& redirect_info, |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 197 | bool* defer_redirect) { |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 198 | EXPECT_TRUE(request->is_redirecting()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 199 | |
| 200 | have_full_request_headers_ = |
| 201 | request->GetFullRequestHeaders(&full_request_headers_); |
| 202 | |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 203 | received_redirect_count_++; |
| 204 | if (quit_on_redirect_) { |
| 205 | *defer_redirect = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 206 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 207 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 208 | } else if (cancel_in_rr_) { |
| 209 | request->Cancel(); |
| 210 | } |
| 211 | } |
| 212 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 213 | void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) { |
| 214 | received_before_network_start_count_++; |
| 215 | if (quit_on_before_network_start_) { |
| 216 | *defer = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 217 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 218 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 222 | void TestDelegate::OnAuthRequired(URLRequest* request, |
| 223 | AuthChallengeInfo* auth_info) { |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 224 | auth_required_ = true; |
mmenke | 8d457fbf | 2015-06-08 19:56:35 | [diff] [blame] | 225 | if (quit_on_auth_required_) { |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 226 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 227 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
mmenke | 8d457fbf | 2015-06-08 19:56:35 | [diff] [blame] | 228 | return; |
| 229 | } |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 230 | if (!credentials_.Empty()) { |
| 231 | request->SetAuth(credentials_); |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 232 | } else { |
| 233 | request->CancelAuth(); |
| 234 | } |
| 235 | } |
| 236 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 237 | void TestDelegate::OnSSLCertificateError(URLRequest* request, |
| 238 | const SSLInfo& ssl_info, |
[email protected] | 46d117e | 2012-01-18 01:53:14 | [diff] [blame] | 239 | bool fatal) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 240 | // The caller can control whether it needs all SSL requests to go through, |
| 241 | // independent of any possible errors, or whether it wants SSL errors to |
| 242 | // cancel the request. |
| 243 | have_certificate_errors_ = true; |
[email protected] | 46d117e | 2012-01-18 01:53:14 | [diff] [blame] | 244 | certificate_errors_are_fatal_ = fatal; |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 245 | if (allow_certificate_errors_) |
| 246 | request->ContinueDespiteLastError(); |
| 247 | else |
| 248 | request->Cancel(); |
| 249 | } |
| 250 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 251 | void TestDelegate::OnResponseStarted(URLRequest* request) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 252 | // It doesn't make sense for the request to have IO pending at this point. |
| 253 | DCHECK(!request->status().is_io_pending()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 254 | EXPECT_FALSE(request->is_redirecting()); |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 255 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 256 | have_full_request_headers_ = |
| 257 | request->GetFullRequestHeaders(&full_request_headers_); |
| 258 | |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 259 | response_started_count_++; |
| 260 | if (cancel_in_rs_) { |
| 261 | request->Cancel(); |
| 262 | OnResponseCompleted(request); |
| 263 | } else if (!request->status().is_success()) { |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 264 | DCHECK(request->status().status() == URLRequestStatus::FAILED || |
| 265 | request->status().status() == URLRequestStatus::CANCELED); |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 266 | request_failed_ = true; |
| 267 | OnResponseCompleted(request); |
| 268 | } else { |
| 269 | // Initiate the first read. |
| 270 | int bytes_read = 0; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 271 | if (request->Read(buf_.get(), kBufferSize, &bytes_read)) |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 272 | OnReadCompleted(request, bytes_read); |
| 273 | else if (!request->status().is_io_pending()) |
| 274 | OnResponseCompleted(request); |
| 275 | } |
| 276 | } |
| 277 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 278 | void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 279 | // It doesn't make sense for the request to have IO pending at this point. |
| 280 | DCHECK(!request->status().is_io_pending()); |
| 281 | |
davidben | cf94bcd | 2015-12-04 02:39:14 | [diff] [blame] | 282 | // If the request was cancelled in a redirect, it should not signal |
| 283 | // OnReadCompleted. Note that |cancel_in_rs_| may be true due to |
| 284 | // https://crbug.com/564848. |
| 285 | EXPECT_FALSE(cancel_in_rr_); |
| 286 | |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 287 | if (response_started_count_ == 0) |
| 288 | received_data_before_response_ = true; |
| 289 | |
| 290 | if (cancel_in_rd_) |
| 291 | request->Cancel(); |
| 292 | |
| 293 | if (bytes_read >= 0) { |
| 294 | // There is data to read. |
| 295 | received_bytes_count_ += bytes_read; |
| 296 | |
| 297 | // consume the data |
| 298 | data_received_.append(buf_->data(), bytes_read); |
| 299 | } |
| 300 | |
| 301 | // If it was not end of stream, request to read more. |
| 302 | if (request->status().is_success() && bytes_read > 0) { |
| 303 | bytes_read = 0; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 304 | while (request->Read(buf_.get(), kBufferSize, &bytes_read)) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 305 | if (bytes_read > 0) { |
| 306 | data_received_.append(buf_->data(), bytes_read); |
| 307 | received_bytes_count_ += bytes_read; |
| 308 | } else { |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | if (!request->status().is_io_pending()) |
| 314 | OnResponseCompleted(request); |
| 315 | else if (cancel_in_rd_pending_) |
| 316 | request->Cancel(); |
| 317 | } |
| 318 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 319 | void TestDelegate::OnResponseCompleted(URLRequest* request) { |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 320 | if (quit_on_complete_) |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 321 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
ki.stfu | 375812e | 2015-10-09 20:23:17 | [diff] [blame] | 322 | FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 323 | } |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 324 | |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 325 | TestNetworkDelegate::TestNetworkDelegate() |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 326 | : last_error_(0), |
| 327 | error_count_(0), |
| 328 | created_requests_(0), |
| 329 | destroyed_requests_(0), |
| 330 | completed_requests_(0), |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 331 | canceled_requests_(0), |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 332 | cookie_options_bit_mask_(0), |
| 333 | blocked_get_cookies_count_(0), |
| 334 | blocked_set_cookie_count_(0), |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 335 | set_cookie_count_(0), |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 336 | observed_before_proxy_headers_sent_callbacks_(0), |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 337 | before_send_headers_count_(0), |
| 338 | headers_received_count_(0), |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 339 | total_network_bytes_received_(0), |
sclittle | 28d558b | 2015-09-28 21:40:52 | [diff] [blame] | 340 | total_network_bytes_sent_(0), |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 341 | has_load_timing_info_before_redirect_(false), |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 342 | has_load_timing_info_before_auth_(false), |
| 343 | can_access_files_(true), |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 344 | experimental_cookie_features_enabled_(false), |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 345 | cancel_request_with_policy_violating_referrer_(false), |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 346 | will_be_intercepted_on_next_error_(false) {} |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 347 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 348 | TestNetworkDelegate::~TestNetworkDelegate() { |
| 349 | for (std::map<int, int>::iterator i = next_states_.begin(); |
| 350 | i != next_states_.end(); ++i) { |
| 351 | event_order_[i->first] += "~TestNetworkDelegate\n"; |
| 352 | EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first]; |
| 353 | } |
| 354 | } |
| 355 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 356 | bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect( |
| 357 | LoadTimingInfo* load_timing_info_before_redirect) const { |
| 358 | *load_timing_info_before_redirect = load_timing_info_before_redirect_; |
| 359 | return has_load_timing_info_before_redirect_; |
| 360 | } |
| 361 | |
| 362 | bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth( |
| 363 | LoadTimingInfo* load_timing_info_before_auth) const { |
| 364 | *load_timing_info_before_auth = load_timing_info_before_auth_; |
| 365 | return has_load_timing_info_before_auth_; |
| 366 | } |
| 367 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 368 | void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) { |
| 369 | if (next_states_.find(request_id) == next_states_.end()) { |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 370 | // TODO(davidben): Although the URLRequest documentation does not allow |
| 371 | // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's |
| 372 | // destructor also calls Cancel. Either officially support this or fix the |
| 373 | // ResourceLoader code. |
| 374 | next_states_[request_id] = kStageBeforeURLRequest | kStageCompletedError; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 375 | event_order_[request_id] = ""; |
| 376 | } |
| 377 | } |
| 378 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 379 | int TestNetworkDelegate::OnBeforeURLRequest( |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 380 | URLRequest* request, |
| 381 | const CompletionCallback& callback, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 382 | GURL* new_url) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 383 | int req_id = request->identifier(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 384 | InitRequestStatesIfNew(req_id); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 385 | event_order_[req_id] += "OnBeforeURLRequest\n"; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 386 | EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) << |
| 387 | event_order_[req_id]; |
| 388 | next_states_[req_id] = |
| 389 | kStageBeforeSendHeaders | |
| 390 | kStageResponseStarted | // data: URLs do not trigger sending headers |
| 391 | kStageBeforeRedirect | // a delegate can trigger a redirection |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 392 | kStageCompletedError | // request canceled by delegate |
| 393 | kStageAuthRequired; // Auth can come next for FTP requests |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 394 | created_requests_++; |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 395 | return OK; |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 396 | } |
| 397 | |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 398 | int TestNetworkDelegate::OnBeforeSendHeaders( |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 399 | URLRequest* request, |
| 400 | const CompletionCallback& callback, |
| 401 | HttpRequestHeaders* headers) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 402 | int req_id = request->identifier(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 403 | InitRequestStatesIfNew(req_id); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 404 | event_order_[req_id] += "OnBeforeSendHeaders\n"; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 405 | EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) << |
| 406 | event_order_[req_id]; |
| 407 | next_states_[req_id] = |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 408 | kStageSendHeaders | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 409 | kStageCompletedError; // request canceled by delegate |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 410 | before_send_headers_count_++; |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 411 | return OK; |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 412 | } |
| 413 | |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 414 | void TestNetworkDelegate::OnBeforeSendProxyHeaders( |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 415 | URLRequest* request, |
| 416 | const ProxyInfo& proxy_info, |
| 417 | HttpRequestHeaders* headers) { |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 418 | ++observed_before_proxy_headers_sent_callbacks_; |
| 419 | last_observed_proxy_ = proxy_info.proxy_server().host_port_pair(); |
| 420 | } |
| 421 | |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 422 | void TestNetworkDelegate::OnSendHeaders( |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 423 | URLRequest* request, |
| 424 | const HttpRequestHeaders& headers) { |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 425 | int req_id = request->identifier(); |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 426 | InitRequestStatesIfNew(req_id); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 427 | event_order_[req_id] += "OnSendHeaders\n"; |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 428 | EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) << |
| 429 | event_order_[req_id]; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 430 | if (!will_be_intercepted_on_next_error_) |
| 431 | next_states_[req_id] = kStageHeadersReceived | kStageCompletedError; |
| 432 | else |
| 433 | next_states_[req_id] = kStageResponseStarted; |
| 434 | will_be_intercepted_on_next_error_ = false; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | int TestNetworkDelegate::OnHeadersReceived( |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 438 | URLRequest* request, |
| 439 | const CompletionCallback& callback, |
| 440 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 441 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 442 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 443 | int req_id = request->identifier(); |
| 444 | event_order_[req_id] += "OnHeadersReceived\n"; |
| 445 | InitRequestStatesIfNew(req_id); |
| 446 | EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) << |
| 447 | event_order_[req_id]; |
| 448 | next_states_[req_id] = |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 449 | kStageBeforeRedirect | |
| 450 | kStageResponseStarted | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 451 | kStageAuthRequired | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 452 | kStageCompletedError; // e.g. proxy resolution problem |
| 453 | |
| 454 | // Basic authentication sends a second request from the URLRequestHttpJob |
| 455 | // layer before the URLRequest reports that a response has started. |
[email protected] | 5796dc94 | 2011-07-14 19:26:10 | [diff] [blame] | 456 | next_states_[req_id] |= kStageBeforeSendHeaders; |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 457 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 458 | if (!redirect_on_headers_received_url_.is_empty()) { |
| 459 | *override_response_headers = |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 460 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 461 | (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found"); |
| 462 | (*override_response_headers)->RemoveHeader("Location"); |
| 463 | (*override_response_headers)->AddHeader( |
| 464 | "Location: " + redirect_on_headers_received_url_.spec()); |
| 465 | |
| 466 | redirect_on_headers_received_url_ = GURL(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 467 | |
| 468 | if (!allowed_unsafe_redirect_url_.is_empty()) |
| 469 | *allowed_unsafe_redirect_url = allowed_unsafe_redirect_url_; |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 470 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 471 | headers_received_count_++; |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 472 | return OK; |
[email protected] | 82b4230 | 2011-04-20 16:28:16 | [diff] [blame] | 473 | } |
| 474 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 475 | void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request, |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 476 | const GURL& new_location) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 477 | load_timing_info_before_redirect_ = LoadTimingInfo(); |
| 478 | request->GetLoadTimingInfo(&load_timing_info_before_redirect_); |
| 479 | has_load_timing_info_before_redirect_ = true; |
| 480 | EXPECT_FALSE(load_timing_info_before_redirect_.request_start_time.is_null()); |
| 481 | EXPECT_FALSE(load_timing_info_before_redirect_.request_start.is_null()); |
| 482 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 483 | int req_id = request->identifier(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 484 | InitRequestStatesIfNew(req_id); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 485 | event_order_[req_id] += "OnBeforeRedirect\n"; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 486 | EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) << |
| 487 | event_order_[req_id]; |
| 488 | next_states_[req_id] = |
| 489 | kStageBeforeURLRequest | // HTTP redirects trigger this. |
| 490 | kStageBeforeSendHeaders | // Redirects from the network delegate do not |
| 491 | // trigger onBeforeURLRequest. |
| 492 | kStageCompletedError; |
| 493 | |
| 494 | // A redirect can lead to a file or a data URL. In this case, we do not send |
| 495 | // headers. |
| 496 | next_states_[req_id] |= kStageResponseStarted; |
[email protected] | 31b2e5f | 2011-04-20 16:58:32 | [diff] [blame] | 497 | } |
| 498 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 499 | void TestNetworkDelegate::OnResponseStarted(URLRequest* request) { |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 500 | LoadTimingInfo load_timing_info; |
| 501 | request->GetLoadTimingInfo(&load_timing_info); |
| 502 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 503 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 504 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 505 | int req_id = request->identifier(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 506 | InitRequestStatesIfNew(req_id); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 507 | event_order_[req_id] += "OnResponseStarted\n"; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 508 | EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) << |
| 509 | event_order_[req_id]; |
| 510 | next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 511 | if (request->status().status() == URLRequestStatus::FAILED) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 512 | error_count_++; |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 513 | last_error_ = request->status().error(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 514 | } |
| 515 | } |
[email protected] | 0651b81 | 2011-02-24 00:22:50 | [diff] [blame] | 516 | |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 517 | void TestNetworkDelegate::OnNetworkBytesReceived(URLRequest* request, |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 518 | int64_t bytes_received) { |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 519 | event_order_[request->identifier()] += "OnNetworkBytesReceived\n"; |
sclittle | ce72c48 | 2015-08-24 20:20:59 | [diff] [blame] | 520 | total_network_bytes_received_ += bytes_received; |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 521 | } |
| 522 | |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 523 | void TestNetworkDelegate::OnNetworkBytesSent(URLRequest* request, |
sclittle | 28d558b | 2015-09-28 21:40:52 | [diff] [blame] | 524 | int64_t bytes_sent) { |
sclittle | a133de0 | 2015-11-10 23:54:21 | [diff] [blame] | 525 | event_order_[request->identifier()] += "OnNetworkBytesSent\n"; |
sclittle | 28d558b | 2015-09-28 21:40:52 | [diff] [blame] | 526 | total_network_bytes_sent_ += bytes_sent; |
| 527 | } |
| 528 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 529 | void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 530 | int req_id = request->identifier(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 531 | InitRequestStatesIfNew(req_id); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 532 | event_order_[req_id] += "OnCompleted\n"; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 533 | // Expect "Success -> (next_states_ & kStageCompletedSuccess)" |
| 534 | // is logically identical to |
| 535 | // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" |
| 536 | EXPECT_TRUE(!request->status().is_success() || |
| 537 | (next_states_[req_id] & kStageCompletedSuccess)) << |
| 538 | event_order_[req_id]; |
| 539 | EXPECT_TRUE(request->status().is_success() || |
| 540 | (next_states_[req_id] & kStageCompletedError)) << |
| 541 | event_order_[req_id]; |
| 542 | next_states_[req_id] = kStageURLRequestDestroyed; |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 543 | completed_requests_++; |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 544 | if (request->status().status() == URLRequestStatus::FAILED) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 545 | error_count_++; |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 546 | last_error_ = request->status().error(); |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 547 | } else if (request->status().status() == URLRequestStatus::CANCELED) { |
| 548 | canceled_requests_++; |
| 549 | } else { |
| 550 | DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 551 | } |
| 552 | } |
[email protected] | 4b50cb5 | 2011-03-10 00:29:37 | [diff] [blame] | 553 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 554 | void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 555 | int req_id = request->identifier(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 556 | InitRequestStatesIfNew(req_id); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 557 | event_order_[req_id] += "OnURLRequestDestroyed\n"; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 558 | EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << |
| 559 | event_order_[req_id]; |
| 560 | next_states_[req_id] = kStageDestruction; |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 561 | destroyed_requests_++; |
[email protected] | 4875ba1 | 2011-03-30 22:31:51 | [diff] [blame] | 562 | } |
| 563 | |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 564 | void TestNetworkDelegate::OnPACScriptError(int line_number, |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 565 | const base::string16& error) { |
[email protected] | 82a3767 | 2011-05-03 12:02:41 | [diff] [blame] | 566 | } |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 567 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 568 | NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired( |
| 569 | URLRequest* request, |
| 570 | const AuthChallengeInfo& auth_info, |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 571 | const AuthCallback& callback, |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 572 | AuthCredentials* credentials) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 573 | load_timing_info_before_auth_ = LoadTimingInfo(); |
| 574 | request->GetLoadTimingInfo(&load_timing_info_before_auth_); |
| 575 | has_load_timing_info_before_auth_ = true; |
| 576 | EXPECT_FALSE(load_timing_info_before_auth_.request_start_time.is_null()); |
| 577 | EXPECT_FALSE(load_timing_info_before_auth_.request_start.is_null()); |
| 578 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 579 | int req_id = request->identifier(); |
| 580 | InitRequestStatesIfNew(req_id); |
| 581 | event_order_[req_id] += "OnAuthRequired\n"; |
| 582 | EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) << |
| 583 | event_order_[req_id]; |
| 584 | next_states_[req_id] = kStageBeforeSendHeaders | |
[email protected] | 7886251 | 2013-04-08 20:12:43 | [diff] [blame] | 585 | kStageAuthRequired | // For example, proxy auth followed by server auth. |
[email protected] | ea8141e | 2011-10-05 13:12:51 | [diff] [blame] | 586 | kStageHeadersReceived | // Request canceled by delegate simulates empty |
| 587 | // response. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 588 | kStageResponseStarted | // data: URLs do not trigger sending headers |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 589 | kStageBeforeRedirect | // a delegate can trigger a redirection |
| 590 | kStageCompletedError; // request cancelled before callback |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 591 | return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
[email protected] | 7efc582d | 2011-08-03 20:46:35 | [diff] [blame] | 592 | } |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 593 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 594 | bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request, |
| 595 | const CookieList& cookie_list) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 596 | bool allow = true; |
| 597 | if (cookie_options_bit_mask_ & NO_GET_COOKIES) |
| 598 | allow = false; |
| 599 | |
| 600 | if (!allow) { |
| 601 | blocked_get_cookies_count_++; |
| 602 | } |
| 603 | |
| 604 | return allow; |
| 605 | } |
| 606 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 607 | bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request, |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 608 | const std::string& cookie_line, |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 609 | CookieOptions* options) { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 610 | bool allow = true; |
| 611 | if (cookie_options_bit_mask_ & NO_SET_COOKIE) |
| 612 | allow = false; |
| 613 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 614 | if (!allow) { |
| 615 | blocked_set_cookie_count_++; |
| 616 | } else { |
| 617 | set_cookie_count_++; |
| 618 | } |
| 619 | |
| 620 | return allow; |
| 621 | } |
| 622 | |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 623 | bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 624 | const base::FilePath& path) const { |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 625 | return can_access_files_; |
[email protected] | 4c219e2 | 2012-05-05 19:41:04 | [diff] [blame] | 626 | } |
| 627 | |
estark | 7625d81 | 2015-10-12 20:10:41 | [diff] [blame] | 628 | bool TestNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const { |
| 629 | return experimental_cookie_features_enabled_; |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 630 | } |
| 631 | |
jww | 79aceda | 2015-12-07 01:56:34 | [diff] [blame] | 632 | bool TestNetworkDelegate::OnAreStrictSecureCookiesEnabled() const { |
| 633 | return experimental_cookie_features_enabled_; |
| 634 | } |
| 635 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 636 | bool TestNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader( |
| 637 | const URLRequest& request, |
| 638 | const GURL& target_url, |
| 639 | const GURL& referrer_url) const { |
| 640 | return cancel_request_with_policy_violating_referrer_; |
| 641 | } |
| 642 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 643 | TestJobInterceptor::TestJobInterceptor() {} |
| 644 | |
| 645 | TestJobInterceptor::~TestJobInterceptor() {} |
[email protected] | 46ae16ba | 2012-09-18 14:02:03 | [diff] [blame] | 646 | |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 647 | URLRequestJob* TestJobInterceptor::MaybeCreateJob( |
| 648 | URLRequest* request, |
| 649 | NetworkDelegate* network_delegate) const { |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 650 | return main_intercept_job_.release(); |
[email protected] | 46ae16ba | 2012-09-18 14:02:03 | [diff] [blame] | 651 | } |
| 652 | |
mmenke | ed0498b | 2015-12-08 23:20:42 | [diff] [blame] | 653 | void TestJobInterceptor::set_main_intercept_job(scoped_ptr<URLRequestJob> job) { |
| 654 | main_intercept_job_ = std::move(job); |
[email protected] | 46ae16ba | 2012-09-18 14:02:03 | [diff] [blame] | 655 | } |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 656 | |
| 657 | } // namespace net |