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