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

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

Issue 11369179: Move url_request_test_util into net namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: patch for landing Created 8 years, 1 month 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
« no previous file with comments | « net/url_request/url_request_test_util.h ('k') | sync/internal_api/http_bridge_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/url_request/url_request_test_util.h" 5 #include "net/url_request/url_request_test_util.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/threading/thread.h" 10 #include "base/threading/thread.h"
11 #include "base/threading/worker_pool.h" 11 #include "base/threading/worker_pool.h"
12 #include "net/base/cert_verifier.h" 12 #include "net/base/cert_verifier.h"
13 #include "net/base/default_server_bound_cert_store.h" 13 #include "net/base/default_server_bound_cert_store.h"
14 #include "net/base/host_port_pair.h" 14 #include "net/base/host_port_pair.h"
15 #include "net/base/mock_host_resolver.h" 15 #include "net/base/mock_host_resolver.h"
16 #include "net/base/server_bound_cert_service.h" 16 #include "net/base/server_bound_cert_service.h"
17 #include "net/http/http_network_session.h" 17 #include "net/http/http_network_session.h"
18 #include "net/http/http_server_properties_impl.h" 18 #include "net/http/http_server_properties_impl.h"
19 #include "net/url_request/static_http_user_agent_settings.h" 19 #include "net/url_request/static_http_user_agent_settings.h"
20 #include "net/url_request/url_request_job_factory_impl.h" 20 #include "net/url_request/url_request_job_factory_impl.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 namespace net {
24
23 namespace { 25 namespace {
24 26
25 // These constants put the net::NetworkDelegate events of TestNetworkDelegate 27 // These constants put the NetworkDelegate events of TestNetworkDelegate
26 // into an order. They are used in conjunction with 28 // into an order. They are used in conjunction with
27 // |TestNetworkDelegate::next_states_| to check that we do not send 29 // |TestNetworkDelegate::next_states_| to check that we do not send
28 // events in the wrong order. 30 // events in the wrong order.
29 const int kStageBeforeURLRequest = 1 << 0; 31 const int kStageBeforeURLRequest = 1 << 0;
30 const int kStageBeforeSendHeaders = 1 << 1; 32 const int kStageBeforeSendHeaders = 1 << 1;
31 const int kStageSendHeaders = 1 << 2; 33 const int kStageSendHeaders = 1 << 2;
32 const int kStageHeadersReceived = 1 << 3; 34 const int kStageHeadersReceived = 1 << 3;
33 const int kStageAuthRequired = 1 << 4; 35 const int kStageAuthRequired = 1 << 4;
34 const int kStageBeforeRedirect = 1 << 5; 36 const int kStageBeforeRedirect = 1 << 5;
35 const int kStageResponseStarted = 1 << 6; 37 const int kStageResponseStarted = 1 << 6;
(...skipping 20 matching lines...) Expand all
56 TestURLRequestContext::~TestURLRequestContext() { 58 TestURLRequestContext::~TestURLRequestContext() {
57 DCHECK(initialized_); 59 DCHECK(initialized_);
58 } 60 }
59 61
60 void TestURLRequestContext::Init() { 62 void TestURLRequestContext::Init() {
61 DCHECK(!initialized_); 63 DCHECK(!initialized_);
62 initialized_ = true; 64 initialized_ = true;
63 65
64 if (!host_resolver()) 66 if (!host_resolver())
65 context_storage_.set_host_resolver( 67 context_storage_.set_host_resolver(
66 scoped_ptr<net::HostResolver>(new net::MockCachingHostResolver())); 68 scoped_ptr<HostResolver>(new MockCachingHostResolver()));
67 if (!proxy_service()) 69 if (!proxy_service())
68 context_storage_.set_proxy_service(net::ProxyService::CreateDirect()); 70 context_storage_.set_proxy_service(ProxyService::CreateDirect());
69 if (!cert_verifier()) 71 if (!cert_verifier())
70 context_storage_.set_cert_verifier(net::CertVerifier::CreateDefault()); 72 context_storage_.set_cert_verifier(CertVerifier::CreateDefault());
71 if (!ftp_transaction_factory()) { 73 if (!ftp_transaction_factory()) {
72 #if !defined(DISABLE_FTP_SUPPORT) 74 #if !defined(DISABLE_FTP_SUPPORT)
73 context_storage_.set_ftp_transaction_factory( 75 context_storage_.set_ftp_transaction_factory(
74 new net::FtpNetworkLayer(host_resolver())); 76 new FtpNetworkLayer(host_resolver()));
75 #else 77 #else
76 context_storage_.set_ftp_transaction_factory(NULL); 78 context_storage_.set_ftp_transaction_factory(NULL);
77 #endif // !defined(DISABLE_FTP_SUPPORT) 79 #endif // !defined(DISABLE_FTP_SUPPORT)
78 } 80 }
79 if (!ssl_config_service()) 81 if (!ssl_config_service())
80 context_storage_.set_ssl_config_service(new net::SSLConfigServiceDefaults); 82 context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
81 if (!http_auth_handler_factory()) { 83 if (!http_auth_handler_factory()) {
82 context_storage_.set_http_auth_handler_factory( 84 context_storage_.set_http_auth_handler_factory(
83 net::HttpAuthHandlerFactory::CreateDefault(host_resolver())); 85 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
84 } 86 }
85 if (!http_server_properties()) { 87 if (!http_server_properties()) {
86 context_storage_.set_http_server_properties( 88 context_storage_.set_http_server_properties(
87 new net::HttpServerPropertiesImpl); 89 new HttpServerPropertiesImpl);
88 } 90 }
89 if (!transport_security_state()) { 91 if (!transport_security_state()) {
90 context_storage_.set_transport_security_state( 92 context_storage_.set_transport_security_state(
91 new net::TransportSecurityState()); 93 new TransportSecurityState());
92 } 94 }
93 net::HttpNetworkSession::Params params; 95 HttpNetworkSession::Params params;
94 params.host_resolver = host_resolver(); 96 params.host_resolver = host_resolver();
95 params.cert_verifier = cert_verifier(); 97 params.cert_verifier = cert_verifier();
96 params.proxy_service = proxy_service(); 98 params.proxy_service = proxy_service();
97 params.ssl_config_service = ssl_config_service(); 99 params.ssl_config_service = ssl_config_service();
98 params.http_auth_handler_factory = http_auth_handler_factory(); 100 params.http_auth_handler_factory = http_auth_handler_factory();
99 params.network_delegate = network_delegate(); 101 params.network_delegate = network_delegate();
100 params.http_server_properties = http_server_properties(); 102 params.http_server_properties = http_server_properties();
101 103
102 if (!http_transaction_factory()) { 104 if (!http_transaction_factory()) {
103 context_storage_.set_http_transaction_factory(new net::HttpCache( 105 context_storage_.set_http_transaction_factory(new HttpCache(
104 new net::HttpNetworkSession(params), 106 new HttpNetworkSession(params),
105 net::HttpCache::DefaultBackend::InMemory(0))); 107 HttpCache::DefaultBackend::InMemory(0)));
106 } 108 }
107 // In-memory cookie store. 109 // In-memory cookie store.
108 if (!cookie_store()) 110 if (!cookie_store())
109 context_storage_.set_cookie_store(new net::CookieMonster(NULL, NULL)); 111 context_storage_.set_cookie_store(new CookieMonster(NULL, NULL));
110 // In-memory origin bound cert service. 112 // In-memory origin bound cert service.
111 if (!server_bound_cert_service()) { 113 if (!server_bound_cert_service()) {
112 context_storage_.set_server_bound_cert_service( 114 context_storage_.set_server_bound_cert_service(
113 new net::ServerBoundCertService( 115 new ServerBoundCertService(
114 new net::DefaultServerBoundCertStore(NULL), 116 new DefaultServerBoundCertStore(NULL),
115 base::WorkerPool::GetTaskRunner(true))); 117 base::WorkerPool::GetTaskRunner(true)));
116 } 118 }
117 if (!http_user_agent_settings()) { 119 if (!http_user_agent_settings()) {
118 context_storage_.set_http_user_agent_settings( 120 context_storage_.set_http_user_agent_settings(
119 new net::StaticHttpUserAgentSettings( 121 new StaticHttpUserAgentSettings(
120 "en-us,fr", "iso-8859-1,*,utf-8", EmptyString())); 122 "en-us,fr", "iso-8859-1,*,utf-8", EmptyString()));
121 } 123 }
122 if (!job_factory()) 124 if (!job_factory())
123 context_storage_.set_job_factory(new net::URLRequestJobFactoryImpl); 125 context_storage_.set_job_factory(new URLRequestJobFactoryImpl);
124 } 126 }
125 127
126 TestURLRequest::TestURLRequest(const GURL& url, 128 TestURLRequest::TestURLRequest(const GURL& url,
127 Delegate* delegate, 129 Delegate* delegate,
128 TestURLRequestContext* context) 130 TestURLRequestContext* context)
129 : net::URLRequest(url, delegate, context) { 131 : URLRequest(url, delegate, context) {
130 } 132 }
131 133
132 TestURLRequest::~TestURLRequest() { 134 TestURLRequest::~TestURLRequest() {
133 } 135 }
134 136
135 TestURLRequestContextGetter::TestURLRequestContextGetter( 137 TestURLRequestContextGetter::TestURLRequestContextGetter(
136 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner) 138 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
137 : network_task_runner_(network_task_runner) { 139 : network_task_runner_(network_task_runner) {
138 DCHECK(network_task_runner_); 140 DCHECK(network_task_runner_);
139 } 141 }
(...skipping 27 matching lines...) Expand all
167 quit_on_redirect_(false), 169 quit_on_redirect_(false),
168 allow_certificate_errors_(false), 170 allow_certificate_errors_(false),
169 response_started_count_(0), 171 response_started_count_(0),
170 received_bytes_count_(0), 172 received_bytes_count_(0),
171 received_redirect_count_(0), 173 received_redirect_count_(0),
172 received_data_before_response_(false), 174 received_data_before_response_(false),
173 request_failed_(false), 175 request_failed_(false),
174 have_certificate_errors_(false), 176 have_certificate_errors_(false),
175 certificate_errors_are_fatal_(false), 177 certificate_errors_are_fatal_(false),
176 auth_required_(false), 178 auth_required_(false),
177 buf_(new net::IOBuffer(kBufferSize)) { 179 buf_(new IOBuffer(kBufferSize)) {
178 } 180 }
179 181
180 TestDelegate::~TestDelegate() {} 182 TestDelegate::~TestDelegate() {}
181 183
182 void TestDelegate::OnReceivedRedirect(net::URLRequest* request, 184 void TestDelegate::OnReceivedRedirect(URLRequest* request,
183 const GURL& new_url, 185 const GURL& new_url,
184 bool* defer_redirect) { 186 bool* defer_redirect) {
185 EXPECT_TRUE(request->is_redirecting()); 187 EXPECT_TRUE(request->is_redirecting());
186 received_redirect_count_++; 188 received_redirect_count_++;
187 if (quit_on_redirect_) { 189 if (quit_on_redirect_) {
188 *defer_redirect = true; 190 *defer_redirect = true;
189 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 191 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
190 } else if (cancel_in_rr_) { 192 } else if (cancel_in_rr_) {
191 request->Cancel(); 193 request->Cancel();
192 } 194 }
193 } 195 }
194 196
195 void TestDelegate::OnAuthRequired(net::URLRequest* request, 197 void TestDelegate::OnAuthRequired(URLRequest* request,
196 net::AuthChallengeInfo* auth_info) { 198 AuthChallengeInfo* auth_info) {
197 auth_required_ = true; 199 auth_required_ = true;
198 if (!credentials_.Empty()) { 200 if (!credentials_.Empty()) {
199 request->SetAuth(credentials_); 201 request->SetAuth(credentials_);
200 } else { 202 } else {
201 request->CancelAuth(); 203 request->CancelAuth();
202 } 204 }
203 } 205 }
204 206
205 void TestDelegate::OnSSLCertificateError(net::URLRequest* request, 207 void TestDelegate::OnSSLCertificateError(URLRequest* request,
206 const net::SSLInfo& ssl_info, 208 const SSLInfo& ssl_info,
207 bool fatal) { 209 bool fatal) {
208 // The caller can control whether it needs all SSL requests to go through, 210 // The caller can control whether it needs all SSL requests to go through,
209 // independent of any possible errors, or whether it wants SSL errors to 211 // independent of any possible errors, or whether it wants SSL errors to
210 // cancel the request. 212 // cancel the request.
211 have_certificate_errors_ = true; 213 have_certificate_errors_ = true;
212 certificate_errors_are_fatal_ = fatal; 214 certificate_errors_are_fatal_ = fatal;
213 if (allow_certificate_errors_) 215 if (allow_certificate_errors_)
214 request->ContinueDespiteLastError(); 216 request->ContinueDespiteLastError();
215 else 217 else
216 request->Cancel(); 218 request->Cancel();
217 } 219 }
218 220
219 void TestDelegate::OnResponseStarted(net::URLRequest* request) { 221 void TestDelegate::OnResponseStarted(URLRequest* request) {
220 // It doesn't make sense for the request to have IO pending at this point. 222 // It doesn't make sense for the request to have IO pending at this point.
221 DCHECK(!request->status().is_io_pending()); 223 DCHECK(!request->status().is_io_pending());
222 EXPECT_FALSE(request->is_redirecting()); 224 EXPECT_FALSE(request->is_redirecting());
223 225
224 response_started_count_++; 226 response_started_count_++;
225 if (cancel_in_rs_) { 227 if (cancel_in_rs_) {
226 request->Cancel(); 228 request->Cancel();
227 OnResponseCompleted(request); 229 OnResponseCompleted(request);
228 } else if (!request->status().is_success()) { 230 } else if (!request->status().is_success()) {
229 DCHECK(request->status().status() == net::URLRequestStatus::FAILED || 231 DCHECK(request->status().status() == URLRequestStatus::FAILED ||
230 request->status().status() == net::URLRequestStatus::CANCELED); 232 request->status().status() == URLRequestStatus::CANCELED);
231 request_failed_ = true; 233 request_failed_ = true;
232 OnResponseCompleted(request); 234 OnResponseCompleted(request);
233 } else { 235 } else {
234 // Initiate the first read. 236 // Initiate the first read.
235 int bytes_read = 0; 237 int bytes_read = 0;
236 if (request->Read(buf_, kBufferSize, &bytes_read)) 238 if (request->Read(buf_, kBufferSize, &bytes_read))
237 OnReadCompleted(request, bytes_read); 239 OnReadCompleted(request, bytes_read);
238 else if (!request->status().is_io_pending()) 240 else if (!request->status().is_io_pending())
239 OnResponseCompleted(request); 241 OnResponseCompleted(request);
240 } 242 }
241 } 243 }
242 244
243 void TestDelegate::OnReadCompleted(net::URLRequest* request, int bytes_read) { 245 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) {
244 // It doesn't make sense for the request to have IO pending at this point. 246 // It doesn't make sense for the request to have IO pending at this point.
245 DCHECK(!request->status().is_io_pending()); 247 DCHECK(!request->status().is_io_pending());
246 248
247 if (response_started_count_ == 0) 249 if (response_started_count_ == 0)
248 received_data_before_response_ = true; 250 received_data_before_response_ = true;
249 251
250 if (cancel_in_rd_) 252 if (cancel_in_rd_)
251 request->Cancel(); 253 request->Cancel();
252 254
253 if (bytes_read >= 0) { 255 if (bytes_read >= 0) {
(...skipping 15 matching lines...) Expand all
269 break; 271 break;
270 } 272 }
271 } 273 }
272 } 274 }
273 if (!request->status().is_io_pending()) 275 if (!request->status().is_io_pending())
274 OnResponseCompleted(request); 276 OnResponseCompleted(request);
275 else if (cancel_in_rd_pending_) 277 else if (cancel_in_rd_pending_)
276 request->Cancel(); 278 request->Cancel();
277 } 279 }
278 280
279 void TestDelegate::OnResponseCompleted(net::URLRequest* request) { 281 void TestDelegate::OnResponseCompleted(URLRequest* request) {
280 if (quit_on_complete_) 282 if (quit_on_complete_)
281 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 283 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
282 } 284 }
283 285
284 TestNetworkDelegate::TestNetworkDelegate() 286 TestNetworkDelegate::TestNetworkDelegate()
285 : last_error_(0), 287 : last_error_(0),
286 error_count_(0), 288 error_count_(0),
287 created_requests_(0), 289 created_requests_(0),
288 destroyed_requests_(0), 290 destroyed_requests_(0),
289 completed_requests_(0), 291 completed_requests_(0),
(...skipping 12 matching lines...) Expand all
302 } 304 }
303 305
304 void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) { 306 void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) {
305 if (next_states_.find(request_id) == next_states_.end()) { 307 if (next_states_.find(request_id) == next_states_.end()) {
306 next_states_[request_id] = kStageBeforeURLRequest; 308 next_states_[request_id] = kStageBeforeURLRequest;
307 event_order_[request_id] = ""; 309 event_order_[request_id] = "";
308 } 310 }
309 } 311 }
310 312
311 int TestNetworkDelegate::OnBeforeURLRequest( 313 int TestNetworkDelegate::OnBeforeURLRequest(
312 net::URLRequest* request, 314 URLRequest* request,
313 const net::CompletionCallback& callback, 315 const CompletionCallback& callback,
314 GURL* new_url ) { 316 GURL* new_url ) {
315 int req_id = request->identifier(); 317 int req_id = request->identifier();
316 InitRequestStatesIfNew(req_id); 318 InitRequestStatesIfNew(req_id);
317 event_order_[req_id] += "OnBeforeURLRequest\n"; 319 event_order_[req_id] += "OnBeforeURLRequest\n";
318 EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) << 320 EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) <<
319 event_order_[req_id]; 321 event_order_[req_id];
320 next_states_[req_id] = 322 next_states_[req_id] =
321 kStageBeforeSendHeaders | 323 kStageBeforeSendHeaders |
322 kStageResponseStarted | // data: URLs do not trigger sending headers 324 kStageResponseStarted | // data: URLs do not trigger sending headers
323 kStageBeforeRedirect | // a delegate can trigger a redirection 325 kStageBeforeRedirect | // a delegate can trigger a redirection
324 kStageCompletedError | // request canceled by delegate 326 kStageCompletedError | // request canceled by delegate
325 kStageAuthRequired; // Auth can come next for FTP requests 327 kStageAuthRequired; // Auth can come next for FTP requests
326 created_requests_++; 328 created_requests_++;
327 return net::OK; 329 return OK;
328 } 330 }
329 331
330 int TestNetworkDelegate::OnBeforeSendHeaders( 332 int TestNetworkDelegate::OnBeforeSendHeaders(
331 net::URLRequest* request, 333 URLRequest* request,
332 const net::CompletionCallback& callback, 334 const CompletionCallback& callback,
333 net::HttpRequestHeaders* headers) { 335 HttpRequestHeaders* headers) {
334 int req_id = request->identifier(); 336 int req_id = request->identifier();
335 InitRequestStatesIfNew(req_id); 337 InitRequestStatesIfNew(req_id);
336 event_order_[req_id] += "OnBeforeSendHeaders\n"; 338 event_order_[req_id] += "OnBeforeSendHeaders\n";
337 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) << 339 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) <<
338 event_order_[req_id]; 340 event_order_[req_id];
339 next_states_[req_id] = 341 next_states_[req_id] =
340 kStageSendHeaders | 342 kStageSendHeaders |
341 kStageCompletedError; // request canceled by delegate 343 kStageCompletedError; // request canceled by delegate
342 344
343 return net::OK; 345 return OK;
344 } 346 }
345 347
346 void TestNetworkDelegate::OnSendHeaders( 348 void TestNetworkDelegate::OnSendHeaders(
347 net::URLRequest* request, 349 URLRequest* request,
348 const net::HttpRequestHeaders& headers) { 350 const HttpRequestHeaders& headers) {
349 int req_id = request->identifier(); 351 int req_id = request->identifier();
350 InitRequestStatesIfNew(req_id); 352 InitRequestStatesIfNew(req_id);
351 event_order_[req_id] += "OnSendHeaders\n"; 353 event_order_[req_id] += "OnSendHeaders\n";
352 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) << 354 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
353 event_order_[req_id]; 355 event_order_[req_id];
354 next_states_[req_id] = 356 next_states_[req_id] =
355 kStageHeadersReceived | 357 kStageHeadersReceived |
356 kStageCompletedError; 358 kStageCompletedError;
357 } 359 }
358 360
359 int TestNetworkDelegate::OnHeadersReceived( 361 int TestNetworkDelegate::OnHeadersReceived(
360 net::URLRequest* request, 362 URLRequest* request,
361 const net::CompletionCallback& callback, 363 const CompletionCallback& callback,
362 const net::HttpResponseHeaders* original_response_headers, 364 const HttpResponseHeaders* original_response_headers,
363 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { 365 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
364 int req_id = request->identifier(); 366 int req_id = request->identifier();
365 event_order_[req_id] += "OnHeadersReceived\n"; 367 event_order_[req_id] += "OnHeadersReceived\n";
366 InitRequestStatesIfNew(req_id); 368 InitRequestStatesIfNew(req_id);
367 EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) << 369 EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) <<
368 event_order_[req_id]; 370 event_order_[req_id];
369 next_states_[req_id] = 371 next_states_[req_id] =
370 kStageBeforeRedirect | 372 kStageBeforeRedirect |
371 kStageResponseStarted | 373 kStageResponseStarted |
372 kStageAuthRequired | 374 kStageAuthRequired |
373 kStageCompletedError; // e.g. proxy resolution problem 375 kStageCompletedError; // e.g. proxy resolution problem
374 376
375 // Basic authentication sends a second request from the URLRequestHttpJob 377 // Basic authentication sends a second request from the URLRequestHttpJob
376 // layer before the URLRequest reports that a response has started. 378 // layer before the URLRequest reports that a response has started.
377 next_states_[req_id] |= kStageBeforeSendHeaders; 379 next_states_[req_id] |= kStageBeforeSendHeaders;
378 380
379 return net::OK; 381 return OK;
380 } 382 }
381 383
382 void TestNetworkDelegate::OnBeforeRedirect(net::URLRequest* request, 384 void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request,
383 const GURL& new_location) { 385 const GURL& new_location) {
384 int req_id = request->identifier(); 386 int req_id = request->identifier();
385 InitRequestStatesIfNew(req_id); 387 InitRequestStatesIfNew(req_id);
386 event_order_[req_id] += "OnBeforeRedirect\n"; 388 event_order_[req_id] += "OnBeforeRedirect\n";
387 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) << 389 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) <<
388 event_order_[req_id]; 390 event_order_[req_id];
389 next_states_[req_id] = 391 next_states_[req_id] =
390 kStageBeforeURLRequest | // HTTP redirects trigger this. 392 kStageBeforeURLRequest | // HTTP redirects trigger this.
391 kStageBeforeSendHeaders | // Redirects from the network delegate do not 393 kStageBeforeSendHeaders | // Redirects from the network delegate do not
392 // trigger onBeforeURLRequest. 394 // trigger onBeforeURLRequest.
393 kStageCompletedError; 395 kStageCompletedError;
394 396
395 // A redirect can lead to a file or a data URL. In this case, we do not send 397 // A redirect can lead to a file or a data URL. In this case, we do not send
396 // headers. 398 // headers.
397 next_states_[req_id] |= kStageResponseStarted; 399 next_states_[req_id] |= kStageResponseStarted;
398 } 400 }
399 401
400 void TestNetworkDelegate::OnResponseStarted(net::URLRequest* request) { 402 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) {
401 int req_id = request->identifier(); 403 int req_id = request->identifier();
402 InitRequestStatesIfNew(req_id); 404 InitRequestStatesIfNew(req_id);
403 event_order_[req_id] += "OnResponseStarted\n"; 405 event_order_[req_id] += "OnResponseStarted\n";
404 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) << 406 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
405 event_order_[req_id]; 407 event_order_[req_id];
406 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; 408 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
407 if (request->status().status() == net::URLRequestStatus::FAILED) { 409 if (request->status().status() == URLRequestStatus::FAILED) {
408 error_count_++; 410 error_count_++;
409 last_error_ = request->status().error(); 411 last_error_ = request->status().error();
410 } 412 }
411 } 413 }
412 414
413 void TestNetworkDelegate::OnRawBytesRead(const net::URLRequest& request, 415 void TestNetworkDelegate::OnRawBytesRead(const URLRequest& request,
414 int bytes_read) { 416 int bytes_read) {
415 } 417 }
416 418
417 void TestNetworkDelegate::OnCompleted(net::URLRequest* request, bool started) { 419 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
418 int req_id = request->identifier(); 420 int req_id = request->identifier();
419 InitRequestStatesIfNew(req_id); 421 InitRequestStatesIfNew(req_id);
420 event_order_[req_id] += "OnCompleted\n"; 422 event_order_[req_id] += "OnCompleted\n";
421 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" 423 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
422 // is logically identical to 424 // is logically identical to
423 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" 425 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
424 EXPECT_TRUE(!request->status().is_success() || 426 EXPECT_TRUE(!request->status().is_success() ||
425 (next_states_[req_id] & kStageCompletedSuccess)) << 427 (next_states_[req_id] & kStageCompletedSuccess)) <<
426 event_order_[req_id]; 428 event_order_[req_id];
427 EXPECT_TRUE(request->status().is_success() || 429 EXPECT_TRUE(request->status().is_success() ||
428 (next_states_[req_id] & kStageCompletedError)) << 430 (next_states_[req_id] & kStageCompletedError)) <<
429 event_order_[req_id]; 431 event_order_[req_id];
430 next_states_[req_id] = kStageURLRequestDestroyed; 432 next_states_[req_id] = kStageURLRequestDestroyed;
431 completed_requests_++; 433 completed_requests_++;
432 if (request->status().status() == net::URLRequestStatus::FAILED) { 434 if (request->status().status() == URLRequestStatus::FAILED) {
433 error_count_++; 435 error_count_++;
434 last_error_ = request->status().error(); 436 last_error_ = request->status().error();
435 } 437 }
436 } 438 }
437 439
438 void TestNetworkDelegate::OnURLRequestDestroyed( 440 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) {
439 net::URLRequest* request) {
440 int req_id = request->identifier(); 441 int req_id = request->identifier();
441 InitRequestStatesIfNew(req_id); 442 InitRequestStatesIfNew(req_id);
442 event_order_[req_id] += "OnURLRequestDestroyed\n"; 443 event_order_[req_id] += "OnURLRequestDestroyed\n";
443 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << 444 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
444 event_order_[req_id]; 445 event_order_[req_id];
445 next_states_[req_id] = kStageDestruction; 446 next_states_[req_id] = kStageDestruction;
446 destroyed_requests_++; 447 destroyed_requests_++;
447 } 448 }
448 449
449 void TestNetworkDelegate::OnPACScriptError(int line_number, 450 void TestNetworkDelegate::OnPACScriptError(int line_number,
450 const string16& error) { 451 const string16& error) {
451 } 452 }
452 453
453 net::NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired( 454 NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired(
454 net::URLRequest* request, 455 URLRequest* request,
455 const net::AuthChallengeInfo& auth_info, 456 const AuthChallengeInfo& auth_info,
456 const AuthCallback& callback, 457 const AuthCallback& callback,
457 net::AuthCredentials* credentials) { 458 AuthCredentials* credentials) {
458 int req_id = request->identifier(); 459 int req_id = request->identifier();
459 InitRequestStatesIfNew(req_id); 460 InitRequestStatesIfNew(req_id);
460 event_order_[req_id] += "OnAuthRequired\n"; 461 event_order_[req_id] += "OnAuthRequired\n";
461 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) << 462 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) <<
462 event_order_[req_id]; 463 event_order_[req_id];
463 next_states_[req_id] = kStageBeforeSendHeaders | 464 next_states_[req_id] = kStageBeforeSendHeaders |
464 kStageHeadersReceived | // Request canceled by delegate simulates empty 465 kStageHeadersReceived | // Request canceled by delegate simulates empty
465 // response. 466 // response.
466 kStageResponseStarted | // data: URLs do not trigger sending headers 467 kStageResponseStarted | // data: URLs do not trigger sending headers
467 kStageBeforeRedirect | // a delegate can trigger a redirection 468 kStageBeforeRedirect | // a delegate can trigger a redirection
468 kStageCompletedError; // request cancelled before callback 469 kStageCompletedError; // request cancelled before callback
469 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; 470 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
470 } 471 }
471 472
472 bool TestNetworkDelegate::OnCanGetCookies(const net::URLRequest& request, 473 bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request,
473 const net::CookieList& cookie_list) { 474 const CookieList& cookie_list) {
474 bool allow = true; 475 bool allow = true;
475 if (cookie_options_bit_mask_ & NO_GET_COOKIES) 476 if (cookie_options_bit_mask_ & NO_GET_COOKIES)
476 allow = false; 477 allow = false;
477 478
478 if (!allow) { 479 if (!allow) {
479 blocked_get_cookies_count_++; 480 blocked_get_cookies_count_++;
480 } 481 }
481 482
482 return allow; 483 return allow;
483 } 484 }
484 485
485 bool TestNetworkDelegate::OnCanSetCookie(const net::URLRequest& request, 486 bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
486 const std::string& cookie_line, 487 const std::string& cookie_line,
487 net::CookieOptions* options) { 488 CookieOptions* options) {
488 bool allow = true; 489 bool allow = true;
489 if (cookie_options_bit_mask_ & NO_SET_COOKIE) 490 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
490 allow = false; 491 allow = false;
491 492
492 if (!allow) { 493 if (!allow) {
493 blocked_set_cookie_count_++; 494 blocked_set_cookie_count_++;
494 } else { 495 } else {
495 set_cookie_count_++; 496 set_cookie_count_++;
496 } 497 }
497 498
498 return allow; 499 return allow;
499 } 500 }
500 501
501 bool TestNetworkDelegate::OnCanAccessFile(const net::URLRequest& request, 502 bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request,
502 const FilePath& path) const { 503 const FilePath& path) const {
503 return true; 504 return true;
504 } 505 }
505 506
506 bool TestNetworkDelegate::OnCanThrottleRequest( 507 bool TestNetworkDelegate::OnCanThrottleRequest(
507 const net::URLRequest& request) const { 508 const URLRequest& request) const {
508 return true; 509 return true;
509 } 510 }
510 511
511 int TestNetworkDelegate::OnBeforeSocketStreamConnect( 512 int TestNetworkDelegate::OnBeforeSocketStreamConnect(
512 net::SocketStream* socket, 513 SocketStream* socket,
513 const net::CompletionCallback& callback) { 514 const CompletionCallback& callback) {
514 return net::OK; 515 return OK;
515 } 516 }
516 517
517 void TestNetworkDelegate::OnRequestWaitStateChange( 518 void TestNetworkDelegate::OnRequestWaitStateChange(
518 const net::URLRequest& request, 519 const URLRequest& request,
519 RequestWaitState state) { 520 RequestWaitState state) {
520 } 521 }
521 522
522 // static 523 // static
523 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1"); 524 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1");
524 525
525 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost( 526 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost(
526 const std::string& new_value) 527 const std::string& new_value)
527 : old_value_(value_), 528 : old_value_(value_),
528 new_value_(new_value) { 529 new_value_(new_value) {
529 value_ = new_value_; 530 value_ = new_value_;
530 } 531 }
531 532
532 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() { 533 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() {
533 DCHECK_EQ(value_, new_value_); 534 DCHECK_EQ(value_, new_value_);
534 value_ = old_value_; 535 value_ = old_value_;
535 } 536 }
536 537
537 // static 538 // static
538 const std::string& ScopedCustomUrlRequestTestHttpHost::value() { 539 const std::string& ScopedCustomUrlRequestTestHttpHost::value() {
539 return value_; 540 return value_;
540 } 541 }
541 542
542 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) { 543 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) {
543 } 544 }
544 545
545 net::URLRequestJob* TestJobInterceptor::MaybeIntercept( 546 URLRequestJob* TestJobInterceptor::MaybeIntercept(
546 net::URLRequest* request, 547 URLRequest* request,
547 net::NetworkDelegate* network_delegate) const { 548 NetworkDelegate* network_delegate) const {
548 net::URLRequestJob* job = main_intercept_job_; 549 URLRequestJob* job = main_intercept_job_;
549 main_intercept_job_ = NULL; 550 main_intercept_job_ = NULL;
550 return job; 551 return job;
551 } 552 }
552 553
553 net::URLRequestJob* TestJobInterceptor::MaybeInterceptRedirect( 554 URLRequestJob* TestJobInterceptor::MaybeInterceptRedirect(
554 const GURL& location, 555 const GURL& location,
555 net::URLRequest* request, 556 URLRequest* request,
556 net::NetworkDelegate* network_delegate) const { 557 NetworkDelegate* network_delegate) const {
557 return NULL; 558 return NULL;
558 } 559 }
559 560
560 net::URLRequestJob* TestJobInterceptor::MaybeInterceptResponse( 561 URLRequestJob* TestJobInterceptor::MaybeInterceptResponse(
561 net::URLRequest* request, 562 URLRequest* request,
562 net::NetworkDelegate* network_delegate) const { 563 NetworkDelegate* network_delegate) const {
563 return NULL; 564 return NULL;
564 } 565 }
565 566
566 void TestJobInterceptor::set_main_intercept_job(net::URLRequestJob* job) { 567 void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) {
567 main_intercept_job_ = job; 568 main_intercept_job_ = job;
568 } 569 }
570
571 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_test_util.h ('k') | sync/internal_api/http_bridge_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698