Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

Side by Side Diff: net/url_request/url_request_test_util.cc

Issue 6264013: Clean up net unit testing code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Re-virtualize TestDelegate::OnResponseCompleted Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "net/url_request/url_request_test_util.h"
6
7 #include "base/logging.h"
8 #include "base/message_loop.h"
9 #include "base/threading/thread.h"
10
11 TestCookiePolicy::TestCookiePolicy(int options_bit_mask)
12 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
13 options_(options_bit_mask),
14 callback_(NULL) {
15 }
16
17 TestCookiePolicy::~TestCookiePolicy() {}
18
19 int TestCookiePolicy::CanGetCookies(const GURL& url,
20 const GURL& first_party,
21 net::CompletionCallback* callback) {
22 if ((options_ & ASYNC) && callback) {
23 callback_ = callback;
24 MessageLoop::current()->PostTask(FROM_HERE,
25 method_factory_.NewRunnableMethod(
26 &TestCookiePolicy::DoGetCookiesPolicy, url, first_party));
27 return net::ERR_IO_PENDING;
28 }
29
30 if (options_ & NO_GET_COOKIES)
31 return net::ERR_ACCESS_DENIED;
32
33 return net::OK;
34 }
35
36 int TestCookiePolicy::CanSetCookie(const GURL& url,
37 const GURL& first_party,
38 const std::string& cookie_line,
39 net::CompletionCallback* callback) {
40 if ((options_ & ASYNC) && callback) {
41 callback_ = callback;
42 MessageLoop::current()->PostTask(FROM_HERE,
43 method_factory_.NewRunnableMethod(
44 &TestCookiePolicy::DoSetCookiePolicy, url, first_party,
45 cookie_line));
46 return net::ERR_IO_PENDING;
47 }
48
49 if (options_ & NO_SET_COOKIE)
50 return net::ERR_ACCESS_DENIED;
51
52 if (options_ & FORCE_SESSION)
53 return net::OK_FOR_SESSION_ONLY;
54
55 return net::OK;
56 }
57
58 void TestCookiePolicy::DoGetCookiesPolicy(const GURL& url,
59 const GURL& first_party) {
60 int policy = CanGetCookies(url, first_party, NULL);
61
62 DCHECK(callback_);
63 net::CompletionCallback* callback = callback_;
64 callback_ = NULL;
65 callback->Run(policy);
66 }
67
68 void TestCookiePolicy::DoSetCookiePolicy(const GURL& url,
69 const GURL& first_party,
70 const std::string& cookie_line) {
71 int policy = CanSetCookie(url, first_party, cookie_line, NULL);
72
73 DCHECK(callback_);
74 net::CompletionCallback* callback = callback_;
75 callback_ = NULL;
76 callback->Run(policy);
77 }
78
79
80 TestURLRequestContext::TestURLRequestContext() {
81 host_resolver_ =
82 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism,
83 NULL, NULL);
84 proxy_service_ = net::ProxyService::CreateDirect();
85 Init();
86 }
87
88 TestURLRequestContext::TestURLRequestContext(const std::string& proxy) {
89 host_resolver_ =
90 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism,
91 NULL, NULL);
92 net::ProxyConfig proxy_config;
93 proxy_config.proxy_rules().ParseFromString(proxy);
94 proxy_service_ = net::ProxyService::CreateFixed(proxy_config);
95 Init();
96 }
97
98 TestURLRequestContext::~TestURLRequestContext() {
99 delete ftp_transaction_factory_;
100 delete http_transaction_factory_;
101 delete http_auth_handler_factory_;
102 delete cert_verifier_;
103 delete host_resolver_;
104 }
105
106 void TestURLRequestContext::Init() {
107 cert_verifier_ = new net::CertVerifier;
108 ftp_transaction_factory_ = new net::FtpNetworkLayer(host_resolver_);
109 ssl_config_service_ = new net::SSLConfigServiceDefaults;
110 http_auth_handler_factory_ = net::HttpAuthHandlerFactory::CreateDefault(
111 host_resolver_);
112 http_transaction_factory_ = new net::HttpCache(
113 net::HttpNetworkLayer::CreateFactory(host_resolver_,
114 cert_verifier_,
115 NULL /* dnsrr_resolver */,
116 NULL /* dns_cert_checker */,
117 NULL /* ssl_host_info_factory */,
118 proxy_service_,
119 ssl_config_service_,
120 http_auth_handler_factory_,
121 network_delegate_,
122 NULL),
123 NULL /* net_log */,
124 net::HttpCache::DefaultBackend::InMemory(0));
125 // In-memory cookie store.
126 cookie_store_ = new net::CookieMonster(NULL, NULL);
127 accept_language_ = "en-us,fr";
128 accept_charset_ = "iso-8859-1,*,utf-8";
129 }
130
131
132 TestURLRequest::TestURLRequest(const GURL& url, Delegate* delegate)
133 : net::URLRequest(url, delegate) {
134 set_context(new TestURLRequestContext());
135 }
136
137 TestURLRequest::~TestURLRequest() {}
138
139 TestDelegate::TestDelegate()
140 : cancel_in_rr_(false),
141 cancel_in_rs_(false),
142 cancel_in_rd_(false),
143 cancel_in_rd_pending_(false),
144 cancel_in_getcookiesblocked_(false),
145 cancel_in_setcookieblocked_(false),
146 quit_on_complete_(true),
147 quit_on_redirect_(false),
148 allow_certificate_errors_(false),
149 response_started_count_(0),
150 received_bytes_count_(0),
151 received_redirect_count_(0),
152 blocked_get_cookies_count_(0),
153 blocked_set_cookie_count_(0),
154 set_cookie_count_(0),
155 received_data_before_response_(false),
156 request_failed_(false),
157 have_certificate_errors_(false),
158 buf_(new net::IOBuffer(kBufferSize)) {
159 }
160
161 TestDelegate::~TestDelegate() {}
162
163 void TestDelegate::OnReceivedRedirect(net::URLRequest* request,
164 const GURL& new_url,
165 bool* defer_redirect) {
166 received_redirect_count_++;
167 if (quit_on_redirect_) {
168 *defer_redirect = true;
169 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
170 } else if (cancel_in_rr_) {
171 request->Cancel();
172 }
173 }
174
175 void TestDelegate::OnAuthRequired(net::URLRequest* request,
176 net::AuthChallengeInfo* auth_info) {
177 if (!username_.empty() || !password_.empty()) {
178 request->SetAuth(username_, password_);
179 } else {
180 request->CancelAuth();
181 }
182 }
183
184 void TestDelegate::OnSSLCertificateError(net::URLRequest* request,
185 int cert_error,
186 net::X509Certificate* cert) {
187 // The caller can control whether it needs all SSL requests to go through,
188 // independent of any possible errors, or whether it wants SSL errors to
189 // cancel the request.
190 have_certificate_errors_ = true;
191 if (allow_certificate_errors_)
192 request->ContinueDespiteLastError();
193 else
194 request->Cancel();
195 }
196
197 void TestDelegate::OnGetCookies(net::URLRequest* request,
198 bool blocked_by_policy) {
199 if (blocked_by_policy) {
200 blocked_get_cookies_count_++;
201 if (cancel_in_getcookiesblocked_)
202 request->Cancel();
203 }
204 }
205
206 void TestDelegate::OnSetCookie(net::URLRequest* request,
207 const std::string& cookie_line,
208 const net::CookieOptions& options,
209 bool blocked_by_policy) {
210 if (blocked_by_policy) {
211 blocked_set_cookie_count_++;
212 if (cancel_in_setcookieblocked_)
213 request->Cancel();
214 } else {
215 set_cookie_count_++;
216 }
217 }
218
219 void TestDelegate::OnResponseStarted(net::URLRequest* request) {
220 // It doesn't make sense for the request to have IO pending at this point.
221 DCHECK(!request->status().is_io_pending());
222
223 response_started_count_++;
224 if (cancel_in_rs_) {
225 request->Cancel();
226 OnResponseCompleted(request);
227 } else if (!request->status().is_success()) {
228 DCHECK(request->status().status() == net::URLRequestStatus::FAILED ||
229 request->status().status() == net::URLRequestStatus::CANCELED);
230 request_failed_ = true;
231 OnResponseCompleted(request);
232 } else {
233 // Initiate the first read.
234 int bytes_read = 0;
235 if (request->Read(buf_, kBufferSize, &bytes_read))
236 OnReadCompleted(request, bytes_read);
237 else if (!request->status().is_io_pending())
238 OnResponseCompleted(request);
239 }
240 }
241
242 void TestDelegate::OnReadCompleted(net::URLRequest* request, int bytes_read) {
243 // It doesn't make sense for the request to have IO pending at this point.
244 DCHECK(!request->status().is_io_pending());
245
246 if (response_started_count_ == 0)
247 received_data_before_response_ = true;
248
249 if (cancel_in_rd_)
250 request->Cancel();
251
252 if (bytes_read >= 0) {
253 // There is data to read.
254 received_bytes_count_ += bytes_read;
255
256 // consume the data
257 data_received_.append(buf_->data(), bytes_read);
258 }
259
260 // If it was not end of stream, request to read more.
261 if (request->status().is_success() && bytes_read > 0) {
262 bytes_read = 0;
263 while (request->Read(buf_, kBufferSize, &bytes_read)) {
264 if (bytes_read > 0) {
265 data_received_.append(buf_->data(), bytes_read);
266 received_bytes_count_ += bytes_read;
267 } else {
268 break;
269 }
270 }
271 }
272 if (!request->status().is_io_pending())
273 OnResponseCompleted(request);
274 else if (cancel_in_rd_pending_)
275 request->Cancel();
276 }
277
278 void TestDelegate::OnResponseCompleted(net::URLRequest* request) {
279 if (quit_on_complete_)
280 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask());
281 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698