blob: 8f70ee73b173f0cbc88818b2e3012472260ad5e5 [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"
[email protected]d2db0292011-01-26 20:23:448#include "base/logging.h"
[email protected]7f86564d2013-07-18 00:41:229#include "base/message_loop/message_loop.h"
[email protected]d2db0292011-01-26 20:23:4410#include "base/threading/thread.h"
[email protected]bf470dfc2012-05-15 21:13:4511#include "base/threading/worker_pool.h"
[email protected]82b42302011-04-20 16:28:1612#include "net/base/host_port_pair.h"
[email protected]6e7845ae2013-03-29 21:48:1113#include "net/cert/cert_verifier.h"
[email protected]f2cb3cf2013-03-21 01:40:5314#include "net/dns/mock_host_resolver.h"
[email protected]57cb0f72011-01-28 06:33:5815#include "net/http/http_network_session.h"
[email protected]e50efea2014-03-24 18:41:0016#include "net/http/http_response_headers.h"
[email protected]17291a022011-10-10 07:32:5317#include "net/http/http_server_properties_impl.h"
[email protected]b1c988b2013-06-13 06:48:1118#include "net/http/transport_security_state.h"
[email protected]6b8a3c742014-07-25 00:25:3519#include "net/ssl/channel_id_service.h"
20#include "net/ssl/default_channel_id_store.h"
[email protected]ee4c30d2012-11-07 15:08:4321#include "net/url_request/static_http_user_agent_settings.h"
[email protected]9d5730b2012-08-24 17:42:4922#include "net/url_request/url_request_job_factory_impl.h"
[email protected]0140bd32012-03-21 01:47:1223#include "testing/gtest/include/gtest/gtest.h"
[email protected]d2db0292011-01-26 20:23:4424
[email protected]2086a3d2012-11-13 17:49:2025namespace net {
26
[email protected]87a09a92011-07-14 15:50:5027namespace {
28
[email protected]2086a3d2012-11-13 17:49:2029// These constants put the NetworkDelegate events of TestNetworkDelegate
[email protected]87a09a92011-07-14 15:50:5030// into an order. They are used in conjunction with
31// |TestNetworkDelegate::next_states_| to check that we do not send
32// events in the wrong order.
33const int kStageBeforeURLRequest = 1 << 0;
34const int kStageBeforeSendHeaders = 1 << 1;
[email protected]5796dc942011-07-14 19:26:1035const int kStageSendHeaders = 1 << 2;
[email protected]ea8141e2011-10-05 13:12:5136const int kStageHeadersReceived = 1 << 3;
37const int kStageAuthRequired = 1 << 4;
38const int kStageBeforeRedirect = 1 << 5;
39const int kStageResponseStarted = 1 << 6;
40const int kStageCompletedSuccess = 1 << 7;
41const int kStageCompletedError = 1 << 8;
42const int kStageURLRequestDestroyed = 1 << 9;
43const int kStageDestruction = 1 << 10;
[email protected]87a09a92011-07-14 15:50:5044
45} // namespace
46
[email protected]529623e2011-02-23 17:37:1647TestURLRequestContext::TestURLRequestContext()
[email protected]87a09a92011-07-14 15:50:5048 : initialized_(false),
[email protected]45499252013-01-23 17:12:5649 client_socket_factory_(NULL),
[email protected]aa249b52013-04-30 01:04:3250 context_storage_(this) {
[email protected]d2db0292011-01-26 20:23:4451 Init();
52}
53
[email protected]87a09a92011-07-14 15:50:5054TestURLRequestContext::TestURLRequestContext(bool delay_initialization)
55 : initialized_(false),
[email protected]45499252013-01-23 17:12:5656 client_socket_factory_(NULL),
[email protected]aa249b52013-04-30 01:04:3257 context_storage_(this) {
[email protected]87a09a92011-07-14 15:50:5058 if (!delay_initialization)
59 Init();
60}
61
[email protected]87a09a92011-07-14 15:50:5062TestURLRequestContext::~TestURLRequestContext() {
63 DCHECK(initialized_);
[email protected]424559492014-07-22 00:27:4064 AssertNoURLRequests();
[email protected]87a09a92011-07-14 15:50:5065}
[email protected]d2db0292011-01-26 20:23:4466
67void TestURLRequestContext::Init() {
[email protected]87a09a92011-07-14 15:50:5068 DCHECK(!initialized_);
69 initialized_ = true;
[email protected]d5a4dd62012-05-23 01:41:0470
71 if (!host_resolver())
[email protected]c54a8912012-10-22 22:09:4372 context_storage_.set_host_resolver(
[email protected]2086a3d2012-11-13 17:49:2073 scoped_ptr<HostResolver>(new MockCachingHostResolver()));
[email protected]d5a4dd62012-05-23 01:41:0474 if (!proxy_service())
[email protected]2086a3d2012-11-13 17:49:2075 context_storage_.set_proxy_service(ProxyService::CreateDirect());
[email protected]87a09a92011-07-14 15:50:5076 if (!cert_verifier())
[email protected]2086a3d2012-11-13 17:49:2077 context_storage_.set_cert_verifier(CertVerifier::CreateDefault());
[email protected]b1c988b2013-06-13 06:48:1178 if (!transport_security_state())
79 context_storage_.set_transport_security_state(new TransportSecurityState);
[email protected]87a09a92011-07-14 15:50:5080 if (!ssl_config_service())
[email protected]2086a3d2012-11-13 17:49:2081 context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
[email protected]87a09a92011-07-14 15:50:5082 if (!http_auth_handler_factory()) {
83 context_storage_.set_http_auth_handler_factory(
[email protected]2086a3d2012-11-13 17:49:2084 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
[email protected]87a09a92011-07-14 15:50:5085 }
[email protected]17291a022011-10-10 07:32:5386 if (!http_server_properties()) {
87 context_storage_.set_http_server_properties(
[email protected]30d4c022013-07-18 22:58:1688 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
[email protected]17291a022011-10-10 07:32:5389 }
[email protected]242d8562012-10-30 21:20:4690 if (!transport_security_state()) {
91 context_storage_.set_transport_security_state(
[email protected]2086a3d2012-11-13 17:49:2092 new TransportSecurityState());
[email protected]242d8562012-10-30 21:20:4693 }
[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();
[email protected]2086a3d2012-11-13 17:49:20111 context_storage_.set_http_transaction_factory(new HttpCache(
112 new HttpNetworkSession(params),
113 HttpCache::DefaultBackend::InMemory(0)));
[email protected]87a09a92011-07-14 15:50:50114 }
[email protected]d2db0292011-01-26 20:23:44115 // In-memory cookie store.
[email protected]87a09a92011-07-14 15:50:50116 if (!cookie_store())
[email protected]2086a3d2012-11-13 17:49:20117 context_storage_.set_cookie_store(new CookieMonster(NULL, NULL));
[email protected]6b8a3c742014-07-25 00:25:35118 // In-memory Channel ID service.
119 if (!channel_id_service()) {
120 context_storage_.set_channel_id_service(
121 new ChannelIDService(
122 new DefaultChannelIDStore(NULL),
[email protected]bf470dfc2012-05-15 21:13:45123 base::WorkerPool::GetTaskRunner(true)));
[email protected]eb7974c2012-01-25 17:38:48124 }
[email protected]ee4c30d2012-11-07 15:08:43125 if (!http_user_agent_settings()) {
126 context_storage_.set_http_user_agent_settings(
[email protected]8790210c2013-12-02 05:29:53127 new StaticHttpUserAgentSettings("en-us,fr", std::string()));
[email protected]ee4c30d2012-11-07 15:08:43128 }
[email protected]87a09a92011-07-14 15:50:50129 if (!job_factory())
[email protected]2086a3d2012-11-13 17:49:20130 context_storage_.set_job_factory(new URLRequestJobFactoryImpl);
[email protected]d2db0292011-01-26 20:23:44131}
132
[email protected]44738602012-03-02 22:56:01133TestURLRequestContextGetter::TestURLRequestContextGetter(
[email protected]4969b0122012-06-16 01:58:28134 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
135 : network_task_runner_(network_task_runner) {
[email protected]90499482013-06-01 00:39:50136 DCHECK(network_task_runner_.get());
[email protected]44738602012-03-02 22:56:01137}
138
[email protected]d5a4dd62012-05-23 01:41:04139TestURLRequestContextGetter::TestURLRequestContextGetter(
[email protected]4969b0122012-06-16 01:58:28140 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
[email protected]d5a4dd62012-05-23 01:41:04141 scoped_ptr<TestURLRequestContext> context)
[email protected]4969b0122012-06-16 01:58:28142 : network_task_runner_(network_task_runner), context_(context.Pass()) {
[email protected]90499482013-06-01 00:39:50143 DCHECK(network_task_runner_.get());
[email protected]d5a4dd62012-05-23 01:41:04144}
145
[email protected]44738602012-03-02 22:56:01146TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
147
148TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() {
[email protected]ef2bf422012-05-11 03:27:09149 if (!context_.get())
150 context_.reset(new TestURLRequestContext);
[email protected]44738602012-03-02 22:56:01151 return context_.get();
152}
153
[email protected]4969b0122012-06-16 01:58:28154scoped_refptr<base::SingleThreadTaskRunner>
155TestURLRequestContextGetter::GetNetworkTaskRunner() const {
156 return network_task_runner_;
[email protected]44738602012-03-02 22:56:01157}
158
[email protected]d2db0292011-01-26 20:23:44159TestDelegate::TestDelegate()
160 : cancel_in_rr_(false),
161 cancel_in_rs_(false),
162 cancel_in_rd_(false),
163 cancel_in_rd_pending_(false),
[email protected]d2db0292011-01-26 20:23:44164 quit_on_complete_(true),
165 quit_on_redirect_(false),
[email protected]1826a402014-01-08 15:40:48166 quit_on_before_network_start_(false),
[email protected]d2db0292011-01-26 20:23:44167 allow_certificate_errors_(false),
168 response_started_count_(0),
169 received_bytes_count_(0),
170 received_redirect_count_(0),
[email protected]1826a402014-01-08 15:40:48171 received_before_network_start_count_(0),
[email protected]d2db0292011-01-26 20:23:44172 received_data_before_response_(false),
173 request_failed_(false),
174 have_certificate_errors_(false),
[email protected]46d117e2012-01-18 01:53:14175 certificate_errors_are_fatal_(false),
[email protected]c2911d72011-10-03 22:16:36176 auth_required_(false),
[email protected]79e1fd62013-06-20 06:50:04177 have_full_request_headers_(false),
[email protected]2086a3d2012-11-13 17:49:20178 buf_(new IOBuffer(kBufferSize)) {
[email protected]d2db0292011-01-26 20:23:44179}
180
181TestDelegate::~TestDelegate() {}
182
[email protected]79e1fd62013-06-20 06:50:04183void TestDelegate::ClearFullRequestHeaders() {
184 full_request_headers_.Clear();
185 have_full_request_headers_ = false;
186}
187
[email protected]2086a3d2012-11-13 17:49:20188void TestDelegate::OnReceivedRedirect(URLRequest* request,
[email protected]cba24642014-08-15 20:49:59189 const RedirectInfo& redirect_info,
[email protected]d2db0292011-01-26 20:23:44190 bool* defer_redirect) {
[email protected]847c0fa92012-11-06 16:37:42191 EXPECT_TRUE(request->is_redirecting());
[email protected]79e1fd62013-06-20 06:50:04192
193 have_full_request_headers_ =
194 request->GetFullRequestHeaders(&full_request_headers_);
195
[email protected]d2db0292011-01-26 20:23:44196 received_redirect_count_++;
197 if (quit_on_redirect_) {
198 *defer_redirect = true;
[email protected]2da659e2013-05-23 20:51:34199 base::MessageLoop::current()->PostTask(FROM_HERE,
200 base::MessageLoop::QuitClosure());
[email protected]d2db0292011-01-26 20:23:44201 } else if (cancel_in_rr_) {
202 request->Cancel();
203 }
204}
205
[email protected]1826a402014-01-08 15:40:48206void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) {
207 received_before_network_start_count_++;
208 if (quit_on_before_network_start_) {
209 *defer = true;
210 base::MessageLoop::current()->PostTask(FROM_HERE,
211 base::MessageLoop::QuitClosure());
212 }
213}
214
[email protected]2086a3d2012-11-13 17:49:20215void TestDelegate::OnAuthRequired(URLRequest* request,
216 AuthChallengeInfo* auth_info) {
[email protected]c2911d72011-10-03 22:16:36217 auth_required_ = true;
[email protected]f3cf9802011-10-28 18:44:58218 if (!credentials_.Empty()) {
219 request->SetAuth(credentials_);
[email protected]d2db0292011-01-26 20:23:44220 } else {
221 request->CancelAuth();
222 }
223}
224
[email protected]2086a3d2012-11-13 17:49:20225void TestDelegate::OnSSLCertificateError(URLRequest* request,
226 const SSLInfo& ssl_info,
[email protected]46d117e2012-01-18 01:53:14227 bool fatal) {
[email protected]d2db0292011-01-26 20:23:44228 // The caller can control whether it needs all SSL requests to go through,
229 // independent of any possible errors, or whether it wants SSL errors to
230 // cancel the request.
231 have_certificate_errors_ = true;
[email protected]46d117e2012-01-18 01:53:14232 certificate_errors_are_fatal_ = fatal;
[email protected]d2db0292011-01-26 20:23:44233 if (allow_certificate_errors_)
234 request->ContinueDespiteLastError();
235 else
236 request->Cancel();
237}
238
[email protected]2086a3d2012-11-13 17:49:20239void TestDelegate::OnResponseStarted(URLRequest* request) {
[email protected]d2db0292011-01-26 20:23:44240 // It doesn't make sense for the request to have IO pending at this point.
241 DCHECK(!request->status().is_io_pending());
[email protected]847c0fa92012-11-06 16:37:42242 EXPECT_FALSE(request->is_redirecting());
[email protected]d2db0292011-01-26 20:23:44243
[email protected]79e1fd62013-06-20 06:50:04244 have_full_request_headers_ =
245 request->GetFullRequestHeaders(&full_request_headers_);
246
[email protected]d2db0292011-01-26 20:23:44247 response_started_count_++;
248 if (cancel_in_rs_) {
249 request->Cancel();
250 OnResponseCompleted(request);
251 } else if (!request->status().is_success()) {
[email protected]2086a3d2012-11-13 17:49:20252 DCHECK(request->status().status() == URLRequestStatus::FAILED ||
253 request->status().status() == URLRequestStatus::CANCELED);
[email protected]d2db0292011-01-26 20:23:44254 request_failed_ = true;
255 OnResponseCompleted(request);
256 } else {
257 // Initiate the first read.
258 int bytes_read = 0;
[email protected]90499482013-06-01 00:39:50259 if (request->Read(buf_.get(), kBufferSize, &bytes_read))
[email protected]d2db0292011-01-26 20:23:44260 OnReadCompleted(request, bytes_read);
261 else if (!request->status().is_io_pending())
262 OnResponseCompleted(request);
263 }
264}
265
[email protected]2086a3d2012-11-13 17:49:20266void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) {
[email protected]d2db0292011-01-26 20:23:44267 // It doesn't make sense for the request to have IO pending at this point.
268 DCHECK(!request->status().is_io_pending());
269
270 if (response_started_count_ == 0)
271 received_data_before_response_ = true;
272
273 if (cancel_in_rd_)
274 request->Cancel();
275
276 if (bytes_read >= 0) {
277 // There is data to read.
278 received_bytes_count_ += bytes_read;
279
280 // consume the data
281 data_received_.append(buf_->data(), bytes_read);
282 }
283
284 // If it was not end of stream, request to read more.
285 if (request->status().is_success() && bytes_read > 0) {
286 bytes_read = 0;
[email protected]90499482013-06-01 00:39:50287 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) {
[email protected]d2db0292011-01-26 20:23:44288 if (bytes_read > 0) {
289 data_received_.append(buf_->data(), bytes_read);
290 received_bytes_count_ += bytes_read;
291 } else {
292 break;
293 }
294 }
295 }
296 if (!request->status().is_io_pending())
297 OnResponseCompleted(request);
298 else if (cancel_in_rd_pending_)
299 request->Cancel();
300}
301
[email protected]2086a3d2012-11-13 17:49:20302void TestDelegate::OnResponseCompleted(URLRequest* request) {
[email protected]d2db0292011-01-26 20:23:44303 if (quit_on_complete_)
[email protected]2da659e2013-05-23 20:51:34304 base::MessageLoop::current()->PostTask(FROM_HERE,
305 base::MessageLoop::QuitClosure());
[email protected]d2db0292011-01-26 20:23:44306}
[email protected]8202d0c2011-02-23 08:31:14307
[email protected]0651b812011-02-24 00:22:50308TestNetworkDelegate::TestNetworkDelegate()
[email protected]9c8ae8c2012-03-09 13:13:35309 : last_error_(0),
310 error_count_(0),
311 created_requests_(0),
312 destroyed_requests_(0),
313 completed_requests_(0),
[email protected]146b8b22013-11-20 03:59:18314 canceled_requests_(0),
[email protected]9c8ae8c2012-03-09 13:13:35315 cookie_options_bit_mask_(0),
316 blocked_get_cookies_count_(0),
317 blocked_set_cookie_count_(0),
[email protected]58e32bb2013-01-21 18:23:25318 set_cookie_count_(0),
[email protected]597a1ab2014-06-26 08:12:27319 observed_before_proxy_headers_sent_callbacks_(0),
bengr1bf8e942014-11-07 01:36:50320 before_send_headers_count_(0),
321 headers_received_count_(0),
[email protected]58e32bb2013-01-21 18:23:25322 has_load_timing_info_before_redirect_(false),
[email protected]3ca8b362013-11-11 22:18:07323 has_load_timing_info_before_auth_(false),
324 can_access_files_(true),
jochen0e3b3a62014-09-16 18:31:23325 can_throttle_requests_(true),
bengr1bf8e942014-11-07 01:36:50326 cancel_request_with_policy_violating_referrer_(false),
327 will_be_intercepted_on_next_error_(false) {
[email protected]8202d0c2011-02-23 08:31:14328}
329
[email protected]87a09a92011-07-14 15:50:50330TestNetworkDelegate::~TestNetworkDelegate() {
331 for (std::map<int, int>::iterator i = next_states_.begin();
332 i != next_states_.end(); ++i) {
333 event_order_[i->first] += "~TestNetworkDelegate\n";
334 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first];
335 }
336}
337
[email protected]58e32bb2013-01-21 18:23:25338bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect(
339 LoadTimingInfo* load_timing_info_before_redirect) const {
340 *load_timing_info_before_redirect = load_timing_info_before_redirect_;
341 return has_load_timing_info_before_redirect_;
342}
343
344bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth(
345 LoadTimingInfo* load_timing_info_before_auth) const {
346 *load_timing_info_before_auth = load_timing_info_before_auth_;
347 return has_load_timing_info_before_auth_;
348}
349
[email protected]87a09a92011-07-14 15:50:50350void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) {
351 if (next_states_.find(request_id) == next_states_.end()) {
[email protected]146b8b22013-11-20 03:59:18352 // TODO(davidben): Although the URLRequest documentation does not allow
353 // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's
354 // destructor also calls Cancel. Either officially support this or fix the
355 // ResourceLoader code.
356 next_states_[request_id] = kStageBeforeURLRequest | kStageCompletedError;
[email protected]87a09a92011-07-14 15:50:50357 event_order_[request_id] = "";
358 }
359}
360
[email protected]4875ba12011-03-30 22:31:51361int TestNetworkDelegate::OnBeforeURLRequest(
[email protected]2086a3d2012-11-13 17:49:20362 URLRequest* request,
363 const CompletionCallback& callback,
[email protected]cba24642014-08-15 20:49:59364 GURL* new_url) {
[email protected]87a09a92011-07-14 15:50:50365 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50366 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36367 event_order_[req_id] += "OnBeforeURLRequest\n";
[email protected]87a09a92011-07-14 15:50:50368 EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) <<
369 event_order_[req_id];
370 next_states_[req_id] =
371 kStageBeforeSendHeaders |
372 kStageResponseStarted | // data: URLs do not trigger sending headers
373 kStageBeforeRedirect | // a delegate can trigger a redirection
[email protected]c2911d72011-10-03 22:16:36374 kStageCompletedError | // request canceled by delegate
375 kStageAuthRequired; // Auth can come next for FTP requests
[email protected]4c76d7c2011-04-15 19:14:12376 created_requests_++;
[email protected]2086a3d2012-11-13 17:49:20377 return OK;
[email protected]8202d0c2011-02-23 08:31:14378}
379
[email protected]4875ba12011-03-30 22:31:51380int TestNetworkDelegate::OnBeforeSendHeaders(
[email protected]2086a3d2012-11-13 17:49:20381 URLRequest* request,
382 const CompletionCallback& callback,
383 HttpRequestHeaders* headers) {
[email protected]87a09a92011-07-14 15:50:50384 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50385 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36386 event_order_[req_id] += "OnBeforeSendHeaders\n";
[email protected]87a09a92011-07-14 15:50:50387 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) <<
388 event_order_[req_id];
389 next_states_[req_id] =
[email protected]5796dc942011-07-14 19:26:10390 kStageSendHeaders |
[email protected]87a09a92011-07-14 15:50:50391 kStageCompletedError; // request canceled by delegate
bengr1bf8e942014-11-07 01:36:50392 before_send_headers_count_++;
[email protected]2086a3d2012-11-13 17:49:20393 return OK;
[email protected]8202d0c2011-02-23 08:31:14394}
395
[email protected]597a1ab2014-06-26 08:12:27396void TestNetworkDelegate::OnBeforeSendProxyHeaders(
397 net::URLRequest* request,
398 const net::ProxyInfo& proxy_info,
399 net::HttpRequestHeaders* headers) {
400 ++observed_before_proxy_headers_sent_callbacks_;
401 last_observed_proxy_ = proxy_info.proxy_server().host_port_pair();
402}
403
[email protected]5796dc942011-07-14 19:26:10404void TestNetworkDelegate::OnSendHeaders(
[email protected]2086a3d2012-11-13 17:49:20405 URLRequest* request,
406 const HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10407 int req_id = request->identifier();
[email protected]5796dc942011-07-14 19:26:10408 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36409 event_order_[req_id] += "OnSendHeaders\n";
[email protected]5796dc942011-07-14 19:26:10410 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
411 event_order_[req_id];
bengr1bf8e942014-11-07 01:36:50412 if (!will_be_intercepted_on_next_error_)
413 next_states_[req_id] = kStageHeadersReceived | kStageCompletedError;
414 else
415 next_states_[req_id] = kStageResponseStarted;
416 will_be_intercepted_on_next_error_ = false;
[email protected]ea8141e2011-10-05 13:12:51417}
418
419int TestNetworkDelegate::OnHeadersReceived(
[email protected]2086a3d2012-11-13 17:49:20420 URLRequest* request,
421 const CompletionCallback& callback,
422 const HttpResponseHeaders* original_response_headers,
[email protected]5f714132014-03-26 10:41:16423 scoped_refptr<HttpResponseHeaders>* override_response_headers,
424 GURL* allowed_unsafe_redirect_url) {
[email protected]ea8141e2011-10-05 13:12:51425 int req_id = request->identifier();
426 event_order_[req_id] += "OnHeadersReceived\n";
427 InitRequestStatesIfNew(req_id);
428 EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) <<
429 event_order_[req_id];
430 next_states_[req_id] =
[email protected]87a09a92011-07-14 15:50:50431 kStageBeforeRedirect |
432 kStageResponseStarted |
[email protected]c2911d72011-10-03 22:16:36433 kStageAuthRequired |
[email protected]87a09a92011-07-14 15:50:50434 kStageCompletedError; // e.g. proxy resolution problem
435
436 // Basic authentication sends a second request from the URLRequestHttpJob
437 // layer before the URLRequest reports that a response has started.
[email protected]5796dc942011-07-14 19:26:10438 next_states_[req_id] |= kStageBeforeSendHeaders;
[email protected]ea8141e2011-10-05 13:12:51439
[email protected]e50efea2014-03-24 18:41:00440 if (!redirect_on_headers_received_url_.is_empty()) {
441 *override_response_headers =
442 new net::HttpResponseHeaders(original_response_headers->raw_headers());
443 (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found");
444 (*override_response_headers)->RemoveHeader("Location");
445 (*override_response_headers)->AddHeader(
446 "Location: " + redirect_on_headers_received_url_.spec());
447
448 redirect_on_headers_received_url_ = GURL();
[email protected]5f714132014-03-26 10:41:16449
450 if (!allowed_unsafe_redirect_url_.is_empty())
451 *allowed_unsafe_redirect_url = allowed_unsafe_redirect_url_;
[email protected]e50efea2014-03-24 18:41:00452 }
bengr1bf8e942014-11-07 01:36:50453 headers_received_count_++;
[email protected]2086a3d2012-11-13 17:49:20454 return OK;
[email protected]82b42302011-04-20 16:28:16455}
456
[email protected]2086a3d2012-11-13 17:49:20457void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request,
[email protected]31b2e5f2011-04-20 16:58:32458 const GURL& new_location) {
[email protected]58e32bb2013-01-21 18:23:25459 load_timing_info_before_redirect_ = LoadTimingInfo();
460 request->GetLoadTimingInfo(&load_timing_info_before_redirect_);
461 has_load_timing_info_before_redirect_ = true;
462 EXPECT_FALSE(load_timing_info_before_redirect_.request_start_time.is_null());
463 EXPECT_FALSE(load_timing_info_before_redirect_.request_start.is_null());
464
[email protected]87a09a92011-07-14 15:50:50465 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50466 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36467 event_order_[req_id] += "OnBeforeRedirect\n";
[email protected]87a09a92011-07-14 15:50:50468 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) <<
469 event_order_[req_id];
470 next_states_[req_id] =
471 kStageBeforeURLRequest | // HTTP redirects trigger this.
472 kStageBeforeSendHeaders | // Redirects from the network delegate do not
473 // trigger onBeforeURLRequest.
474 kStageCompletedError;
475
476 // A redirect can lead to a file or a data URL. In this case, we do not send
477 // headers.
478 next_states_[req_id] |= kStageResponseStarted;
[email protected]31b2e5f2011-04-20 16:58:32479}
480
[email protected]2086a3d2012-11-13 17:49:20481void TestNetworkDelegate::OnResponseStarted(URLRequest* request) {
[email protected]0a703912013-02-02 04:25:17482 LoadTimingInfo load_timing_info;
483 request->GetLoadTimingInfo(&load_timing_info);
484 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
485 EXPECT_FALSE(load_timing_info.request_start.is_null());
[email protected]58e32bb2013-01-21 18:23:25486
[email protected]87a09a92011-07-14 15:50:50487 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50488 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36489 event_order_[req_id] += "OnResponseStarted\n";
[email protected]87a09a92011-07-14 15:50:50490 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
491 event_order_[req_id];
492 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
[email protected]2086a3d2012-11-13 17:49:20493 if (request->status().status() == URLRequestStatus::FAILED) {
[email protected]8202d0c2011-02-23 08:31:14494 error_count_++;
[email protected]d0cc35b2011-09-08 12:02:05495 last_error_ = request->status().error();
[email protected]8202d0c2011-02-23 08:31:14496 }
497}
[email protected]0651b812011-02-24 00:22:50498
[email protected]2086a3d2012-11-13 17:49:20499void TestNetworkDelegate::OnRawBytesRead(const URLRequest& request,
[email protected]8523ba52011-05-22 19:00:58500 int bytes_read) {
501}
502
[email protected]2086a3d2012-11-13 17:49:20503void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
[email protected]87a09a92011-07-14 15:50:50504 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50505 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36506 event_order_[req_id] += "OnCompleted\n";
[email protected]87a09a92011-07-14 15:50:50507 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
508 // is logically identical to
509 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
510 EXPECT_TRUE(!request->status().is_success() ||
511 (next_states_[req_id] & kStageCompletedSuccess)) <<
512 event_order_[req_id];
513 EXPECT_TRUE(request->status().is_success() ||
514 (next_states_[req_id] & kStageCompletedError)) <<
515 event_order_[req_id];
516 next_states_[req_id] = kStageURLRequestDestroyed;
[email protected]a83dd332011-07-13 10:41:01517 completed_requests_++;
[email protected]2086a3d2012-11-13 17:49:20518 if (request->status().status() == URLRequestStatus::FAILED) {
[email protected]8202d0c2011-02-23 08:31:14519 error_count_++;
[email protected]d0cc35b2011-09-08 12:02:05520 last_error_ = request->status().error();
[email protected]146b8b22013-11-20 03:59:18521 } else if (request->status().status() == URLRequestStatus::CANCELED) {
522 canceled_requests_++;
523 } else {
524 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status());
[email protected]8202d0c2011-02-23 08:31:14525 }
526}
[email protected]4b50cb52011-03-10 00:29:37527
[email protected]2086a3d2012-11-13 17:49:20528void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) {
[email protected]87a09a92011-07-14 15:50:50529 int req_id = request->identifier();
[email protected]87a09a92011-07-14 15:50:50530 InitRequestStatesIfNew(req_id);
[email protected]c2911d72011-10-03 22:16:36531 event_order_[req_id] += "OnURLRequestDestroyed\n";
[email protected]87a09a92011-07-14 15:50:50532 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
533 event_order_[req_id];
534 next_states_[req_id] = kStageDestruction;
[email protected]4c76d7c2011-04-15 19:14:12535 destroyed_requests_++;
[email protected]4875ba12011-03-30 22:31:51536}
537
[email protected]82a37672011-05-03 12:02:41538void TestNetworkDelegate::OnPACScriptError(int line_number,
[email protected]42cba2fb2013-03-29 19:58:57539 const base::string16& error) {
[email protected]82a37672011-05-03 12:02:41540}
[email protected]7efc582d2011-08-03 20:46:35541
[email protected]2086a3d2012-11-13 17:49:20542NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired(
543 URLRequest* request,
544 const AuthChallengeInfo& auth_info,
[email protected]c2911d72011-10-03 22:16:36545 const AuthCallback& callback,
[email protected]2086a3d2012-11-13 17:49:20546 AuthCredentials* credentials) {
[email protected]58e32bb2013-01-21 18:23:25547 load_timing_info_before_auth_ = LoadTimingInfo();
548 request->GetLoadTimingInfo(&load_timing_info_before_auth_);
549 has_load_timing_info_before_auth_ = true;
550 EXPECT_FALSE(load_timing_info_before_auth_.request_start_time.is_null());
551 EXPECT_FALSE(load_timing_info_before_auth_.request_start.is_null());
552
[email protected]c2911d72011-10-03 22:16:36553 int req_id = request->identifier();
554 InitRequestStatesIfNew(req_id);
555 event_order_[req_id] += "OnAuthRequired\n";
556 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) <<
557 event_order_[req_id];
558 next_states_[req_id] = kStageBeforeSendHeaders |
[email protected]78862512013-04-08 20:12:43559 kStageAuthRequired | // For example, proxy auth followed by server auth.
[email protected]ea8141e2011-10-05 13:12:51560 kStageHeadersReceived | // Request canceled by delegate simulates empty
561 // response.
[email protected]c2911d72011-10-03 22:16:36562 kStageResponseStarted | // data: URLs do not trigger sending headers
[email protected]dc5a5cf2012-09-26 02:49:30563 kStageBeforeRedirect | // a delegate can trigger a redirection
564 kStageCompletedError; // request cancelled before callback
[email protected]2086a3d2012-11-13 17:49:20565 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
[email protected]7efc582d2011-08-03 20:46:35566}
[email protected]1700c6a2012-02-22 18:07:07567
[email protected]2086a3d2012-11-13 17:49:20568bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request,
569 const CookieList& cookie_list) {
[email protected]9c8ae8c2012-03-09 13:13:35570 bool allow = true;
571 if (cookie_options_bit_mask_ & NO_GET_COOKIES)
572 allow = false;
573
574 if (!allow) {
575 blocked_get_cookies_count_++;
576 }
577
578 return allow;
579}
580
[email protected]2086a3d2012-11-13 17:49:20581bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
[email protected]4c219e22012-05-05 19:41:04582 const std::string& cookie_line,
[email protected]2086a3d2012-11-13 17:49:20583 CookieOptions* options) {
[email protected]9c8ae8c2012-03-09 13:13:35584 bool allow = true;
585 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
586 allow = false;
587
[email protected]9c8ae8c2012-03-09 13:13:35588 if (!allow) {
589 blocked_set_cookie_count_++;
590 } else {
591 set_cookie_count_++;
592 }
593
594 return allow;
595}
596
[email protected]2086a3d2012-11-13 17:49:20597bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request,
[email protected]6cdfd7f2013-02-08 20:40:15598 const base::FilePath& path) const {
[email protected]3ca8b362013-11-11 22:18:07599 return can_access_files_;
[email protected]4c219e22012-05-05 19:41:04600}
601
[email protected]a1d4ab072012-06-07 13:21:15602bool TestNetworkDelegate::OnCanThrottleRequest(
[email protected]2086a3d2012-11-13 17:49:20603 const URLRequest& request) const {
[email protected]3ca8b362013-11-11 22:18:07604 return can_throttle_requests_;
[email protected]a1d4ab072012-06-07 13:21:15605}
606
[email protected]aa28181e2012-06-13 00:53:58607int TestNetworkDelegate::OnBeforeSocketStreamConnect(
[email protected]2086a3d2012-11-13 17:49:20608 SocketStream* socket,
609 const CompletionCallback& callback) {
610 return OK;
[email protected]aa28181e2012-06-13 00:53:58611}
612
jochen0e3b3a62014-09-16 18:31:23613bool TestNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
614 const URLRequest& request,
615 const GURL& target_url,
616 const GURL& referrer_url) const {
617 return cancel_request_with_policy_violating_referrer_;
618}
619
[email protected]1700c6a2012-02-22 18:07:07620// static
621std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1");
622
623ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost(
624 const std::string& new_value)
625 : old_value_(value_),
626 new_value_(new_value) {
627 value_ = new_value_;
628}
629
630ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() {
631 DCHECK_EQ(value_, new_value_);
632 value_ = old_value_;
633}
634
635// static
636const std::string& ScopedCustomUrlRequestTestHttpHost::value() {
637 return value_;
638}
[email protected]46ae16ba2012-09-18 14:02:03639
640TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) {
641}
642
[email protected]f53b4802012-12-20 17:04:23643URLRequestJob* TestJobInterceptor::MaybeCreateJob(
644 URLRequest* request,
645 NetworkDelegate* network_delegate) const {
[email protected]2086a3d2012-11-13 17:49:20646 URLRequestJob* job = main_intercept_job_;
[email protected]46ae16ba2012-09-18 14:02:03647 main_intercept_job_ = NULL;
648 return job;
649}
650
[email protected]2086a3d2012-11-13 17:49:20651void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) {
[email protected]46ae16ba2012-09-18 14:02:03652 main_intercept_job_ = job;
653}
[email protected]2086a3d2012-11-13 17:49:20654
655} // namespace net