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

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

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