| 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_(ERR_IO_PENDING), |
| 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 void TestDelegate::OnResponseStarted(URLRequest* request) { | 253 void TestDelegate::OnResponseStarted(URLRequest* request, int net_error) { |
| 254 // It doesn't make sense for the request to have IO pending at this point. | 254 // It doesn't make sense for the request to have IO pending at this point. |
| 255 DCHECK(!request->status().is_io_pending()); | 255 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 256 EXPECT_FALSE(request->is_redirecting()); | 256 EXPECT_FALSE(request->is_redirecting()); |
| 257 | 257 |
| 258 have_full_request_headers_ = | 258 have_full_request_headers_ = |
| 259 request->GetFullRequestHeaders(&full_request_headers_); | 259 request->GetFullRequestHeaders(&full_request_headers_); |
| 260 | 260 |
| 261 response_started_count_++; | 261 response_started_count_++; |
| 262 request_status_ = net_error; |
| 262 if (cancel_in_rs_) { | 263 if (cancel_in_rs_) { |
| 263 request->Cancel(); | 264 request_status_ = request->Cancel(); |
| 264 OnResponseCompleted(request); | 265 OnResponseCompleted(request); |
| 265 } else if (!request->status().is_success()) { | 266 } else if (net_error != OK) { |
| 266 DCHECK(request->status().status() == URLRequestStatus::FAILED || | |
| 267 request->status().status() == URLRequestStatus::CANCELED); | |
| 268 request_failed_ = true; | 267 request_failed_ = true; |
| 269 OnResponseCompleted(request); | 268 OnResponseCompleted(request); |
| 270 } else { | 269 } else { |
| 271 // Initiate the first read. | 270 // Initiate the first read. |
| 272 int bytes_read = 0; | 271 int bytes_read = request->Read(buf_.get(), kBufferSize); |
| 273 if (request->Read(buf_.get(), kBufferSize, &bytes_read)) | 272 if (bytes_read >= 0) |
| 274 OnReadCompleted(request, bytes_read); | 273 OnReadCompleted(request, bytes_read); |
| 275 else if (!request->status().is_io_pending()) | 274 else if (bytes_read != ERR_IO_PENDING) |
| 276 OnResponseCompleted(request); | 275 OnResponseCompleted(request); |
| 277 } | 276 } |
| 278 } | 277 } |
| 279 | 278 |
| 280 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) { | 279 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) { |
| 281 // It doesn't make sense for the request to have IO pending at this point. | 280 // It doesn't make sense for the request to have IO pending at this point. |
| 282 DCHECK(!request->status().is_io_pending()); | 281 DCHECK_NE(bytes_read, ERR_IO_PENDING); |
| 283 | 282 |
| 284 // If the request was cancelled in a redirect, it should not signal | 283 // If the request was cancelled in a redirect, it should not signal |
| 285 // OnReadCompleted. Note that |cancel_in_rs_| may be true due to | 284 // OnReadCompleted. Note that |cancel_in_rs_| may be true due to |
| 286 // https://crbug.com/564848. | 285 // https://crbug.com/564848. |
| 287 EXPECT_FALSE(cancel_in_rr_); | 286 EXPECT_FALSE(cancel_in_rr_); |
| 288 | 287 |
| 289 if (response_started_count_ == 0) | 288 if (response_started_count_ == 0) |
| 290 received_data_before_response_ = true; | 289 received_data_before_response_ = true; |
| 291 | 290 |
| 292 if (cancel_in_rd_) | 291 if (cancel_in_rd_) |
| 293 request->Cancel(); | 292 request_status_ = request->Cancel(); |
| 294 | 293 |
| 295 if (bytes_read >= 0) { | 294 if (bytes_read >= 0) { |
| 296 // There is data to read. | 295 // There is data to read. |
| 297 received_bytes_count_ += bytes_read; | 296 received_bytes_count_ += bytes_read; |
| 298 | 297 |
| 299 // consume the data | 298 // consume the data |
| 300 data_received_.append(buf_->data(), bytes_read); | 299 data_received_.append(buf_->data(), bytes_read); |
| 301 } | 300 } |
| 302 | 301 |
| 303 // If it was not end of stream, request to read more. | 302 // If it was not end of stream, request to read more. |
| 304 if (request->status().is_success() && bytes_read > 0) { | 303 while (bytes_read > 0) { |
| 305 bytes_read = 0; | 304 bytes_read = request->Read(buf_.get(), kBufferSize); |
| 306 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) { | 305 if (bytes_read > 0) { |
| 307 if (bytes_read > 0) { | 306 data_received_.append(buf_->data(), bytes_read); |
| 308 data_received_.append(buf_->data(), bytes_read); | 307 received_bytes_count_ += bytes_read; |
| 309 received_bytes_count_ += bytes_read; | |
| 310 } else { | |
| 311 break; | |
| 312 } | |
| 313 } | 308 } |
| 314 } | 309 } |
| 315 if (!request->status().is_io_pending()) | 310 |
| 311 request_status_ = bytes_read; |
| 312 if (request_status_ != ERR_IO_PENDING) |
| 316 OnResponseCompleted(request); | 313 OnResponseCompleted(request); |
| 317 else if (cancel_in_rd_pending_) | 314 else if (cancel_in_rd_pending_) |
| 318 request->Cancel(); | 315 request_status_ = request->Cancel(); |
| 319 } | 316 } |
| 320 | 317 |
| 321 void TestDelegate::OnResponseCompleted(URLRequest* request) { | 318 void TestDelegate::OnResponseCompleted(URLRequest* request) { |
| 322 if (quit_on_complete_) | 319 if (quit_on_complete_) |
| 323 base::ThreadTaskRunnerHandle::Get()->PostTask( | 320 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 324 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | 321 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 325 } | 322 } |
| 326 | 323 |
| 327 TestNetworkDelegate::TestNetworkDelegate() | 324 TestNetworkDelegate::TestNetworkDelegate() |
| 328 : last_error_(0), | 325 : last_error_(0), |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 kStageBeforeStartTransaction | // Redirects from the network delegate do | 495 kStageBeforeStartTransaction | // Redirects from the network delegate do |
| 499 // not | 496 // not |
| 500 // trigger onBeforeURLRequest. | 497 // trigger onBeforeURLRequest. |
| 501 kStageCompletedError; | 498 kStageCompletedError; |
| 502 | 499 |
| 503 // A redirect can lead to a file or a data URL. In this case, we do not send | 500 // A redirect can lead to a file or a data URL. In this case, we do not send |
| 504 // headers. | 501 // headers. |
| 505 next_states_[req_id] |= kStageResponseStarted; | 502 next_states_[req_id] |= kStageResponseStarted; |
| 506 } | 503 } |
| 507 | 504 |
| 508 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) { | 505 void TestNetworkDelegate::OnResponseStarted(URLRequest* request, |
| 506 int net_error) { |
| 507 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 508 |
| 509 LoadTimingInfo load_timing_info; | 509 LoadTimingInfo load_timing_info; |
| 510 request->GetLoadTimingInfo(&load_timing_info); | 510 request->GetLoadTimingInfo(&load_timing_info); |
| 511 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); | 511 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 512 EXPECT_FALSE(load_timing_info.request_start.is_null()); | 512 EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 513 | 513 |
| 514 int req_id = request->identifier(); | 514 int req_id = request->identifier(); |
| 515 InitRequestStatesIfNew(req_id); | 515 InitRequestStatesIfNew(req_id); |
| 516 event_order_[req_id] += "OnResponseStarted\n"; | 516 event_order_[req_id] += "OnResponseStarted\n"; |
| 517 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) << | 517 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) |
| 518 event_order_[req_id]; | 518 << event_order_[req_id]; |
| 519 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; | 519 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError; |
| 520 if (request->status().status() == URLRequestStatus::FAILED) { | 520 if (net_error == ERR_ABORTED) |
| 521 return; |
| 522 |
| 523 if (net_error != OK) { |
| 521 error_count_++; | 524 error_count_++; |
| 522 last_error_ = request->status().error(); | 525 last_error_ = net_error; |
| 523 } | 526 } |
| 524 } | 527 } |
| 525 | 528 |
| 526 void TestNetworkDelegate::OnNetworkBytesReceived(URLRequest* request, | 529 void TestNetworkDelegate::OnNetworkBytesReceived(URLRequest* request, |
| 527 int64_t bytes_received) { | 530 int64_t bytes_received) { |
| 528 event_order_[request->identifier()] += "OnNetworkBytesReceived\n"; | 531 event_order_[request->identifier()] += "OnNetworkBytesReceived\n"; |
| 529 total_network_bytes_received_ += bytes_received; | 532 total_network_bytes_received_ += bytes_received; |
| 530 } | 533 } |
| 531 | 534 |
| 532 void TestNetworkDelegate::OnNetworkBytesSent(URLRequest* request, | 535 void TestNetworkDelegate::OnNetworkBytesSent(URLRequest* request, |
| 533 int64_t bytes_sent) { | 536 int64_t bytes_sent) { |
| 534 event_order_[request->identifier()] += "OnNetworkBytesSent\n"; | 537 event_order_[request->identifier()] += "OnNetworkBytesSent\n"; |
| 535 total_network_bytes_sent_ += bytes_sent; | 538 total_network_bytes_sent_ += bytes_sent; |
| 536 } | 539 } |
| 537 | 540 |
| 538 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) { | 541 void TestNetworkDelegate::OnCompleted(URLRequest* request, |
| 542 bool started, |
| 543 int net_error) { |
| 544 DCHECK_NE(net_error, net::ERR_IO_PENDING); |
| 545 |
| 539 int req_id = request->identifier(); | 546 int req_id = request->identifier(); |
| 540 InitRequestStatesIfNew(req_id); | 547 InitRequestStatesIfNew(req_id); |
| 541 event_order_[req_id] += "OnCompleted\n"; | 548 event_order_[req_id] += "OnCompleted\n"; |
| 542 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" | 549 // Expect "Success -> (next_states_ & kStageCompletedSuccess)" |
| 543 // is logically identical to | 550 // is logically identical to |
| 544 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" | 551 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)" |
| 545 EXPECT_TRUE(!request->status().is_success() || | 552 EXPECT_TRUE(net_error != OK || |
| 546 (next_states_[req_id] & kStageCompletedSuccess)) << | 553 (next_states_[req_id] & kStageCompletedSuccess)) |
| 547 event_order_[req_id]; | 554 << event_order_[req_id]; |
| 548 EXPECT_TRUE(request->status().is_success() || | 555 EXPECT_TRUE(net_error == OK || (next_states_[req_id] & kStageCompletedError)) |
| 549 (next_states_[req_id] & kStageCompletedError)) << | 556 << event_order_[req_id]; |
| 550 event_order_[req_id]; | |
| 551 next_states_[req_id] = kStageURLRequestDestroyed; | 557 next_states_[req_id] = kStageURLRequestDestroyed; |
| 552 completed_requests_++; | 558 completed_requests_++; |
| 553 if (request->status().status() == URLRequestStatus::FAILED) { | 559 if (net_error == ERR_ABORTED) { |
| 560 canceled_requests_++; |
| 561 } else if (net_error != OK) { |
| 554 error_count_++; | 562 error_count_++; |
| 555 last_error_ = request->status().error(); | 563 last_error_ = net_error; |
| 556 } else if (request->status().status() == URLRequestStatus::CANCELED) { | |
| 557 canceled_requests_++; | |
| 558 } else { | 564 } else { |
| 559 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status()); | 565 DCHECK_EQ(OK, net_error); |
| 560 } | 566 } |
| 561 } | 567 } |
| 562 | 568 |
| 563 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) { | 569 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) { |
| 564 int req_id = request->identifier(); | 570 int req_id = request->identifier(); |
| 565 InitRequestStatesIfNew(req_id); | 571 InitRequestStatesIfNew(req_id); |
| 566 event_order_[req_id] += "OnURLRequestDestroyed\n"; | 572 event_order_[req_id] += "OnURLRequestDestroyed\n"; |
| 567 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << | 573 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) << |
| 568 event_order_[req_id]; | 574 event_order_[req_id]; |
| 569 next_states_[req_id] = kStageDestruction; | 575 next_states_[req_id] = kStageDestruction; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 NetworkDelegate* network_delegate) const { | 665 NetworkDelegate* network_delegate) const { |
| 660 return main_intercept_job_.release(); | 666 return main_intercept_job_.release(); |
| 661 } | 667 } |
| 662 | 668 |
| 663 void TestJobInterceptor::set_main_intercept_job( | 669 void TestJobInterceptor::set_main_intercept_job( |
| 664 std::unique_ptr<URLRequestJob> job) { | 670 std::unique_ptr<URLRequestJob> job) { |
| 665 main_intercept_job_ = std::move(job); | 671 main_intercept_job_ = std::move(job); |
| 666 } | 672 } |
| 667 | 673 |
| 668 } // namespace net | 674 } // namespace net |
| OLD | NEW |