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 | 244 |
244 int MockNetworkTransaction::Start(const HttpRequestInfo* request, | 245 int MockNetworkTransaction::Start(const HttpRequestInfo* request, |
245 const CompletionCallback& callback, | 246 const CompletionCallback& callback, |
246 const NetLogWithSource& net_log) { | 247 const NetLogWithSource& net_log) { |
247 if (request_) | 248 if (request_) |
248 return ERR_FAILED; | 249 return ERR_FAILED; |
249 | 250 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 // Allow the mock server to decide whether authentication is required or not. | 294 // Allow the mock server to decide whether authentication is required or not. |
294 std::string status_line = response_.headers->GetStatusLine(); | 295 std::string status_line = response_.headers->GetStatusLine(); |
295 return status_line.find(" 401 ") != std::string::npos || | 296 return status_line.find(" 401 ") != std::string::npos || |
296 status_line.find(" 407 ") != std::string::npos; | 297 status_line.find(" 407 ") != std::string::npos; |
297 } | 298 } |
298 | 299 |
299 int MockNetworkTransaction::Read(net::IOBuffer* buf, | 300 int MockNetworkTransaction::Read(net::IOBuffer* buf, |
300 int buf_len, | 301 int buf_len, |
301 const CompletionCallback& callback) { | 302 const CompletionCallback& callback) { |
302 CHECK(!done_reading_called_); | 303 CHECK(!done_reading_called_); |
303 int num = 0; | 304 int num = read_error_; |
304 if (read_handler_) { | 305 |
305 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); | 306 if (!num) { |
Randy Smith (Not in Mondays)
2017/06/22 21:45:05
I find it surprising that if both read_error_ and
shivanisha
2017/06/26 20:45:35
Yes, the intention is that if error is set it will
| |
306 data_cursor_ += num; | 307 if (read_handler_) { |
307 } else { | 308 num = (*read_handler_)(content_length_, data_cursor_, buf, buf_len); |
308 int data_len = static_cast<int>(data_.size()); | |
309 num = std::min(static_cast<int64_t>(buf_len), data_len - data_cursor_); | |
310 if (test_mode_ & TEST_MODE_SLOW_READ) | |
311 num = std::min(num, 1); | |
312 if (num) { | |
313 memcpy(buf->data(), data_.data() + data_cursor_, num); | |
314 data_cursor_ += num; | 309 data_cursor_ += num; |
310 } else { | |
311 int data_len = static_cast<int>(data_.size()); | |
312 num = std::min(static_cast<int64_t>(buf_len), data_len - data_cursor_); | |
313 if (test_mode_ & TEST_MODE_SLOW_READ) | |
314 num = std::min(num, 1); | |
315 if (num) { | |
316 memcpy(buf->data(), data_.data() + data_cursor_, num); | |
317 data_cursor_ += num; | |
318 } | |
315 } | 319 } |
316 } | 320 } |
321 | |
317 if (test_mode_ & TEST_MODE_SYNC_NET_READ) | 322 if (test_mode_ & TEST_MODE_SYNC_NET_READ) |
318 return num; | 323 return num; |
319 | 324 |
320 CallbackLater(callback, num); | 325 CallbackLater(callback, num); |
321 return ERR_IO_PENDING; | 326 return ERR_IO_PENDING; |
322 } | 327 } |
323 | 328 |
324 void MockNetworkTransaction::StopCaching() { | 329 void MockNetworkTransaction::StopCaching() { |
325 if (transaction_factory_.get()) | 330 if (transaction_factory_.get()) |
326 transaction_factory_->TransactionStopCaching(); | 331 transaction_factory_->TransactionStopCaching(); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
488 DCHECK(!resume_start_callback_.is_null()); | 493 DCHECK(!resume_start_callback_.is_null()); |
489 CallbackLater(resume_start_callback_, OK); | 494 CallbackLater(resume_start_callback_, OK); |
490 return ERR_IO_PENDING; | 495 return ERR_IO_PENDING; |
491 } | 496 } |
492 | 497 |
493 void MockNetworkTransaction::GetConnectionAttempts( | 498 void MockNetworkTransaction::GetConnectionAttempts( |
494 ConnectionAttempts* out) const { | 499 ConnectionAttempts* out) const { |
495 NOTIMPLEMENTED(); | 500 NOTIMPLEMENTED(); |
496 } | 501 } |
497 | 502 |
503 void MockNetworkTransaction::SetReadError(int error) { | |
504 read_error_ = error; | |
505 } | |
506 | |
498 void MockNetworkTransaction::CallbackLater(const CompletionCallback& callback, | 507 void MockNetworkTransaction::CallbackLater(const CompletionCallback& callback, |
499 int result) { | 508 int result) { |
500 base::ThreadTaskRunnerHandle::Get()->PostTask( | 509 base::ThreadTaskRunnerHandle::Get()->PostTask( |
501 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, | 510 FROM_HERE, base::Bind(&MockNetworkTransaction::RunCallback, |
502 weak_factory_.GetWeakPtr(), callback, result)); | 511 weak_factory_.GetWeakPtr(), callback, result)); |
503 } | 512 } |
504 | 513 |
505 void MockNetworkTransaction::RunCallback(const CompletionCallback& callback, | 514 void MockNetworkTransaction::RunCallback(const CompletionCallback& callback, |
506 int result) { | 515 int result) { |
507 callback.Run(result); | 516 callback.Run(result); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
582 content.append(buf->data(), rv); | 591 content.append(buf->data(), rv); |
583 else if (rv < 0) | 592 else if (rv < 0) |
584 return rv; | 593 return rv; |
585 } while (rv > 0); | 594 } while (rv > 0); |
586 | 595 |
587 result->swap(content); | 596 result->swap(content); |
588 return OK; | 597 return OK; |
589 } | 598 } |
590 | 599 |
591 } // namespace net | 600 } // namespace net |
OLD | NEW |