OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
6 #include <map> | 6 #include <map> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
11 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
12 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "chrome/browser/google_apis/auth_service.h" | 18 #include "chrome/browser/google_apis/auth_service.h" |
18 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | 19 #include "chrome/browser/google_apis/gdata_wapi_parser.h" |
(...skipping 11 matching lines...) Expand all Loading... |
30 #include "net/url_request/url_request_test_util.h" | 31 #include "net/url_request/url_request_test_util.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
32 | 33 |
33 namespace google_apis { | 34 namespace google_apis { |
34 | 35 |
35 namespace { | 36 namespace { |
36 | 37 |
37 const char kTestGDataAuthToken[] = "testtoken"; | 38 const char kTestGDataAuthToken[] = "testtoken"; |
38 const char kTestUserAgent[] = "test-user-agent"; | 39 const char kTestUserAgent[] = "test-user-agent"; |
39 const char kTestETag[] = "test_etag"; | 40 const char kTestETag[] = "test_etag"; |
| 41 const char kTestDownloadPathPrefix[] = "/download/"; |
40 | 42 |
41 class GDataWapiRequestsTest : public testing::Test { | 43 class GDataWapiRequestsTest : public testing::Test { |
42 public: | 44 public: |
43 GDataWapiRequestsTest() | 45 GDataWapiRequestsTest() |
44 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), | 46 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
45 test_server_(content::BrowserThread::GetMessageLoopProxyForThread( | 47 test_server_(content::BrowserThread::GetMessageLoopProxyForThread( |
46 content::BrowserThread::IO)) { | 48 content::BrowserThread::IO)) { |
47 } | 49 } |
48 | 50 |
49 virtual void SetUp() OVERRIDE { | 51 virtual void SetUp() OVERRIDE { |
(...skipping 22 matching lines...) Expand all Loading... |
72 base::Unretained(this))); | 74 base::Unretained(this))); |
73 test_server_.RegisterRequestHandler( | 75 test_server_.RegisterRequestHandler( |
74 base::Bind(&GDataWapiRequestsTest::HandleMetadataRequest, | 76 base::Bind(&GDataWapiRequestsTest::HandleMetadataRequest, |
75 base::Unretained(this))); | 77 base::Unretained(this))); |
76 test_server_.RegisterRequestHandler( | 78 test_server_.RegisterRequestHandler( |
77 base::Bind(&GDataWapiRequestsTest::HandleCreateSessionRequest, | 79 base::Bind(&GDataWapiRequestsTest::HandleCreateSessionRequest, |
78 base::Unretained(this))); | 80 base::Unretained(this))); |
79 test_server_.RegisterRequestHandler( | 81 test_server_.RegisterRequestHandler( |
80 base::Bind(&GDataWapiRequestsTest::HandleUploadRequest, | 82 base::Bind(&GDataWapiRequestsTest::HandleUploadRequest, |
81 base::Unretained(this))); | 83 base::Unretained(this))); |
| 84 test_server_.RegisterRequestHandler( |
| 85 base::Bind(&GDataWapiRequestsTest::HandleDownloadRequest, |
| 86 base::Unretained(this))); |
82 | 87 |
83 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port()); | 88 GURL test_base_url = test_util::GetBaseUrlForTesting(test_server_.port()); |
84 url_generator_.reset(new GDataWapiUrlGenerator( | 89 url_generator_.reset(new GDataWapiUrlGenerator( |
85 test_base_url, test_base_url.Resolve("download/"))); | 90 test_base_url, test_base_url.Resolve(kTestDownloadPathPrefix))); |
86 | 91 |
87 received_bytes_ = 0; | 92 received_bytes_ = 0; |
88 content_length_ = 0; | 93 content_length_ = 0; |
89 } | 94 } |
90 | 95 |
91 virtual void TearDown() OVERRIDE { | 96 virtual void TearDown() OVERRIDE { |
92 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 97 EXPECT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
93 request_context_getter_ = NULL; | 98 request_context_getter_ = NULL; |
94 } | 99 } |
95 | 100 |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 "bytes=0-" + base::Int64ToString(received_bytes_ - 1)); | 310 "bytes=0-" + base::Int64ToString(received_bytes_ - 1)); |
306 } | 311 } |
307 | 312 |
308 // Change the code to RESUME_INCOMPLETE if upload is not complete. | 313 // Change the code to RESUME_INCOMPLETE if upload is not complete. |
309 if (received_bytes_ < content_length_) | 314 if (received_bytes_ < content_length_) |
310 response->set_code(static_cast<net::HttpStatusCode>(308)); | 315 response->set_code(static_cast<net::HttpStatusCode>(308)); |
311 | 316 |
312 return response.PassAs<net::test_server::HttpResponse>(); | 317 return response.PassAs<net::test_server::HttpResponse>(); |
313 } | 318 } |
314 | 319 |
| 320 // Handles a request for downloading a file. |
| 321 scoped_ptr<net::test_server::HttpResponse> HandleDownloadRequest( |
| 322 const net::test_server::HttpRequest& request) { |
| 323 http_request_ = request; |
| 324 |
| 325 const GURL absolute_url = test_server_.GetURL(request.relative_url); |
| 326 std::string id; |
| 327 if (!test_util::RemovePrefix(absolute_url.path(), |
| 328 kTestDownloadPathPrefix, |
| 329 &id)) { |
| 330 return scoped_ptr<net::test_server::HttpResponse>(); |
| 331 } |
| 332 |
| 333 // For testing, returns a text with |id| repeated 3 times. |
| 334 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 335 new net::test_server::BasicHttpResponse); |
| 336 response->set_code(net::HTTP_OK); |
| 337 response->set_content(id + id + id); |
| 338 response->set_content_type("text/plain"); |
| 339 return response.PassAs<net::test_server::HttpResponse>(); |
| 340 } |
| 341 |
315 content::TestBrowserThreadBundle thread_bundle_; | 342 content::TestBrowserThreadBundle thread_bundle_; |
316 net::test_server::EmbeddedTestServer test_server_; | 343 net::test_server::EmbeddedTestServer test_server_; |
317 scoped_ptr<TestingProfile> profile_; | 344 scoped_ptr<TestingProfile> profile_; |
318 scoped_ptr<RequestSender> request_sender_; | 345 scoped_ptr<RequestSender> request_sender_; |
319 scoped_ptr<GDataWapiUrlGenerator> url_generator_; | 346 scoped_ptr<GDataWapiUrlGenerator> url_generator_; |
320 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 347 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
321 base::ScopedTempDir temp_dir_; | 348 base::ScopedTempDir temp_dir_; |
322 | 349 |
323 // These fields are used to keep the current upload state during a | 350 // These fields are used to keep the current upload state during a |
324 // test case. These values are updated by the request from | 351 // test case. These values are updated by the request from |
(...skipping 1210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1535 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]); | 1562 EXPECT_EQ("text/plain", http_request_.headers["X-Upload-Content-Type"]); |
1536 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), | 1563 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), |
1537 http_request_.headers["X-Upload-Content-Length"]); | 1564 http_request_.headers["X-Upload-Content-Length"]); |
1538 // For updating an existing file, an empty body should be attached (PUT | 1565 // For updating an existing file, an empty body should be attached (PUT |
1539 // requires a body) | 1566 // requires a body) |
1540 EXPECT_TRUE(http_request_.has_content); | 1567 EXPECT_TRUE(http_request_.has_content); |
1541 EXPECT_EQ("", http_request_.content); | 1568 EXPECT_EQ("", http_request_.content); |
1542 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); | 1569 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); |
1543 } | 1570 } |
1544 | 1571 |
| 1572 TEST_F(GDataWapiRequestsTest, DownloadFileRequest) { |
| 1573 const base::FilePath kDownloadedFilePath = |
| 1574 temp_dir_.path().AppendASCII("cache_file"); |
| 1575 const std::string kTestIdWithTypeLabel("file:dummyId"); |
| 1576 const std::string kTestId("dummyId"); |
| 1577 |
| 1578 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
| 1579 base::FilePath temp_file; |
| 1580 { |
| 1581 base::RunLoop run_loop; |
| 1582 DownloadFileRequest* request = new DownloadFileRequest( |
| 1583 request_sender_.get(), |
| 1584 *url_generator_, |
| 1585 test_util::CreateQuitCallback( |
| 1586 &run_loop, |
| 1587 test_util::CreateCopyResultCallback(&result_code, &temp_file)), |
| 1588 GetContentCallback(), |
| 1589 ProgressCallback(), |
| 1590 kTestIdWithTypeLabel, |
| 1591 kDownloadedFilePath); |
| 1592 request_sender_->StartRequestWithRetry(request); |
| 1593 run_loop.Run(); |
| 1594 } |
| 1595 |
| 1596 std::string contents; |
| 1597 file_util::ReadFileToString(temp_file, &contents); |
| 1598 base::Delete(temp_file, false); |
| 1599 |
| 1600 EXPECT_EQ(HTTP_SUCCESS, result_code); |
| 1601 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
| 1602 EXPECT_EQ(kTestDownloadPathPrefix + kTestId, http_request_.relative_url); |
| 1603 EXPECT_EQ(kDownloadedFilePath, temp_file); |
| 1604 |
| 1605 const std::string expected_contents = kTestId + kTestId + kTestId; |
| 1606 EXPECT_EQ(expected_contents, contents); |
| 1607 } |
| 1608 |
1545 } // namespace google_apis | 1609 } // namespace google_apis |
OLD | NEW |