OLD | NEW |
| (Empty) |
1 // 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 #ifndef NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | |
6 #define NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | |
7 #pragma once | |
8 | |
9 #include <stdlib.h> | |
10 | |
11 #include <sstream> | |
12 #include <string> | |
13 | |
14 #include "base/logging.h" | |
15 #include "base/message_loop.h" | |
16 #include "base/path_service.h" | |
17 #include "base/process_util.h" | |
18 #include "base/string_util.h" | |
19 #include "base/string16.h" | |
20 #include "base/threading/thread.h" | |
21 #include "base/time.h" | |
22 #include "base/utf_string_conversions.h" | |
23 #include "net/base/cert_verifier.h" | |
24 #include "net/base/cookie_monster.h" | |
25 #include "net/base/cookie_policy.h" | |
26 #include "net/base/host_resolver.h" | |
27 #include "net/base/io_buffer.h" | |
28 #include "net/base/net_errors.h" | |
29 #include "net/base/ssl_config_service_defaults.h" | |
30 #include "net/disk_cache/disk_cache.h" | |
31 #include "net/ftp/ftp_network_layer.h" | |
32 #include "net/http/http_auth_handler_factory.h" | |
33 #include "net/http/http_cache.h" | |
34 #include "net/http/http_network_layer.h" | |
35 #include "net/test/test_server.h" | |
36 #include "net/url_request/url_request.h" | |
37 #include "net/url_request/url_request_context.h" | |
38 #include "net/proxy/proxy_service.h" | |
39 #include "testing/gtest/include/gtest/gtest.h" | |
40 #include "googleurl/src/url_util.h" | |
41 | |
42 using base::TimeDelta; | |
43 | |
44 //----------------------------------------------------------------------------- | |
45 | |
46 class TestCookiePolicy : public net::CookiePolicy { | |
47 public: | |
48 enum Options { | |
49 NO_GET_COOKIES = 1 << 0, | |
50 NO_SET_COOKIE = 1 << 1, | |
51 ASYNC = 1 << 2, | |
52 FORCE_SESSION = 1 << 3, | |
53 }; | |
54 | |
55 explicit TestCookiePolicy(int options_bit_mask) | |
56 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | |
57 options_(options_bit_mask), | |
58 callback_(NULL) { | |
59 } | |
60 | |
61 virtual int CanGetCookies(const GURL& url, const GURL& first_party, | |
62 net::CompletionCallback* callback) { | |
63 if ((options_ & ASYNC) && callback) { | |
64 callback_ = callback; | |
65 MessageLoop::current()->PostTask(FROM_HERE, | |
66 method_factory_.NewRunnableMethod( | |
67 &TestCookiePolicy::DoGetCookiesPolicy, url, first_party)); | |
68 return net::ERR_IO_PENDING; | |
69 } | |
70 | |
71 if (options_ & NO_GET_COOKIES) | |
72 return net::ERR_ACCESS_DENIED; | |
73 | |
74 return net::OK; | |
75 } | |
76 | |
77 virtual int CanSetCookie(const GURL& url, const GURL& first_party, | |
78 const std::string& cookie_line, | |
79 net::CompletionCallback* callback) { | |
80 if ((options_ & ASYNC) && callback) { | |
81 callback_ = callback; | |
82 MessageLoop::current()->PostTask(FROM_HERE, | |
83 method_factory_.NewRunnableMethod( | |
84 &TestCookiePolicy::DoSetCookiePolicy, url, first_party, | |
85 cookie_line)); | |
86 return net::ERR_IO_PENDING; | |
87 } | |
88 | |
89 if (options_ & NO_SET_COOKIE) | |
90 return net::ERR_ACCESS_DENIED; | |
91 | |
92 if (options_ & FORCE_SESSION) | |
93 return net::OK_FOR_SESSION_ONLY; | |
94 | |
95 return net::OK; | |
96 } | |
97 | |
98 private: | |
99 void DoGetCookiesPolicy(const GURL& url, const GURL& first_party) { | |
100 int policy = CanGetCookies(url, first_party, NULL); | |
101 | |
102 DCHECK(callback_); | |
103 net::CompletionCallback* callback = callback_; | |
104 callback_ = NULL; | |
105 callback->Run(policy); | |
106 } | |
107 | |
108 void DoSetCookiePolicy(const GURL& url, const GURL& first_party, | |
109 const std::string& cookie_line) { | |
110 int policy = CanSetCookie(url, first_party, cookie_line, NULL); | |
111 | |
112 DCHECK(callback_); | |
113 net::CompletionCallback* callback = callback_; | |
114 callback_ = NULL; | |
115 callback->Run(policy); | |
116 } | |
117 | |
118 ScopedRunnableMethodFactory<TestCookiePolicy> method_factory_; | |
119 int options_; | |
120 net::CompletionCallback* callback_; | |
121 }; | |
122 | |
123 //----------------------------------------------------------------------------- | |
124 | |
125 class TestURLRequestContext : public net::URLRequestContext { | |
126 public: | |
127 TestURLRequestContext() { | |
128 host_resolver_ = | |
129 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
130 NULL, NULL); | |
131 proxy_service_ = net::ProxyService::CreateDirect(); | |
132 Init(); | |
133 } | |
134 | |
135 explicit TestURLRequestContext(const std::string& proxy) { | |
136 host_resolver_ = | |
137 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | |
138 NULL, NULL); | |
139 net::ProxyConfig proxy_config; | |
140 proxy_config.proxy_rules().ParseFromString(proxy); | |
141 proxy_service_ = net::ProxyService::CreateFixed(proxy_config); | |
142 Init(); | |
143 } | |
144 | |
145 void set_cookie_policy(net::CookiePolicy* policy) { | |
146 cookie_policy_ = policy; | |
147 } | |
148 | |
149 protected: | |
150 virtual ~TestURLRequestContext() { | |
151 delete ftp_transaction_factory_; | |
152 delete http_transaction_factory_; | |
153 delete http_auth_handler_factory_; | |
154 delete cert_verifier_; | |
155 delete host_resolver_; | |
156 } | |
157 | |
158 private: | |
159 void Init() { | |
160 cert_verifier_ = new net::CertVerifier; | |
161 ftp_transaction_factory_ = new net::FtpNetworkLayer(host_resolver_); | |
162 ssl_config_service_ = new net::SSLConfigServiceDefaults; | |
163 http_auth_handler_factory_ = net::HttpAuthHandlerFactory::CreateDefault( | |
164 host_resolver_); | |
165 http_transaction_factory_ = new net::HttpCache( | |
166 net::HttpNetworkLayer::CreateFactory(host_resolver_, | |
167 cert_verifier_, | |
168 NULL /* dnsrr_resolver */, | |
169 NULL /* dns_cert_checker */, | |
170 NULL /* ssl_host_info_factory */, | |
171 proxy_service_, | |
172 ssl_config_service_, | |
173 http_auth_handler_factory_, | |
174 network_delegate_, | |
175 NULL), | |
176 NULL /* net_log */, | |
177 net::HttpCache::DefaultBackend::InMemory(0)); | |
178 // In-memory cookie store. | |
179 cookie_store_ = new net::CookieMonster(NULL, NULL); | |
180 accept_language_ = "en-us,fr"; | |
181 accept_charset_ = "iso-8859-1,*,utf-8"; | |
182 } | |
183 }; | |
184 | |
185 //----------------------------------------------------------------------------- | |
186 | |
187 class TestURLRequest : public net::URLRequest { | |
188 public: | |
189 TestURLRequest(const GURL& url, Delegate* delegate) | |
190 : net::URLRequest(url, delegate) { | |
191 set_context(new TestURLRequestContext()); | |
192 } | |
193 }; | |
194 | |
195 //----------------------------------------------------------------------------- | |
196 | |
197 class TestDelegate : public net::URLRequest::Delegate { | |
198 public: | |
199 TestDelegate() | |
200 : cancel_in_rr_(false), | |
201 cancel_in_rs_(false), | |
202 cancel_in_rd_(false), | |
203 cancel_in_rd_pending_(false), | |
204 cancel_in_getcookiesblocked_(false), | |
205 cancel_in_setcookieblocked_(false), | |
206 quit_on_complete_(true), | |
207 quit_on_redirect_(false), | |
208 allow_certificate_errors_(false), | |
209 response_started_count_(0), | |
210 received_bytes_count_(0), | |
211 received_redirect_count_(0), | |
212 blocked_get_cookies_count_(0), | |
213 blocked_set_cookie_count_(0), | |
214 set_cookie_count_(0), | |
215 received_data_before_response_(false), | |
216 request_failed_(false), | |
217 have_certificate_errors_(false), | |
218 buf_(new net::IOBuffer(kBufferSize)) { | |
219 } | |
220 | |
221 virtual void OnReceivedRedirect(net::URLRequest* request, const GURL& new_url, | |
222 bool* defer_redirect) { | |
223 received_redirect_count_++; | |
224 if (quit_on_redirect_) { | |
225 *defer_redirect = true; | |
226 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
227 } else if (cancel_in_rr_) { | |
228 request->Cancel(); | |
229 } | |
230 } | |
231 | |
232 virtual void OnResponseStarted(net::URLRequest* request) { | |
233 // It doesn't make sense for the request to have IO pending at this point. | |
234 DCHECK(!request->status().is_io_pending()); | |
235 | |
236 response_started_count_++; | |
237 if (cancel_in_rs_) { | |
238 request->Cancel(); | |
239 OnResponseCompleted(request); | |
240 } else if (!request->status().is_success()) { | |
241 DCHECK(request->status().status() == net::URLRequestStatus::FAILED || | |
242 request->status().status() == net::URLRequestStatus::CANCELED); | |
243 request_failed_ = true; | |
244 OnResponseCompleted(request); | |
245 } else { | |
246 // Initiate the first read. | |
247 int bytes_read = 0; | |
248 if (request->Read(buf_, kBufferSize, &bytes_read)) | |
249 OnReadCompleted(request, bytes_read); | |
250 else if (!request->status().is_io_pending()) | |
251 OnResponseCompleted(request); | |
252 } | |
253 } | |
254 | |
255 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) { | |
256 // It doesn't make sense for the request to have IO pending at this point. | |
257 DCHECK(!request->status().is_io_pending()); | |
258 | |
259 if (response_started_count_ == 0) | |
260 received_data_before_response_ = true; | |
261 | |
262 if (cancel_in_rd_) | |
263 request->Cancel(); | |
264 | |
265 if (bytes_read >= 0) { | |
266 // There is data to read. | |
267 received_bytes_count_ += bytes_read; | |
268 | |
269 // consume the data | |
270 data_received_.append(buf_->data(), bytes_read); | |
271 } | |
272 | |
273 // If it was not end of stream, request to read more. | |
274 if (request->status().is_success() && bytes_read > 0) { | |
275 bytes_read = 0; | |
276 while (request->Read(buf_, kBufferSize, &bytes_read)) { | |
277 if (bytes_read > 0) { | |
278 data_received_.append(buf_->data(), bytes_read); | |
279 received_bytes_count_ += bytes_read; | |
280 } else { | |
281 break; | |
282 } | |
283 } | |
284 } | |
285 if (!request->status().is_io_pending()) | |
286 OnResponseCompleted(request); | |
287 else if (cancel_in_rd_pending_) | |
288 request->Cancel(); | |
289 } | |
290 | |
291 virtual void OnResponseCompleted(net::URLRequest* request) { | |
292 if (quit_on_complete_) | |
293 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
294 } | |
295 | |
296 void OnAuthRequired(net::URLRequest* request, | |
297 net::AuthChallengeInfo* auth_info) { | |
298 if (!username_.empty() || !password_.empty()) { | |
299 request->SetAuth(username_, password_); | |
300 } else { | |
301 request->CancelAuth(); | |
302 } | |
303 } | |
304 | |
305 virtual void OnSSLCertificateError(net::URLRequest* request, | |
306 int cert_error, | |
307 net::X509Certificate* cert) { | |
308 // The caller can control whether it needs all SSL requests to go through, | |
309 // independent of any possible errors, or whether it wants SSL errors to | |
310 // cancel the request. | |
311 have_certificate_errors_ = true; | |
312 if (allow_certificate_errors_) | |
313 request->ContinueDespiteLastError(); | |
314 else | |
315 request->Cancel(); | |
316 } | |
317 | |
318 virtual void OnGetCookies(net::URLRequest* request, bool blocked_by_policy) { | |
319 if (blocked_by_policy) { | |
320 blocked_get_cookies_count_++; | |
321 if (cancel_in_getcookiesblocked_) | |
322 request->Cancel(); | |
323 } | |
324 } | |
325 | |
326 virtual void OnSetCookie(net::URLRequest* request, | |
327 const std::string& cookie_line, | |
328 const net::CookieOptions& options, | |
329 bool blocked_by_policy) { | |
330 if (blocked_by_policy) { | |
331 blocked_set_cookie_count_++; | |
332 if (cancel_in_setcookieblocked_) | |
333 request->Cancel(); | |
334 } else { | |
335 set_cookie_count_++; | |
336 } | |
337 } | |
338 | |
339 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } | |
340 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } | |
341 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } | |
342 void set_cancel_in_received_data_pending(bool val) { | |
343 cancel_in_rd_pending_ = val; | |
344 } | |
345 void set_cancel_in_get_cookies_blocked(bool val) { | |
346 cancel_in_getcookiesblocked_ = val; | |
347 } | |
348 void set_cancel_in_set_cookie_blocked(bool val) { | |
349 cancel_in_setcookieblocked_ = val; | |
350 } | |
351 void set_quit_on_complete(bool val) { quit_on_complete_ = val; } | |
352 void set_quit_on_redirect(bool val) { quit_on_redirect_ = val; } | |
353 void set_allow_certificate_errors(bool val) { | |
354 allow_certificate_errors_ = val; | |
355 } | |
356 void set_username(const string16& u) { username_ = u; } | |
357 void set_password(const string16& p) { password_ = p; } | |
358 | |
359 // query state | |
360 const std::string& data_received() const { return data_received_; } | |
361 int bytes_received() const { return static_cast<int>(data_received_.size()); } | |
362 int response_started_count() const { return response_started_count_; } | |
363 int received_redirect_count() const { return received_redirect_count_; } | |
364 int blocked_get_cookies_count() const { return blocked_get_cookies_count_; } | |
365 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } | |
366 int set_cookie_count() const { return set_cookie_count_; } | |
367 bool received_data_before_response() const { | |
368 return received_data_before_response_; | |
369 } | |
370 bool request_failed() const { return request_failed_; } | |
371 bool have_certificate_errors() const { return have_certificate_errors_; } | |
372 | |
373 private: | |
374 static const int kBufferSize = 4096; | |
375 // options for controlling behavior | |
376 bool cancel_in_rr_; | |
377 bool cancel_in_rs_; | |
378 bool cancel_in_rd_; | |
379 bool cancel_in_rd_pending_; | |
380 bool cancel_in_getcookiesblocked_; | |
381 bool cancel_in_setcookieblocked_; | |
382 bool quit_on_complete_; | |
383 bool quit_on_redirect_; | |
384 bool allow_certificate_errors_; | |
385 | |
386 string16 username_; | |
387 string16 password_; | |
388 | |
389 // tracks status of callbacks | |
390 int response_started_count_; | |
391 int received_bytes_count_; | |
392 int received_redirect_count_; | |
393 int blocked_get_cookies_count_; | |
394 int blocked_set_cookie_count_; | |
395 int set_cookie_count_; | |
396 bool received_data_before_response_; | |
397 bool request_failed_; | |
398 bool have_certificate_errors_; | |
399 std::string data_received_; | |
400 | |
401 // our read buffer | |
402 scoped_refptr<net::IOBuffer> buf_; | |
403 }; | |
404 | |
405 #endif // NET_URL_REQUEST_URL_REQUEST_UNITTEST_H_ | |
OLD | NEW |