blob: 269203489f9f8c64580acec3836c3647a933ac19 [file] [log] [blame]
[email protected]9045b8822012-01-13 20:35:351// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d2db0292011-01-26 20:23:442// 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]529623e2011-02-23 17:37:167#include "base/compiler_specific.h"
skyostil4891b25b2015-06-11 11:43:458#include "base/location.h"
[email protected]d2db0292011-01-26 20:23:449#include "base/logging.h"
[email protected]7f86564d2013-07-18 00:41:2210#include "base/message_loop/message_loop.h"
skyostil4891b25b2015-06-11 11:43:4511#include "base/single_thread_task_runner.h"
12#include "base/thread_task_runner_handle.h"
[email protected]d2db0292011-01-26 20:23:4413#include "base/threading/thread.h"
[email protected]bf470dfc2012-05-15 21:13:4514#include "base/threading/worker_pool.h"
[email protected]82b42302011-04-20 16:28:1615#include "net/base/host_port_pair.h"
[email protected]6e7845ae2013-03-29 21:48:1116#include "net/cert/cert_verifier.h"
[email protected]f2cb3cf2013-03-21 01:40:5317#include "net/dns/mock_host_resolver.h"
[email protected]57cb0f72011-01-28 06:33:5818#include "net/http/http_network_session.h"
[email protected]e50efea2014-03-24 18:41:0019#include "net/http/http_response_headers.h"
[email protected]17291a022011-10-10 07:32:5320#include "net/http/http_server_properties_impl.h"
[email protected]b1c988b2013-06-13 06:48:1121#include "net/http/transport_security_state.h"
[email protected]6b8a3c742014-07-25 00:25:3522#include "net/ssl/channel_id_service.h"
23#include "net/ssl/default_channel_id_store.h"
[email protected]ee4c30d2012-11-07 15:08:4324#include "net/url_request/static_http_user_agent_settings.h"
[email protected]9d5730b2012-08-24 17:42:4925#include "net/url_request/url_request_job_factory_impl.h"
[email protected]0140bd32012-03-21 01:47:1226#include "testing/gtest/include/gtest/gtest.h"
[email protected]d2db0292011-01-26 20:23:4427
[email protected]2086a3d2012-11-13 17:49:2028namespace net {
29
[email protected]87a09a92011-07-14 15:50:5030namespace {
31
[email protected]2086a3d2012-11-13 17:49:2032// These constants put the NetworkDelegate events of TestNetworkDelegate
[email protected]87a09a92011-07-14 15:50:5033// into an order. They are used in conjunction with
34// |TestNetworkDelegate::next_states_| to check that we do not send
35// events in the wrong order.
36const int kStageBeforeURLRequest = 1 << 0;
37const int kStageBeforeSendHeaders = 1 << 1;
[email protected]5796dc942011-07-14 19:26:1038const int kStageSendHeaders = 1 << 2;
[email protected]ea8141e2011-10-05 13:12:5139const int kStageHeadersReceived = 1 << 3;
40const int kStageAuthRequired = 1 << 4;
41const int kStageBeforeRedirect = 1 << 5;
42const int kStageResponseStarted = 1 << 6;
43const int kStageCompletedSuccess = 1 << 7;
44const int kStageCompletedError = 1 << 8;
45const int kStageURLRequestDestroyed = 1 << 9;
46const int kStageDestruction = 1 << 10;
[email protected]87a09a92011-07-14 15:50:5047
48} // namespace
49
[email protected]529623e2011-02-23 17:37:1650TestURLRequestContext::TestURLRequestContext()
[email protected]87a09a92011-07-14 15:50:5051 : initialized_(false),
[email protected]45499252013-01-23 17:12:5652 client_socket_factory_(NULL),
[email protected]aa249b52013-04-30 01:04:3253 context_storage_(this) {
[email protected]d2db0292011-01-26 20:23:4454 Init();
55}
56
[email protected]87a09a92011-07-14 15:50:5057TestURLRequestContext::TestURLRequestContext(bool delay_initialization)
58 : initialized_(false),
[email protected]45499252013-01-23 17:12:5659 client_socket_factory_(NULL),
[email protected]aa249b52013-04-30 01:04:3260 context_storage_(this) {
[email protected]87a09a92011-07-14 15:50:5061 if (!delay_initialization)
62 Init();
63}
64
[email protected]87a09a92011-07-14 15:50:5065TestURLRequestContext::~TestURLRequestContext() {
66 DCHECK(initialized_);
[email protected]424559492014-07-22 00:27:4067 AssertNoURLRequests();
[email protected]87a09a92011-07-14 15:50:5068}
[email protected]d2db0292011-01-26 20:23:4469
70void TestURLRequestContext::Init() {
[email protected]87a09a92011-07-14 15:50:5071 DCHECK(!initialized_);
72 initialized_ = true;
[email protected]d5a4dd62012-05-23 01:41:0473
74 if (!host_resolver())
[email protected]c54a8912012-10-22 22:09:4375 context_storage_.set_host_resolver(
[email protected]2086a3d2012-11-13 17:49:2076 scoped_ptr<HostResolver>(new MockCachingHostResolver()));
[email protected]d5a4dd62012-05-23 01:41:0477 if (!proxy_service())
[email protected]2086a3d2012-11-13 17:49:2078 context_storage_.set_proxy_service(ProxyService::CreateDirect());
[email protected]87a09a92011-07-14 15:50:5079 if (!cert_verifier())
[email protected]2086a3d2012-11-13 17:49:2080 context_storage_.set_cert_verifier(CertVerifier::CreateDefault());
[email protected]b1c988b2013-06-13 06:48:1181 if (!transport_security_state())
82 context_storage_.set_transport_security_state(new TransportSecurityState);
[email protected]87a09a92011-07-14 15:50:5083 if (!ssl_config_service())
[email protected]2086a3d2012-11-13 17:49:2084 context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
[email protected]87a09a92011-07-14 15:50:5085 if (!http_auth_handler_factory()) {
86 context_storage_.set_http_auth_handler_factory(
[email protected]2086a3d2012-11-13 17:49:2087 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
[email protected]87a09a92011-07-14 15:50:5088 }
[email protected]17291a022011-10-10 07:32:5389 if (!http_server_properties()) {
90 context_storage_.set_http_server_properties(
[email protected]30d4c022013-07-18 22:58:1691 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
[email protected]17291a022011-10-10 07:32:5392 }
[email protected]242d8562012-10-30 21:20:4693 if (!transport_security_state()) {
94 context_storage_.set_transport_security_state(
[email protected]2086a3d2012-11-13 17:49:2095 new TransportSecurityState());
[email protected]242d8562012-10-30 21:20:4696 }
[email protected]45499252013-01-23 17:12:5697 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]067ce8b02014-06-02 22:20:02102 if (http_network_session_params_)
103 params = *http_network_session_params_;
[email protected]45499252013-01-23 17:12:56104 params.client_socket_factory = client_socket_factory();
105 params.host_resolver = host_resolver();
106 params.cert_verifier = cert_verifier();
[email protected]b1c988b2013-06-13 06:48:11107 params.transport_security_state = transport_security_state();
[email protected]45499252013-01-23 17:12:56108 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();
[email protected]2086a3d2012-11-13 17:49:20114 context_storage_.set_http_transaction_factory(new HttpCache(
115 new HttpNetworkSession(params),
116 HttpCache::DefaultBackend::InMemory(0)));
[email protected]87a09a92011-07-14 15:50:50117 }
[email protected]d2db0292011-01-26 20:23:44118 // In-memory cookie store.
[email protected]87a09a92011-07-14 15:50:50119 if (!cookie_store())
[email protected]2086a3d2012-11-13 17:49:20120 context_storage_.set_cookie_store(new CookieMonster(NULL, NULL));
[email protected]6b8a3c742014-07-25 00:25:35121 // In-memory Channel ID service.
122 if (!channel_id_service()) {
mattm368d94622015-01-21 13:54:00123 context_storage_.set_channel_id_service(make_scoped_ptr(
124 new ChannelIDService(new DefaultChannelIDStore(NULL),
125 base::WorkerPool::GetTaskRunner(true))));
[email protected]eb7974c2012-01-25 17:38:48126 }
[email protected]ee4c30d2012-11-07 15:08:43127 if (!http_user_agent_settings()) {
128 context_storage_.set_http_user_agent_settings(
[email protected]8790210c2013-12-02 05:29:53129 new StaticHttpUserAgentSettings("en-us,fr", std::string()));
[email protected]ee4c30d2012-11-07 15:08:43130 }
[email protected]87a09a92011-07-14 15:50:50131 if (!job_factory())
[email protected]2086a3d2012-11-13 17:49:20132 context_storage_.set_job_factory(new URLRequestJobFactoryImpl);
[email protected]d2db0292011-01-26 20:23:44133}
134
[email protected]44738602012-03-02 22:56:01135TestURLRequestContextGetter::TestURLRequestContextGetter(
[email protected]4969b0122012-06-16 01:58:28136 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
137 : network_task_runner_(network_task_runner) {
[email protected]90499482013-06-01 00:39:50138 DCHECK(network_task_runner_.get());
[email protected]44738602012-03-02 22:56:01139}
140
[email protected]d5a4dd62012-05-23 01:41:04141TestURLRequestContextGetter::TestURLRequestContextGetter(
[email protected]4969b0122012-06-16 01:58:28142 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
[email protected]d5a4dd62012-05-23 01:41:04143 scoped_ptr<TestURLRequestContext> context)
[email protected]4969b0122012-06-16 01:58:28144 : network_task_runner_(network_task_runner), context_(context.Pass()) {
[email protected]90499482013-06-01 00:39:50145 DCHECK(network_task_runner_.get());
[email protected]d5a4dd62012-05-23 01:41:04146}
147
[email protected]44738602012-03-02 22:56:01148TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
149
150TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() {
[email protected]ef2bf422012-05-11 03:27:09151 if (!context_.get())
152 context_.reset(new TestURLRequestContext);
[email protected]44738602012-03-02 22:56:01153 return context_.get();
154}
155
[email protected]4969b0122012-06-16 01:58:28156scoped_refptr<base::SingleThreadTaskRunner>
157TestURLRequestContextGetter::GetNetworkTaskRunner() const {
158 return network_task_runner_;
[email protected]44738602012-03-02 22:56:01159}
160
[email protected]d2db0292011-01-26 20:23:44161TestDelegate::TestDelegate()
162 : cancel_in_rr_(false),
163 cancel_in_rs_(false),
164 cancel_in_rd_(false),
165 cancel_in_rd_pending_(false),
[email protected]d2db0292011-01-26 20:23:44166 quit_on_complete_(true),
167 quit_on_redirect_(false),
mmenke8d457fbf2015-06-08 19:56:35168 quit_on_auth_required_(false),
[email protected]1826a402014-01-08 15:40:48169 quit_on_before_network_start_(false),
[email protected]d2db0292011-01-26 20:23:44170 allow_certificate_errors_(false),
171 response_started_count_(0),
172 received_bytes_count_(0),
173 received_redirect_count_(0),
[email protected]1826a402014-01-08 15:40:48174 received_before_network_start_count_(0),
[email protected]d2db0292011-01-26 20:23:44175 received_data_before_response_(false),
176 request_failed_(false),
177 have_certificate_errors_(false),
[email protected]46d117e2012-01-18 01:53:14178 certificate_errors_are_fatal_(false),
[email protected]c2911d72011-10-03 22:16:36179 auth_required_(false),
[email protected]79e1fd62013-06-20 06:50:04180 have_full_request_headers_(false),
[email protected]2086a3d2012-11-13 17:49:20181 buf_(new IOBuffer(kBufferSize)) {
[email protected]d2db0292011-01-26 20:23:44182}
183
184TestDelegate::~TestDelegate() {}
185
[email protected]79e1fd62013-06-20 06:50:04186void TestDelegate::ClearFullRequestHeaders() {
187 full_request_headers_.Clear();
188 have_full_request_headers_ = false;
189}
190
[email protected]2086a3d2012-11-13 17:49:20191void TestDelegate::OnReceivedRedirect(URLRequest* request,
[email protected]cba24642014-08-15 20:49:59192 const RedirectInfo& redirect_info,
[email protected]d2db0292011-01-26 20:23:44193 bool* defer_redirect) {
[email protected]847c0fa92012-11-06 16:37:42194 EXPECT_TRUE(request->is_redirecting());
[email protected]79e1fd62013-06-20 06:50:04195
196 have_full_request_headers_ =
197 request->GetFullRequestHeaders(&full_request_headers_);
198
[email protected]d2db0292011-01-26 20:23:44199 received_redirect_count_++;
200 if (quit_on_redirect_) {
201 *defer_redirect = true;
skyostil4891b25b2015-06-11 11:43:45202 base::ThreadTaskRunnerHandle::Get()->PostTask(
203 FROM_HERE, base::MessageLoop::QuitClosure());
[email protected]d2db0292011-01-26 20:23:44204 } else if (cancel_in_rr_) {
205 request->Cancel();
206 }
207}
208
[email protected]1826a402014-01-08 15:40:48209void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) {
210 received_before_network_start_count_++;
211 if (quit_on_before_network_start_) {
212 *defer = true;
skyostil4891b25b2015-06-11 11:43:45213 base::ThreadTaskRunnerHandle::Get()->PostTask(
214 FROM_HERE, base::MessageLoop::QuitClosure());
[email protected]1826a402014-01-08 15:40:48215 }
216}
217
[email protected]2086a3d2012-11-13 17:49:20218void TestDelegate::OnAuthRequired(URLRequest* request,
219 AuthChallengeInfo* auth_info) {
[email protected]c2911d72011-10-03 22:16:36220 auth_required_ = true;
mmenke8d457fbf2015-06-08 19:56:35221 if (quit_on_auth_required_) {
skyostil4891b25b2015-06-11 11:43:45222 base::ThreadTaskRunnerHandle::Get()->PostTask(
223 FROM_HERE, base::MessageLoop::QuitClosure());
mmenke8d457fbf2015-06-08 19:56:35224 return;
225 }
[email protected]f3cf9802011-10-28 18:44:58226 if (!credentials_.Empty()) {
227 request->SetAuth(credentials_);
[email protected]d2db0292011-01-26 20:23:44228 } else {
229 request->CancelAuth();
230 }
231}
232
[email protected]2086a3d2012-11-13 17:49:20233void TestDelegate::OnSSLCertificateError(URLRequest* request,
234 const SSLInfo& ssl_info,
[email protected]46d117e2012-01-18 01:53:14235 bool fatal) {
[email protected]d2db0292011-01-26 20:23:44236 // The caller can control whether it needs all SSL requests to go through,
237 // independent of any possible errors, or whether it wants SSL errors to
238 // cancel the request.
239 have_certificate_errors_ = true;
[email protected]46d117e2012-01-18 01:53:14240 certificate_errors_are_fatal_ = fatal;
[email protected]d2db0292011-01-26 20:23:44241 if (allow_certificate_errors_)
242 request->ContinueDespiteLastError();
243 else
244 request->Cancel();
245}
246
[email protected]2086a3d2012-11-13 17:49:20247void TestDelegate::OnResponseStarted(URLRequest* request) {
[email protected]d2db0292011-01-26 20:23:44248 // It doesn't make sense for the request to have IO pending at this point.
249 DCHECK(!request->status().is_io_pending());
[email protected]847c0fa92012-11-06 16:37:42250 EXPECT_FALSE(request->is_redirecting());
[email protected]d2db0292011-01-26 20:23:44251
[email protected]79e1fd62013-06-20 06:50:04252 have_full_request_headers_ =
253 request->GetFullRequestHeaders(&full_request_headers_);
254
[email protected]d2db0292011-01-26 20:23:44255 response_started_count_++;
256 if (cancel_in_rs_) {
257 request->Cancel();
258 OnResponseCompleted(request);
259 } else if (!request->status().is_success()) {
[email protected]2086a3d2012-11-13 17:49:20260 DCHECK(request->status().status() == URLRequestStatus::FAILED ||
261 request->status().status() == URLRequestStatus::CANCELED);
[email protected]d2db0292011-01-26 20:23:44262 request_failed_ = true;
263 OnResponseCompleted(request);
264 } else {
265 // Initiate the first read.
266 int bytes_read = 0;
[email protected]90499482013-06-01 00:39:50267 if (request->Read(buf_.get(), kBufferSize, &bytes_read))
[email protected]d2db0292011-01-26 20:23:44268 OnReadCompleted(request, bytes_read);
269 else if (!request->status().is_io_pending())
270 OnResponseCompleted(request);
271 }
272}
273
[email protected]2086a3d2012-11-13 17:49:20274void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) {
[email protected]d2db0292011-01-26 20:23:44275 // It doesn't make sense for the request to have IO pending at this point.
276 DCHECK(!request->status().is_io_pending());
277
278 if (response_started_count_ == 0)
279 received_data_before_response_ = true;
280
281 if (cancel_in_rd_)
282 request->Cancel();
283
284 if (bytes_read >= 0) {
285 // There is data to read.
286 received_bytes_count_ += bytes_read;
287
288 // consume the data
289 data_received_.append(buf_->data(), bytes_read);
290 }
291
292 // If it was not end of stream, request to read more.
293 if (request->status().is_success() && bytes_read > 0) {
294 bytes_read = 0;
[email protected]90499482013-06-01 00:39:50295 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) {
[email protected]d2db0292011-01-26 20:23:44296 if (bytes_read > 0) {
297 data_received_.append(buf_->data(), bytes_read);
298 received_bytes_count_ += bytes_read;
299 } else {
300 break;
301 }
302 }
303 }
304 if (!request->status().is_io_pending())
305 OnResponseCompleted(request);
306 else if (cancel_in_rd_pending_)
307 request->Cancel();
308}
309
[email protected]2086a3d2012-11-13 17:49:20310void TestDelegate::OnResponseCompleted(URLRequest* request) {
[email protected]d2db0292011-01-26 20:23:44311 if (quit_on_complete_)
skyostil4891b25b2015-06-11 11:43:45312 base::ThreadTaskRunnerHandle::Get()->PostTask(
313 FROM_HERE, base::MessageLoop::QuitClosure());
[email protected]d2db0292011-01-26 20:23:44314}
[email protected]8202d0c2011-02-23 08:31:14315
[email protected]0651b812011-02-24 00:22:50316TestNetworkDelegate::TestNetworkDelegate()
[email protected]9c8ae8c2012-03-09 13:13:35317 : last_error_(0),
318 error_count_(0),
319 created_requests_(0),
320 destroyed_requests_(0),
321 completed_requests_(0),
[email protected]146b8b22013-11-20 03:59:18322 canceled_requests_(0),
[email protected]9c8ae8c2012-03-09 13:13:35323 cookie_options_bit_mask_(0),
324 blocked_get_cookies_count_(0),
325 blocked_set_cookie_count_(0),
[email protected]58e32bb2013-01-21 18:23:25326 set_cookie_count_(0),
[email protected]597a1ab2014-06-26 08:12:27327 observed_before_proxy_headers_sent_callbacks_(0),
bengr1bf8e942014-11-07 01:36:50328 before_send_headers_count_(0),
329 headers_received_count_(0),
sclittlece72c482015-08-24 20:20:59330 total_network_bytes_received_(0),
[email protected]58e32bb2013-01-21 18:23:25331 has_load_timing_info_before_redirect_(false),
[email protected]3ca8b362013-11-11 22:18:07332 has_load_timing_info_before_auth_(false),
333 can_access_files_(true),
mkwst0513c9d2015-04-01 05:53:15334 first_party_only_cookies_enabled_(false),
bengr1bf8e942014-11-07 01:36:50335 cancel_request_with_policy_violating_referrer_(false),
sclittlece72c482015-08-24 20:20:59336 will_be_intercepted_on_next_error_(false) {}
[email protected]8202d0c2011-02-23 08:31:14337
[email protected]87a09a92011-07-14 15:50:50338TestNetworkDelegate::~TestNetworkDelegate() {
339 for (std::map<int, int>::iterator i = next_states_.begin();
340 i != next_states_.end(); ++i) {
341 event_order_[i->first] += "~TestNetworkDelegate\n";
342 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first];
343 }
344}
345
[email protected]58e32bb2013-01-21 18:23:25346bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect(
347 LoadTimingInfo* load_timing_info_before_redirect) const {
348 *load_timing_info_before_redirect = load_timing_info_before_redirect_;
349 return has_load_timing_info_before_redirect_;
350}
351
352bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth(
353 LoadTimingInfo* load_timing_info_before_auth) const {
354 *load_timing_info_before_auth = load_timing_info_before_auth_;
355 return has_load_timing_info_before_auth_;
356}
357
[email protected]87a09a92011-07-14 15:50:50358void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) {
359 if (next_states_.find(request_id) == next_states_.end()) {
[email protected]146b8b22013-11-20 03:59:18360 // TODO(davidben): Although the URLRequest documentation does not allow
361 // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's
362 // destructor also calls Cancel. Either officially support this or fix the
363 // ResourceLoader code.
364 next_states_[request_id] = kStageBeforeURLRequest | kStageCompletedError;
[email protected]87a09a92011-07-14 15:50:50365 event_order_[request_id] = "";
366 }
367}
368
[email protected]4875ba12011-03-30 22:31:51369int TestNetworkDelegate::OnBeforeURLRequest(
[email protected]2086a3d2012-11-13 17:49:20370 URLRequest* request,
371 const CompletionCallback& callback,
[email protected]cba24642014-08-15 20:49:59372 GURL* new_url) {
[email protected]87a09a92011-07-14 15:50:50373 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50374 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36375 event_order_[req_id] += "OnBeforeURLRequest\n";
[email protected]87a09a92011-07-14 15:50:50376 EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) <<
377 event_order_[req_id];
378 next_states_[req_id] =
379 kStageBeforeSendHeaders |
380 kStageResponseStarted | // data: URLs do not trigger sending headers
381 kStageBeforeRedirect | // a delegate can trigger a redirection
[email protected]c2911d72011-10-03 22:16:36382 kStageCompletedError | // request canceled by delegate
383 kStageAuthRequired; // Auth can come next for FTP requests
[email protected]4c76d7c2011-04-15 19:14:12384 created_requests_++;
[email protected]2086a3d2012-11-13 17:49:20385 return OK;
[email protected]8202d0c2011-02-23 08:31:14386}
387
[email protected]4875ba12011-03-30 22:31:51388int TestNetworkDelegate::OnBeforeSendHeaders(
[email protected]2086a3d2012-11-13 17:49:20389 URLRequest* request,
390 const CompletionCallback& callback,
391 HttpRequestHeaders* headers) {
[email protected]87a09a92011-07-14 15:50:50392 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50393 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36394 event_order_[req_id] += "OnBeforeSendHeaders\n";
[email protected]87a09a92011-07-14 15:50:50395 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) <<
396 event_order_[req_id];
397 next_states_[req_id] =
[email protected]5796dc942011-07-14 19:26:10398 kStageSendHeaders |
[email protected]87a09a92011-07-14 15:50:50399 kStageCompletedError; // request canceled by delegate
bengr1bf8e942014-11-07 01:36:50400 before_send_headers_count_++;
[email protected]2086a3d2012-11-13 17:49:20401 return OK;
[email protected]8202d0c2011-02-23 08:31:14402}
403
[email protected]597a1ab2014-06-26 08:12:27404void TestNetworkDelegate::OnBeforeSendProxyHeaders(
ttuttle859dc7a2015-04-23 19:42:29405 URLRequest* request,
406 const ProxyInfo& proxy_info,
407 HttpRequestHeaders* headers) {
[email protected]597a1ab2014-06-26 08:12:27408 ++observed_before_proxy_headers_sent_callbacks_;
409 last_observed_proxy_ = proxy_info.proxy_server().host_port_pair();
410}
411
[email protected]5796dc942011-07-14 19:26:10412void TestNetworkDelegate::OnSendHeaders(
[email protected]2086a3d2012-11-13 17:49:20413 URLRequest* request,
414 const HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10415 int req_id = request->identifier();
[email protected]5796dc942011-07-14 19:26:10416 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36417 event_order_[req_id] += "OnSendHeaders\n";
[email protected]5796dc942011-07-14 19:26:10418 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
419 event_order_[req_id];
bengr1bf8e942014-11-07 01:36:50420 if (!will_be_intercepted_on_next_error_)
421 next_states_[req_id] = kStageHeadersReceived | kStageCompletedError;
422 else
423 next_states_[req_id] = kStageResponseStarted;
424 will_be_intercepted_on_next_error_ = false;
[email protected]ea8141e2011-10-05 13:12:51425}
426
427int TestNetworkDelegate::OnHeadersReceived(
[email protected]2086a3d2012-11-13 17:49:20428 URLRequest* request,
429 const CompletionCallback& callback,
430 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16431 scoped_refptr<HttpResponseHeaders>* override_response_headers,
432 GURL* allowed_unsafe_redirect_url) {
[email protected]ea8141e2011-10-05 13:12:51433 int req_id = request->identifier();
434 event_order_[req_id] += "OnHeadersReceived\n";
435 InitRequestStatesIfNew(req_id);
436 EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) <<
437 event_order_[req_id];
438 next_states_[req_id] =
[email protected]87a09a92011-07-14 15:50:50439 kStageBeforeRedirect |
440 kStageResponseStarted |
[email protected]c2911d72011-10-03 22:16:36441 kStageAuthRequired |
[email protected]87a09a92011-07-14 15:50:50442 kStageCompletedError; // e.g. proxy resolution problem
443
444 // Basic authentication sends a second request from the URLRequestHttpJob
445 // layer before the URLRequest reports that a response has started.
[email protected]5796dc942011-07-14 19:26:10446 next_states_[req_id] |= kStageBeforeSendHeaders;
[email protected]ea8141e2011-10-05 13:12:51447
[email protected]e50efea2014-03-24 18:41:00448 if (!redirect_on_headers_received_url_.is_empty()) {
449 *override_response_headers =
ttuttle859dc7a2015-04-23 19:42:29450 new HttpResponseHeaders(original_response_headers->raw_headers());
[email protected]e50efea2014-03-24 18:41:00451 (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found");
452 (*override_response_headers)->RemoveHeader("Location");
453 (*override_response_headers)->AddHeader(
454 "Location: " + redirect_on_headers_received_url_.spec());
455
456 redirect_on_headers_received_url_ = GURL();
[email protected]5f714132014-03-26 10:41:16457
458 if (!allowed_unsafe_redirect_url_.is_empty())
459 *allowed_unsafe_redirect_url = allowed_unsafe_redirect_url_;
[email protected]e50efea2014-03-24 18:41:00460 }
bengr1bf8e942014-11-07 01:36:50461 headers_received_count_++;
[email protected]2086a3d2012-11-13 17:49:20462 return OK;
[email protected]82b42302011-04-20 16:28:16463}
464
[email protected]2086a3d2012-11-13 17:49:20465void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request,
[email protected]31b2e5f2011-04-20 16:58:32466 const GURL& new_location) {
[email protected]58e32bb2013-01-21 18:23:25467 load_timing_info_before_redirect_ = LoadTimingInfo();
468 request->GetLoadTimingInfo(&load_timing_info_before_redirect_);
469 has_load_timing_info_before_redirect_ = true;
470 EXPECT_FALSE(load_timing_info_before_redirect_.request_start_time.is_null());
471 EXPECT_FALSE(load_timing_info_before_redirect_.request_start.is_null());
472
[email protected]87a09a92011-07-14 15:50:50473 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50474 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36475 event_order_[req_id] += "OnBeforeRedirect\n";
[email protected]87a09a92011-07-14 15:50:50476 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) <<
477 event_order_[req_id];
478 next_states_[req_id] =
479 kStageBeforeURLRequest | // HTTP redirects trigger this.
480 kStageBeforeSendHeaders | // Redirects from the network delegate do not
481 // trigger onBeforeURLRequest.
482 kStageCompletedError;
483
484 // A redirect can lead to a file or a data URL. In this case, we do not send
485 // headers.
486 next_states_[req_id] |= kStageResponseStarted;
[email protected]31b2e5f2011-04-20 16:58:32487}
488
[email protected]2086a3d2012-11-13 17:49:20489void TestNetworkDelegate::OnResponseStarted(URLRequest* request) {
[email protected]0a703912013-02-02 04:25:17490 LoadTimingInfo load_timing_info;
491 request->GetLoadTimingInfo(&load_timing_info);
492 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
493 EXPECT_FALSE(load_timing_info.request_start.is_null());
[email protected]58e32bb2013-01-21 18:23:25494
[email protected]87a09a92011-07-14 15:50:50495 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50496 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36497 event_order_[req_id] += "OnResponseStarted\n";
[email protected]87a09a92011-07-14 15:50:50498 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
499 event_order_[req_id];
500 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
[email protected]2086a3d2012-11-13 17:49:20501 if (request->status().status() == URLRequestStatus::FAILED) {
[email protected]8202d0c2011-02-23 08:31:14502 error_count_++;
[email protected]d0cc35b2011-09-08 12:02:05503 last_error_ = request->status().error();
[email protected]8202d0c2011-02-23 08:31:14504 }
505}
[email protected]0651b812011-02-24 00:22:50506
sclittlece72c482015-08-24 20:20:59507void TestNetworkDelegate::OnNetworkBytesReceived(const URLRequest& request,
508 int64_t bytes_received) {
509 event_order_[request.identifier()] += "OnNetworkBytesReceived\n";
510 total_network_bytes_received_ += bytes_received;
[email protected]8523ba52011-05-22 19:00:58511}
512
[email protected]2086a3d2012-11-13 17:49:20513void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
[email protected]87a09a92011-07-14 15:50:50514 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50515 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36516 event_order_[req_id] += "OnCompleted\n";
[email protected]87a09a92011-07-14 15:50:50517 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
518 // is logically identical to
519 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
520 EXPECT_TRUE(!request->status().is_success() ||
521 (next_states_[req_id] & kStageCompletedSuccess)) <<
522 event_order_[req_id];
523 EXPECT_TRUE(request->status().is_success() ||
524 (next_states_[req_id] & kStageCompletedError)) <<
525 event_order_[req_id];
526 next_states_[req_id] = kStageURLRequestDestroyed;
[email protected]a83dd332011-07-13 10:41:01527 completed_requests_++;
[email protected]2086a3d2012-11-13 17:49:20528 if (request->status().status() == URLRequestStatus::FAILED) {
[email protected]8202d0c2011-02-23 08:31:14529 error_count_++;
[email protected]d0cc35b2011-09-08 12:02:05530 last_error_ = request->status().error();
[email protected]146b8b22013-11-20 03:59:18531 } else if (request->status().status() == URLRequestStatus::CANCELED) {
532 canceled_requests_++;
533 } else {
534 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status());
[email protected]8202d0c2011-02-23 08:31:14535 }
536}
[email protected]4b50cb52011-03-10 00:29:37537
[email protected]2086a3d2012-11-13 17:49:20538void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) {
[email protected]87a09a92011-07-14 15:50:50539 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50540 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36541 event_order_[req_id] += "OnURLRequestDestroyed\n";
[email protected]87a09a92011-07-14 15:50:50542 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
543 event_order_[req_id];
544 next_states_[req_id] = kStageDestruction;
[email protected]4c76d7c2011-04-15 19:14:12545 destroyed_requests_++;
[email protected]4875ba12011-03-30 22:31:51546}
547
[email protected]82a37672011-05-03 12:02:41548void TestNetworkDelegate::OnPACScriptError(int line_number,
[email protected]42cba2fb2013-03-29 19:58:57549 const base::string16& error) {
[email protected]82a37672011-05-03 12:02:41550}
[email protected]7efc582d2011-08-03 20:46:35551
[email protected]2086a3d2012-11-13 17:49:20552NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired(
553 URLRequest* request,
554 const AuthChallengeInfo& auth_info,
[email protected]c2911d72011-10-03 22:16:36555 const AuthCallback& callback,
[email protected]2086a3d2012-11-13 17:49:20556 AuthCredentials* credentials) {
[email protected]58e32bb2013-01-21 18:23:25557 load_timing_info_before_auth_ = LoadTimingInfo();
558 request->GetLoadTimingInfo(&load_timing_info_before_auth_);
559 has_load_timing_info_before_auth_ = true;
560 EXPECT_FALSE(load_timing_info_before_auth_.request_start_time.is_null());
561 EXPECT_FALSE(load_timing_info_before_auth_.request_start.is_null());
562
[email protected]c2911d72011-10-03 22:16:36563 int req_id = request->identifier();
564 InitRequestStatesIfNew(req_id);
565 event_order_[req_id] += "OnAuthRequired\n";
566 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) <<
567 event_order_[req_id];
568 next_states_[req_id] = kStageBeforeSendHeaders |
[email protected]78862512013-04-08 20:12:43569 kStageAuthRequired | // For example, proxy auth followed by server auth.
[email protected]ea8141e2011-10-05 13:12:51570 kStageHeadersReceived | // Request canceled by delegate simulates empty
571 // response.
[email protected]c2911d72011-10-03 22:16:36572 kStageResponseStarted | // data: URLs do not trigger sending headers
[email protected]dc5a5cf2012-09-26 02:49:30573 kStageBeforeRedirect | // a delegate can trigger a redirection
574 kStageCompletedError; // request cancelled before callback
[email protected]2086a3d2012-11-13 17:49:20575 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
[email protected]7efc582d2011-08-03 20:46:35576}
[email protected]1700c6a2012-02-22 18:07:07577
[email protected]2086a3d2012-11-13 17:49:20578bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request,
579 const CookieList& cookie_list) {
[email protected]9c8ae8c2012-03-09 13:13:35580 bool allow = true;
581 if (cookie_options_bit_mask_ & NO_GET_COOKIES)
582 allow = false;
583
584 if (!allow) {
585 blocked_get_cookies_count_++;
586 }
587
588 return allow;
589}
590
[email protected]2086a3d2012-11-13 17:49:20591bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
[email protected]4c219e22012-05-05 19:41:04592 const std::string& cookie_line,
[email protected]2086a3d2012-11-13 17:49:20593 CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35594 bool allow = true;
595 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
596 allow = false;
597
[email protected]9c8ae8c2012-03-09 13:13:35598 if (!allow) {
599 blocked_set_cookie_count_++;
600 } else {
601 set_cookie_count_++;
602 }
603
604 return allow;
605}
606
[email protected]2086a3d2012-11-13 17:49:20607bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request,
[email protected]6cdfd7f2013-02-08 20:40:15608 const base::FilePath& path) const {
[email protected]3ca8b362013-11-11 22:18:07609 return can_access_files_;
[email protected]4c219e22012-05-05 19:41:04610}
611
mkwst0513c9d2015-04-01 05:53:15612bool TestNetworkDelegate::OnFirstPartyOnlyCookieExperimentEnabled() const {
613 return first_party_only_cookies_enabled_;
614}
615
jochen0e3b3a62014-09-16 18:31:23616bool TestNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
617 const URLRequest& request,
618 const GURL& target_url,
619 const GURL& referrer_url) const {
620 return cancel_request_with_policy_violating_referrer_;
621}
622
[email protected]46ae16ba2012-09-18 14:02:03623TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) {
624}
625
[email protected]f53b4802012-12-20 17:04:23626URLRequestJob* TestJobInterceptor::MaybeCreateJob(
627 URLRequest* request,
628 NetworkDelegate* network_delegate) const {
[email protected]2086a3d2012-11-13 17:49:20629 URLRequestJob* job = main_intercept_job_;
[email protected]46ae16ba2012-09-18 14:02:03630 main_intercept_job_ = NULL;
631 return job;
632}
633
[email protected]2086a3d2012-11-13 17:49:20634void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) {
[email protected]46ae16ba2012-09-18 14:02:03635 main_intercept_job_ = job;
636}
[email protected]2086a3d2012-11-13 17:49:20637
638} // namespace net