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