OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS 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 "update_engine/mock_http_fetcher.h" | 5 #include "update_engine/mock_http_fetcher.h" |
6 #include <algorithm> | 6 #include <algorithm> |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 // This is a mac implementation of HttpFetcher which is useful for testing. | 9 // This is a mac implementation of HttpFetcher which is useful for testing. |
10 | 10 |
11 using std::min; | 11 using std::min; |
12 | 12 |
13 namespace chromeos_update_engine { | 13 namespace chromeos_update_engine { |
14 | 14 |
15 MockHttpFetcher::~MockHttpFetcher() { | 15 MockHttpFetcher::~MockHttpFetcher() { |
16 CHECK(!timeout_source_) << "Call TerminateTransfer() before dtor."; | 16 CHECK(!timeout_source_) << "Call TerminateTransfer() before dtor."; |
17 } | 17 } |
18 | 18 |
19 void MockHttpFetcher::BeginTransfer(const std::string& url) { | 19 void MockHttpFetcher::BeginTransfer(const std::string& url) { |
20 http_response_code_ = 0; | 20 if (fail_transfer_ || data_.empty()) { |
| 21 // No data to send, just notify of completion.. |
| 22 SignalTransferComplete(); |
| 23 return; |
| 24 } |
21 if (sent_size_ < data_.size()) | 25 if (sent_size_ < data_.size()) |
22 SendData(true); | 26 SendData(true); |
23 } | 27 } |
24 | 28 |
25 // Returns false on one condition: If timeout_source_ was already set | 29 // Returns false on one condition: If timeout_source_ was already set |
26 // and it needs to be deleted by the caller. If timeout_source_ is NULL | 30 // and it needs to be deleted by the caller. If timeout_source_ is NULL |
27 // when this function is called, this function will always return true. | 31 // when this function is called, this function will always return true. |
28 bool MockHttpFetcher::SendData(bool skip_delivery) { | 32 bool MockHttpFetcher::SendData(bool skip_delivery) { |
| 33 if (fail_transfer_) { |
| 34 SignalTransferComplete(); |
| 35 return timeout_source_; |
| 36 } |
| 37 |
29 CHECK_LT(sent_size_, data_.size()); | 38 CHECK_LT(sent_size_, data_.size()); |
30 if (!skip_delivery) { | 39 if (!skip_delivery) { |
31 const size_t chunk_size = min(kMockHttpFetcherChunkSize, | 40 const size_t chunk_size = min(kMockHttpFetcherChunkSize, |
32 data_.size() - sent_size_); | 41 data_.size() - sent_size_); |
33 CHECK(delegate_); | 42 CHECK(delegate_); |
34 delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size); | 43 delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size); |
35 sent_size_ += chunk_size; | 44 sent_size_ += chunk_size; |
36 CHECK_LE(sent_size_, data_.size()); | 45 CHECK_LE(sent_size_, data_.size()); |
37 if (sent_size_ == data_.size()) { | 46 if (sent_size_ == data_.size()) { |
38 // We've sent all the data. Notify of success. | 47 // We've sent all the data. Notify of success. |
39 http_response_code_ = 200; | 48 SignalTransferComplete(); |
40 delegate_->TransferComplete(this, true); | |
41 } | 49 } |
42 } | 50 } |
43 | 51 |
44 if (paused_) { | 52 if (paused_) { |
45 // If we're paused, we should return true if timeout_source_ is set, | 53 // If we're paused, we should return true if timeout_source_ is set, |
46 // since we need the caller to delete it. | 54 // since we need the caller to delete it. |
47 return timeout_source_; | 55 return timeout_source_; |
48 } | 56 } |
49 | 57 |
50 if (timeout_source_) { | 58 if (timeout_source_) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 102 } |
95 | 103 |
96 void MockHttpFetcher::Unpause() { | 104 void MockHttpFetcher::Unpause() { |
97 CHECK(paused_) << "You must pause before unpause."; | 105 CHECK(paused_) << "You must pause before unpause."; |
98 paused_ = false; | 106 paused_ = false; |
99 if (sent_size_ < data_.size()) { | 107 if (sent_size_ < data_.size()) { |
100 SendData(false); | 108 SendData(false); |
101 } | 109 } |
102 } | 110 } |
103 | 111 |
| 112 void MockHttpFetcher::FailTransfer(int http_response_code) { |
| 113 fail_transfer_ = true; |
| 114 http_response_code_ = http_response_code; |
| 115 } |
| 116 |
| 117 void MockHttpFetcher::SignalTransferComplete() { |
| 118 // If the transfer has been failed, the HTTP response code should be set |
| 119 // already. |
| 120 if (!fail_transfer_) { |
| 121 http_response_code_ = 200; |
| 122 } |
| 123 delegate_->TransferComplete(this, !fail_transfer_); |
| 124 } |
| 125 |
104 } // namespace chromeos_update_engine | 126 } // namespace chromeos_update_engine |
OLD | NEW |