| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/http/http_transaction_test_util.h" | 5 #include "net/http/http_transaction_test_util.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 data_cursor_(0), | 230 data_cursor_(0), |
| 231 content_length_(0), | 231 content_length_(0), |
| 232 priority_(priority), | 232 priority_(priority), |
| 233 read_handler_(nullptr), | 233 read_handler_(nullptr), |
| 234 websocket_handshake_stream_create_helper_(nullptr), | 234 websocket_handshake_stream_create_helper_(nullptr), |
| 235 transaction_factory_(factory->AsWeakPtr()), | 235 transaction_factory_(factory->AsWeakPtr()), |
| 236 received_bytes_(0), | 236 received_bytes_(0), |
| 237 sent_bytes_(0), | 237 sent_bytes_(0), |
| 238 socket_log_id_(NetLogSource::kInvalidId), | 238 socket_log_id_(NetLogSource::kInvalidId), |
| 239 done_reading_called_(false), | 239 done_reading_called_(false), |
| 240 read_error_(0), |
| 240 weak_factory_(this) {} | 241 weak_factory_(this) {} |
| 241 | 242 |
| 242 MockNetworkTransaction::~MockNetworkTransaction() { | 243 MockNetworkTransaction::~MockNetworkTransaction() { |
| 243 // Use request_ as in ~HttpNetworkTransaction to make sure its valid and not | 244 // Use request_ as in ~HttpNetworkTransaction to make sure its valid and not |
| 244 // already freed by the consumer. See crbug.com/734037. | 245 // already freed by the consumer. See crbug.com/734037. |
| 245 if (request_) | 246 if (request_) |
| 246 DCHECK(request_->load_flags >= 0); | 247 DCHECK(request_->load_flags >= 0); |
| 247 } | 248 } |
| 248 | 249 |
| 249 int MockNetworkTransaction::Start(const HttpRequestInfo* request, | 250 int MockNetworkTransaction::Start(const HttpRequestInfo* request, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 // Allow the mock server to decide whether authentication is required or not. | 299 // Allow the mock server to decide whether authentication is required or not. |
| 299 std::string status_line = response_.headers->GetStatusLine(); | 300 std::string status_line = response_.headers->GetStatusLine(); |
| 300 return status_line.find(" 401 ") != std::string::npos || | 301 return status_line.find(" 401 ") != std::string::npos || |
| 301 status_line.find(" 407 ") != std::string::npos; | 302 status_line.find(" 407 ") != std::string::npos; |
| 302 } | 303 } |
| 303 | 304 |
| 304 int MockNetworkTransaction::Read(net::IOBuffer* buf, | 305 int MockNetworkTransaction::Read(net::IOBuffer* buf, |
| 305 int buf_len, | 306 int buf_len, |
| 306 const CompletionCallback& callback) { | 307 const CompletionCallback& callback) { |
| 307 CHECK(!done_reading_called_); | 308 CHECK(!done_reading_called_); |
| 308 int num = 0; | 309 int num = read_error_; |
| 309 if (read_handler_) { | 310 |
| 310 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); | 311 if (OK == num) { |
| 311 data_cursor_ += num; | 312 if (read_handler_) { |
| 312 } else { | 313 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); |
| 313 int data_len = static_cast<int>(data_.size()); | |
| 314 num = std::min(static_cast<int64_t>(buf_len), data_len - data_cursor_); | |
| 315 if (test_mode_ & TEST_MODE_SLOW_READ) | |
| 316 num = std::min(num, 1); | |
| 317 if (num) { | |
| 318 memcpy(buf->data(), data_.data() + data_cursor_, num); | |
| 319 data_cursor_ += num; | 314 data_cursor_ += num; |
| 315 } else { |
| 316 int data_len = static_cast<int>(data_.size()); |
| 317 num = std::min(static_cast<int64_t>(buf_len), data_len - data_cursor_); |
| 318 if (test_mode_ & TEST_MODE_SLOW_READ) |
| 319 num = std::min(num, 1); |
| 320 if (num) { |
| 321 memcpy(buf->data(), data_.data() + data_cursor_, num); |
| 322 data_cursor_ += num; |
| 323 } |
| 320 } | 324 } |
| 321 } | 325 } |
| 326 |
| 322 if (test_mode_ & TEST_MODE_SYNC_NET_READ) | 327 if (test_mode_ & TEST_MODE_SYNC_NET_READ) |
| 323 return num; | 328 return num; |
| 324 | 329 |
| 325 CallbackLater(callback, num); | 330 CallbackLater(callback, num); |
| 326 return ERR_IO_PENDING; | 331 return ERR_IO_PENDING; |
| 327 } | 332 } |
| 328 | 333 |
| 329 void MockNetworkTransaction::StopCaching() { | 334 void MockNetworkTransaction::StopCaching() { |
| 330 if (transaction_factory_.get()) | 335 if (transaction_factory_.get()) |
| 331 transaction_factory_->TransactionStopCaching(); | 336 transaction_factory_->TransactionStopCaching(); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 DCHECK(!resume_start_callback_.is_null()); | 498 DCHECK(!resume_start_callback_.is_null()); |
| 494 CallbackLater(resume_start_callback_, OK); | 499 CallbackLater(resume_start_callback_, OK); |
| 495 return ERR_IO_PENDING; | 500 return ERR_IO_PENDING; |
| 496 } | 501 } |
| 497 | 502 |
| 498 void MockNetworkTransaction::GetConnectionAttempts( | 503 void MockNetworkTransaction::GetConnectionAttempts( |
| 499 ConnectionAttempts* out) const { | 504 ConnectionAttempts* out) const { |
| 500 NOTIMPLEMENTED(); | 505 NOTIMPLEMENTED(); |
| 501 } | 506 } |
| 502 | 507 |
| 508 void MockNetworkTransaction::SetReadError(int error) { |
| 509 read_error_ = error; |
| 510 } |
| 511 |
| 503 void MockNetworkTransaction::CallbackLater(const CompletionCallback& callback, | 512 void MockNetworkTransaction::CallbackLater(const CompletionCallback& callback, |
| 504 int result) { | 513 int result) { |
| 505 base::ThreadTaskRunnerHandle::Get()->PostTask( | 514 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 506 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, | 515 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, |
| 507 weak_factory_.GetWeakPtr(), callback, result)); | 516 weak_factory_.GetWeakPtr(), callback, result)); |
| 508 } | 517 } |
| 509 | 518 |
| 510 void MockNetworkTransaction::RunCallback(const CompletionCallback& callback, | 519 void MockNetworkTransaction::RunCallback(const CompletionCallback& callback, |
| 511 int result) { | 520 int result) { |
| 512 callback.Run(result); | 521 callback.Run(result); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 content.append(buf->data(), rv); | 596 content.append(buf->data(), rv); |
| 588 else if (rv < 0) | 597 else if (rv < 0) |
| 589 return rv; | 598 return rv; |
| 590 } while (rv > 0); | 599 } while (rv > 0); |
| 591 | 600 |
| 592 result->swap(content); | 601 result->swap(content); |
| 593 return OK; | 602 return OK; |
| 594 } | 603 } |
| 595 | 604 |
| 596 } // namespace net | 605 } // namespace net |
| OLD | NEW |