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