blob: 0c240b8416fbc816b0323971581cc1bf9c1ba861 [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())
rdsmith82957ad2015-09-16 19:42:0382 context_storage_.set_transport_security_state(
83 make_scoped_ptr(new TransportSecurityState()));
[email protected]87a09a92011-07-14 15:50:5084 if (!ssl_config_service())
rdsmith82957ad2015-09-16 19:42:0385 context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults());
[email protected]87a09a92011-07-14 15:50:5086 if (!http_auth_handler_factory()) {
87 context_storage_.set_http_auth_handler_factory(
[email protected]2086a3d2012-11-13 17:49:2088 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
[email protected]87a09a92011-07-14 15:50:5089 }
[email protected]17291a022011-10-10 07:32:5390 if (!http_server_properties()) {
91 context_storage_.set_http_server_properties(
[email protected]30d4c022013-07-18 22:58:1692 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
[email protected]17291a022011-10-10 07:32:5393 }
[email protected]45499252013-01-23 17:12:5694 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]067ce8b02014-06-02 22:20:0299 if (http_network_session_params_)
100 params = *http_network_session_params_;
[email protected]45499252013-01-23 17:12:56101 params.client_socket_factory = client_socket_factory();
102 params.host_resolver = host_resolver();
103 params.cert_verifier = cert_verifier();
[email protected]b1c988b2013-06-13 06:48:11104 params.transport_security_state = transport_security_state();
[email protected]45499252013-01-23 17:12:56105 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();
mmenkee65e7af2015-10-13 17:16:42111 context_storage_.set_http_network_session(
112 make_scoped_ptr(new HttpNetworkSession(params)));
113 context_storage_.set_http_transaction_factory(make_scoped_ptr(
114 new HttpCache(context_storage_.http_network_session(),
115 HttpCache::DefaultBackend::InMemory(0), false)));
[email protected]87a09a92011-07-14 15:50:50116 }
[email protected]d2db0292011-01-26 20:23:44117 // In-memory cookie store.
[email protected]87a09a92011-07-14 15:50:50118 if (!cookie_store())
[email protected]2086a3d2012-11-13 17:49:20119 context_storage_.set_cookie_store(new CookieMonster(NULL, NULL));
[email protected]6b8a3c742014-07-25 00:25:35120 // In-memory Channel ID service.
121 if (!channel_id_service()) {
mattm368d94622015-01-21 13:54:00122 context_storage_.set_channel_id_service(make_scoped_ptr(
123 new ChannelIDService(new DefaultChannelIDStore(NULL),
124 base::WorkerPool::GetTaskRunner(true))));
[email protected]eb7974c2012-01-25 17:38:48125 }
[email protected]ee4c30d2012-11-07 15:08:43126 if (!http_user_agent_settings()) {
rdsmith12c553e2015-09-30 22:44:21127 context_storage_.set_http_user_agent_settings(make_scoped_ptr(
128 new StaticHttpUserAgentSettings("en-us,fr", std::string())));
[email protected]ee4c30d2012-11-07 15:08:43129 }
rdsmith82957ad2015-09-16 19:42:03130 if (!job_factory()) {
131 context_storage_.set_job_factory(
rdsmith12c553e2015-09-30 22:44:21132 make_scoped_ptr(new URLRequestJobFactoryImpl()));
rdsmith82957ad2015-09-16 19:42:03133 }
[email protected]d2db0292011-01-26 20:23:44134}
135
[email protected]44738602012-03-02 22:56:01136TestURLRequestContextGetter::TestURLRequestContextGetter(
[email protected]4969b0122012-06-16 01:58:28137 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
138 : network_task_runner_(network_task_runner) {
[email protected]90499482013-06-01 00:39:50139 DCHECK(network_task_runner_.get());
[email protected]44738602012-03-02 22:56:01140}
141
[email protected]d5a4dd62012-05-23 01:41:04142TestURLRequestContextGetter::TestURLRequestContextGetter(
[email protected]4969b0122012-06-16 01:58:28143 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
[email protected]d5a4dd62012-05-23 01:41:04144 scoped_ptr<TestURLRequestContext> context)
[email protected]4969b0122012-06-16 01:58:28145 : network_task_runner_(network_task_runner), context_(context.Pass()) {
[email protected]90499482013-06-01 00:39:50146 DCHECK(network_task_runner_.get());
[email protected]d5a4dd62012-05-23 01:41:04147}
148
[email protected]44738602012-03-02 22:56:01149TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
150
151TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() {
[email protected]ef2bf422012-05-11 03:27:09152 if (!context_.get())
153 context_.reset(new TestURLRequestContext);
[email protected]44738602012-03-02 22:56:01154 return context_.get();
155}
156
[email protected]4969b0122012-06-16 01:58:28157scoped_refptr<base::SingleThreadTaskRunner>
158TestURLRequestContextGetter::GetNetworkTaskRunner() const {
159 return network_task_runner_;
[email protected]44738602012-03-02 22:56:01160}
161
[email protected]d2db0292011-01-26 20:23:44162TestDelegate::TestDelegate()
163 : cancel_in_rr_(false),
164 cancel_in_rs_(false),
165 cancel_in_rd_(false),
166 cancel_in_rd_pending_(false),
[email protected]d2db0292011-01-26 20:23:44167 quit_on_complete_(true),
168 quit_on_redirect_(false),
mmenke8d457fbf2015-06-08 19:56:35169 quit_on_auth_required_(false),
[email protected]1826a402014-01-08 15:40:48170 quit_on_before_network_start_(false),
[email protected]d2db0292011-01-26 20:23:44171 allow_certificate_errors_(false),
172 response_started_count_(0),
173 received_bytes_count_(0),
174 received_redirect_count_(0),
[email protected]1826a402014-01-08 15:40:48175 received_before_network_start_count_(0),
[email protected]d2db0292011-01-26 20:23:44176 received_data_before_response_(false),
177 request_failed_(false),
178 have_certificate_errors_(false),
[email protected]46d117e2012-01-18 01:53:14179 certificate_errors_are_fatal_(false),
[email protected]c2911d72011-10-03 22:16:36180 auth_required_(false),
[email protected]79e1fd62013-06-20 06:50:04181 have_full_request_headers_(false),
[email protected]2086a3d2012-11-13 17:49:20182 buf_(new IOBuffer(kBufferSize)) {
[email protected]d2db0292011-01-26 20:23:44183}
184
185TestDelegate::~TestDelegate() {}
186
[email protected]79e1fd62013-06-20 06:50:04187void TestDelegate::ClearFullRequestHeaders() {
188 full_request_headers_.Clear();
189 have_full_request_headers_ = false;
190}
191
[email protected]2086a3d2012-11-13 17:49:20192void TestDelegate::OnReceivedRedirect(URLRequest* request,
[email protected]cba24642014-08-15 20:49:59193 const RedirectInfo& redirect_info,
[email protected]d2db0292011-01-26 20:23:44194 bool* defer_redirect) {
[email protected]847c0fa92012-11-06 16:37:42195 EXPECT_TRUE(request->is_redirecting());
[email protected]79e1fd62013-06-20 06:50:04196
197 have_full_request_headers_ =
198 request->GetFullRequestHeaders(&full_request_headers_);
199
[email protected]d2db0292011-01-26 20:23:44200 received_redirect_count_++;
201 if (quit_on_redirect_) {
202 *defer_redirect = true;
skyostil4891b25b2015-06-11 11:43:45203 base::ThreadTaskRunnerHandle::Get()->PostTask(
ki.stfu375812e2015-10-09 20:23:17204 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]d2db0292011-01-26 20:23:44205 } else if (cancel_in_rr_) {
206 request->Cancel();
207 }
208}
209
[email protected]1826a402014-01-08 15:40:48210void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) {
211 received_before_network_start_count_++;
212 if (quit_on_before_network_start_) {
213 *defer = true;
skyostil4891b25b2015-06-11 11:43:45214 base::ThreadTaskRunnerHandle::Get()->PostTask(
ki.stfu375812e2015-10-09 20:23:17215 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]1826a402014-01-08 15:40:48216 }
217}
218
[email protected]2086a3d2012-11-13 17:49:20219void TestDelegate::OnAuthRequired(URLRequest* request,
220 AuthChallengeInfo* auth_info) {
[email protected]c2911d72011-10-03 22:16:36221 auth_required_ = true;
mmenke8d457fbf2015-06-08 19:56:35222 if (quit_on_auth_required_) {
skyostil4891b25b2015-06-11 11:43:45223 base::ThreadTaskRunnerHandle::Get()->PostTask(
ki.stfu375812e2015-10-09 20:23:17224 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
mmenke8d457fbf2015-06-08 19:56:35225 return;
226 }
[email protected]f3cf9802011-10-28 18:44:58227 if (!credentials_.Empty()) {
228 request->SetAuth(credentials_);
[email protected]d2db0292011-01-26 20:23:44229 } else {
230 request->CancelAuth();
231 }
232}
233
[email protected]2086a3d2012-11-13 17:49:20234void TestDelegate::OnSSLCertificateError(URLRequest* request,
235 const SSLInfo& ssl_info,
[email protected]46d117e2012-01-18 01:53:14236 bool fatal) {
[email protected]d2db0292011-01-26 20:23:44237 // The caller can control whether it needs all SSL requests to go through,
238 // independent of any possible errors, or whether it wants SSL errors to
239 // cancel the request.
240 have_certificate_errors_ = true;
[email protected]46d117e2012-01-18 01:53:14241 certificate_errors_are_fatal_ = fatal;
[email protected]d2db0292011-01-26 20:23:44242 if (allow_certificate_errors_)
243 request->ContinueDespiteLastError();
244 else
245 request->Cancel();
246}
247
[email protected]2086a3d2012-11-13 17:49:20248void TestDelegate::OnResponseStarted(URLRequest* request) {
[email protected]d2db0292011-01-26 20:23:44249 // It doesn't make sense for the request to have IO pending at this point.
250 DCHECK(!request->status().is_io_pending());
[email protected]847c0fa92012-11-06 16:37:42251 EXPECT_FALSE(request->is_redirecting());
[email protected]d2db0292011-01-26 20:23:44252
[email protected]79e1fd62013-06-20 06:50:04253 have_full_request_headers_ =
254 request->GetFullRequestHeaders(&full_request_headers_);
255
[email protected]d2db0292011-01-26 20:23:44256 response_started_count_++;
257 if (cancel_in_rs_) {
258 request->Cancel();
259 OnResponseCompleted(request);
260 } else if (!request->status().is_success()) {
[email protected]2086a3d2012-11-13 17:49:20261 DCHECK(request->status().status() == URLRequestStatus::FAILED ||
262 request->status().status() == URLRequestStatus::CANCELED);
[email protected]d2db0292011-01-26 20:23:44263 request_failed_ = true;
264 OnResponseCompleted(request);
265 } else {
266 // Initiate the first read.
267 int bytes_read = 0;
[email protected]90499482013-06-01 00:39:50268 if (request->Read(buf_.get(), kBufferSize, &bytes_read))
[email protected]d2db0292011-01-26 20:23:44269 OnReadCompleted(request, bytes_read);
270 else if (!request->status().is_io_pending())
271 OnResponseCompleted(request);
272 }
273}
274
[email protected]2086a3d2012-11-13 17:49:20275void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) {
[email protected]d2db0292011-01-26 20:23:44276 // It doesn't make sense for the request to have IO pending at this point.
277 DCHECK(!request->status().is_io_pending());
278
279 if (response_started_count_ == 0)
280 received_data_before_response_ = true;
281
282 if (cancel_in_rd_)
283 request->Cancel();
284
285 if (bytes_read >= 0) {
286 // There is data to read.
287 received_bytes_count_ += bytes_read;
288
289 // consume the data
290 data_received_.append(buf_->data(), bytes_read);
291 }
292
293 // If it was not end of stream, request to read more.
294 if (request->status().is_success() && bytes_read > 0) {
295 bytes_read = 0;
[email protected]90499482013-06-01 00:39:50296 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) {
[email protected]d2db0292011-01-26 20:23:44297 if (bytes_read > 0) {
298 data_received_.append(buf_->data(), bytes_read);
299 received_bytes_count_ += bytes_read;
300 } else {
301 break;
302 }
303 }
304 }
305 if (!request->status().is_io_pending())
306 OnResponseCompleted(request);
307 else if (cancel_in_rd_pending_)
308 request->Cancel();
309}
310
[email protected]2086a3d2012-11-13 17:49:20311void TestDelegate::OnResponseCompleted(URLRequest* request) {
[email protected]d2db0292011-01-26 20:23:44312 if (quit_on_complete_)
skyostil4891b25b2015-06-11 11:43:45313 base::ThreadTaskRunnerHandle::Get()->PostTask(
ki.stfu375812e2015-10-09 20:23:17314 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure());
[email protected]d2db0292011-01-26 20:23:44315}
[email protected]8202d0c2011-02-23 08:31:14316
[email protected]0651b812011-02-24 00:22:50317TestNetworkDelegate::TestNetworkDelegate()
[email protected]9c8ae8c2012-03-09 13:13:35318 : last_error_(0),
319 error_count_(0),
320 created_requests_(0),
321 destroyed_requests_(0),
322 completed_requests_(0),
[email protected]146b8b22013-11-20 03:59:18323 canceled_requests_(0),
[email protected]9c8ae8c2012-03-09 13:13:35324 cookie_options_bit_mask_(0),
325 blocked_get_cookies_count_(0),
326 blocked_set_cookie_count_(0),
[email protected]58e32bb2013-01-21 18:23:25327 set_cookie_count_(0),
[email protected]597a1ab2014-06-26 08:12:27328 observed_before_proxy_headers_sent_callbacks_(0),
bengr1bf8e942014-11-07 01:36:50329 before_send_headers_count_(0),
330 headers_received_count_(0),
sclittlece72c482015-08-24 20:20:59331 total_network_bytes_received_(0),
sclittle28d558b2015-09-28 21:40:52332 total_network_bytes_sent_(0),
[email protected]58e32bb2013-01-21 18:23:25333 has_load_timing_info_before_redirect_(false),
[email protected]3ca8b362013-11-11 22:18:07334 has_load_timing_info_before_auth_(false),
335 can_access_files_(true),
estark7625d812015-10-12 20:10:41336 experimental_cookie_features_enabled_(false),
bengr1bf8e942014-11-07 01:36:50337 cancel_request_with_policy_violating_referrer_(false),
sclittlece72c482015-08-24 20:20:59338 will_be_intercepted_on_next_error_(false) {}
[email protected]8202d0c2011-02-23 08:31:14339
[email protected]87a09a92011-07-14 15:50:50340TestNetworkDelegate::~TestNetworkDelegate() {
341 for (std::map<int, int>::iterator i = next_states_.begin();
342 i != next_states_.end(); ++i) {
343 event_order_[i->first] += "~TestNetworkDelegate\n";
344 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first];
345 }
346}
347
[email protected]58e32bb2013-01-21 18:23:25348bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect(
349 LoadTimingInfo* load_timing_info_before_redirect) const {
350 *load_timing_info_before_redirect = load_timing_info_before_redirect_;
351 return has_load_timing_info_before_redirect_;
352}
353
354bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth(
355 LoadTimingInfo* load_timing_info_before_auth) const {
356 *load_timing_info_before_auth = load_timing_info_before_auth_;
357 return has_load_timing_info_before_auth_;
358}
359
[email protected]87a09a92011-07-14 15:50:50360void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) {
361 if (next_states_.find(request_id) == next_states_.end()) {
[email protected]146b8b22013-11-20 03:59:18362 // TODO(davidben): Although the URLRequest documentation does not allow
363 // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's
364 // destructor also calls Cancel. Either officially support this or fix the
365 // ResourceLoader code.
366 next_states_[request_id] = kStageBeforeURLRequest | kStageCompletedError;
[email protected]87a09a92011-07-14 15:50:50367 event_order_[request_id] = "";
368 }
369}
370
[email protected]4875ba12011-03-30 22:31:51371int TestNetworkDelegate::OnBeforeURLRequest(
[email protected]2086a3d2012-11-13 17:49:20372 URLRequest* request,
373 const CompletionCallback& callback,
[email protected]cba24642014-08-15 20:49:59374 GURL* new_url) {
[email protected]87a09a92011-07-14 15:50:50375 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50376 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36377 event_order_[req_id] += "OnBeforeURLRequest\n";
[email protected]87a09a92011-07-14 15:50:50378 EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) <<
379 event_order_[req_id];
380 next_states_[req_id] =
381 kStageBeforeSendHeaders |
382 kStageResponseStarted | // data: URLs do not trigger sending headers
383 kStageBeforeRedirect | // a delegate can trigger a redirection
[email protected]c2911d72011-10-03 22:16:36384 kStageCompletedError | // request canceled by delegate
385 kStageAuthRequired; // Auth can come next for FTP requests
[email protected]4c76d7c2011-04-15 19:14:12386 created_requests_++;
[email protected]2086a3d2012-11-13 17:49:20387 return OK;
[email protected]8202d0c2011-02-23 08:31:14388}
389
[email protected]4875ba12011-03-30 22:31:51390int TestNetworkDelegate::OnBeforeSendHeaders(
[email protected]2086a3d2012-11-13 17:49:20391 URLRequest* request,
392 const CompletionCallback& callback,
393 HttpRequestHeaders* headers) {
[email protected]87a09a92011-07-14 15:50:50394 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50395 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36396 event_order_[req_id] += "OnBeforeSendHeaders\n";
[email protected]87a09a92011-07-14 15:50:50397 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) <<
398 event_order_[req_id];
399 next_states_[req_id] =
[email protected]5796dc942011-07-14 19:26:10400 kStageSendHeaders |
[email protected]87a09a92011-07-14 15:50:50401 kStageCompletedError; // request canceled by delegate
bengr1bf8e942014-11-07 01:36:50402 before_send_headers_count_++;
[email protected]2086a3d2012-11-13 17:49:20403 return OK;
[email protected]8202d0c2011-02-23 08:31:14404}
405
[email protected]597a1ab2014-06-26 08:12:27406void TestNetworkDelegate::OnBeforeSendProxyHeaders(
ttuttle859dc7a2015-04-23 19:42:29407 URLRequest* request,
408 const ProxyInfo& proxy_info,
409 HttpRequestHeaders* headers) {
[email protected]597a1ab2014-06-26 08:12:27410 ++observed_before_proxy_headers_sent_callbacks_;
411 last_observed_proxy_ = proxy_info.proxy_server().host_port_pair();
412}
413
[email protected]5796dc942011-07-14 19:26:10414void TestNetworkDelegate::OnSendHeaders(
[email protected]2086a3d2012-11-13 17:49:20415 URLRequest* request,
416 const HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10417 int req_id = request->identifier();
[email protected]5796dc942011-07-14 19:26:10418 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36419 event_order_[req_id] += "OnSendHeaders\n";
[email protected]5796dc942011-07-14 19:26:10420 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
421 event_order_[req_id];
bengr1bf8e942014-11-07 01:36:50422 if (!will_be_intercepted_on_next_error_)
423 next_states_[req_id] = kStageHeadersReceived | kStageCompletedError;
424 else
425 next_states_[req_id] = kStageResponseStarted;
426 will_be_intercepted_on_next_error_ = false;
[email protected]ea8141e2011-10-05 13:12:51427}
428
429int TestNetworkDelegate::OnHeadersReceived(
[email protected]2086a3d2012-11-13 17:49:20430 URLRequest* request,
431 const CompletionCallback& callback,
432 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16433 scoped_refptr<HttpResponseHeaders>* override_response_headers,
434 GURL* allowed_unsafe_redirect_url) {
[email protected]ea8141e2011-10-05 13:12:51435 int req_id = request->identifier();
436 event_order_[req_id] += "OnHeadersReceived\n";
437 InitRequestStatesIfNew(req_id);
438 EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) <<
439 event_order_[req_id];
440 next_states_[req_id] =
[email protected]87a09a92011-07-14 15:50:50441 kStageBeforeRedirect |
442 kStageResponseStarted |
[email protected]c2911d72011-10-03 22:16:36443 kStageAuthRequired |
[email protected]87a09a92011-07-14 15:50:50444 kStageCompletedError; // e.g. proxy resolution problem
445
446 // Basic authentication sends a second request from the URLRequestHttpJob
447 // layer before the URLRequest reports that a response has started.
[email protected]5796dc942011-07-14 19:26:10448 next_states_[req_id] |= kStageBeforeSendHeaders;
[email protected]ea8141e2011-10-05 13:12:51449
[email protected]e50efea2014-03-24 18:41:00450 if (!redirect_on_headers_received_url_.is_empty()) {
451 *override_response_headers =
ttuttle859dc7a2015-04-23 19:42:29452 new HttpResponseHeaders(original_response_headers->raw_headers());
[email protected]e50efea2014-03-24 18:41:00453 (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found");
454 (*override_response_headers)->RemoveHeader("Location");
455 (*override_response_headers)->AddHeader(
456 "Location: " + redirect_on_headers_received_url_.spec());
457
458 redirect_on_headers_received_url_ = GURL();
[email protected]5f714132014-03-26 10:41:16459
460 if (!allowed_unsafe_redirect_url_.is_empty())
461 *allowed_unsafe_redirect_url = allowed_unsafe_redirect_url_;
[email protected]e50efea2014-03-24 18:41:00462 }
bengr1bf8e942014-11-07 01:36:50463 headers_received_count_++;
[email protected]2086a3d2012-11-13 17:49:20464 return OK;
[email protected]82b42302011-04-20 16:28:16465}
466
[email protected]2086a3d2012-11-13 17:49:20467void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request,
[email protected]31b2e5f2011-04-20 16:58:32468 const GURL& new_location) {
[email protected]58e32bb2013-01-21 18:23:25469 load_timing_info_before_redirect_ = LoadTimingInfo();
470 request->GetLoadTimingInfo(&load_timing_info_before_redirect_);
471 has_load_timing_info_before_redirect_ = true;
472 EXPECT_FALSE(load_timing_info_before_redirect_.request_start_time.is_null());
473 EXPECT_FALSE(load_timing_info_before_redirect_.request_start.is_null());
474
[email protected]87a09a92011-07-14 15:50:50475 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50476 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36477 event_order_[req_id] += "OnBeforeRedirect\n";
[email protected]87a09a92011-07-14 15:50:50478 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) <<
479 event_order_[req_id];
480 next_states_[req_id] =
481 kStageBeforeURLRequest | // HTTP redirects trigger this.
482 kStageBeforeSendHeaders | // Redirects from the network delegate do not
483 // trigger onBeforeURLRequest.
484 kStageCompletedError;
485
486 // A redirect can lead to a file or a data URL. In this case, we do not send
487 // headers.
488 next_states_[req_id] |= kStageResponseStarted;
[email protected]31b2e5f2011-04-20 16:58:32489}
490
[email protected]2086a3d2012-11-13 17:49:20491void TestNetworkDelegate::OnResponseStarted(URLRequest* request) {
[email protected]0a703912013-02-02 04:25:17492 LoadTimingInfo load_timing_info;
493 request->GetLoadTimingInfo(&load_timing_info);
494 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
495 EXPECT_FALSE(load_timing_info.request_start.is_null());
[email protected]58e32bb2013-01-21 18:23:25496
[email protected]87a09a92011-07-14 15:50:50497 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50498 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36499 event_order_[req_id] += "OnResponseStarted\n";
[email protected]87a09a92011-07-14 15:50:50500 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
501 event_order_[req_id];
502 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
[email protected]2086a3d2012-11-13 17:49:20503 if (request->status().status() == URLRequestStatus::FAILED) {
[email protected]8202d0c2011-02-23 08:31:14504 error_count_++;
[email protected]d0cc35b2011-09-08 12:02:05505 last_error_ = request->status().error();
[email protected]8202d0c2011-02-23 08:31:14506 }
507}
[email protected]0651b812011-02-24 00:22:50508
sclittlece72c482015-08-24 20:20:59509void TestNetworkDelegate::OnNetworkBytesReceived(const URLRequest& request,
510 int64_t bytes_received) {
511 event_order_[request.identifier()] += "OnNetworkBytesReceived\n";
512 total_network_bytes_received_ += bytes_received;
[email protected]8523ba52011-05-22 19:00:58513}
514
sclittle28d558b2015-09-28 21:40:52515void TestNetworkDelegate::OnNetworkBytesSent(const URLRequest& request,
516 int64_t bytes_sent) {
517 event_order_[request.identifier()] += "OnNetworkBytesSent\n";
518 total_network_bytes_sent_ += bytes_sent;
519}
520
[email protected]2086a3d2012-11-13 17:49:20521void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
[email protected]87a09a92011-07-14 15:50:50522 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50523 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36524 event_order_[req_id] += "OnCompleted\n";
[email protected]87a09a92011-07-14 15:50:50525 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
526 // is logically identical to
527 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
528 EXPECT_TRUE(!request->status().is_success() ||
529 (next_states_[req_id] & kStageCompletedSuccess)) <<
530 event_order_[req_id];
531 EXPECT_TRUE(request->status().is_success() ||
532 (next_states_[req_id] & kStageCompletedError)) <<
533 event_order_[req_id];
534 next_states_[req_id] = kStageURLRequestDestroyed;
[email protected]a83dd332011-07-13 10:41:01535 completed_requests_++;
[email protected]2086a3d2012-11-13 17:49:20536 if (request->status().status() == URLRequestStatus::FAILED) {
[email protected]8202d0c2011-02-23 08:31:14537 error_count_++;
[email protected]d0cc35b2011-09-08 12:02:05538 last_error_ = request->status().error();
[email protected]146b8b22013-11-20 03:59:18539 } else if (request->status().status() == URLRequestStatus::CANCELED) {
540 canceled_requests_++;
541 } else {
542 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status());
[email protected]8202d0c2011-02-23 08:31:14543 }
544}
[email protected]4b50cb52011-03-10 00:29:37545
[email protected]2086a3d2012-11-13 17:49:20546void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) {
[email protected]87a09a92011-07-14 15:50:50547 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50548 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36549 event_order_[req_id] += "OnURLRequestDestroyed\n";
[email protected]87a09a92011-07-14 15:50:50550 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
551 event_order_[req_id];
552 next_states_[req_id] = kStageDestruction;
[email protected]4c76d7c2011-04-15 19:14:12553 destroyed_requests_++;
[email protected]4875ba12011-03-30 22:31:51554}
555
[email protected]82a37672011-05-03 12:02:41556void TestNetworkDelegate::OnPACScriptError(int line_number,
[email protected]42cba2fb2013-03-29 19:58:57557 const base::string16& error) {
[email protected]82a37672011-05-03 12:02:41558}
[email protected]7efc582d2011-08-03 20:46:35559
[email protected]2086a3d2012-11-13 17:49:20560NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired(
561 URLRequest* request,
562 const AuthChallengeInfo& auth_info,
[email protected]c2911d72011-10-03 22:16:36563 const AuthCallback& callback,
[email protected]2086a3d2012-11-13 17:49:20564 AuthCredentials* credentials) {
[email protected]58e32bb2013-01-21 18:23:25565 load_timing_info_before_auth_ = LoadTimingInfo();
566 request->GetLoadTimingInfo(&load_timing_info_before_auth_);
567 has_load_timing_info_before_auth_ = true;
568 EXPECT_FALSE(load_timing_info_before_auth_.request_start_time.is_null());
569 EXPECT_FALSE(load_timing_info_before_auth_.request_start.is_null());
570
[email protected]c2911d72011-10-03 22:16:36571 int req_id = request->identifier();
572 InitRequestStatesIfNew(req_id);
573 event_order_[req_id] += "OnAuthRequired\n";
574 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) <<
575 event_order_[req_id];
576 next_states_[req_id] = kStageBeforeSendHeaders |
[email protected]78862512013-04-08 20:12:43577 kStageAuthRequired | // For example, proxy auth followed by server auth.
[email protected]ea8141e2011-10-05 13:12:51578 kStageHeadersReceived | // Request canceled by delegate simulates empty
579 // response.
[email protected]c2911d72011-10-03 22:16:36580 kStageResponseStarted | // data: URLs do not trigger sending headers
[email protected]dc5a5cf2012-09-26 02:49:30581 kStageBeforeRedirect | // a delegate can trigger a redirection
582 kStageCompletedError; // request cancelled before callback
[email protected]2086a3d2012-11-13 17:49:20583 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
[email protected]7efc582d2011-08-03 20:46:35584}
[email protected]1700c6a2012-02-22 18:07:07585
[email protected]2086a3d2012-11-13 17:49:20586bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request,
587 const CookieList& cookie_list) {
[email protected]9c8ae8c2012-03-09 13:13:35588 bool allow = true;
589 if (cookie_options_bit_mask_ & NO_GET_COOKIES)
590 allow = false;
591
592 if (!allow) {
593 blocked_get_cookies_count_++;
594 }
595
596 return allow;
597}
598
[email protected]2086a3d2012-11-13 17:49:20599bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
[email protected]4c219e22012-05-05 19:41:04600 const std::string& cookie_line,
[email protected]2086a3d2012-11-13 17:49:20601 CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35602 bool allow = true;
603 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
604 allow = false;
605
[email protected]9c8ae8c2012-03-09 13:13:35606 if (!allow) {
607 blocked_set_cookie_count_++;
608 } else {
609 set_cookie_count_++;
610 }
611
612 return allow;
613}
614
[email protected]2086a3d2012-11-13 17:49:20615bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request,
[email protected]6cdfd7f2013-02-08 20:40:15616 const base::FilePath& path) const {
[email protected]3ca8b362013-11-11 22:18:07617 return can_access_files_;
[email protected]4c219e22012-05-05 19:41:04618}
619
estark7625d812015-10-12 20:10:41620bool TestNetworkDelegate::OnAreExperimentalCookieFeaturesEnabled() const {
621 return experimental_cookie_features_enabled_;
mkwst0513c9d2015-04-01 05:53:15622}
623
jochen0e3b3a62014-09-16 18:31:23624bool TestNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
625 const URLRequest& request,
626 const GURL& target_url,
627 const GURL& referrer_url) const {
628 return cancel_request_with_policy_violating_referrer_;
629}
630
[email protected]46ae16ba2012-09-18 14:02:03631TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) {
632}
633
[email protected]f53b4802012-12-20 17:04:23634URLRequestJob* TestJobInterceptor::MaybeCreateJob(
635 URLRequest* request,
636 NetworkDelegate* network_delegate) const {
[email protected]2086a3d2012-11-13 17:49:20637 URLRequestJob* job = main_intercept_job_;
[email protected]46ae16ba2012-09-18 14:02:03638 main_intercept_job_ = NULL;
639 return job;
640}
641
[email protected]2086a3d2012-11-13 17:49:20642void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) {
[email protected]46ae16ba2012-09-18 14:02:03643 main_intercept_job_ = job;
644}
[email protected]2086a3d2012-11-13 17:49:20645
646} // namespace net