OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/drive/drive_url_request_job.h" | 5 #include "chrome/browser/chromeos/drive/drive_url_request_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.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" |
11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
12 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" | 14 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
15 #include "chrome/browser/chromeos/drive/fake_file_system.h" | 15 #include "chrome/browser/chromeos/drive/fake_file_system.h" |
16 #include "chrome/browser/chromeos/drive/file_system_util.h" | 16 #include "chrome/browser/chromeos/drive/file_system_util.h" |
17 #include "chrome/browser/chromeos/drive/test_util.h" | 17 #include "chrome/browser/chromeos/drive/test_util.h" |
18 #include "chrome/browser/drive/fake_drive_service.h" | 18 #include "chrome/browser/drive/fake_drive_service.h" |
19 #include "chrome/browser/drive/test_util.h" | 19 #include "chrome/browser/drive/test_util.h" |
20 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/test/test_browser_thread_bundle.h" | 22 #include "content/public/test/test_browser_thread_bundle.h" |
23 #include "google_apis/drive/test_util.h" | 23 #include "google_apis/drive/test_util.h" |
24 #include "net/base/request_priority.h" | 24 #include "net/base/request_priority.h" |
25 #include "net/base/test_completion_callback.h" | 25 #include "net/base/test_completion_callback.h" |
26 #include "net/http/http_byte_range.h" | 26 #include "net/http/http_byte_range.h" |
27 #include "net/url_request/redirect_info.h" | 27 #include "net/url_request/redirect_info.h" |
| 28 #include "net/url_request/url_request.h" |
| 29 #include "net/url_request/url_request_context.h" |
28 #include "net/url_request/url_request_test_util.h" | 30 #include "net/url_request/url_request_test_util.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
30 #include "url/gurl.h" | 32 #include "url/gurl.h" |
31 | 33 |
32 namespace drive { | 34 namespace drive { |
33 namespace { | 35 namespace { |
34 | 36 |
35 // A simple URLRequestJobFactory implementation to create DriveURLRequestJob. | 37 // A simple URLRequestJobFactory implementation to create DriveURLRequestJob. |
36 class TestURLRequestJobFactory : public net::URLRequestJobFactory { | 38 class TestURLRequestJobFactory : public net::URLRequestJobFactory { |
37 public: | 39 public: |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 scoped_ptr<FakeDriveService> fake_drive_service_; | 179 scoped_ptr<FakeDriveService> fake_drive_service_; |
178 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | 180 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; |
179 | 181 |
180 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; | 182 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; |
181 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; | 183 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; |
182 scoped_ptr<net::URLRequestContext> url_request_context_; | 184 scoped_ptr<net::URLRequestContext> url_request_context_; |
183 scoped_ptr<TestDelegate> test_delegate_; | 185 scoped_ptr<TestDelegate> test_delegate_; |
184 }; | 186 }; |
185 | 187 |
186 TEST_F(DriveURLRequestJobTest, NonGetMethod) { | 188 TEST_F(DriveURLRequestJobTest, NonGetMethod) { |
187 net::URLRequest request(GURL("drive:drive/root/File 1.txt"), | 189 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
188 net::DEFAULT_PRIORITY, | 190 GURL("drive:drive/root/File 1.txt"), |
189 test_delegate_.get(), | 191 net::DEFAULT_PRIORITY, |
190 url_request_context_.get()); | 192 test_delegate_.get(), |
191 request.set_method("POST"); // Set non "GET" method. | 193 NULL)); |
192 request.Start(); | 194 request->set_method("POST"); // Set non "GET" method. |
| 195 request->Start(); |
193 | 196 |
194 base::RunLoop().Run(); | 197 base::RunLoop().Run(); |
195 | 198 |
196 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 199 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
197 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error()); | 200 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request->status().error()); |
198 } | 201 } |
199 | 202 |
200 TEST_F(DriveURLRequestJobTest, RegularFile) { | 203 TEST_F(DriveURLRequestJobTest, RegularFile) { |
201 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 204 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
202 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 205 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
203 | 206 |
204 // For the first time, the file should be fetched from the server. | 207 // For the first time, the file should be fetched from the server. |
205 { | 208 { |
206 net::URLRequest request(kTestUrl, | 209 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
207 net::DEFAULT_PRIORITY, | 210 kTestUrl, |
208 test_delegate_.get(), | 211 net::DEFAULT_PRIORITY, |
209 url_request_context_.get()); | 212 test_delegate_.get(), |
210 request.Start(); | 213 NULL)); |
| 214 request->Start(); |
211 | 215 |
212 base::RunLoop().Run(); | 216 base::RunLoop().Run(); |
213 | 217 |
214 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 218 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
215 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" | 219 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" |
216 // on the server. | 220 // on the server. |
217 std::string mime_type; | 221 std::string mime_type; |
218 request.GetMimeType(&mime_type); | 222 request->GetMimeType(&mime_type); |
219 EXPECT_EQ("audio/mpeg", mime_type); | 223 EXPECT_EQ("audio/mpeg", mime_type); |
220 | 224 |
221 // Reading file must be done after |request| runs, otherwise | 225 // Reading file must be done after |request| runs, otherwise |
222 // it'll create a local cache file, and we cannot test correctly. | 226 // it'll create a local cache file, and we cannot test correctly. |
223 std::string expected_data; | 227 std::string expected_data; |
224 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 228 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
225 EXPECT_EQ(expected_data, test_delegate_->data_received()); | 229 EXPECT_EQ(expected_data, test_delegate_->data_received()); |
226 } | 230 } |
227 | 231 |
228 // For the second time, the locally cached file should be used. | 232 // For the second time, the locally cached file should be used. |
229 // The caching emulation is done by FakeFileSystem. | 233 // The caching emulation is done by FakeFileSystem. |
230 { | 234 { |
231 test_delegate_.reset(new TestDelegate); | 235 test_delegate_.reset(new TestDelegate); |
232 net::URLRequest request(GURL("drive:drive/root/File 1.txt"), | 236 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
233 net::DEFAULT_PRIORITY, | 237 GURL("drive:drive/root/File 1.txt"), |
234 test_delegate_.get(), | 238 net::DEFAULT_PRIORITY, |
235 url_request_context_.get()); | 239 test_delegate_.get(), |
236 request.Start(); | 240 NULL)); |
| 241 request->Start(); |
237 | 242 |
238 base::RunLoop().Run(); | 243 base::RunLoop().Run(); |
239 | 244 |
240 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 245 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
241 std::string mime_type; | 246 std::string mime_type; |
242 request.GetMimeType(&mime_type); | 247 request->GetMimeType(&mime_type); |
243 EXPECT_EQ("audio/mpeg", mime_type); | 248 EXPECT_EQ("audio/mpeg", mime_type); |
244 | 249 |
245 std::string expected_data; | 250 std::string expected_data; |
246 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 251 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
247 EXPECT_EQ(expected_data, test_delegate_->data_received()); | 252 EXPECT_EQ(expected_data, test_delegate_->data_received()); |
248 } | 253 } |
249 } | 254 } |
250 | 255 |
251 TEST_F(DriveURLRequestJobTest, HostedDocument) { | 256 TEST_F(DriveURLRequestJobTest, HostedDocument) { |
252 // Open a gdoc file. | 257 // Open a gdoc file. |
253 test_delegate_->set_quit_on_redirect(true); | 258 test_delegate_->set_quit_on_redirect(true); |
254 net::URLRequest request( | 259 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
255 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), | 260 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), |
256 net::DEFAULT_PRIORITY, | 261 net::DEFAULT_PRIORITY, |
257 test_delegate_.get(), | 262 test_delegate_.get(), |
258 url_request_context_.get()); | 263 NULL)); |
259 request.Start(); | 264 request->Start(); |
260 | 265 |
261 base::RunLoop().Run(); | 266 base::RunLoop().Run(); |
262 | 267 |
263 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 268 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
264 // Make sure that a hosted document triggers redirection. | 269 // Make sure that a hosted document triggers redirection. |
265 EXPECT_TRUE(request.is_redirecting()); | 270 EXPECT_TRUE(request->is_redirecting()); |
266 EXPECT_TRUE(test_delegate_->redirect_url().is_valid()); | 271 EXPECT_TRUE(test_delegate_->redirect_url().is_valid()); |
267 } | 272 } |
268 | 273 |
269 TEST_F(DriveURLRequestJobTest, RootDirectory) { | 274 TEST_F(DriveURLRequestJobTest, RootDirectory) { |
270 net::URLRequest request(GURL("drive:drive/root"), | 275 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
271 net::DEFAULT_PRIORITY, | 276 GURL("drive:drive/root"), |
272 test_delegate_.get(), | 277 net::DEFAULT_PRIORITY, |
273 url_request_context_.get()); | 278 test_delegate_.get(), |
274 request.Start(); | 279 NULL)); |
| 280 request->Start(); |
275 | 281 |
276 base::RunLoop().Run(); | 282 base::RunLoop().Run(); |
277 | 283 |
278 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 284 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
279 EXPECT_EQ(net::ERR_FAILED, request.status().error()); | 285 EXPECT_EQ(net::ERR_FAILED, request->status().error()); |
280 } | 286 } |
281 | 287 |
282 TEST_F(DriveURLRequestJobTest, Directory) { | 288 TEST_F(DriveURLRequestJobTest, Directory) { |
283 net::URLRequest request(GURL("drive:drive/root/Directory 1"), | 289 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
284 net::DEFAULT_PRIORITY, | 290 GURL("drive:drive/root/Directory 1"), |
285 test_delegate_.get(), | 291 net::DEFAULT_PRIORITY, |
286 url_request_context_.get()); | 292 test_delegate_.get(), |
287 request.Start(); | 293 NULL)); |
| 294 request->Start(); |
288 | 295 |
289 base::RunLoop().Run(); | 296 base::RunLoop().Run(); |
290 | 297 |
291 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 298 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
292 EXPECT_EQ(net::ERR_FAILED, request.status().error()); | 299 EXPECT_EQ(net::ERR_FAILED, request->status().error()); |
293 } | 300 } |
294 | 301 |
295 TEST_F(DriveURLRequestJobTest, NonExistingFile) { | 302 TEST_F(DriveURLRequestJobTest, NonExistingFile) { |
296 net::URLRequest request(GURL("drive:drive/root/non-existing-file.txt"), | 303 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
297 net::DEFAULT_PRIORITY, | 304 GURL("drive:drive/root/non-existing-file.txt"), |
298 test_delegate_.get(), | 305 net::DEFAULT_PRIORITY, |
299 url_request_context_.get()); | 306 test_delegate_.get(), |
300 request.Start(); | 307 NULL)); |
| 308 request->Start(); |
301 | 309 |
302 base::RunLoop().Run(); | 310 base::RunLoop().Run(); |
303 | 311 |
304 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 312 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
305 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()); | 313 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()); |
306 } | 314 } |
307 | 315 |
308 TEST_F(DriveURLRequestJobTest, WrongFormat) { | 316 TEST_F(DriveURLRequestJobTest, WrongFormat) { |
309 net::URLRequest request(GURL("drive:"), | 317 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
310 net::DEFAULT_PRIORITY, | 318 GURL("drive:"), |
311 test_delegate_.get(), | 319 net::DEFAULT_PRIORITY, |
312 url_request_context_.get()); | 320 test_delegate_.get(), |
313 request.Start(); | 321 NULL)); |
| 322 request->Start(); |
314 | 323 |
315 base::RunLoop().Run(); | 324 base::RunLoop().Run(); |
316 | 325 |
317 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 326 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
318 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error()); | 327 EXPECT_EQ(net::ERR_INVALID_URL, request->status().error()); |
319 } | 328 } |
320 | 329 |
321 TEST_F(DriveURLRequestJobTest, Cancel) { | 330 TEST_F(DriveURLRequestJobTest, Cancel) { |
322 net::URLRequest request(GURL("drive:drive/root/File 1.txt"), | 331 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
323 net::DEFAULT_PRIORITY, | 332 GURL("drive:drive/root/File 1.txt"), |
324 test_delegate_.get(), | 333 net::DEFAULT_PRIORITY, |
325 url_request_context_.get()); | 334 test_delegate_.get(), |
| 335 NULL)); |
326 | 336 |
327 // Start the request, and cancel it immediately after it. | 337 // Start the request, and cancel it immediately after it. |
328 request.Start(); | 338 request->Start(); |
329 request.Cancel(); | 339 request->Cancel(); |
330 | 340 |
331 base::RunLoop().Run(); | 341 base::RunLoop().Run(); |
332 | 342 |
333 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); | 343 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status()); |
334 } | 344 } |
335 | 345 |
336 TEST_F(DriveURLRequestJobTest, RangeHeader) { | 346 TEST_F(DriveURLRequestJobTest, RangeHeader) { |
337 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 347 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
338 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 348 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
339 | 349 |
340 net::URLRequest request(kTestUrl, | 350 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
341 net::DEFAULT_PRIORITY, | 351 kTestUrl, |
342 test_delegate_.get(), | 352 net::DEFAULT_PRIORITY, |
343 url_request_context_.get()); | 353 test_delegate_.get(), |
| 354 NULL)); |
344 | 355 |
345 // Set range header. | 356 // Set range header. |
346 request.SetExtraRequestHeaderByName( | 357 request->SetExtraRequestHeaderByName( |
347 "Range", "bytes=3-5", false /* overwrite */); | 358 "Range", "bytes=3-5", false /* overwrite */); |
348 request.Start(); | 359 request->Start(); |
349 | 360 |
350 base::RunLoop().Run(); | 361 base::RunLoop().Run(); |
351 | 362 |
352 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 363 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
353 | 364 |
354 // Reading file must be done after |request| runs, otherwise | 365 // Reading file must be done after |request| runs, otherwise |
355 // it'll create a local cache file, and we cannot test correctly. | 366 // it'll create a local cache file, and we cannot test correctly. |
356 std::string expected_data; | 367 std::string expected_data; |
357 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 368 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
358 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); | 369 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); |
359 } | 370 } |
360 | 371 |
361 TEST_F(DriveURLRequestJobTest, WrongRangeHeader) { | 372 TEST_F(DriveURLRequestJobTest, WrongRangeHeader) { |
362 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 373 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
363 | 374 |
364 net::URLRequest request(kTestUrl, | 375 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
365 net::DEFAULT_PRIORITY, | 376 kTestUrl, |
366 test_delegate_.get(), | 377 net::DEFAULT_PRIORITY, |
367 url_request_context_.get()); | 378 test_delegate_.get(), |
| 379 NULL)); |
368 | 380 |
369 // Set range header. | 381 // Set range header. |
370 request.SetExtraRequestHeaderByName( | 382 request->SetExtraRequestHeaderByName( |
371 "Range", "Wrong Range Header Value", false /* overwrite */); | 383 "Range", "Wrong Range Header Value", false /* overwrite */); |
372 request.Start(); | 384 request->Start(); |
373 | 385 |
374 base::RunLoop().Run(); | 386 base::RunLoop().Run(); |
375 | 387 |
376 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 388 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
377 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request.status().error()); | 389 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request->status().error()); |
378 } | 390 } |
379 | 391 |
380 } // namespace drive | 392 } // namespace drive |
OLD | NEW |