Chromium Code Reviews| 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), |
|
mmenke
2016/08/30 22:09:12
Suggest ERR_IO_PENDING (It's the one status we sho
maksims (do not use this acc)
2016/08/31 11:17:00
Done.
| |
| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 243 // independent of any possible errors, or whether it wants SSL errors to | 243 // independent of any possible errors, or whether it wants SSL errors to |
| 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 // Deprecated. | |
| 253 void TestDelegate::OnResponseStarted(URLRequest* request) { | 254 void TestDelegate::OnResponseStarted(URLRequest* request) { |
| 255 int net_error = request->status().error(); | |
| 256 OnResponseStarted(request, net_error); | |
| 257 } | |
|
mmenke
2016/08/30 22:09:12
This method is no longer needed (Be sure to remove
maksims (do not use this acc)
2016/08/31 11:16:59
No, it's needed. Callers are not modified yet. For
mmenke
2016/08/31 21:35:47
So...You're right about that, but I don't see how
mmenke
2016/08/31 21:39:27
Wait, aren't you updating all those callers, for j
maksims (do not use this acc)
2016/09/01 07:13:17
Yes, I am. I'll add the embedded_test_server_unitt
| |
| 258 | |
| 259 void TestDelegate::OnResponseStarted(URLRequest* request, int net_error) { | |
| 254 // It doesn't make sense for the request to have IO pending at this point. | 260 // It doesn't make sense for the request to have IO pending at this point. |
| 255 DCHECK(!request->status().is_io_pending()); | 261 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 256 EXPECT_FALSE(request->is_redirecting()); | 262 EXPECT_FALSE(request->is_redirecting()); |
| 257 | 263 |
| 258 have_full_request_headers_ = | 264 have_full_request_headers_ = |
| 259 request->GetFullRequestHeaders(&full_request_headers_); | 265 request->GetFullRequestHeaders(&full_request_headers_); |
| 260 | 266 |
| 261 response_started_count_++; | 267 response_started_count_++; |
| 268 request_status_ = net_error; | |
| 262 if (cancel_in_rs_) { | 269 if (cancel_in_rs_) { |
| 263 request->Cancel(); | 270 request_status_ = request->Cancel(); |
| 264 OnResponseCompleted(request); | 271 OnResponseCompleted(request); |
| 265 } else if (!request->status().is_success()) { | 272 } else if (net_error != OK) { |
| 266 DCHECK(request->status().status() == URLRequestStatus::FAILED || | |
| 267 request->status().status() == URLRequestStatus::CANCELED); | |
| 268 request_failed_ = true; | 273 request_failed_ = true; |
| 269 OnResponseCompleted(request); | 274 OnResponseCompleted(request); |
| 270 } else { | 275 } else { |
| 271 // Initiate the first read. | 276 // Initiate the first read. |
| 272 int bytes_read = 0; | 277 int bytes_read = request->Read(buf_.get(), kBufferSize); |
| 273 if (request->Read(buf_.get(), kBufferSize, &bytes_read)) | 278 if (bytes_read >= 0) |
| 274 OnReadCompleted(request, bytes_read); | 279 OnReadCompleted(request, bytes_read); |
| 275 else if (!request->status().is_io_pending()) | 280 else if (bytes_read != ERR_IO_PENDING) |
| 276 OnResponseCompleted(request); | 281 OnResponseCompleted(request); |
| 277 } | 282 } |
| 278 } | 283 } |
| 279 | 284 |
| 280 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) { | 285 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) { |
| 281 // It doesn't make sense for the request to have IO pending at this point. | 286 // It doesn't make sense for the request to have IO pending at this point. |
| 282 DCHECK(!request->status().is_io_pending()); | 287 DCHECK_NE(bytes_read, ERR_IO_PENDING); |
| 283 | 288 |
| 284 // If the request was cancelled in a redirect, it should not signal | 289 // If the request was cancelled in a redirect, it should not signal |
| 285 // OnReadCompleted. Note that |cancel_in_rs_| may be true due to | 290 // OnReadCompleted. Note that |cancel_in_rs_| may be true due to |
| 286 // https://crbug.com/564848. | 291 // https://crbug.com/564848. |
| 287 EXPECT_FALSE(cancel_in_rr_); | 292 EXPECT_FALSE(cancel_in_rr_); |
| 288 | 293 |
| 289 if (response_started_count_ == 0) | 294 if (response_started_count_ == 0) |
| 290 received_data_before_response_ = true; | 295 received_data_before_response_ = true; |
| 291 | 296 |
| 292 if (cancel_in_rd_) | 297 if (cancel_in_rd_) |
| 293 request->Cancel(); | 298 request_status_ = request->Cancel(); |
| 294 | 299 |
| 295 if (bytes_read >= 0) { | 300 if (bytes_read >= 0) { |
| 296 // There is data to read. | 301 // There is data to read. |
| 297 received_bytes_count_ += bytes_read; | 302 received_bytes_count_ += bytes_read; |
| 298 | 303 |
| 299 // consume the data | 304 // consume the data |
| 300 data_received_.append(buf_->data(), bytes_read); | 305 data_received_.append(buf_->data(), bytes_read); |
| 301 } | 306 } |
| 302 | 307 |
| 303 // If it was not end of stream, request to read more. | 308 // If it was not end of stream, request to read more. |
| 304 if (request->status().is_success() && bytes_read > 0) { | 309 while (bytes_read > 0) { |
| 305 bytes_read = 0; | 310 bytes_read = request->Read(buf_.get(), kBufferSize); |
| 306 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) { | 311 if (bytes_read > 0) { |
| 307 if (bytes_read > 0) { | 312 data_received_.append(buf_->data(), bytes_read); |
| 308 data_received_.append(buf_->data(), bytes_read); | 313 received_bytes_count_ += bytes_read; |
| 309 received_bytes_count_ += bytes_read; | |
| 310 } else { | |
| 311 break; | |
| 312 } | |
| 313 } | 314 } |
| 314 } | 315 } |
| 315 if (!request->status().is_io_pending()) | 316 |
| 317 request_status_ = bytes_read; | |
| 318 if (request_status_ != ERR_IO_PENDING) | |
| 316 OnResponseCompleted(request); | 319 OnResponseCompleted(request); |
| 317 else if (cancel_in_rd_pending_) | 320 else if (cancel_in_rd_pending_) |
| 318 request->Cancel(); | 321 request_status_ = request->Cancel(); |
| 319 } | 322 } |
| 320 | 323 |
| 321 void TestDelegate::OnResponseCompleted(URLRequest* request) { | 324 void TestDelegate::OnResponseCompleted(URLRequest* request) { |
| 322 if (quit_on_complete_) | 325 if (quit_on_complete_) |
| 323 base::ThreadTaskRunnerHandle::Get()->PostTask( | 326 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 324 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 327 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 325 } | 328 } |
| 326 | 329 |
| 327 TestNetworkDelegate::TestNetworkDelegate() | 330 TestNetworkDelegate::TestNetworkDelegate() |
| 328 : last_error_(0), | 331 : last_error_(0), |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 kStageBeforeStartTransaction | // Redirects from the network delegate do | 501 kStageBeforeStartTransaction | // Redirects from the network delegate do |
| 499 // not | 502 // not |
| 500 // trigger onBeforeURLRequest. | 503 // trigger onBeforeURLRequest. |
| 501 kStageCompletedError; | 504 kStageCompletedError; |
| 502 | 505 |
| 503 // A redirect can lead to a file or a data URL. In this case, we do not send | 506 // A redirect can lead to a file or a data URL. In this case, we do not send |
| 504 // headers. | 507 // headers. |
| 505 next_states_[req_id] |= kStageResponseStarted; | 508 next_states_[req_id] |= kStageResponseStarted; |
| 506 } | 509 } |
| 507 | 510 |
| 511 // Deprecated. | |
| 508 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) { | 512 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) { |
| 513 int net_error = request->status().error(); | |
| 514 OnResponseStarted(request, net_error); | |
| 515 } | |
|
mmenke
2016/08/30 22:09:12
No longer needed.
maksims (do not use this acc)
2016/09/01 07:13:17
Done.
| |
| 516 | |
| 517 void TestNetworkDelegate::OnResponseStarted(URLRequest* request, | |
| 518 int net_error) { | |
| 519 DCHECK_NE(ERR_IO_PENDING, net_error); | |
| 520 | |
| 509 LoadTimingInfo load_timing_info; | 521 LoadTimingInfo load_timing_info; |
| 510 request->GetLoadTimingInfo(&load_timing_info); | 522 request->GetLoadTimingInfo(&load_timing_info); |
| 511 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); | 523 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 512 EXPECT_FALSE(load_timing_info.request_start.is_null()); | 524 EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 513 | 525 |
| 514 int req_id = request->identifier(); | 526 int req_id = request->identifier(); |
| 515 InitRequestStatesIfNew(req_id); | 527 InitRequestStatesIfNew(req_id); |
| 516 event_order_[req_id] += "OnResponseStarted\n"; | 528 event_order_[req_id] += "OnResponseStarted\n"; |
| 517 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) << | 529 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) |
| 518 event_order_[req_id]; | 530 << event_order_[req_id]; |
| 519 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; | 531 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; |
| 520 if (request->status().status() == URLRequestStatus::FAILED) { | 532 if (net_error == ERR_ABORTED) |
| 533 return; | |
| 534 | |
| 535 if (net_error != OK) { | |
| 521 error_count_++; | 536 error_count_++; |
| 522 last_error_ = request->status().error(); | 537 last_error_ = net_error; |
| 523 } | 538 } |
| 524 } | 539 } |
| 525 | 540 |
| 526 void TestNetworkDelegate::OnNetworkBytesReceived(URLRequest* request, | 541 void TestNetworkDelegate::OnNetworkBytesReceived(URLRequest* request, |
| 527 int64_t bytes_received) { | 542 int64_t bytes_received) { |
| 528 event_order_[request->identifier()] += "OnNetworkBytesReceived\n"; | 543 event_order_[request->identifier()] += "OnNetworkBytesReceived\n"; |
| 529 total_network_bytes_received_ += bytes_received; | 544 total_network_bytes_received_ += bytes_received; |
| 530 } | 545 } |
| 531 | 546 |
| 532 void TestNetworkDelegate::OnNetworkBytesSent(URLRequest* request, | 547 void TestNetworkDelegate::OnNetworkBytesSent(URLRequest* request, |
| 533 int64_t bytes_sent) { | 548 int64_t bytes_sent) { |
| 534 event_order_[request->identifier()] += "OnNetworkBytesSent\n"; | 549 event_order_[request->identifier()] += "OnNetworkBytesSent\n"; |
| 535 total_network_bytes_sent_ += bytes_sent; | 550 total_network_bytes_sent_ += bytes_sent; |
| 536 } | 551 } |
| 537 | 552 |
| 553 // Deprecated. | |
| 538 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) { | 554 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) { |
| 555 int net_error = request->status().error(); | |
| 556 OnCompleted(request, started, net_error); | |
| 557 } | |
| 558 | |
| 559 void TestNetworkDelegate::OnCompleted(URLRequest* request, | |
| 560 bool started, | |
| 561 int net_error) { | |
| 562 DCHECK_NE(net_error, net::ERR_IO_PENDING); | |
| 563 | |
| 539 int req_id = request->identifier(); | 564 int req_id = request->identifier(); |
| 540 InitRequestStatesIfNew(req_id); | 565 InitRequestStatesIfNew(req_id); |
| 541 event_order_[req_id] += "OnCompleted\n"; | 566 event_order_[req_id] += "OnCompleted\n"; |
| 542 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" | 567 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" |
| 543 // is logically identical to | 568 // is logically identical to |
| 544 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" | 569 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" |
| 545 EXPECT_TRUE(!request->status().is_success() || | 570 EXPECT_TRUE(net_error != OK || |
| 546 (next_states_[req_id] & kStageCompletedSuccess)) << | 571 (next_states_[req_id] & kStageCompletedSuccess)) |
| 547 event_order_[req_id]; | 572 << event_order_[req_id]; |
| 548 EXPECT_TRUE(request->status().is_success() || | 573 EXPECT_TRUE(net_error == OK || (next_states_[req_id] & kStageCompletedError)) |
| 549 (next_states_[req_id] & kStageCompletedError)) << | 574 << event_order_[req_id]; |
| 550 event_order_[req_id]; | |
| 551 next_states_[req_id] = kStageURLRequestDestroyed; | 575 next_states_[req_id] = kStageURLRequestDestroyed; |
| 552 completed_requests_++; | 576 completed_requests_++; |
| 553 if (request->status().status() == URLRequestStatus::FAILED) { | 577 if (net_error == ERR_ABORTED) { |
| 578 canceled_requests_++; | |
| 579 } else if (net_error != OK) { | |
| 554 error_count_++; | 580 error_count_++; |
| 555 last_error_ = request->status().error(); | 581 last_error_ = net_error; |
| 556 } else if (request->status().status() == URLRequestStatus::CANCELED) { | |
| 557 canceled_requests_++; | |
| 558 } else { | 582 } else { |
| 559 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status()); | 583 DCHECK_EQ(net_error, OK); |
|
mmenke
2016/08/30 22:09:12
Flip order - expected should be first value.
maksims (do not use this acc)
2016/08/31 11:17:00
Done.
| |
| 560 } | 584 } |
| 561 } | 585 } |
| 562 | 586 |
| 563 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) { | 587 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) { |
| 564 int req_id = request->identifier(); | 588 int req_id = request->identifier(); |
| 565 InitRequestStatesIfNew(req_id); | 589 InitRequestStatesIfNew(req_id); |
| 566 event_order_[req_id] += "OnURLRequestDestroyed\n"; | 590 event_order_[req_id] += "OnURLRequestDestroyed\n"; |
| 567 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << | 591 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << |
| 568 event_order_[req_id]; | 592 event_order_[req_id]; |
| 569 next_states_[req_id] = kStageDestruction; | 593 next_states_[req_id] = kStageDestruction; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 659 NetworkDelegate* network_delegate) const { | 683 NetworkDelegate* network_delegate) const { |
| 660 return main_intercept_job_.release(); | 684 return main_intercept_job_.release(); |
| 661 } | 685 } |
| 662 | 686 |
| 663 void TestJobInterceptor::set_main_intercept_job( | 687 void TestJobInterceptor::set_main_intercept_job( |
| 664 std::unique_ptr<URLRequestJob> job) { | 688 std::unique_ptr<URLRequestJob> job) { |
| 665 main_intercept_job_ = std::move(job); | 689 main_intercept_job_ = std::move(job); |
| 666 } | 690 } |
| 667 | 691 |
| 668 } // namespace net | 692 } // namespace net |
| OLD | NEW |