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 (sent_size_ < data_.size()) | 21 if (sent_size_ < data_.size()) |
21 SendData(true); | 22 SendData(true); |
22 } | 23 } |
23 | 24 |
24 // Returns false on one condition: If timeout_source_ was already set | 25 // Returns false on one condition: If timeout_source_ was already set |
25 // and it needs to be deleted by the caller. If timeout_source_ is NULL | 26 // and it needs to be deleted by the caller. If timeout_source_ is NULL |
26 // when this function is called, this function will always return true. | 27 // when this function is called, this function will always return true. |
27 bool MockHttpFetcher::SendData(bool skip_delivery) { | 28 bool MockHttpFetcher::SendData(bool skip_delivery) { |
28 CHECK_LT(sent_size_, data_.size()); | 29 CHECK_LT(sent_size_, data_.size()); |
29 if (!skip_delivery) { | 30 if (!skip_delivery) { |
30 const size_t chunk_size = min(kMockHttpFetcherChunkSize, | 31 const size_t chunk_size = min(kMockHttpFetcherChunkSize, |
31 data_.size() - sent_size_); | 32 data_.size() - sent_size_); |
32 CHECK(delegate_); | 33 CHECK(delegate_); |
33 delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size); | 34 delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size); |
34 sent_size_ += chunk_size; | 35 sent_size_ += chunk_size; |
35 CHECK_LE(sent_size_, data_.size()); | 36 CHECK_LE(sent_size_, data_.size()); |
36 if (sent_size_ == data_.size()) { | 37 if (sent_size_ == data_.size()) { |
37 // We've sent all the data. notify of success | 38 // We've sent all the data. Notify of success. |
| 39 http_response_code_ = 200; |
38 delegate_->TransferComplete(this, true); | 40 delegate_->TransferComplete(this, true); |
39 } | 41 } |
40 } | 42 } |
41 | 43 |
42 if (paused_) { | 44 if (paused_) { |
43 // If we're paused, we should return true if timeout_source_ is set, | 45 // If we're paused, we should return true if timeout_source_ is set, |
44 // since we need the caller to delete it. | 46 // since we need the caller to delete it. |
45 return timeout_source_; | 47 return timeout_source_; |
46 } | 48 } |
47 | 49 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 | 95 |
94 void MockHttpFetcher::Unpause() { | 96 void MockHttpFetcher::Unpause() { |
95 CHECK(paused_) << "You must pause before unpause."; | 97 CHECK(paused_) << "You must pause before unpause."; |
96 paused_ = false; | 98 paused_ = false; |
97 if (sent_size_ < data_.size()) { | 99 if (sent_size_ < data_.size()) { |
98 SendData(false); | 100 SendData(false); |
99 } | 101 } |
100 } | 102 } |
101 | 103 |
102 } // namespace chromeos_update_engine | 104 } // namespace chromeos_update_engine |
OLD | NEW |