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