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 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 MockNetworkTransaction::MockNetworkTransaction(RequestPriority priority, | 237 MockNetworkTransaction::MockNetworkTransaction(RequestPriority priority, |
238 MockNetworkLayer* factory) | 238 MockNetworkLayer* factory) |
239 : request_(NULL), | 239 : request_(NULL), |
240 data_cursor_(0), | 240 data_cursor_(0), |
241 priority_(priority), | 241 priority_(priority), |
242 websocket_handshake_stream_create_helper_(NULL), | 242 websocket_handshake_stream_create_helper_(NULL), |
243 transaction_factory_(factory->AsWeakPtr()), | 243 transaction_factory_(factory->AsWeakPtr()), |
244 received_bytes_(0), | 244 received_bytes_(0), |
245 sent_bytes_(0), | 245 sent_bytes_(0), |
246 socket_log_id_(NetLog::Source::kInvalidId), | 246 socket_log_id_(NetLog::Source::kInvalidId), |
| 247 done_reading_called_(false), |
247 weak_factory_(this) {} | 248 weak_factory_(this) {} |
248 | 249 |
249 MockNetworkTransaction::~MockNetworkTransaction() {} | 250 MockNetworkTransaction::~MockNetworkTransaction() {} |
250 | 251 |
251 int MockNetworkTransaction::Start(const HttpRequestInfo* request, | 252 int MockNetworkTransaction::Start(const HttpRequestInfo* request, |
252 const CompletionCallback& callback, | 253 const CompletionCallback& callback, |
253 const BoundNetLog& net_log) { | 254 const BoundNetLog& net_log) { |
254 if (request_) | 255 if (request_) |
255 return ERR_FAILED; | 256 return ERR_FAILED; |
256 | 257 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 | 295 |
295 // Allow the mock server to decide whether authentication is required or not. | 296 // Allow the mock server to decide whether authentication is required or not. |
296 std::string status_line = response_.headers->GetStatusLine(); | 297 std::string status_line = response_.headers->GetStatusLine(); |
297 return status_line.find(" 401 ") != std::string::npos || | 298 return status_line.find(" 401 ") != std::string::npos || |
298 status_line.find(" 407 ") != std::string::npos; | 299 status_line.find(" 407 ") != std::string::npos; |
299 } | 300 } |
300 | 301 |
301 int MockNetworkTransaction::Read(IOBuffer* buf, | 302 int MockNetworkTransaction::Read(IOBuffer* buf, |
302 int buf_len, | 303 int buf_len, |
303 const CompletionCallback& callback) { | 304 const CompletionCallback& callback) { |
| 305 CHECK(!done_reading_called_); |
304 int data_len = static_cast<int>(data_.size()); | 306 int data_len = static_cast<int>(data_.size()); |
305 int num = std::min(buf_len, data_len - data_cursor_); | 307 int num = std::min(buf_len, data_len - data_cursor_); |
306 if (test_mode_ & TEST_MODE_SLOW_READ) | 308 if (test_mode_ & TEST_MODE_SLOW_READ) |
307 num = std::min(num, 1); | 309 num = std::min(num, 1); |
308 if (num) { | 310 if (num) { |
309 memcpy(buf->data(), data_.data() + data_cursor_, num); | 311 memcpy(buf->data(), data_.data() + data_cursor_, num); |
310 data_cursor_ += num; | 312 data_cursor_ += num; |
311 } | 313 } |
312 if (test_mode_ & TEST_MODE_SYNC_NET_READ) | 314 if (test_mode_ & TEST_MODE_SYNC_NET_READ) |
313 return num; | 315 return num; |
(...skipping 14 matching lines...) Expand all Loading... |
328 | 330 |
329 int64_t MockNetworkTransaction::GetTotalReceivedBytes() const { | 331 int64_t MockNetworkTransaction::GetTotalReceivedBytes() const { |
330 return received_bytes_; | 332 return received_bytes_; |
331 } | 333 } |
332 | 334 |
333 int64_t MockNetworkTransaction::GetTotalSentBytes() const { | 335 int64_t MockNetworkTransaction::GetTotalSentBytes() const { |
334 return sent_bytes_; | 336 return sent_bytes_; |
335 } | 337 } |
336 | 338 |
337 void MockNetworkTransaction::DoneReading() { | 339 void MockNetworkTransaction::DoneReading() { |
| 340 CHECK(!done_reading_called_); |
| 341 done_reading_called_ = true; |
338 if (transaction_factory_.get()) | 342 if (transaction_factory_.get()) |
339 transaction_factory_->TransactionDoneReading(); | 343 transaction_factory_->TransactionDoneReading(); |
340 } | 344 } |
341 | 345 |
342 const HttpResponseInfo* MockNetworkTransaction::GetResponseInfo() const { | 346 const HttpResponseInfo* MockNetworkTransaction::GetResponseInfo() const { |
343 return &response_; | 347 return &response_; |
344 } | 348 } |
345 | 349 |
346 LoadState MockNetworkTransaction::GetLoadState() const { | 350 LoadState MockNetworkTransaction::GetLoadState() const { |
347 if (data_cursor_) | 351 if (data_cursor_) |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 : transaction_count_(0), | 503 : transaction_count_(0), |
500 done_reading_called_(false), | 504 done_reading_called_(false), |
501 stop_caching_called_(false), | 505 stop_caching_called_(false), |
502 last_create_transaction_priority_(DEFAULT_PRIORITY), | 506 last_create_transaction_priority_(DEFAULT_PRIORITY), |
503 clock_(nullptr) { | 507 clock_(nullptr) { |
504 } | 508 } |
505 | 509 |
506 MockNetworkLayer::~MockNetworkLayer() {} | 510 MockNetworkLayer::~MockNetworkLayer() {} |
507 | 511 |
508 void MockNetworkLayer::TransactionDoneReading() { | 512 void MockNetworkLayer::TransactionDoneReading() { |
| 513 CHECK(!done_reading_called_); |
509 done_reading_called_ = true; | 514 done_reading_called_ = true; |
510 } | 515 } |
511 | 516 |
512 void MockNetworkLayer::TransactionStopCaching() { | 517 void MockNetworkLayer::TransactionStopCaching() { |
513 stop_caching_called_ = true; | 518 stop_caching_called_ = true; |
514 } | 519 } |
515 | 520 |
516 int MockNetworkLayer::CreateTransaction(RequestPriority priority, | 521 int MockNetworkLayer::CreateTransaction(RequestPriority priority, |
517 scoped_ptr<HttpTransaction>* trans) { | 522 scoped_ptr<HttpTransaction>* trans) { |
518 transaction_count_++; | 523 transaction_count_++; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 content.append(buf->data(), rv); | 567 content.append(buf->data(), rv); |
563 else if (rv < 0) | 568 else if (rv < 0) |
564 return rv; | 569 return rv; |
565 } while (rv > 0); | 570 } while (rv > 0); |
566 | 571 |
567 result->swap(content); | 572 result->swap(content); |
568 return OK; | 573 return OK; |
569 } | 574 } |
570 | 575 |
571 } // namespace net | 576 } // namespace net |
OLD | NEW |