blob: 59d00f0fb93e1b45c36e1e49917285e7f5635c75 [file] [log] [blame]
[email protected]d2db0292011-01-26 20:23:441// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/url_request/url_request_test_util.h"
6
[email protected]529623e2011-02-23 17:37:167#include "base/compiler_specific.h"
[email protected]d2db0292011-01-26 20:23:448#include "base/logging.h"
9#include "base/message_loop.h"
10#include "base/threading/thread.h"
[email protected]82b42302011-04-20 16:28:1611#include "net/base/host_port_pair.h"
[email protected]57cb0f72011-01-28 06:33:5812#include "net/http/http_network_session.h"
[email protected]a8c1e7452011-05-14 06:17:0713#include "net/url_request/url_request_job_factory.h"
[email protected]d2db0292011-01-26 20:23:4414
[email protected]87a09a92011-07-14 15:50:5015namespace {
16
17// These constants put the net::NetworkDelegate events of TestNetworkDelegate
18// into an order. They are used in conjunction with
19// |TestNetworkDelegate::next_states_| to check that we do not send
20// events in the wrong order.
21const int kStageBeforeURLRequest = 1 << 0;
22const int kStageBeforeSendHeaders = 1 << 1;
[email protected]5796dc942011-07-14 19:26:1023const int kStageSendHeaders = 1 << 2;
[email protected]87a09a92011-07-14 15:50:5024const int kStageBeforeRedirect = 1 << 3;
25const int kStageResponseStarted = 1 << 4;
26const int kStageCompletedSuccess = 1 << 5;
27const int kStageCompletedError = 1 << 6;
28const int kStageURLRequestDestroyed = 1 << 7;
29const int kStageDestruction = 1 << 8;
30
31} // namespace
32
[email protected]529623e2011-02-23 17:37:1633TestURLRequestContext::TestURLRequestContext()
[email protected]87a09a92011-07-14 15:50:5034 : initialized_(false),
35 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) {
[email protected]529623e2011-02-23 17:37:1636 context_storage_.set_host_resolver(
[email protected]d2db0292011-01-26 20:23:4437 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism,
[email protected]06ef6d92011-05-19 04:24:5838 net::HostResolver::kDefaultRetryAttempts,
[email protected]6e7fd162011-04-27 18:08:2039 NULL));
[email protected]87a09a92011-07-14 15:50:5040 SetProxyDirect();
[email protected]d2db0292011-01-26 20:23:4441 Init();
42}
43
[email protected]87a09a92011-07-14 15:50:5044TestURLRequestContext::TestURLRequestContext(bool delay_initialization)
45 : initialized_(false),
46 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) {
[email protected]529623e2011-02-23 17:37:1647 context_storage_.set_host_resolver(
[email protected]d2db0292011-01-26 20:23:4448 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism,
[email protected]06ef6d92011-05-19 04:24:5849 net::HostResolver::kDefaultRetryAttempts,
[email protected]6e7fd162011-04-27 18:08:2050 NULL));
[email protected]87a09a92011-07-14 15:50:5051 SetProxyDirect();
52 if (!delay_initialization)
53 Init();
54}
55
56TestURLRequestContext::TestURLRequestContext(const std::string& proxy)
57 : initialized_(false),
58 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) {
59 context_storage_.set_host_resolver(
60 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism,
61 net::HostResolver::kDefaultRetryAttempts,
62 NULL));
63 SetProxyFromString(proxy);
64 Init();
65}
66
67TestURLRequestContext::TestURLRequestContext(const char* proxy)
68 : initialized_(false),
69 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) {
70 context_storage_.set_host_resolver(
71 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism,
72 net::HostResolver::kDefaultRetryAttempts,
73 NULL));
74 SetProxyFromString(proxy);
[email protected]d2db0292011-01-26 20:23:4475 Init();
76}
77
[email protected]8202d0c2011-02-23 08:31:1478TestURLRequestContext::TestURLRequestContext(const std::string& proxy,
[email protected]529623e2011-02-23 17:37:1679 net::HostResolver* host_resolver)
[email protected]87a09a92011-07-14 15:50:5080 : initialized_(false),
81 ALLOW_THIS_IN_INITIALIZER_LIST(context_storage_(this)) {
[email protected]529623e2011-02-23 17:37:1682 context_storage_.set_host_resolver(host_resolver);
[email protected]87a09a92011-07-14 15:50:5083 SetProxyFromString(proxy);
84 Init();
85}
86
87void TestURLRequestContext::SetProxyFromString(const std::string& proxy) {
88 DCHECK(!initialized_);
[email protected]8202d0c2011-02-23 08:31:1489 net::ProxyConfig proxy_config;
90 proxy_config.proxy_rules().ParseFromString(proxy);
[email protected]529623e2011-02-23 17:37:1691 context_storage_.set_proxy_service(
92 net::ProxyService::CreateFixed(proxy_config));
[email protected]8202d0c2011-02-23 08:31:1493}
94
[email protected]87a09a92011-07-14 15:50:5095void TestURLRequestContext::SetProxyDirect() {
96 DCHECK(!initialized_);
97 context_storage_.set_proxy_service(net::ProxyService::CreateDirect());
98}
99
100TestURLRequestContext::~TestURLRequestContext() {
101 DCHECK(initialized_);
102}
[email protected]d2db0292011-01-26 20:23:44103
104void TestURLRequestContext::Init() {
[email protected]87a09a92011-07-14 15:50:50105 DCHECK(!initialized_);
106 initialized_ = true;
107 if (!cert_verifier())
108 context_storage_.set_cert_verifier(new net::CertVerifier);
109 if (!ftp_transaction_factory()) {
110 context_storage_.set_ftp_transaction_factory(
111 new net::FtpNetworkLayer(host_resolver()));
112 }
113 if (!ssl_config_service())
114 context_storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults);
115 if (!http_auth_handler_factory()) {
116 context_storage_.set_http_auth_handler_factory(
117 net::HttpAuthHandlerFactory::CreateDefault(host_resolver()));
118 }
[email protected]9e1bdd32011-02-03 21:48:34119 net::HttpNetworkSession::Params params;
[email protected]f6c21cb2011-02-16 19:45:41120 params.host_resolver = host_resolver();
121 params.cert_verifier = cert_verifier();
122 params.proxy_service = proxy_service();
123 params.ssl_config_service = ssl_config_service();
124 params.http_auth_handler_factory = http_auth_handler_factory();
125 params.network_delegate = network_delegate();
[email protected]9e1bdd32011-02-03 21:48:34126
[email protected]87a09a92011-07-14 15:50:50127 if (!http_transaction_factory()) {
128 context_storage_.set_http_transaction_factory(new net::HttpCache(
129 new net::HttpNetworkSession(params),
130 net::HttpCache::DefaultBackend::InMemory(0)));
131 }
[email protected]d2db0292011-01-26 20:23:44132 // In-memory cookie store.
[email protected]87a09a92011-07-14 15:50:50133 if (!cookie_store())
134 context_storage_.set_cookie_store(new net::CookieMonster(NULL, NULL));
135 if (accept_language().empty())
136 set_accept_language("en-us,fr");
137 if (accept_charset().empty())
138 set_accept_charset("iso-8859-1,*,utf-8");
139 if (!job_factory())
140 context_storage_.set_job_factory(new net::URLRequestJobFactory);
[email protected]d2db0292011-01-26 20:23:44141}
142
143
144TestURLRequest::TestURLRequest(const GURL& url, Delegate* delegate)
145 : net::URLRequest(url, delegate) {
146 set_context(new TestURLRequestContext());
147}
148
149TestURLRequest::~TestURLRequest() {}
150
151TestDelegate::TestDelegate()
152 : cancel_in_rr_(false),
153 cancel_in_rs_(false),
154 cancel_in_rd_(false),
155 cancel_in_rd_pending_(false),
156 cancel_in_getcookiesblocked_(false),
157 cancel_in_setcookieblocked_(false),
158 quit_on_complete_(true),
159 quit_on_redirect_(false),
160 allow_certificate_errors_(false),
[email protected]ed24fad2011-05-10 22:44:01161 cookie_options_bit_mask_(0),
[email protected]d2db0292011-01-26 20:23:44162 response_started_count_(0),
163 received_bytes_count_(0),
164 received_redirect_count_(0),
165 blocked_get_cookies_count_(0),
166 blocked_set_cookie_count_(0),
167 set_cookie_count_(0),
168 received_data_before_response_(false),
169 request_failed_(false),
170 have_certificate_errors_(false),
171 buf_(new net::IOBuffer(kBufferSize)) {
172}
173
174TestDelegate::~TestDelegate() {}
175
176void TestDelegate::OnReceivedRedirect(net::URLRequest* request,
177 const GURL& new_url,
178 bool* defer_redirect) {
179 received_redirect_count_++;
180 if (quit_on_redirect_) {
181 *defer_redirect = true;
182 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
183 } else if (cancel_in_rr_) {
184 request->Cancel();
185 }
186}
187
188void TestDelegate::OnAuthRequired(net::URLRequest* request,
189 net::AuthChallengeInfo* auth_info) {
190 if (!username_.empty() || !password_.empty()) {
191 request->SetAuth(username_, password_);
192 } else {
193 request->CancelAuth();
194 }
195}
196
197void TestDelegate::OnSSLCertificateError(net::URLRequest* request,
198 int cert_error,
199 net::X509Certificate* cert) {
200 // The caller can control whether it needs all SSL requests to go through,
201 // independent of any possible errors, or whether it wants SSL errors to
202 // cancel the request.
203 have_certificate_errors_ = true;
204 if (allow_certificate_errors_)
205 request->ContinueDespiteLastError();
206 else
207 request->Cancel();
208}
209
[email protected]ed24fad2011-05-10 22:44:01210bool TestDelegate::CanGetCookies(net::URLRequest* request) {
211 bool allow = true;
212 if (cookie_options_bit_mask_ & NO_GET_COOKIES)
213 allow = false;
214
215 if (!allow) {
[email protected]d2db0292011-01-26 20:23:44216 blocked_get_cookies_count_++;
217 if (cancel_in_getcookiesblocked_)
218 request->Cancel();
219 }
[email protected]ed24fad2011-05-10 22:44:01220
221 return allow;
[email protected]d2db0292011-01-26 20:23:44222}
223
[email protected]ed24fad2011-05-10 22:44:01224bool TestDelegate::CanSetCookie(net::URLRequest* request,
225 const std::string& cookie_line,
226 net::CookieOptions* options) {
227 bool allow = true;
228 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
229 allow = false;
230
231 if (cookie_options_bit_mask_ & FORCE_SESSION)
232 options->set_force_session();
233
234
235 if (!allow) {
[email protected]d2db0292011-01-26 20:23:44236 blocked_set_cookie_count_++;
237 if (cancel_in_setcookieblocked_)
238 request->Cancel();
239 } else {
240 set_cookie_count_++;
241 }
[email protected]ed24fad2011-05-10 22:44:01242
243 return allow;
[email protected]d2db0292011-01-26 20:23:44244}
245
246void TestDelegate::OnResponseStarted(net::URLRequest* request) {
247 // It doesn't make sense for the request to have IO pending at this point.
248 DCHECK(!request->status().is_io_pending());
249
250 response_started_count_++;
251 if (cancel_in_rs_) {
252 request->Cancel();
253 OnResponseCompleted(request);
254 } else if (!request->status().is_success()) {
255 DCHECK(request->status().status() == net::URLRequestStatus::FAILED ||
256 request->status().status() == net::URLRequestStatus::CANCELED);
257 request_failed_ = true;
258 OnResponseCompleted(request);
259 } else {
260 // Initiate the first read.
261 int bytes_read = 0;
262 if (request->Read(buf_, kBufferSize, &bytes_read))
263 OnReadCompleted(request, bytes_read);
264 else if (!request->status().is_io_pending())
265 OnResponseCompleted(request);
266 }
267}
268
269void TestDelegate::OnReadCompleted(net::URLRequest* request, int bytes_read) {
270 // It doesn't make sense for the request to have IO pending at this point.
271 DCHECK(!request->status().is_io_pending());
272
273 if (response_started_count_ == 0)
274 received_data_before_response_ = true;
275
276 if (cancel_in_rd_)
277 request->Cancel();
278
279 if (bytes_read >= 0) {
280 // There is data to read.
281 received_bytes_count_ += bytes_read;
282
283 // consume the data
284 data_received_.append(buf_->data(), bytes_read);
285 }
286
287 // If it was not end of stream, request to read more.
288 if (request->status().is_success() && bytes_read > 0) {
289 bytes_read = 0;
290 while (request->Read(buf_, kBufferSize, &bytes_read)) {
291 if (bytes_read > 0) {
292 data_received_.append(buf_->data(), bytes_read);
293 received_bytes_count_ += bytes_read;
294 } else {
295 break;
296 }
297 }
298 }
299 if (!request->status().is_io_pending())
300 OnResponseCompleted(request);
301 else if (cancel_in_rd_pending_)
302 request->Cancel();
303}
304
305void TestDelegate::OnResponseCompleted(net::URLRequest* request) {
306 if (quit_on_complete_)
307 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
308}
[email protected]8202d0c2011-02-23 08:31:14309
[email protected]0651b812011-02-24 00:22:50310TestNetworkDelegate::TestNetworkDelegate()
[email protected]8202d0c2011-02-23 08:31:14311 : last_os_error_(0),
[email protected]4c76d7c2011-04-15 19:14:12312 error_count_(0),
313 created_requests_(0),
[email protected]a83dd332011-07-13 10:41:01314 destroyed_requests_(0),
315 completed_requests_(0) {
[email protected]8202d0c2011-02-23 08:31:14316}
317
[email protected]87a09a92011-07-14 15:50:50318TestNetworkDelegate::~TestNetworkDelegate() {
319 for (std::map<int, int>::iterator i = next_states_.begin();
320 i != next_states_.end(); ++i) {
321 event_order_[i->first] += "~TestNetworkDelegate\n";
322 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first];
323 }
324}
325
326void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) {
327 if (next_states_.find(request_id) == next_states_.end()) {
328 next_states_[request_id] = kStageBeforeURLRequest;
329 event_order_[request_id] = "";
330 }
331}
332
[email protected]8202d0c2011-02-23 08:31:14333
[email protected]4875ba12011-03-30 22:31:51334int TestNetworkDelegate::OnBeforeURLRequest(
[email protected]4c76d7c2011-04-15 19:14:12335 net::URLRequest* request,
336 net::CompletionCallback* callback,
[email protected]8523ba52011-05-22 19:00:58337 GURL* new_url ) {
[email protected]87a09a92011-07-14 15:50:50338 int req_id = request->identifier();
339 event_order_[req_id] += "OnBeforeURLRequest\n";
340 InitRequestStatesIfNew(req_id);
341 EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) <<
342 event_order_[req_id];
343 next_states_[req_id] =
344 kStageBeforeSendHeaders |
345 kStageResponseStarted | // data: URLs do not trigger sending headers
346 kStageBeforeRedirect | // a delegate can trigger a redirection
347 kStageCompletedError; // request canceled by delegate
[email protected]4c76d7c2011-04-15 19:14:12348 created_requests_++;
[email protected]4875ba12011-03-30 22:31:51349 return net::OK;
[email protected]8202d0c2011-02-23 08:31:14350}
351
[email protected]4875ba12011-03-30 22:31:51352int TestNetworkDelegate::OnBeforeSendHeaders(
[email protected]636eccd2011-06-28 12:28:01353 net::URLRequest* request,
[email protected]4c76d7c2011-04-15 19:14:12354 net::CompletionCallback* callback,
355 net::HttpRequestHeaders* headers) {
[email protected]87a09a92011-07-14 15:50:50356 int req_id = request->identifier();
357 event_order_[req_id] += "OnBeforeSendHeaders\n";
358 InitRequestStatesIfNew(req_id);
359 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) <<
360 event_order_[req_id];
361 next_states_[req_id] =
[email protected]5796dc942011-07-14 19:26:10362 kStageSendHeaders |
[email protected]87a09a92011-07-14 15:50:50363 kStageCompletedError; // request canceled by delegate
364
[email protected]4875ba12011-03-30 22:31:51365 return net::OK;
[email protected]8202d0c2011-02-23 08:31:14366}
367
[email protected]5796dc942011-07-14 19:26:10368void TestNetworkDelegate::OnSendHeaders(
369 net::URLRequest* request,
[email protected]783573b2011-05-13 11:05:15370 const net::HttpRequestHeaders& headers) {
[email protected]5796dc942011-07-14 19:26:10371 int req_id = request->identifier();
372 event_order_[req_id] += "OnSendHeaders\n";
373 InitRequestStatesIfNew(req_id);
374 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
375 event_order_[req_id];
376 next_states_[req_id] =
[email protected]87a09a92011-07-14 15:50:50377 kStageBeforeRedirect |
378 kStageResponseStarted |
379 kStageCompletedError; // e.g. proxy resolution problem
380
381 // Basic authentication sends a second request from the URLRequestHttpJob
382 // layer before the URLRequest reports that a response has started.
[email protected]5796dc942011-07-14 19:26:10383 next_states_[req_id] |= kStageBeforeSendHeaders;
[email protected]82b42302011-04-20 16:28:16384}
385
[email protected]31b2e5f2011-04-20 16:58:32386void TestNetworkDelegate::OnBeforeRedirect(net::URLRequest* request,
387 const GURL& new_location) {
[email protected]87a09a92011-07-14 15:50:50388 int req_id = request->identifier();
389 event_order_[req_id] += "OnBeforeRedirect\n";
390 InitRequestStatesIfNew(req_id);
391 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) <<
392 event_order_[req_id];
393 next_states_[req_id] =
394 kStageBeforeURLRequest | // HTTP redirects trigger this.
395 kStageBeforeSendHeaders | // Redirects from the network delegate do not
396 // trigger onBeforeURLRequest.
397 kStageCompletedError;
398
399 // A redirect can lead to a file or a data URL. In this case, we do not send
400 // headers.
401 next_states_[req_id] |= kStageResponseStarted;
[email protected]31b2e5f2011-04-20 16:58:32402}
403
[email protected]0651b812011-02-24 00:22:50404void TestNetworkDelegate::OnResponseStarted(net::URLRequest* request) {
[email protected]87a09a92011-07-14 15:50:50405 int req_id = request->identifier();
406 event_order_[req_id] += "OnResponseStarted\n";
407 InitRequestStatesIfNew(req_id);
408 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
409 event_order_[req_id];
410 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
[email protected]8202d0c2011-02-23 08:31:14411 if (request->status().status() == net::URLRequestStatus::FAILED) {
412 error_count_++;
413 last_os_error_ = request->status().os_error();
414 }
415}
[email protected]0651b812011-02-24 00:22:50416
[email protected]8523ba52011-05-22 19:00:58417void TestNetworkDelegate::OnRawBytesRead(const net::URLRequest& request,
418 int bytes_read) {
419}
420
[email protected]48944382011-04-23 13:28:16421void TestNetworkDelegate::OnCompleted(net::URLRequest* request) {
[email protected]87a09a92011-07-14 15:50:50422 int req_id = request->identifier();
423 event_order_[req_id] += "OnCompleted\n";
424 InitRequestStatesIfNew(req_id);
425 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
426 // is logically identical to
427 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
428 EXPECT_TRUE(!request->status().is_success() ||
429 (next_states_[req_id] & kStageCompletedSuccess)) <<
430 event_order_[req_id];
431 EXPECT_TRUE(request->status().is_success() ||
432 (next_states_[req_id] & kStageCompletedError)) <<
433 event_order_[req_id];
434 next_states_[req_id] = kStageURLRequestDestroyed;
[email protected]a83dd332011-07-13 10:41:01435 completed_requests_++;
[email protected]8202d0c2011-02-23 08:31:14436 if (request->status().status() == net::URLRequestStatus::FAILED) {
437 error_count_++;
438 last_os_error_ = request->status().os_error();
439 }
440}
[email protected]4b50cb52011-03-10 00:29:37441
[email protected]8523ba52011-05-22 19:00:58442void TestNetworkDelegate::OnURLRequestDestroyed(
443 net::URLRequest* request) {
[email protected]87a09a92011-07-14 15:50:50444 int req_id = request->identifier();
445 event_order_[req_id] += "OnURLRequestDestroyed\n";
446 InitRequestStatesIfNew(req_id);
447 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
448 event_order_[req_id];
449 next_states_[req_id] = kStageDestruction;
[email protected]4c76d7c2011-04-15 19:14:12450 destroyed_requests_++;
[email protected]4875ba12011-03-30 22:31:51451}
452
[email protected]5aa20132011-04-27 23:11:34453void TestNetworkDelegate::OnHttpTransactionDestroyed(uint64 request_id) {
454}
455
[email protected]4b50cb52011-03-10 00:29:37456net::URLRequestJob* TestNetworkDelegate::OnMaybeCreateURLRequestJob(
457 net::URLRequest* request) {
458 return NULL;
459}
[email protected]82a37672011-05-03 12:02:41460
461void TestNetworkDelegate::OnPACScriptError(int line_number,
462 const string16& error) {
463}