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 "chrome/browser/google_apis/base_requests.h" | 5 #include "chrome/browser/google_apis/base_requests.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop.h" | 10 #include "base/run_loop.h" |
11 #include "base/values.h" | |
12 #include "chrome/browser/google_apis/auth_service.h" | 11 #include "chrome/browser/google_apis/auth_service.h" |
13 #include "chrome/browser/google_apis/request_sender.h" | 12 #include "chrome/browser/google_apis/request_sender.h" |
14 #include "chrome/browser/google_apis/task_util.h" | 13 #include "chrome/browser/google_apis/task_util.h" |
15 #include "chrome/browser/google_apis/test_util.h" | 14 #include "chrome/browser/google_apis/test_util.h" |
16 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
17 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
19 #include "net/test/embedded_test_server/embedded_test_server.h" | 18 #include "net/test/embedded_test_server/embedded_test_server.h" |
20 #include "net/test/embedded_test_server/http_request.h" | 19 #include "net/test/embedded_test_server/http_request.h" |
21 #include "net/test/embedded_test_server/http_response.h" | 20 #include "net/test/embedded_test_server/http_response.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 77 |
79 // The incoming HTTP request is saved so tests can verify the request | 78 // The incoming HTTP request is saved so tests can verify the request |
80 // parameters like HTTP method (ex. some requests should use DELETE | 79 // parameters like HTTP method (ex. some requests should use DELETE |
81 // instead of GET). | 80 // instead of GET). |
82 net::test_server::HttpRequest http_request_; | 81 net::test_server::HttpRequest http_request_; |
83 }; | 82 }; |
84 | 83 |
85 TEST_F(BaseRequestsServerTest, DownloadFileRequest_ValidFile) { | 84 TEST_F(BaseRequestsServerTest, DownloadFileRequest_ValidFile) { |
86 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 85 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
87 base::FilePath temp_file; | 86 base::FilePath temp_file; |
88 DownloadFileRequest* request = new DownloadFileRequest( | 87 { |
89 request_sender_.get(), | 88 base::RunLoop run_loop; |
90 request_context_getter_.get(), | 89 DownloadFileRequest* request = new DownloadFileRequest( |
91 CreateComposedCallback( | 90 request_sender_.get(), |
92 base::Bind(&test_util::RunAndQuit), | 91 request_context_getter_.get(), |
93 test_util::CreateCopyResultCallback(&result_code, &temp_file)), | 92 test_util::CreateQuitCallback( |
94 GetContentCallback(), | 93 &run_loop, |
95 ProgressCallback(), | 94 test_util::CreateCopyResultCallback(&result_code, &temp_file)), |
96 test_server_.GetURL("/files/chromeos/gdata/testfile.txt"), | 95 GetContentCallback(), |
97 GetTestCachedFilePath( | 96 ProgressCallback(), |
98 base::FilePath::FromUTF8Unsafe("cached_testfile.txt"))); | 97 test_server_.GetURL("/files/chromeos/gdata/testfile.txt"), |
99 request_sender_->StartRequestWithRetry(request); | 98 GetTestCachedFilePath( |
100 base::MessageLoop::current()->Run(); | 99 base::FilePath::FromUTF8Unsafe("cached_testfile.txt"))); |
| 100 request_sender_->StartRequestWithRetry(request); |
| 101 run_loop.Run(); |
| 102 } |
101 | 103 |
102 std::string contents; | 104 std::string contents; |
103 file_util::ReadFileToString(temp_file, &contents); | 105 file_util::ReadFileToString(temp_file, &contents); |
104 file_util::Delete(temp_file, false); | 106 file_util::Delete(temp_file, false); |
105 | 107 |
106 EXPECT_EQ(HTTP_SUCCESS, result_code); | 108 EXPECT_EQ(HTTP_SUCCESS, result_code); |
107 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); | 109 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
108 EXPECT_EQ("/files/chromeos/gdata/testfile.txt", http_request_.relative_url); | 110 EXPECT_EQ("/files/chromeos/gdata/testfile.txt", http_request_.relative_url); |
109 | 111 |
110 const base::FilePath expected_path = | 112 const base::FilePath expected_path = |
111 test_util::GetTestFilePath("chromeos/gdata/testfile.txt"); | 113 test_util::GetTestFilePath("chromeos/gdata/testfile.txt"); |
112 std::string expected_contents; | 114 std::string expected_contents; |
113 file_util::ReadFileToString(expected_path, &expected_contents); | 115 file_util::ReadFileToString(expected_path, &expected_contents); |
114 EXPECT_EQ(expected_contents, contents); | 116 EXPECT_EQ(expected_contents, contents); |
115 } | 117 } |
116 | 118 |
117 TEST_F(BaseRequestsServerTest, DownloadFileRequest_NonExistentFile) { | 119 TEST_F(BaseRequestsServerTest, DownloadFileRequest_NonExistentFile) { |
118 GDataErrorCode result_code = GDATA_OTHER_ERROR; | 120 GDataErrorCode result_code = GDATA_OTHER_ERROR; |
119 base::FilePath temp_file; | 121 base::FilePath temp_file; |
120 DownloadFileRequest* request = new DownloadFileRequest( | 122 { |
121 request_sender_.get(), | 123 base::RunLoop run_loop; |
122 request_context_getter_.get(), | 124 DownloadFileRequest* request = new DownloadFileRequest( |
123 CreateComposedCallback( | 125 request_sender_.get(), |
124 base::Bind(&test_util::RunAndQuit), | 126 request_context_getter_.get(), |
125 test_util::CreateCopyResultCallback(&result_code, &temp_file)), | 127 test_util::CreateQuitCallback( |
126 GetContentCallback(), | 128 &run_loop, |
127 ProgressCallback(), | 129 test_util::CreateCopyResultCallback(&result_code, &temp_file)), |
128 test_server_.GetURL("/files/chromeos/gdata/no-such-file.txt"), | 130 GetContentCallback(), |
129 GetTestCachedFilePath( | 131 ProgressCallback(), |
130 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt"))); | 132 test_server_.GetURL("/files/chromeos/gdata/no-such-file.txt"), |
131 request_sender_->StartRequestWithRetry(request); | 133 GetTestCachedFilePath( |
132 base::MessageLoop::current()->Run(); | 134 base::FilePath::FromUTF8Unsafe("cache_no-such-file.txt"))); |
133 | 135 request_sender_->StartRequestWithRetry(request); |
| 136 run_loop.Run(); |
| 137 } |
134 EXPECT_EQ(HTTP_NOT_FOUND, result_code); | 138 EXPECT_EQ(HTTP_NOT_FOUND, result_code); |
135 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); | 139 EXPECT_EQ(net::test_server::METHOD_GET, http_request_.method); |
136 EXPECT_EQ("/files/chromeos/gdata/no-such-file.txt", | 140 EXPECT_EQ("/files/chromeos/gdata/no-such-file.txt", |
137 http_request_.relative_url); | 141 http_request_.relative_url); |
138 // Do not verify the not found message. | 142 // Do not verify the not found message. |
139 } | 143 } |
140 | 144 |
141 } // namespace google_apis | 145 } // namespace google_apis |
OLD | NEW |