| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/file_util.h" |
| 6 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "chrome/browser/google_apis/auth_service.h" | 12 #include "chrome/browser/google_apis/auth_service.h" |
| 12 #include "chrome/browser/google_apis/drive_api_parser.h" | 13 #include "chrome/browser/google_apis/drive_api_parser.h" |
| 13 #include "chrome/browser/google_apis/drive_api_requests.h" | 14 #include "chrome/browser/google_apis/drive_api_requests.h" |
| 14 #include "chrome/browser/google_apis/drive_api_url_generator.h" | 15 #include "chrome/browser/google_apis/drive_api_url_generator.h" |
| 15 #include "chrome/browser/google_apis/request_sender.h" | 16 #include "chrome/browser/google_apis/request_sender.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 34 const char kTestChildrenResponse[] = | 35 const char kTestChildrenResponse[] = |
| 35 "{\n" | 36 "{\n" |
| 36 "\"kind\": \"drive#childReference\",\n" | 37 "\"kind\": \"drive#childReference\",\n" |
| 37 "\"id\": \"resource_id\",\n" | 38 "\"id\": \"resource_id\",\n" |
| 38 "\"selfLink\": \"self_link\",\n" | 39 "\"selfLink\": \"self_link\",\n" |
| 39 "\"childLink\": \"child_link\",\n" | 40 "\"childLink\": \"child_link\",\n" |
| 40 "}\n"; | 41 "}\n"; |
| 41 | 42 |
| 42 const char kTestUploadExistingFilePath[] = "/upload/existingfile/path"; | 43 const char kTestUploadExistingFilePath[] = "/upload/existingfile/path"; |
| 43 const char kTestUploadNewFilePath[] = "/upload/newfile/path"; | 44 const char kTestUploadNewFilePath[] = "/upload/newfile/path"; |
| 45 const char kTestDownloadPathPrefix[] = "/download/"; |
| 44 | 46 |
| 45 } // namespace | 47 } // namespace |
| 46 | 48 |
| 47 class DriveApiRequestsTest : public testing::Test { | 49 class DriveApiRequestsTest : public testing::Test { |
| 48 public: | 50 public: |
| 49 DriveApiRequestsTest() | 51 DriveApiRequestsTest() |
| 50 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), | 52 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
| 51 test_server_(content::BrowserThread::GetMessageLoopProxyForThread( | 53 test_server_(content::BrowserThread::GetMessageLoopProxyForThread( |
| 52 content::BrowserThread::IO)) { | 54 content::BrowserThread::IO)) { |
| 53 } | 55 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 77 base::Unretained(this))); | 79 base::Unretained(this))); |
| 78 test_server_.RegisterRequestHandler( | 80 test_server_.RegisterRequestHandler( |
| 79 base::Bind(&DriveApiRequestsTest::HandleResumeUploadRequest, | 81 base::Bind(&DriveApiRequestsTest::HandleResumeUploadRequest, |
| 80 base::Unretained(this))); | 82 base::Unretained(this))); |
| 81 test_server_.RegisterRequestHandler( | 83 test_server_.RegisterRequestHandler( |
| 82 base::Bind(&DriveApiRequestsTest::HandleInitiateUploadRequest, | 84 base::Bind(&DriveApiRequestsTest::HandleInitiateUploadRequest, |
| 83 base::Unretained(this))); | 85 base::Unretained(this))); |
| 84 test_server_.RegisterRequestHandler( | 86 test_server_.RegisterRequestHandler( |
| 85 base::Bind(&DriveApiRequestsTest::HandleContentResponse, | 87 base::Bind(&DriveApiRequestsTest::HandleContentResponse, |
| 86 base::Unretained(this))); | 88 base::Unretained(this))); |
| 89 test_server_.RegisterRequestHandler( |
| 90 base::Bind(&DriveApiRequestsTest::HandleDownloadRequest, |
| 91 base::Unretained(this))); |
| 87 | 92 |
| 88 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port()); | 93 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port()); |
| 89 url_generator_.reset(new DriveApiUrlGenerator( | 94 url_generator_.reset(new DriveApiUrlGenerator( |
| 90 test_base_url, test_base_url.Resolve("download/"))); | 95 test_base_url, test_base_url.Resolve(kTestDownloadPathPrefix))); |
| 91 | 96 |
| 92 // Reset the server's expected behavior just in case. | 97 // Reset the server's expected behavior just in case. |
| 93 ResetExpectedResponse(); | 98 ResetExpectedResponse(); |
| 94 received_bytes_ = 0; | 99 received_bytes_ = 0; |
| 95 content_length_ = 0; | 100 content_length_ = 0; |
| 96 } | 101 } |
| 97 | 102 |
| 98 virtual void TearDown() OVERRIDE { | 103 virtual void TearDown() OVERRIDE { |
| 99 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 104 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 100 request_context_getter_ = NULL; | 105 request_context_getter_ = NULL; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 http_request_ = request; | 303 http_request_ = request; |
| 299 | 304 |
| 300 scoped_ptr<net::test_server::BasicHttpResponse> response( | 305 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 301 new net::test_server::BasicHttpResponse); | 306 new net::test_server::BasicHttpResponse); |
| 302 response->set_code(net::HTTP_OK); | 307 response->set_code(net::HTTP_OK); |
| 303 response->set_content_type(expected_content_type_); | 308 response->set_content_type(expected_content_type_); |
| 304 response->set_content(expected_content_); | 309 response->set_content(expected_content_); |
| 305 return response.PassAs<net::test_server::HttpResponse>(); | 310 return response.PassAs<net::test_server::HttpResponse>(); |
| 306 } | 311 } |
| 307 | 312 |
| 313 // Handles a request for downloading a file. |
| 314 scoped_ptr<net::test_server::HttpResponse> HandleDownloadRequest( |
| 315 const net::test_server::HttpRequest& request) { |
| 316 http_request_ = request; |
| 317 |
| 318 const GURL absolute_url = test_server_.GetURL(request.relative_url); |
| 319 std::string id; |
| 320 if (!test_util::RemovePrefix(absolute_url.path(), |
| 321 kTestDownloadPathPrefix, |
| 322 &id)) { |
| 323 return scoped_ptr<net::test_server::HttpResponse>(); |
| 324 } |
| 325 |
| 326 // For testing, returns a text with |id| repeated 3 times. |
| 327 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 328 new net::test_server::BasicHttpResponse); |
| 329 response->set_code(net::HTTP_OK); |
| 330 response->set_content(id + id + id); |
| 331 response->set_content_type("text/plain"); |
| 332 return response.PassAs<net::test_server::HttpResponse>(); |
| 333 } |
| 334 |
| 308 // These are for the current upload file status. | 335 // These are for the current upload file status. |
| 309 int64 received_bytes_; | 336 int64 received_bytes_; |
| 310 int64 content_length_; | 337 int64 content_length_; |
| 311 }; | 338 }; |
| 312 | 339 |
| 313 TEST_F(DriveApiRequestsTest, GetAboutRequest_ValidJson) { | 340 TEST_F(DriveApiRequestsTest, GetAboutRequest_ValidJson) { |
| 314 // Set an expected data file containing valid result. | 341 // Set an expected data file containing valid result. |
| 315 expected_data_file_path_ = test_util::GetTestFilePath( | 342 expected_data_file_path_ = test_util::GetTestFilePath( |
| 316 "drive/about.json"); | 343 "drive/about.json"); |
| 317 | 344 |
| (...skipping 1016 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1334 http_request_.headers["X-Upload-Content-Length"]); | 1361 http_request_.headers["X-Upload-Content-Length"]); |
| 1335 EXPECT_EQ("Conflicting-etag", http_request_.headers["If-Match"]); | 1362 EXPECT_EQ("Conflicting-etag", http_request_.headers["If-Match"]); |
| 1336 | 1363 |
| 1337 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); | 1364 EXPECT_EQ(net::test_server::METHOD_PUT, http_request_.method); |
| 1338 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable", | 1365 EXPECT_EQ("/upload/drive/v2/files/resource_id?uploadType=resumable", |
| 1339 http_request_.relative_url); | 1366 http_request_.relative_url); |
| 1340 EXPECT_TRUE(http_request_.has_content); | 1367 EXPECT_TRUE(http_request_.has_content); |
| 1341 EXPECT_TRUE(http_request_.content.empty()); | 1368 EXPECT_TRUE(http_request_.content.empty()); |
| 1342 } | 1369 } |
| 1343 | 1370 |
| 1371 TEST_F(DriveApiRequestsTest, DownloadFileRequest) { |
| 1372 const base::FilePath kDownloadedFilePath = |
| 1373 temp_dir_.path().AppendASCII("cache_file"); |
| 1374 const std::string kTestId("dummyId"); |
| 1375 |
| 1376 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
| 1377 base::FilePath temp_file; |
| 1378 { |
| 1379 base::RunLoop run_loop; |
| 1380 drive::DownloadFileRequest* request = new drive::DownloadFileRequest( |
| 1381 request_sender_.get(), |
| 1382 *url_generator_, |
| 1383 test_util::CreateQuitCallback( |
| 1384 &run_loop, |
| 1385 test_util::CreateCopyResultCallback(&result_code, &temp_file)), |
| 1386 GetContentCallback(), |
| 1387 ProgressCallback(), |
| 1388 kTestId, |
| 1389 kDownloadedFilePath); |
| 1390 request_sender_->StartRequestWithRetry(request); |
| 1391 run_loop.Run(); |
| 1392 } |
| 1393 |
| 1394 std::string contents; |
| 1395 file_util::ReadFileToString(temp_file, &contents); |
| 1396 base::Delete(temp_file, false); |
| 1397 |
| 1398 EXPECT_EQ(HTTP_SUCCESS, result_code); |
| 1399 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
| 1400 EXPECT_EQ(kTestDownloadPathPrefix + kTestId, http_request_.relative_url); |
| 1401 EXPECT_EQ(kDownloadedFilePath, temp_file); |
| 1402 |
| 1403 const std::string expected_contents = kTestId + kTestId + kTestId; |
| 1404 EXPECT_EQ(expected_contents, contents); |
| 1405 } |
| 1406 |
| 1344 } // namespace google_apis | 1407 } // namespace google_apis |
| OLD | NEW |