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/fileapi/external_file_url_request_job.h" | 5 #include "chrome/browser/chromeos/fileapi/external_file_url_request_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; | 235 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; |
236 | 236 |
237 scoped_ptr<TestingProfileManager> profile_manager_; | 237 scoped_ptr<TestingProfileManager> profile_manager_; |
238 base::ScopedTempDir drive_cache_dir_; | 238 base::ScopedTempDir drive_cache_dir_; |
239 scoped_refptr<storage::FileSystemContext> file_system_context_; | 239 scoped_refptr<storage::FileSystemContext> file_system_context_; |
240 }; | 240 }; |
241 | 241 |
242 TEST_F(ExternalFileURLRequestJobTest, NonGetMethod) { | 242 TEST_F(ExternalFileURLRequestJobTest, NonGetMethod) { |
243 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 243 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
244 GURL("externalfile:drive-test-user-hash/root/File 1.txt"), | 244 GURL("externalfile:drive-test-user-hash/root/File 1.txt"), |
245 net::DEFAULT_PRIORITY, | 245 net::DEFAULT_PRIORITY, test_delegate_.get())); |
246 test_delegate_.get(), | |
247 NULL)); | |
248 request->set_method("POST"); // Set non "GET" method. | 246 request->set_method("POST"); // Set non "GET" method. |
249 request->Start(); | 247 request->Start(); |
250 | 248 |
251 base::RunLoop().Run(); | 249 base::RunLoop().Run(); |
252 | 250 |
253 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 251 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
254 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request->status().error()); | 252 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request->status().error()); |
255 } | 253 } |
256 | 254 |
257 TEST_F(ExternalFileURLRequestJobTest, RegularFile) { | 255 TEST_F(ExternalFileURLRequestJobTest, RegularFile) { |
258 const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt"); | 256 const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt"); |
259 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 257 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
260 | 258 |
261 // For the first time, the file should be fetched from the server. | 259 // For the first time, the file should be fetched from the server. |
262 { | 260 { |
263 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 261 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
264 kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get(), NULL)); | 262 kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get())); |
265 request->Start(); | 263 request->Start(); |
266 | 264 |
267 base::RunLoop().Run(); | 265 base::RunLoop().Run(); |
268 | 266 |
269 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 267 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
270 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" | 268 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" |
271 // on the server. | 269 // on the server. |
272 std::string mime_type; | 270 std::string mime_type; |
273 request->GetMimeType(&mime_type); | 271 request->GetMimeType(&mime_type); |
274 EXPECT_EQ("audio/mpeg", mime_type); | 272 EXPECT_EQ("audio/mpeg", mime_type); |
275 | 273 |
276 // Reading file must be done after |request| runs, otherwise | 274 // Reading file must be done after |request| runs, otherwise |
277 // it'll create a local cache file, and we cannot test correctly. | 275 // it'll create a local cache file, and we cannot test correctly. |
278 std::string expected_data; | 276 std::string expected_data; |
279 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 277 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
280 EXPECT_EQ(expected_data, test_delegate_->data_received()); | 278 EXPECT_EQ(expected_data, test_delegate_->data_received()); |
281 } | 279 } |
282 | 280 |
283 // For the second time, the locally cached file should be used. | 281 // For the second time, the locally cached file should be used. |
284 // The caching emulation is done by FakeFileSystem. | 282 // The caching emulation is done by FakeFileSystem. |
285 { | 283 { |
286 test_delegate_.reset(new TestDelegate); | 284 test_delegate_.reset(new TestDelegate); |
287 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 285 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
288 GURL("externalfile:drive-test-user-hash/root/File 1.txt"), | 286 GURL("externalfile:drive-test-user-hash/root/File 1.txt"), |
289 net::DEFAULT_PRIORITY, | 287 net::DEFAULT_PRIORITY, test_delegate_.get())); |
290 test_delegate_.get(), | |
291 NULL)); | |
292 request->Start(); | 288 request->Start(); |
293 | 289 |
294 base::RunLoop().Run(); | 290 base::RunLoop().Run(); |
295 | 291 |
296 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 292 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
297 std::string mime_type; | 293 std::string mime_type; |
298 request->GetMimeType(&mime_type); | 294 request->GetMimeType(&mime_type); |
299 EXPECT_EQ("audio/mpeg", mime_type); | 295 EXPECT_EQ("audio/mpeg", mime_type); |
300 | 296 |
301 std::string expected_data; | 297 std::string expected_data; |
302 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 298 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
303 EXPECT_EQ(expected_data, test_delegate_->data_received()); | 299 EXPECT_EQ(expected_data, test_delegate_->data_received()); |
304 } | 300 } |
305 } | 301 } |
306 | 302 |
307 TEST_F(ExternalFileURLRequestJobTest, HostedDocument) { | 303 TEST_F(ExternalFileURLRequestJobTest, HostedDocument) { |
308 // Open a gdoc file. | 304 // Open a gdoc file. |
309 test_delegate_->set_quit_on_redirect(true); | 305 test_delegate_->set_quit_on_redirect(true); |
310 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 306 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
311 GURL( | 307 GURL( |
312 "externalfile:drive-test-user-hash/root/Document 1 " | 308 "externalfile:drive-test-user-hash/root/Document 1 " |
313 "excludeDir-test.gdoc"), | 309 "excludeDir-test.gdoc"), |
314 net::DEFAULT_PRIORITY, | 310 net::DEFAULT_PRIORITY, test_delegate_.get())); |
315 test_delegate_.get(), | |
316 NULL)); | |
317 request->Start(); | 311 request->Start(); |
318 | 312 |
319 base::RunLoop().Run(); | 313 base::RunLoop().Run(); |
320 | 314 |
321 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 315 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
322 // Make sure that a hosted document triggers redirection. | 316 // Make sure that a hosted document triggers redirection. |
323 EXPECT_TRUE(request->is_redirecting()); | 317 EXPECT_TRUE(request->is_redirecting()); |
324 EXPECT_TRUE(test_delegate_->redirect_url().is_valid()); | 318 EXPECT_TRUE(test_delegate_->redirect_url().is_valid()); |
325 } | 319 } |
326 | 320 |
327 TEST_F(ExternalFileURLRequestJobTest, RootDirectory) { | 321 TEST_F(ExternalFileURLRequestJobTest, RootDirectory) { |
328 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 322 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
329 GURL("externalfile:drive-test-user-hash/root"), | 323 GURL("externalfile:drive-test-user-hash/root"), net::DEFAULT_PRIORITY, |
330 net::DEFAULT_PRIORITY, | 324 test_delegate_.get())); |
331 test_delegate_.get(), | |
332 NULL)); | |
333 request->Start(); | 325 request->Start(); |
334 | 326 |
335 base::RunLoop().Run(); | 327 base::RunLoop().Run(); |
336 | 328 |
337 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 329 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
338 EXPECT_EQ(net::ERR_FAILED, request->status().error()); | 330 EXPECT_EQ(net::ERR_FAILED, request->status().error()); |
339 } | 331 } |
340 | 332 |
341 TEST_F(ExternalFileURLRequestJobTest, Directory) { | 333 TEST_F(ExternalFileURLRequestJobTest, Directory) { |
342 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 334 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
343 GURL("externalfile:drive-test-user-hash/root/Directory 1"), | 335 GURL("externalfile:drive-test-user-hash/root/Directory 1"), |
344 net::DEFAULT_PRIORITY, | 336 net::DEFAULT_PRIORITY, test_delegate_.get())); |
345 test_delegate_.get(), | |
346 NULL)); | |
347 request->Start(); | 337 request->Start(); |
348 | 338 |
349 base::RunLoop().Run(); | 339 base::RunLoop().Run(); |
350 | 340 |
351 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 341 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
352 EXPECT_EQ(net::ERR_FAILED, request->status().error()); | 342 EXPECT_EQ(net::ERR_FAILED, request->status().error()); |
353 } | 343 } |
354 | 344 |
355 TEST_F(ExternalFileURLRequestJobTest, NonExistingFile) { | 345 TEST_F(ExternalFileURLRequestJobTest, NonExistingFile) { |
356 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 346 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
357 GURL("externalfile:drive-test-user-hash/root/non-existing-file.txt"), | 347 GURL("externalfile:drive-test-user-hash/root/non-existing-file.txt"), |
358 net::DEFAULT_PRIORITY, | 348 net::DEFAULT_PRIORITY, test_delegate_.get())); |
359 test_delegate_.get(), | |
360 NULL)); | |
361 request->Start(); | 349 request->Start(); |
362 | 350 |
363 base::RunLoop().Run(); | 351 base::RunLoop().Run(); |
364 | 352 |
365 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 353 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
366 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()); | 354 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()); |
367 } | 355 } |
368 | 356 |
369 TEST_F(ExternalFileURLRequestJobTest, WrongFormat) { | 357 TEST_F(ExternalFileURLRequestJobTest, WrongFormat) { |
370 scoped_ptr<net::URLRequest> request( | 358 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
371 url_request_context_->CreateRequest(GURL("externalfile:"), | 359 GURL("externalfile:"), net::DEFAULT_PRIORITY, test_delegate_.get())); |
372 net::DEFAULT_PRIORITY, | |
373 test_delegate_.get(), | |
374 NULL)); | |
375 request->Start(); | 360 request->Start(); |
376 | 361 |
377 base::RunLoop().Run(); | 362 base::RunLoop().Run(); |
378 | 363 |
379 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 364 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
380 EXPECT_EQ(net::ERR_INVALID_URL, request->status().error()); | 365 EXPECT_EQ(net::ERR_INVALID_URL, request->status().error()); |
381 } | 366 } |
382 | 367 |
383 TEST_F(ExternalFileURLRequestJobTest, Cancel) { | 368 TEST_F(ExternalFileURLRequestJobTest, Cancel) { |
384 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 369 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
385 GURL("externalfile:drive-test-user-hash/root/File 1.txt"), | 370 GURL("externalfile:drive-test-user-hash/root/File 1.txt"), |
386 net::DEFAULT_PRIORITY, | 371 net::DEFAULT_PRIORITY, test_delegate_.get())); |
387 test_delegate_.get(), | |
388 NULL)); | |
389 | 372 |
390 // Start the request, and cancel it immediately after it. | 373 // Start the request, and cancel it immediately after it. |
391 request->Start(); | 374 request->Start(); |
392 request->Cancel(); | 375 request->Cancel(); |
393 | 376 |
394 base::RunLoop().Run(); | 377 base::RunLoop().Run(); |
395 | 378 |
396 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status()); | 379 EXPECT_EQ(net::URLRequestStatus::CANCELED, request->status().status()); |
397 } | 380 } |
398 | 381 |
399 TEST_F(ExternalFileURLRequestJobTest, RangeHeader) { | 382 TEST_F(ExternalFileURLRequestJobTest, RangeHeader) { |
400 const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt"); | 383 const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt"); |
401 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 384 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
402 | 385 |
403 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 386 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
404 kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get(), NULL)); | 387 kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get())); |
405 | 388 |
406 // Set range header. | 389 // Set range header. |
407 request->SetExtraRequestHeaderByName( | 390 request->SetExtraRequestHeaderByName( |
408 "Range", "bytes=3-5", false /* overwrite */); | 391 "Range", "bytes=3-5", false /* overwrite */); |
409 request->Start(); | 392 request->Start(); |
410 | 393 |
411 base::RunLoop().Run(); | 394 base::RunLoop().Run(); |
412 | 395 |
413 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 396 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
414 | 397 |
415 // Reading file must be done after |request| runs, otherwise | 398 // Reading file must be done after |request| runs, otherwise |
416 // it'll create a local cache file, and we cannot test correctly. | 399 // it'll create a local cache file, and we cannot test correctly. |
417 std::string expected_data; | 400 std::string expected_data; |
418 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 401 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
419 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); | 402 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); |
420 } | 403 } |
421 | 404 |
422 TEST_F(ExternalFileURLRequestJobTest, WrongRangeHeader) { | 405 TEST_F(ExternalFileURLRequestJobTest, WrongRangeHeader) { |
423 const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt"); | 406 const GURL kTestUrl("externalfile:drive-test-user-hash/root/File 1.txt"); |
424 | 407 |
425 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( | 408 scoped_ptr<net::URLRequest> request(url_request_context_->CreateRequest( |
426 kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get(), NULL)); | 409 kTestUrl, net::DEFAULT_PRIORITY, test_delegate_.get())); |
427 | 410 |
428 // Set range header. | 411 // Set range header. |
429 request->SetExtraRequestHeaderByName( | 412 request->SetExtraRequestHeaderByName( |
430 "Range", "Wrong Range Header Value", false /* overwrite */); | 413 "Range", "Wrong Range Header Value", false /* overwrite */); |
431 request->Start(); | 414 request->Start(); |
432 | 415 |
433 base::RunLoop().Run(); | 416 base::RunLoop().Run(); |
434 | 417 |
435 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 418 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
436 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request->status().error()); | 419 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request->status().error()); |
437 } | 420 } |
438 | 421 |
439 } // namespace chromeos | 422 } // namespace chromeos |
OLD | NEW |