| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <string.h> | 5 #include <string.h> |
| 6 #include <algorithm> | 6 #include <algorithm> |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 } | 47 } |
| 48 | 48 |
| 49 return true; | 49 return true; |
| 50 } | 50 } |
| 51 | 51 |
| 52 class MockJobObserver : public net::URLRequestJobTracker::JobObserver { | 52 class MockJobObserver : public net::URLRequestJobTracker::JobObserver { |
| 53 public: | 53 public: |
| 54 MOCK_METHOD1(OnJobAdded, void(net::URLRequestJob* job)); | 54 MOCK_METHOD1(OnJobAdded, void(net::URLRequestJob* job)); |
| 55 MOCK_METHOD1(OnJobRemoved, void(net::URLRequestJob* job)); | 55 MOCK_METHOD1(OnJobRemoved, void(net::URLRequestJob* job)); |
| 56 MOCK_METHOD2(OnJobDone, void(net::URLRequestJob* job, | 56 MOCK_METHOD2(OnJobDone, void(net::URLRequestJob* job, |
| 57 const URLRequestStatus& status)); | 57 const net::URLRequestStatus& status)); |
| 58 MOCK_METHOD3(OnJobRedirect, void(net::URLRequestJob* job, | 58 MOCK_METHOD3(OnJobRedirect, void(net::URLRequestJob* job, |
| 59 const GURL& location, | 59 const GURL& location, |
| 60 int status_code)); | 60 int status_code)); |
| 61 MOCK_METHOD3(OnBytesRead, void(net::URLRequestJob* job, | 61 MOCK_METHOD3(OnBytesRead, void(net::URLRequestJob* job, |
| 62 const char* buf, | 62 const char* buf, |
| 63 int byte_count)); | 63 int byte_count)); |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 // A net::URLRequestJob that returns static content for given URLs. We do | 66 // A net::URLRequestJob that returns static content for given URLs. We do |
| 67 // not use net::URLRequestTestJob here because net::URLRequestTestJob fakes | 67 // not use net::URLRequestTestJob here because net::URLRequestTestJob fakes |
| (...skipping 19 matching lines...) Expand all Loading... |
| 87 const size_t bytes_to_read = std::min( | 87 const size_t bytes_to_read = std::min( |
| 88 response_data_.size(), static_cast<size_t>(buf_size)); | 88 response_data_.size(), static_cast<size_t>(buf_size)); |
| 89 | 89 |
| 90 // Regardless of whether we're performing a sync or async read, | 90 // Regardless of whether we're performing a sync or async read, |
| 91 // copy the data into the caller's buffer now. That way we don't | 91 // copy the data into the caller's buffer now. That way we don't |
| 92 // have to hold on to the buffers in the async case. | 92 // have to hold on to the buffers in the async case. |
| 93 memcpy(buf->data(), response_data_.data(), bytes_to_read); | 93 memcpy(buf->data(), response_data_.data(), bytes_to_read); |
| 94 response_data_.erase(0, bytes_to_read); | 94 response_data_.erase(0, bytes_to_read); |
| 95 | 95 |
| 96 if (async_reads_) { | 96 if (async_reads_) { |
| 97 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 97 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 98 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 98 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 99 this, &URLRequestJobTrackerTestJob::OnReadCompleted, | 99 this, &URLRequestJobTrackerTestJob::OnReadCompleted, |
| 100 bytes_to_read)); | 100 bytes_to_read)); |
| 101 } else { | 101 } else { |
| 102 SetStatus(URLRequestStatus()); | 102 SetStatus(net::URLRequestStatus()); |
| 103 *bytes_read = bytes_to_read; | 103 *bytes_read = bytes_to_read; |
| 104 } | 104 } |
| 105 return !async_reads_; | 105 return !async_reads_; |
| 106 } | 106 } |
| 107 | 107 |
| 108 void OnReadCompleted(int status) { | 108 void OnReadCompleted(int status) { |
| 109 if (status == 0) { | 109 if (status == 0) { |
| 110 NotifyDone(URLRequestStatus()); | 110 NotifyDone(net::URLRequestStatus()); |
| 111 } else if (status > 0) { | 111 } else if (status > 0) { |
| 112 SetStatus(URLRequestStatus()); | 112 SetStatus(net::URLRequestStatus()); |
| 113 } else { | 113 } else { |
| 114 ASSERT_FALSE(true) << "Unexpected OnReadCompleted callback."; | 114 ASSERT_FALSE(true) << "Unexpected OnReadCompleted callback."; |
| 115 } | 115 } |
| 116 | 116 |
| 117 NotifyReadComplete(status); | 117 NotifyReadComplete(status); |
| 118 } | 118 } |
| 119 | 119 |
| 120 bool GetContentEncodings( | 120 bool GetContentEncodings( |
| 121 std::vector<Filter::FilterType>* encoding_types) { | 121 std::vector<Filter::FilterType>* encoding_types) { |
| 122 if (request_->url().spec() == "test:basic") { | 122 if (request_->url().spec() == "test:basic") { |
| 123 return false; | 123 return false; |
| 124 } else if (request_->url().spec() == "test:compressed") { | 124 } else if (request_->url().spec() == "test:compressed") { |
| 125 encoding_types->push_back(Filter::FILTER_TYPE_GZIP); | 125 encoding_types->push_back(Filter::FILTER_TYPE_GZIP); |
| 126 return true; | 126 return true; |
| 127 } else { | 127 } else { |
| 128 return net::URLRequestJob::GetContentEncodings(encoding_types); | 128 return net::URLRequestJob::GetContentEncodings(encoding_types); |
| 129 } | 129 } |
| 130 } | 130 } |
| 131 | 131 |
| 132 // The data to send, will be set in Start(). | 132 // The data to send, will be set in Start(). |
| 133 std::string response_data_; | 133 std::string response_data_; |
| 134 | 134 |
| 135 // Should reads be synchronous or asynchronous? | 135 // Should reads be synchronous or asynchronous? |
| 136 const bool async_reads_; | 136 const bool async_reads_; |
| 137 }; | 137 }; |
| 138 | 138 |
| 139 // Google Mock Matcher to check two URLRequestStatus instances for | 139 // Google Mock Matcher to check two net::URLRequestStatus instances for |
| 140 // equality. | 140 // equality. |
| 141 MATCHER_P(StatusEq, other, "") { | 141 MATCHER_P(StatusEq, other, "") { |
| 142 return (arg.status() == other.status() && | 142 return (arg.status() == other.status() && |
| 143 arg.os_error() == other.os_error()); | 143 arg.os_error() == other.os_error()); |
| 144 } | 144 } |
| 145 | 145 |
| 146 // Google Mock Matcher to check that two blocks of memory are equal. | 146 // Google Mock Matcher to check that two blocks of memory are equal. |
| 147 MATCHER_P2(MemEq, other, len, "") { | 147 MATCHER_P2(MemEq, other, len, "") { |
| 148 return memcmp(arg, other, len) == 0; | 148 return memcmp(arg, other, len) == 0; |
| 149 } | 149 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 165 const GURL gurl(url); | 165 const GURL gurl(url); |
| 166 std::string body; | 166 std::string body; |
| 167 ASSERT_TRUE(GetResponseBody(gurl, &body)); | 167 ASSERT_TRUE(GetResponseBody(gurl, &body)); |
| 168 | 168 |
| 169 // We expect to receive one call for each method on the JobObserver, | 169 // We expect to receive one call for each method on the JobObserver, |
| 170 // in the following order: | 170 // in the following order: |
| 171 EXPECT_CALL(observer, OnJobAdded(NotNull())); | 171 EXPECT_CALL(observer, OnJobAdded(NotNull())); |
| 172 EXPECT_CALL(observer, OnBytesRead(NotNull(), | 172 EXPECT_CALL(observer, OnBytesRead(NotNull(), |
| 173 MemEq(body.data(), body.size()), | 173 MemEq(body.data(), body.size()), |
| 174 Eq(static_cast<int>(body.size())))); | 174 Eq(static_cast<int>(body.size())))); |
| 175 EXPECT_CALL(observer, OnJobDone(NotNull(), StatusEq(URLRequestStatus()))); | 175 EXPECT_CALL(observer, OnJobDone(NotNull(), |
| 176 StatusEq(net::URLRequestStatus()))); |
| 176 EXPECT_CALL(observer, OnJobRemoved(NotNull())); | 177 EXPECT_CALL(observer, OnJobRemoved(NotNull())); |
| 177 | 178 |
| 178 // Attach our observer and perform the resource fetch. | 179 // Attach our observer and perform the resource fetch. |
| 179 net::g_url_request_job_tracker.AddObserver(&observer); | 180 net::g_url_request_job_tracker.AddObserver(&observer); |
| 180 Fetch(gurl); | 181 Fetch(gurl); |
| 181 net::g_url_request_job_tracker.RemoveObserver(&observer); | 182 net::g_url_request_job_tracker.RemoveObserver(&observer); |
| 182 } | 183 } |
| 183 | 184 |
| 184 void Fetch(const GURL& url) { | 185 void Fetch(const GURL& url) { |
| 185 TestDelegate d; | 186 TestDelegate d; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 g_async_reads = true; | 225 g_async_reads = true; |
| 225 AssertJobTrackerCallbacks("test:compressed"); | 226 AssertJobTrackerCallbacks("test:compressed"); |
| 226 } | 227 } |
| 227 | 228 |
| 228 TEST_F(URLRequestJobTrackerTest, CompressedSync) { | 229 TEST_F(URLRequestJobTrackerTest, CompressedSync) { |
| 229 g_async_reads = false; | 230 g_async_reads = false; |
| 230 AssertJobTrackerCallbacks("test:compressed"); | 231 AssertJobTrackerCallbacks("test:compressed"); |
| 231 } | 232 } |
| 232 | 233 |
| 233 } // namespace | 234 } // namespace |
| OLD | NEW |