blob: b3c03c7116432cd4f2c8e313e3be194c131d9ef1 [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
dchengc7eeda422015-12-26 03:56:487#include <utility>
8
[email protected]529623e2011-02-23 17:37:169#include "base/compiler_specific.h"
skyostil4891b25b2015-06-11 11:43:4510#include "base/location.h"
[email protected]d2db0292011-01-26 20:23:4411#include "base/logging.h"
[email protected]7f86564d2013-07-18 00:41:2212#include "base/message_loop/message_loop.h"
skyostil4891b25b2015-06-11 11:43:4513#include "base/single_thread_task_runner.h"
14#include "base/thread_task_runner_handle.h"
[email protected]d2db0292011-01-26 20:23:4415#include "base/threading/thread.h"
[email protected]bf470dfc2012-05-15 21:13:4516#include "base/threading/worker_pool.h"
[email protected]82b42302011-04-20 16:28:1617#include "net/base/host_port_pair.h"
[email protected]6e7845ae2013-03-29 21:48:1118#include "net/cert/cert_verifier.h"
[email protected]f2cb3cf2013-03-21 01:40:5319#include "net/dns/mock_host_resolver.h"
[email protected]57cb0f72011-01-28 06:33:5820#include "net/http/http_network_session.h"
[email protected]e50efea2014-03-24 18:41:0021#include "net/http/http_response_headers.h"
[email protected]17291a022011-10-10 07:32:5322#include "net/http/http_server_properties_impl.h"
[email protected]b1c988b2013-06-13 06:48:1123#include "net/http/transport_security_state.h"
[email protected]6b8a3c742014-07-25 00:25:3524#include "net/ssl/channel_id_service.h"
25#include "net/ssl/default_channel_id_store.h"
[email protected]ee4c30d2012-11-07 15:08:4326#include "net/url_request/static_http_user_agent_settings.h"
mmenkeed0498b2015-12-08 23:20:4227#include "net/url_request/url_request_job.h"
[email protected]9d5730b2012-08-24 17:42:4928#include "net/url_request/url_request_job_factory_impl.h"
[email protected]0140bd32012-03-21 01:47:1229#include "testing/gtest/include/gtest/gtest.h"
[email protected]d2db0292011-01-26 20:23:4430
[email protected]2086a3d2012-11-13 17:49:2031namespace net {
32
[email protected]87a09a92011-07-14 15:50:5033namespace {
34
[email protected]2086a3d2012-11-13 17:49:2035// These constants put the NetworkDelegate events of TestNetworkDelegate
[email protected]87a09a92011-07-14 15:50:5036// 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.
39const int kStageBeforeURLRequest = 1 << 0;
40const int kStageBeforeSendHeaders = 1 << 1;
[email protected]5796dc942011-07-14 19:26:1041const int kStageSendHeaders = 1 << 2;
[email protected]ea8141e2011-10-05 13:12:5142const int kStageHeadersReceived = 1 << 3;
43const int kStageAuthRequired = 1 << 4;
44const int kStageBeforeRedirect = 1 << 5;
45const int kStageResponseStarted = 1 << 6;
46const int kStageCompletedSuccess = 1 << 7;
47const int kStageCompletedError = 1 << 8;
48const int kStageURLRequestDestroyed = 1 << 9;
49const int kStageDestruction = 1 << 10;
[email protected]87a09a92011-07-14 15:50:5050
51} // namespace
52
[email protected]529623e2011-02-23 17:37:1653TestURLRequestContext::TestURLRequestContext()
[email protected]87a09a92011-07-14 15:50:5054 : initialized_(false),
[email protected]45499252013-01-23 17:12:5655 client_socket_factory_(NULL),
[email protected]aa249b52013-04-30 01:04:3256 context_storage_(this) {
[email protected]d2db0292011-01-26 20:23:4457 Init();
58}
59
[email protected]87a09a92011-07-14 15:50:5060TestURLRequestContext::TestURLRequestContext(bool delay_initialization)
61 : initialized_(false),
[email protected]45499252013-01-23 17:12:5662 client_socket_factory_(NULL),
[email protected]aa249b52013-04-30 01:04:3263 context_storage_(this) {
[email protected]87a09a92011-07-14 15:50:5064 if (!delay_initialization)
65 Init();
66}
67
[email protected]87a09a92011-07-14 15:50:5068TestURLRequestContext::~TestURLRequestContext() {
69 DCHECK(initialized_);
[email protected]424559492014-07-22 00:27:4070 AssertNoURLRequests();
[email protected]87a09a92011-07-14 15:50:5071}
[email protected]d2db0292011-01-26 20:23:4472
73void TestURLRequestContext::Init() {
[email protected]87a09a92011-07-14 15:50:5074 DCHECK(!initialized_);
75 initialized_ = true;
[email protected]d5a4dd62012-05-23 01:41:0476
77 if (!host_resolver())
[email protected]c54a8912012-10-22 22:09:4378 context_storage_.set_host_resolver(
[email protected]2086a3d2012-11-13 17:49:2079 scoped_ptr<HostResolver>(new MockCachingHostResolver()));
[email protected]d5a4dd62012-05-23 01:41:0480 if (!proxy_service())
[email protected]2086a3d2012-11-13 17:49:2081 context_storage_.set_proxy_service(ProxyService::CreateDirect());
[email protected]87a09a92011-07-14 15:50:5082 if (!cert_verifier())
[email protected]2086a3d2012-11-13 17:49:2083 context_storage_.set_cert_verifier(CertVerifier::CreateDefault());
[email protected]b1c988b2013-06-13 06:48:1184 if (!transport_security_state())
rdsmith82957ad2015-09-16 19:42:0385 context_storage_.set_transport_security_state(
86 make_scoped_ptr(new TransportSecurityState()));
[email protected]87a09a92011-07-14 15:50:5087 if (!ssl_config_service())
rdsmith82957ad2015-09-16 19:42:0388 context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults());
[email protected]87a09a92011-07-14 15:50:5089 if (!http_auth_handler_factory()) {
90 context_storage_.set_http_auth_handler_factory(
[email protected]2086a3d2012-11-13 17:49:2091 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
[email protected]87a09a92011-07-14 15:50:5092 }
[email protected]17291a022011-10-10 07:32:5393 if (!http_server_properties()) {
94 context_storage_.set_http_server_properties(
[email protected]30d4c022013-07-18 22:58:1695 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
[email protected]17291a022011-10-10 07:32:5396 }
[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();
mmenkee65e7af2015-10-13 17:16:42114 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]87a09a92011-07-14 15:50:50119 }
[email protected]d2db0292011-01-26 20:23:44120 // In-memory cookie store.
[email protected]87a09a92011-07-14 15:50:50121 if (!cookie_store())
[email protected]2086a3d2012-11-13 17:49:20122 context_storage_.set_cookie_store(new CookieMonster(NULL, NULL));
[email protected]6b8a3c742014-07-25 00:25:35123 // In-memory Channel ID service.
124 if (!channel_id_service()) {
mattm368d94622015-01-21 13:54:00125 context_storage_.set_channel_id_service(make_scoped_ptr(
126 new ChannelIDService(new DefaultChannelIDStore(NULL),
127 base::WorkerPool::GetTaskRunner(true))));
[email protected]eb7974c2012-01-25 17:38:48128 }
[email protected]ee4c30d2012-11-07 15:08:43129 if (!http_user_agent_settings()) {
rdsmith12c553e2015-09-30 22:44:21130 context_storage_.set_http_user_agent_settings(make_scoped_ptr(
131 new StaticHttpUserAgentSettings("en-us,fr", std::string())));
[email protected]ee4c30d2012-11-07 15:08:43132 }
rdsmith82957ad2015-09-16 19:42:03133 if (!job_factory()) {
134 context_storage_.set_job_factory(
rdsmith12c553e2015-09-30 22:44:21135 make_scoped_ptr(new URLRequestJobFactoryImpl()));
rdsmith82957ad2015-09-16 19:42:03136 }
[email protected]d2db0292011-01-26 20:23:44137}
138
[email protected]44738602012-03-02 22:56:01139TestURLRequestContextGetter::TestURLRequestContextGetter(
[email protected]4969b0122012-06-16 01:58:28140 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
141 : network_task_runner_(network_task_runner) {
[email protected]90499482013-06-01 00:39:50142 DCHECK(network_task_runner_.get());
[email protected]44738602012-03-02 22:56:01143}
144
[email protected]d5a4dd62012-05-23 01:41:04145TestURLRequestContextGetter::TestURLRequestContextGetter(
[email protected]4969b0122012-06-16 01:58:28146 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
[email protected]d5a4dd62012-05-23 01:41:04147 scoped_ptr<TestURLRequestContext> context)
dchengc7eeda422015-12-26 03:56:48148 : network_task_runner_(network_task_runner), context_(std::move(context)) {
[email protected]90499482013-06-01 00:39:50149 DCHECK(network_task_runner_.get());
[email protected]d5a4dd62012-05-23 01:41:04150}
151
[email protected]44738602012-03-02 22:56:01152TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
153
154TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() {
[email protected]ef2bf422012-05-11 03:27:09155 if (!context_.get())
156 context_.reset(new TestURLRequestContext);
[email protected]44738602012-03-02 22:56:01157 return context_.get();
158}
159
[email protected]4969b0122012-06-16 01:58:28160scoped_refptr<base::SingleThreadTaskRunner>
161TestURLRequestContextGetter::GetNetworkTaskRunner() const {
162 return network_task_runner_;
[email protected]44738602012-03-02 22:56:01163}
164
[email protected]d2db0292011-01-26 20:23:44165TestDelegate::TestDelegate()
166 : cancel_in_rr_(false),
167 cancel_in_rs_(false),
168 cancel_in_rd_(false),
169 cancel_in_rd_pending_(false),
[email protected]d2db0292011-01-26 20:23:44170 quit_on_complete_(true),
171 quit_on_redirect_(false),
mmenke8d457fbf2015-06-08 19:56:35172 quit_on_auth_required_(false),
[email protected]1826a402014-01-08 15:40:48173 quit_on_before_network_start_(false),
[email protected]d2db0292011-01-26 20:23:44174 allow_certificate_errors_(false),
175 response_started_count_(0),
176 received_bytes_count_(0),
177 received_redirect_count_(0),
[email protected]1826a402014-01-08 15:40:48178 received_before_network_start_count_(0),
[email protected]d2db0292011-01-26 20:23:44179 received_data_before_response_(false),
180 request_failed_(false),
181 have_certificate_errors_(false),
[email protected]46d117e2012-01-18 01:53:14182 certificate_errors_are_fatal_(false),
[email protected]c2911d72011-10-03 22:16:36183 auth_required_(false),
[email protected]79e1fd62013-06-20 06:50:04184 have_full_request_headers_(false),
[email protected]2086a3d2012-11-13 17:49:20185 buf_(new IOBuffer(kBufferSize)) {
[email protected]d2db0292011-01-26 20:23:44186}
187
188TestDelegate::~TestDelegate() {}
189
[email protected]79e1fd62013-06-20 06:50:04190void TestDelegate::ClearFullRequestHeaders() {
191 full_request_headers_.Clear();
192 have_full_request_headers_ = false;
193}
194
[email protected]2086a3d2012-11-13 17:49:20195void TestDelegate::OnReceivedRedirect(URLRequest* request,
[email protected]cba24642014-08-15 20:49:59196 const RedirectInfo& redirect_info,
[email protected]d2db0292011-01-26 20:23:44197 bool* defer_redirect) {
[email protected]847c0fa92012-11-06 16:37:42198 EXPECT_TRUE(request->is_redirecting());
[email protected]79e1fd62013-06-20 06:50:04199
200 have_full_request_headers_ =
201 request->GetFullRequestHeaders(&full_request_headers_);
202
[email protected]d2db0292011-01-26 20:23:44203 received_redirect_count_++;
204 if (quit_on_redirect_) {
205 *defer_redirect = true;
skyostil4891b25b2015-06-11 11:43:45206 base::ThreadTaskRunnerHandle::Get()->PostTask(
ki.stfu375812e2015-10-09 20:23:17207 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]d2db0292011-01-26 20:23:44208 } else if (cancel_in_rr_) {
209 request->Cancel();
210 }
211}
212
[email protected]1826a402014-01-08 15:40:48213void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) {
214 received_before_network_start_count_++;
215 if (quit_on_before_network_start_) {
216 *defer = true;
skyostil4891b25b2015-06-11 11:43:45217 base::ThreadTaskRunnerHandle::Get()->PostTask(
ki.stfu375812e2015-10-09 20:23:17218 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]1826a402014-01-08 15:40:48219 }
220}
221
[email protected]2086a3d2012-11-13 17:49:20222void TestDelegate::OnAuthRequired(URLRequest* request,
223 AuthChallengeInfo* auth_info) {
[email protected]c2911d72011-10-03 22:16:36224 auth_required_ = true;
mmenke8d457fbf2015-06-08 19:56:35225 if (quit_on_auth_required_) {
skyostil4891b25b2015-06-11 11:43:45226 base::ThreadTaskRunnerHandle::Get()->PostTask(
ki.stfu375812e2015-10-09 20:23:17227 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
mmenke8d457fbf2015-06-08 19:56:35228 return;
229 }
[email protected]f3cf9802011-10-28 18:44:58230 if (!credentials_.Empty()) {
231 request->SetAuth(credentials_);
[email protected]d2db0292011-01-26 20:23:44232 } else {
233 request->CancelAuth();
234 }
235}
236
[email protected]2086a3d2012-11-13 17:49:20237void TestDelegate::OnSSLCertificateError(URLRequest* request,
238 const SSLInfo& ssl_info,
[email protected]46d117e2012-01-18 01:53:14239 bool fatal) {
[email protected]d2db0292011-01-26 20:23:44240 // 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]46d117e2012-01-18 01:53:14244 certificate_errors_are_fatal_ = fatal;
[email protected]d2db0292011-01-26 20:23:44245 if (allow_certificate_errors_)
246 request->ContinueDespiteLastError();
247 else
248 request->Cancel();
249}
250
[email protected]2086a3d2012-11-13 17:49:20251void TestDelegate::OnResponseStarted(URLRequest* request) {
[email protected]d2db0292011-01-26 20:23:44252 // It doesn't make sense for the request to have IO pending at this point.
253 DCHECK(!request->status().is_io_pending());
[email protected]847c0fa92012-11-06 16:37:42254 EXPECT_FALSE(request->is_redirecting());
[email protected]d2db0292011-01-26 20:23:44255
[email protected]79e1fd62013-06-20 06:50:04256 have_full_request_headers_ =
257 request->GetFullRequestHeaders(&full_request_headers_);
258
[email protected]d2db0292011-01-26 20:23:44259 response_started_count_++;
260 if (cancel_in_rs_) {
261 request->Cancel();
262 OnResponseCompleted(request);
263 } else if (!request->status().is_success()) {
[email protected]2086a3d2012-11-13 17:49:20264 DCHECK(request->status().status() == URLRequestStatus::FAILED ||
265 request->status().status() == URLRequestStatus::CANCELED);
[email protected]d2db0292011-01-26 20:23:44266 request_failed_ = true;
267 OnResponseCompleted(request);
268 } else {
269 // Initiate the first read.
270 int bytes_read = 0;
[email protected]90499482013-06-01 00:39:50271 if (request->Read(buf_.get(), kBufferSize, &bytes_read))
[email protected]d2db0292011-01-26 20:23:44272 OnReadCompleted(request, bytes_read);
273 else if (!request->status().is_io_pending())
274 OnResponseCompleted(request);
275 }
276}
277
[email protected]2086a3d2012-11-13 17:49:20278void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) {
[email protected]d2db0292011-01-26 20:23:44279 // It doesn't make sense for the request to have IO pending at this point.
280 DCHECK(!request->status().is_io_pending());
281
davidbencf94bcd2015-12-04 02:39:14282 // 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]d2db0292011-01-26 20:23:44287 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]90499482013-06-01 00:39:50304 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) {
[email protected]d2db0292011-01-26 20:23:44305 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]2086a3d2012-11-13 17:49:20319void TestDelegate::OnResponseCompleted(URLRequest* request) {
[email protected]d2db0292011-01-26 20:23:44320 if (quit_on_complete_)
skyostil4891b25b2015-06-11 11:43:45321 base::ThreadTaskRunnerHandle::Get()->PostTask(
ki.stfu375812e2015-10-09 20:23:17322 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]d2db0292011-01-26 20:23:44323}
[email protected]8202d0c2011-02-23 08:31:14324
[email protected]0651b812011-02-24 00:22:50325TestNetworkDelegate::TestNetworkDelegate()
[email protected]9c8ae8c2012-03-09 13:13:35326 : last_error_(0),
327 error_count_(0),
328 created_requests_(0),
329 destroyed_requests_(0),
330 completed_requests_(0),
[email protected]146b8b22013-11-20 03:59:18331 canceled_requests_(0),
[email protected]9c8ae8c2012-03-09 13:13:35332 cookie_options_bit_mask_(0),
333 blocked_get_cookies_count_(0),
334 blocked_set_cookie_count_(0),
[email protected]58e32bb2013-01-21 18:23:25335 set_cookie_count_(0),
[email protected]597a1ab2014-06-26 08:12:27336 observed_before_proxy_headers_sent_callbacks_(0),
bengr1bf8e942014-11-07 01:36:50337 before_send_headers_count_(0),
338 headers_received_count_(0),
sclittlece72c482015-08-24 20:20:59339 total_network_bytes_received_(0),
sclittle28d558b2015-09-28 21:40:52340 total_network_bytes_sent_(0),
[email protected]58e32bb2013-01-21 18:23:25341 has_load_timing_info_before_redirect_(false),
[email protected]3ca8b362013-11-11 22:18:07342 has_load_timing_info_before_auth_(false),
343 can_access_files_(true),
estark7625d812015-10-12 20:10:41344 experimental_cookie_features_enabled_(false),
bengr1bf8e942014-11-07 01:36:50345 cancel_request_with_policy_violating_referrer_(false),
sclittlece72c482015-08-24 20:20:59346 will_be_intercepted_on_next_error_(false) {}
[email protected]8202d0c2011-02-23 08:31:14347
[email protected]87a09a92011-07-14 15:50:50348TestNetworkDelegate::~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]58e32bb2013-01-21 18:23:25356bool 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
362bool 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]87a09a92011-07-14 15:50:50368void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) {
369 if (next_states_.find(request_id) == next_states_.end()) {
[email protected]146b8b22013-11-20 03:59:18370 // 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]87a09a92011-07-14 15:50:50375 event_order_[request_id] = "";
376 }
377}
378
[email protected]4875ba12011-03-30 22:31:51379int TestNetworkDelegate::OnBeforeURLRequest(
[email protected]2086a3d2012-11-13 17:49:20380 URLRequest* request,
381 const CompletionCallback& callback,
[email protected]cba24642014-08-15 20:49:59382 GURL* new_url) {
[email protected]87a09a92011-07-14 15:50:50383 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50384 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36385 event_order_[req_id] += "OnBeforeURLRequest\n";
[email protected]87a09a92011-07-14 15:50:50386 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]c2911d72011-10-03 22:16:36392 kStageCompletedError | // request canceled by delegate
393 kStageAuthRequired; // Auth can come next for FTP requests
[email protected]4c76d7c2011-04-15 19:14:12394 created_requests_++;
[email protected]2086a3d2012-11-13 17:49:20395 return OK;
[email protected]8202d0c2011-02-23 08:31:14396}
397
[email protected]4875ba12011-03-30 22:31:51398int TestNetworkDelegate::OnBeforeSendHeaders(
[email protected]2086a3d2012-11-13 17:49:20399 URLRequest* request,
400 const CompletionCallback& callback,
401 HttpRequestHeaders* headers) {
[email protected]87a09a92011-07-14 15:50:50402 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50403 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36404 event_order_[req_id] += "OnBeforeSendHeaders\n";
[email protected]87a09a92011-07-14 15:50:50405 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) <<
406 event_order_[req_id];
407 next_states_[req_id] =
[email protected]5796dc942011-07-14 19:26:10408 kStageSendHeaders |
[email protected]87a09a92011-07-14 15:50:50409 kStageCompletedError; // request canceled by delegate
bengr1bf8e942014-11-07 01:36:50410 before_send_headers_count_++;
[email protected]2086a3d2012-11-13 17:49:20411 return OK;
[email protected]8202d0c2011-02-23 08:31:14412}
413
[email protected]597a1ab2014-06-26 08:12:27414void TestNetworkDelegate::OnBeforeSendProxyHeaders(
ttuttle859dc7a2015-04-23 19:42:29415 URLRequest* request,
416 const ProxyInfo& proxy_info,
417 HttpRequestHeaders* headers) {
[email protected]597a1ab2014-06-26 08:12:27418 ++observed_before_proxy_headers_sent_callbacks_;
419 last_observed_proxy_ = proxy_info.proxy_server().host_port_pair();
420}
421
[email protected]5796dc942011-07-14 19:26:10422void TestNetworkDelegate::OnSendHeaders(
[email protected]2086a3d2012-11-13 17:49:20423 URLRequest* request,
424 const HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10425 int req_id = request->identifier();
[email protected]5796dc942011-07-14 19:26:10426 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36427 event_order_[req_id] += "OnSendHeaders\n";
[email protected]5796dc942011-07-14 19:26:10428 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
429 event_order_[req_id];
bengr1bf8e942014-11-07 01:36:50430 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]ea8141e2011-10-05 13:12:51435}
436
437int TestNetworkDelegate::OnHeadersReceived(
[email protected]2086a3d2012-11-13 17:49:20438 URLRequest* request,
439 const CompletionCallback& callback,
440 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16441 scoped_refptr<HttpResponseHeaders>* override_response_headers,
442 GURL* allowed_unsafe_redirect_url) {
[email protected]ea8141e2011-10-05 13:12:51443 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]87a09a92011-07-14 15:50:50449 kStageBeforeRedirect |
450 kStageResponseStarted |
[email protected]c2911d72011-10-03 22:16:36451 kStageAuthRequired |
[email protected]87a09a92011-07-14 15:50:50452 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]5796dc942011-07-14 19:26:10456 next_states_[req_id] |= kStageBeforeSendHeaders;
[email protected]ea8141e2011-10-05 13:12:51457
[email protected]e50efea2014-03-24 18:41:00458 if (!redirect_on_headers_received_url_.is_empty()) {
459 *override_response_headers =
ttuttle859dc7a2015-04-23 19:42:29460 new HttpResponseHeaders(original_response_headers->raw_headers());
[email protected]e50efea2014-03-24 18:41:00461 (*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]5f714132014-03-26 10:41:16467
468 if (!allowed_unsafe_redirect_url_.is_empty())
469 *allowed_unsafe_redirect_url = allowed_unsafe_redirect_url_;
[email protected]e50efea2014-03-24 18:41:00470 }
bengr1bf8e942014-11-07 01:36:50471 headers_received_count_++;
[email protected]2086a3d2012-11-13 17:49:20472 return OK;
[email protected]82b42302011-04-20 16:28:16473}
474
[email protected]2086a3d2012-11-13 17:49:20475void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request,
[email protected]31b2e5f2011-04-20 16:58:32476 const GURL& new_location) {
[email protected]58e32bb2013-01-21 18:23:25477 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]87a09a92011-07-14 15:50:50483 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50484 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36485 event_order_[req_id] += "OnBeforeRedirect\n";
[email protected]87a09a92011-07-14 15:50:50486 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]31b2e5f2011-04-20 16:58:32497}
498
[email protected]2086a3d2012-11-13 17:49:20499void TestNetworkDelegate::OnResponseStarted(URLRequest* request) {
[email protected]0a703912013-02-02 04:25:17500 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]58e32bb2013-01-21 18:23:25504
[email protected]87a09a92011-07-14 15:50:50505 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50506 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36507 event_order_[req_id] += "OnResponseStarted\n";
[email protected]87a09a92011-07-14 15:50:50508 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
509 event_order_[req_id];
510 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
[email protected]2086a3d2012-11-13 17:49:20511 if (request->status().status() == URLRequestStatus::FAILED) {
[email protected]8202d0c2011-02-23 08:31:14512 error_count_++;
[email protected]d0cc35b2011-09-08 12:02:05513 last_error_ = request->status().error();
[email protected]8202d0c2011-02-23 08:31:14514 }
515}
[email protected]0651b812011-02-24 00:22:50516
sclittlea133de02015-11-10 23:54:21517void TestNetworkDelegate::OnNetworkBytesReceived(URLRequest* request,
sclittlece72c482015-08-24 20:20:59518 int64_t bytes_received) {
sclittlea133de02015-11-10 23:54:21519 event_order_[request->identifier()] += "OnNetworkBytesReceived\n";
sclittlece72c482015-08-24 20:20:59520 total_network_bytes_received_ += bytes_received;
[email protected]8523ba52011-05-22 19:00:58521}
522
sclittlea133de02015-11-10 23:54:21523void TestNetworkDelegate::OnNetworkBytesSent(URLRequest* request,
sclittle28d558b2015-09-28 21:40:52524 int64_t bytes_sent) {
sclittlea133de02015-11-10 23:54:21525 event_order_[request->identifier()] += "OnNetworkBytesSent\n";
sclittle28d558b2015-09-28 21:40:52526 total_network_bytes_sent_ += bytes_sent;
527}
528
[email protected]2086a3d2012-11-13 17:49:20529void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
[email protected]87a09a92011-07-14 15:50:50530 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50531 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36532 event_order_[req_id] += "OnCompleted\n";
[email protected]87a09a92011-07-14 15:50:50533 // 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]a83dd332011-07-13 10:41:01543 completed_requests_++;
[email protected]2086a3d2012-11-13 17:49:20544 if (request->status().status() == URLRequestStatus::FAILED) {
[email protected]8202d0c2011-02-23 08:31:14545 error_count_++;
[email protected]d0cc35b2011-09-08 12:02:05546 last_error_ = request->status().error();
[email protected]146b8b22013-11-20 03:59:18547 } else if (request->status().status() == URLRequestStatus::CANCELED) {
548 canceled_requests_++;
549 } else {
550 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status());
[email protected]8202d0c2011-02-23 08:31:14551 }
552}
[email protected]4b50cb52011-03-10 00:29:37553
[email protected]2086a3d2012-11-13 17:49:20554void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) {
[email protected]87a09a92011-07-14 15:50:50555 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50556 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36557 event_order_[req_id] += "OnURLRequestDestroyed\n";
[email protected]87a09a92011-07-14 15:50:50558 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
559 event_order_[req_id];
560 next_states_[req_id] = kStageDestruction;
[email protected]4c76d7c2011-04-15 19:14:12561 destroyed_requests_++;
[email protected]4875ba12011-03-30 22:31:51562}
563
[email protected]82a37672011-05-03 12:02:41564void TestNetworkDelegate::OnPACScriptError(int line_number,
[email protected]42cba2fb2013-03-29 19:58:57565 const base::string16& error) {
[email protected]82a37672011-05-03 12:02:41566}
[email protected]7efc582d2011-08-03 20:46:35567
[email protected]2086a3d2012-11-13 17:49:20568NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired(
569 URLRequest* request,
570 const AuthChallengeInfo& auth_info,
[email protected]c2911d72011-10-03 22:16:36571 const AuthCallback& callback,
[email protected]2086a3d2012-11-13 17:49:20572 AuthCredentials* credentials) {
[email protected]58e32bb2013-01-21 18:23:25573 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]c2911d72011-10-03 22:16:36579 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]78862512013-04-08 20:12:43585 kStageAuthRequired | // For example, proxy auth followed by server auth.
[email protected]ea8141e2011-10-05 13:12:51586 kStageHeadersReceived | // Request canceled by delegate simulates empty
587 // response.
[email protected]c2911d72011-10-03 22:16:36588 kStageResponseStarted | // data: URLs do not trigger sending headers
[email protected]dc5a5cf2012-09-26 02:49:30589 kStageBeforeRedirect | // a delegate can trigger a redirection
590 kStageCompletedError; // request cancelled before callback
[email protected]2086a3d2012-11-13 17:49:20591 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
[email protected]7efc582d2011-08-03 20:46:35592}
[email protected]1700c6a2012-02-22 18:07:07593
[email protected]2086a3d2012-11-13 17:49:20594bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request,
595 const CookieList& cookie_list) {
[email protected]9c8ae8c2012-03-09 13:13:35596 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]2086a3d2012-11-13 17:49:20607bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
[email protected]4c219e22012-05-05 19:41:04608 const std::string& cookie_line,
[email protected]2086a3d2012-11-13 17:49:20609 CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35610 bool allow = true;
611 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
612 allow = false;
613
[email protected]9c8ae8c2012-03-09 13:13:35614 if (!allow) {
615 blocked_set_cookie_count_++;
616 } else {
617 set_cookie_count_++;
618 }
619
620 return allow;
621}
622
[email protected]2086a3d2012-11-13 17:49:20623bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request,
[email protected]6cdfd7f2013-02-08 20:40:15624 const base::FilePath& path) const {
[email protected]3ca8b362013-11-11 22:18:07625 return can_access_files_;
[email protected]4c219e22012-05-05 19:41:04626}
627
estark7625d812015-10-12 20:10:41628bool TestNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
629 return experimental_cookie_features_enabled_;
mkwst0513c9d2015-04-01 05:53:15630}
631
jww79aceda2015-12-07 01:56:34632bool TestNetworkDelegate::OnAreStrictSecureCookiesEnabled() const {
633 return experimental_cookie_features_enabled_;
634}
635
jochen0e3b3a62014-09-16 18:31:23636bool 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
mmenkeed0498b2015-12-08 23:20:42643TestJobInterceptor::TestJobInterceptor() {}
644
645TestJobInterceptor::~TestJobInterceptor() {}
[email protected]46ae16ba2012-09-18 14:02:03646
[email protected]f53b4802012-12-20 17:04:23647URLRequestJob* TestJobInterceptor::MaybeCreateJob(
648 URLRequest* request,
649 NetworkDelegate* network_delegate) const {
mmenkeed0498b2015-12-08 23:20:42650 return main_intercept_job_.release();
[email protected]46ae16ba2012-09-18 14:02:03651}
652
mmenkeed0498b2015-12-08 23:20:42653void TestJobInterceptor::set_main_intercept_job(scoped_ptr<URLRequestJob> job) {
654 main_intercept_job_ = std::move(job);
[email protected]46ae16ba2012-09-18 14:02:03655}
[email protected]2086a3d2012-11-13 17:49:20656
657} // namespace net