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