OLD | NEW |
---|---|
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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 allow_certificate_errors_(false), | 186 allow_certificate_errors_(false), |
187 response_started_count_(0), | 187 response_started_count_(0), |
188 received_bytes_count_(0), | 188 received_bytes_count_(0), |
189 received_redirect_count_(0), | 189 received_redirect_count_(0), |
190 received_data_before_response_(false), | 190 received_data_before_response_(false), |
191 request_failed_(false), | 191 request_failed_(false), |
192 have_certificate_errors_(false), | 192 have_certificate_errors_(false), |
193 certificate_errors_are_fatal_(false), | 193 certificate_errors_are_fatal_(false), |
194 auth_required_(false), | 194 auth_required_(false), |
195 have_full_request_headers_(false), | 195 have_full_request_headers_(false), |
196 buf_(new IOBuffer(kBufferSize)) { | 196 request_status_(1), |
197 } | 197 buf_(new IOBuffer(kBufferSize)) {} |
198 | 198 |
199 TestDelegate::~TestDelegate() {} | 199 TestDelegate::~TestDelegate() {} |
200 | 200 |
201 void TestDelegate::ClearFullRequestHeaders() { | 201 void TestDelegate::ClearFullRequestHeaders() { |
202 full_request_headers_.Clear(); | 202 full_request_headers_.Clear(); |
203 have_full_request_headers_ = false; | 203 have_full_request_headers_ = false; |
204 } | 204 } |
205 | 205 |
206 void TestDelegate::OnReceivedRedirect(URLRequest* request, | 206 void TestDelegate::OnReceivedRedirect(URLRequest* request, |
207 const RedirectInfo& redirect_info, | 207 const RedirectInfo& redirect_info, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 // cancel the request. | 244 // cancel the request. |
245 have_certificate_errors_ = true; | 245 have_certificate_errors_ = true; |
246 certificate_errors_are_fatal_ = fatal; | 246 certificate_errors_are_fatal_ = fatal; |
247 if (allow_certificate_errors_) | 247 if (allow_certificate_errors_) |
248 request->ContinueDespiteLastError(); | 248 request->ContinueDespiteLastError(); |
249 else | 249 else |
250 request->Cancel(); | 250 request->Cancel(); |
251 } | 251 } |
252 | 252 |
253 void TestDelegate::OnResponseStarted(URLRequest* request) { | 253 void TestDelegate::OnResponseStarted(URLRequest* request) { |
254 // Temporarly used to distinguish old and new code for OnReadCompleted(). | |
255 old_ = true; | |
mmenke
2016/08/26 19:36:48
Rather than having two paths, can we just make the
maksims (do not use this acc)
2016/08/29 12:30:36
Done.
| |
256 | |
254 // It doesn't make sense for the request to have IO pending at this point. | 257 // It doesn't make sense for the request to have IO pending at this point. |
255 DCHECK(!request->status().is_io_pending()); | 258 DCHECK(!request->status().is_io_pending()); |
256 EXPECT_FALSE(request->is_redirecting()); | 259 EXPECT_FALSE(request->is_redirecting()); |
257 | 260 |
258 have_full_request_headers_ = | 261 have_full_request_headers_ = |
259 request->GetFullRequestHeaders(&full_request_headers_); | 262 request->GetFullRequestHeaders(&full_request_headers_); |
260 | 263 |
261 response_started_count_++; | 264 response_started_count_++; |
262 if (cancel_in_rs_) { | 265 if (cancel_in_rs_) { |
263 request->Cancel(); | 266 request->Cancel(); |
264 OnResponseCompleted(request); | 267 OnResponseCompleted(request); |
265 } else if (!request->status().is_success()) { | 268 } else if (!request->status().is_success()) { |
266 DCHECK(request->status().status() == URLRequestStatus::FAILED || | 269 DCHECK(request->status().status() == URLRequestStatus::FAILED || |
267 request->status().status() == URLRequestStatus::CANCELED); | 270 request->status().status() == URLRequestStatus::CANCELED); |
268 request_failed_ = true; | 271 request_failed_ = true; |
269 OnResponseCompleted(request); | 272 OnResponseCompleted(request); |
270 } else { | 273 } else { |
271 // Initiate the first read. | 274 // Initiate the first read. |
272 int bytes_read = 0; | 275 int bytes_read = 0; |
273 if (request->Read(buf_.get(), kBufferSize, &bytes_read)) | 276 if (request->Read(buf_.get(), kBufferSize, &bytes_read)) |
274 OnReadCompleted(request, bytes_read); | 277 OnReadCompleted(request, bytes_read); |
275 else if (!request->status().is_io_pending()) | 278 else if (!request->status().is_io_pending()) |
276 OnResponseCompleted(request); | 279 OnResponseCompleted(request); |
277 } | 280 } |
278 } | 281 } |
279 | 282 |
283 void TestDelegate::OnResponseStarted(URLRequest* request, int net_error) { | |
284 // Temporarly used to distinguish old and new code for OnReadCompleted(). | |
285 old_ = false; | |
286 | |
287 // It doesn't make sense for the request to have IO pending at this point. | |
288 DCHECK_NE(ERR_IO_PENDING, net_error); | |
289 EXPECT_FALSE(request->is_redirecting()); | |
290 | |
291 have_full_request_headers_ = | |
292 request->GetFullRequestHeaders(&full_request_headers_); | |
293 | |
294 response_started_count_++; | |
295 request_status_ = net_error; | |
296 if (cancel_in_rs_) { | |
297 request_status_ = request->Cancel(); | |
298 OnResponseCompleted(request); | |
299 } else if (net_error != OK) { | |
300 request_failed_ = true; | |
301 OnResponseCompleted(request); | |
302 } else { | |
303 // Initiate the first read. | |
304 int bytes_read = request->Read(buf_.get(), kBufferSize); | |
305 if (bytes_read >= 0) | |
306 OnReadCompleted(request, bytes_read); | |
307 else if (bytes_read != ERR_IO_PENDING) | |
308 OnResponseCompleted(request); | |
309 } | |
310 } | |
311 | |
280 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) { | 312 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) { |
281 // It doesn't make sense for the request to have IO pending at this point. | 313 if (old_) { |
282 DCHECK(!request->status().is_io_pending()); | 314 // It doesn't make sense for the request to have IO pending at this point. |
315 DCHECK(!request->status().is_io_pending()); | |
283 | 316 |
284 // If the request was cancelled in a redirect, it should not signal | 317 // If the request was cancelled in a redirect, it should not signal |
285 // OnReadCompleted. Note that |cancel_in_rs_| may be true due to | 318 // OnReadCompleted. Note that |cancel_in_rs_| may be true due to |
286 // https://crbug.com/564848. | 319 // https://crbug.com/564848. |
287 EXPECT_FALSE(cancel_in_rr_); | 320 EXPECT_FALSE(cancel_in_rr_); |
288 | 321 |
289 if (response_started_count_ == 0) | 322 if (response_started_count_ == 0) |
290 received_data_before_response_ = true; | 323 received_data_before_response_ = true; |
291 | 324 |
292 if (cancel_in_rd_) | 325 if (cancel_in_rd_) |
293 request->Cancel(); | 326 request->Cancel(); |
294 | 327 |
295 if (bytes_read >= 0) { | 328 if (bytes_read >= 0) { |
296 // There is data to read. | 329 // There is data to read. |
297 received_bytes_count_ += bytes_read; | 330 received_bytes_count_ += bytes_read; |
298 | 331 |
299 // consume the data | 332 // consume the data |
300 data_received_.append(buf_->data(), bytes_read); | 333 data_received_.append(buf_->data(), bytes_read); |
301 } | 334 } |
302 | 335 |
303 // If it was not end of stream, request to read more. | 336 // If it was not end of stream, request to read more. |
304 if (request->status().is_success() && bytes_read > 0) { | 337 if (request->status().is_success() && bytes_read > 0) { |
305 bytes_read = 0; | 338 bytes_read = 0; |
306 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) { | 339 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) { |
340 if (bytes_read > 0) { | |
341 data_received_.append(buf_->data(), bytes_read); | |
342 received_bytes_count_ += bytes_read; | |
343 } else { | |
344 break; | |
345 } | |
346 } | |
347 } | |
348 if (!request->status().is_io_pending()) | |
349 OnResponseCompleted(request); | |
350 else if (cancel_in_rd_pending_) | |
351 request->Cancel(); | |
352 } else { | |
353 // It doesn't make sense for the request to have IO pending at this point. | |
354 DCHECK_NE(bytes_read, ERR_IO_PENDING); | |
355 | |
356 // If the request was cancelled in a redirect, it should not signal | |
357 // OnReadCompleted. Note that |cancel_in_rs_| may be true due to | |
358 // https://crbug.com/564848. | |
359 EXPECT_FALSE(cancel_in_rr_); | |
360 | |
361 if (response_started_count_ == 0) | |
362 received_data_before_response_ = true; | |
363 | |
364 if (cancel_in_rd_) | |
365 request_status_ = request->Cancel(); | |
366 | |
367 if (bytes_read >= 0) { | |
368 // There is data to read. | |
369 received_bytes_count_ += bytes_read; | |
370 | |
371 // consume the data | |
372 data_received_.append(buf_->data(), bytes_read); | |
373 } | |
374 | |
375 // If it was not end of stream, request to read more. | |
376 while (bytes_read > 0) { | |
377 bytes_read = request->Read(buf_.get(), kBufferSize); | |
307 if (bytes_read > 0) { | 378 if (bytes_read > 0) { |
308 data_received_.append(buf_->data(), bytes_read); | 379 data_received_.append(buf_->data(), bytes_read); |
309 received_bytes_count_ += bytes_read; | 380 received_bytes_count_ += bytes_read; |
310 } else { | |
311 break; | |
312 } | 381 } |
313 } | 382 } |
383 | |
384 request_status_ = bytes_read; | |
385 if (request_status_ != ERR_IO_PENDING) | |
386 OnResponseCompleted(request); | |
387 else if (cancel_in_rd_pending_) | |
388 request_status_ = request->Cancel(); | |
314 } | 389 } |
315 if (!request->status().is_io_pending()) | |
316 OnResponseCompleted(request); | |
317 else if (cancel_in_rd_pending_) | |
318 request->Cancel(); | |
319 } | 390 } |
320 | 391 |
321 void TestDelegate::OnResponseCompleted(URLRequest* request) { | 392 void TestDelegate::OnResponseCompleted(URLRequest* request) { |
322 if (quit_on_complete_) | 393 if (quit_on_complete_) |
323 base::ThreadTaskRunnerHandle::Get()->PostTask( | 394 base::ThreadTaskRunnerHandle::Get()->PostTask( |
324 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 395 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
325 } | 396 } |
326 | 397 |
327 TestNetworkDelegate::TestNetworkDelegate() | 398 TestNetworkDelegate::TestNetworkDelegate() |
328 : last_error_(0), | 399 : last_error_(0), |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 kStageBeforeStartTransaction | // Redirects from the network delegate do | 569 kStageBeforeStartTransaction | // Redirects from the network delegate do |
499 // not | 570 // not |
500 // trigger onBeforeURLRequest. | 571 // trigger onBeforeURLRequest. |
501 kStageCompletedError; | 572 kStageCompletedError; |
502 | 573 |
503 // A redirect can lead to a file or a data URL. In this case, we do not send | 574 // A redirect can lead to a file or a data URL. In this case, we do not send |
504 // headers. | 575 // headers. |
505 next_states_[req_id] |= kStageResponseStarted; | 576 next_states_[req_id] |= kStageResponseStarted; |
506 } | 577 } |
507 | 578 |
579 // Deprecated. | |
508 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) { | 580 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) { |
509 LoadTimingInfo load_timing_info; | 581 LoadTimingInfo load_timing_info; |
510 request->GetLoadTimingInfo(&load_timing_info); | 582 request->GetLoadTimingInfo(&load_timing_info); |
511 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); | 583 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
512 EXPECT_FALSE(load_timing_info.request_start.is_null()); | 584 EXPECT_FALSE(load_timing_info.request_start.is_null()); |
513 | 585 |
514 int req_id = request->identifier(); | 586 int req_id = request->identifier(); |
515 InitRequestStatesIfNew(req_id); | 587 InitRequestStatesIfNew(req_id); |
516 event_order_[req_id] += "OnResponseStarted\n"; | 588 event_order_[req_id] += "OnResponseStarted\n"; |
517 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) << | 589 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) << |
518 event_order_[req_id]; | 590 event_order_[req_id]; |
519 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; | 591 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; |
520 if (request->status().status() == URLRequestStatus::FAILED) { | 592 if (request->status().status() == URLRequestStatus::FAILED) { |
521 error_count_++; | 593 error_count_++; |
522 last_error_ = request->status().error(); | 594 last_error_ = request->status().error(); |
523 } | 595 } |
524 } | 596 } |
525 | 597 |
598 void TestNetworkDelegate::OnResponseStarted(URLRequest* request, | |
599 int net_error) { | |
600 DCHECK_NE(ERR_IO_PENDING, net_error); | |
601 | |
602 LoadTimingInfo load_timing_info; | |
603 request->GetLoadTimingInfo(&load_timing_info); | |
604 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); | |
605 EXPECT_FALSE(load_timing_info.request_start.is_null()); | |
606 | |
607 int req_id = request->identifier(); | |
608 InitRequestStatesIfNew(req_id); | |
609 event_order_[req_id] += "OnResponseStarted\n"; | |
610 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) | |
611 << event_order_[req_id]; | |
612 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; | |
613 if (net_error == ERR_ABORTED) | |
614 return; | |
615 | |
616 if (net_error != OK) { | |
617 error_count_++; | |
618 last_error_ = net_error; | |
619 } | |
620 } | |
621 | |
526 void TestNetworkDelegate::OnNetworkBytesReceived(URLRequest* request, | 622 void TestNetworkDelegate::OnNetworkBytesReceived(URLRequest* request, |
527 int64_t bytes_received) { | 623 int64_t bytes_received) { |
528 event_order_[request->identifier()] += "OnNetworkBytesReceived\n"; | 624 event_order_[request->identifier()] += "OnNetworkBytesReceived\n"; |
529 total_network_bytes_received_ += bytes_received; | 625 total_network_bytes_received_ += bytes_received; |
530 } | 626 } |
531 | 627 |
532 void TestNetworkDelegate::OnNetworkBytesSent(URLRequest* request, | 628 void TestNetworkDelegate::OnNetworkBytesSent(URLRequest* request, |
533 int64_t bytes_sent) { | 629 int64_t bytes_sent) { |
534 event_order_[request->identifier()] += "OnNetworkBytesSent\n"; | 630 event_order_[request->identifier()] += "OnNetworkBytesSent\n"; |
535 total_network_bytes_sent_ += bytes_sent; | 631 total_network_bytes_sent_ += bytes_sent; |
536 } | 632 } |
537 | 633 |
634 // Deprecated. | |
538 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) { | 635 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) { |
539 int req_id = request->identifier(); | 636 int req_id = request->identifier(); |
540 InitRequestStatesIfNew(req_id); | 637 InitRequestStatesIfNew(req_id); |
541 event_order_[req_id] += "OnCompleted\n"; | 638 event_order_[req_id] += "OnCompleted\n"; |
542 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" | 639 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" |
543 // is logically identical to | 640 // is logically identical to |
544 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" | 641 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" |
545 EXPECT_TRUE(!request->status().is_success() || | 642 EXPECT_TRUE(!request->status().is_success() || |
546 (next_states_[req_id] & kStageCompletedSuccess)) << | 643 (next_states_[req_id] & kStageCompletedSuccess)) << |
547 event_order_[req_id]; | 644 event_order_[req_id]; |
548 EXPECT_TRUE(request->status().is_success() || | 645 EXPECT_TRUE(request->status().is_success() || |
549 (next_states_[req_id] & kStageCompletedError)) << | 646 (next_states_[req_id] & kStageCompletedError)) << |
550 event_order_[req_id]; | 647 event_order_[req_id]; |
551 next_states_[req_id] = kStageURLRequestDestroyed; | 648 next_states_[req_id] = kStageURLRequestDestroyed; |
552 completed_requests_++; | 649 completed_requests_++; |
553 if (request->status().status() == URLRequestStatus::FAILED) { | 650 if (request->status().status() == URLRequestStatus::FAILED) { |
554 error_count_++; | 651 error_count_++; |
555 last_error_ = request->status().error(); | 652 last_error_ = request->status().error(); |
556 } else if (request->status().status() == URLRequestStatus::CANCELED) { | 653 } else if (request->status().status() == URLRequestStatus::CANCELED) { |
557 canceled_requests_++; | 654 canceled_requests_++; |
558 } else { | 655 } else { |
559 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status()); | 656 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
560 } | 657 } |
561 } | 658 } |
562 | 659 |
660 void TestNetworkDelegate::OnCompleted(URLRequest* request, | |
661 bool started, | |
662 int net_error) { | |
663 DCHECK_NE(net_error, net::ERR_IO_PENDING); | |
664 | |
665 int req_id = request->identifier(); | |
666 InitRequestStatesIfNew(req_id); | |
667 event_order_[req_id] += "OnCompleted\n"; | |
668 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" | |
669 // is logically identical to | |
670 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" | |
671 EXPECT_TRUE(net_error != OK || | |
672 (next_states_[req_id] & kStageCompletedSuccess)) | |
673 << event_order_[req_id]; | |
674 EXPECT_TRUE(net_error == OK || (next_states_[req_id] & kStageCompletedError)) | |
675 << event_order_[req_id]; | |
676 next_states_[req_id] = kStageURLRequestDestroyed; | |
677 completed_requests_++; | |
678 if (net_error == ERR_ABORTED) { | |
679 canceled_requests_++; | |
680 } else if (net_error != OK) { | |
681 error_count_++; | |
682 last_error_ = net_error; | |
683 } else { | |
684 DCHECK_EQ(net_error, OK); | |
685 } | |
686 } | |
687 | |
563 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) { | 688 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) { |
564 int req_id = request->identifier(); | 689 int req_id = request->identifier(); |
565 InitRequestStatesIfNew(req_id); | 690 InitRequestStatesIfNew(req_id); |
566 event_order_[req_id] += "OnURLRequestDestroyed\n"; | 691 event_order_[req_id] += "OnURLRequestDestroyed\n"; |
567 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << | 692 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << |
568 event_order_[req_id]; | 693 event_order_[req_id]; |
569 next_states_[req_id] = kStageDestruction; | 694 next_states_[req_id] = kStageDestruction; |
570 destroyed_requests_++; | 695 destroyed_requests_++; |
571 } | 696 } |
572 | 697 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
659 NetworkDelegate* network_delegate) const { | 784 NetworkDelegate* network_delegate) const { |
660 return main_intercept_job_.release(); | 785 return main_intercept_job_.release(); |
661 } | 786 } |
662 | 787 |
663 void TestJobInterceptor::set_main_intercept_job( | 788 void TestJobInterceptor::set_main_intercept_job( |
664 std::unique_ptr<URLRequestJob> job) { | 789 std::unique_ptr<URLRequestJob> job) { |
665 main_intercept_job_ = std::move(job); | 790 main_intercept_job_ = std::move(job); |
666 } | 791 } |
667 | 792 |
668 } // namespace net | 793 } // namespace net |
OLD | NEW |