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/url_request/url_request_file_job.h" | 5 #include "net/url_request/url_request_file_job.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 }; | 175 }; |
176 | 176 |
177 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} | 177 URLRequestFileJobEventsTest::URLRequestFileJobEventsTest() {} |
178 | 178 |
179 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, | 179 void URLRequestFileJobEventsTest::RunRequest(const std::string& content, |
180 const Range* range) { | 180 const Range* range) { |
181 base::ScopedTempDir directory; | 181 base::ScopedTempDir directory; |
182 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 182 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
183 base::FilePath path; | 183 base::FilePath path; |
184 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); | 184 ASSERT_TRUE(CreateTempFileWithContent(content, directory, &path)); |
185 CallbacksJobFactory factory(path, &observer_); | |
186 context_.set_job_factory(&factory); | |
187 | 185 |
188 scoped_ptr<URLRequest> request(context_.CreateRequest( | 186 { |
189 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); | 187 CallbacksJobFactory factory(path, &observer_); |
190 if (range) { | 188 context_.set_job_factory(&factory); |
191 ASSERT_GE(range->start, 0); | 189 |
192 ASSERT_GE(range->end, 0); | 190 scoped_ptr<URLRequest> request(context_.CreateRequest( |
193 ASSERT_LE(range->start, range->end); | 191 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); |
194 ASSERT_LT(static_cast<unsigned int>(range->end), content.length()); | 192 if (range) { |
195 std::string range_value = | 193 ASSERT_GE(range->start, 0); |
196 base::StringPrintf("bytes=%d-%d", range->start, range->end); | 194 ASSERT_GE(range->end, 0); |
197 request->SetExtraRequestHeaderByName( | 195 ASSERT_LE(range->start, range->end); |
198 HttpRequestHeaders::kRange, range_value, true /*overwrite*/); | 196 ASSERT_LT(static_cast<unsigned int>(range->end), content.length()); |
| 197 std::string range_value = |
| 198 base::StringPrintf("bytes=%d-%d", range->start, range->end); |
| 199 request->SetExtraRequestHeaderByName(HttpRequestHeaders::kRange, |
| 200 range_value, true /*overwrite*/); |
| 201 } |
| 202 request->Start(); |
| 203 |
| 204 base::RunLoop().Run(); |
| 205 |
| 206 EXPECT_FALSE(delegate_.request_failed()); |
| 207 int expected_length = |
| 208 range ? (range->end - range->start + 1) : content.length(); |
| 209 EXPECT_EQ(delegate_.bytes_received(), expected_length); |
| 210 |
| 211 std::string expected_content; |
| 212 if (range) { |
| 213 expected_content.insert(0, content, range->start, expected_length); |
| 214 } else { |
| 215 expected_content = content; |
| 216 } |
| 217 EXPECT_TRUE(delegate_.data_received() == expected_content); |
| 218 |
| 219 ASSERT_EQ(observer_.jobs().size(), 1u); |
| 220 ASSERT_EQ(observer_.jobs().at(0)->seek_position(), |
| 221 range ? range->start : 0); |
| 222 |
| 223 std::string observed_content; |
| 224 const std::vector<std::string>& chunks = |
| 225 observer_.jobs().at(0)->data_chunks(); |
| 226 for (std::vector<std::string>::const_iterator i = chunks.begin(); |
| 227 i != chunks.end(); ++i) { |
| 228 observed_content.append(*i); |
| 229 } |
| 230 EXPECT_EQ(expected_content, observed_content); |
199 } | 231 } |
200 request->Start(); | |
201 | 232 |
202 base::RunLoop loop; | 233 base::RunLoop().RunUntilIdle(); |
203 loop.Run(); | |
204 | |
205 EXPECT_FALSE(delegate_.request_failed()); | |
206 int expected_length = | |
207 range ? (range->end - range->start + 1) : content.length(); | |
208 EXPECT_EQ(delegate_.bytes_received(), expected_length); | |
209 | |
210 std::string expected_content; | |
211 if (range) { | |
212 expected_content.insert(0, content, range->start, expected_length); | |
213 } else { | |
214 expected_content = content; | |
215 } | |
216 EXPECT_TRUE(delegate_.data_received() == expected_content); | |
217 | |
218 ASSERT_EQ(observer_.jobs().size(), 1u); | |
219 ASSERT_EQ(observer_.jobs().at(0)->seek_position(), range ? range->start : 0); | |
220 | |
221 std::string observed_content; | |
222 const std::vector<std::string>& chunks = | |
223 observer_.jobs().at(0)->data_chunks(); | |
224 for (std::vector<std::string>::const_iterator i = chunks.begin(); | |
225 i != chunks.end(); | |
226 ++i) { | |
227 observed_content.append(*i); | |
228 } | |
229 EXPECT_EQ(expected_content, observed_content); | |
230 } | 234 } |
231 | 235 |
232 // Helper function to make a character array filled with |size| bytes of | 236 // Helper function to make a character array filled with |size| bytes of |
233 // test content. | 237 // test content. |
234 std::string MakeContentOfSize(int size) { | 238 std::string MakeContentOfSize(int size) { |
235 EXPECT_GE(size, 0); | 239 EXPECT_GE(size, 0); |
236 std::string result; | 240 std::string result; |
237 result.reserve(size); | 241 result.reserve(size); |
238 for (int i = 0; i < size; i++) { | 242 for (int i = 0; i < size; i++) { |
239 result.append(1, static_cast<char>(i % 256)); | 243 result.append(1, static_cast<char>(i % 256)); |
(...skipping 17 matching lines...) Expand all Loading... |
257 // Use a 15KB content file and read a range chosen somewhat arbitrarily but | 261 // Use a 15KB content file and read a range chosen somewhat arbitrarily but |
258 // not aligned on any likely page boundaries. | 262 // not aligned on any likely page boundaries. |
259 int size = 15 * 1024; | 263 int size = 15 * 1024; |
260 Range range(1701, (6 * 1024) + 3); | 264 Range range(1701, (6 * 1024) + 3); |
261 RunRequest(MakeContentOfSize(size), &range); | 265 RunRequest(MakeContentOfSize(size), &range); |
262 } | 266 } |
263 | 267 |
264 } // namespace | 268 } // namespace |
265 | 269 |
266 } // namespace net | 270 } // namespace net |
OLD | NEW |