Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: chrome/browser/google_apis/gdata_wapi_operations_unittest.cc

Issue 15505003: GTTF: Convert most tests in content to use EmbeddedTestServer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: clang Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 const net::test_server::HttpRequest& request) { 102 const net::test_server::HttpRequest& request) {
103 http_request_ = request; 103 http_request_ = request;
104 104
105 const GURL absolute_url = test_server_.GetURL(request.relative_url); 105 const GURL absolute_url = test_server_.GetURL(request.relative_url);
106 std::string remaining_path; 106 std::string remaining_path;
107 if (absolute_url.path() == "/feeds/default/private/full" && 107 if (absolute_url.path() == "/feeds/default/private/full" &&
108 request.method == net::test_server::METHOD_POST) { 108 request.method == net::test_server::METHOD_POST) {
109 // This is a request for copying a document. 109 // This is a request for copying a document.
110 // TODO(satorux): we should generate valid JSON data for the newly 110 // TODO(satorux): we should generate valid JSON data for the newly
111 // copied document but for now, just return "file_entry.json" 111 // copied document but for now, just return "file_entry.json"
112 return test_util::CreateHttpResponseFromFile( 112 scoped_ptr<net::test_server::BasicHttpResponse> result(
113 test_util::GetTestFilePath("chromeos/gdata/file_entry.json")); 113 test_util::CreateHttpResponseFromFile(
114 test_util::GetTestFilePath("chromeos/gdata/file_entry.json")));
115 return result.PassAs<net::test_server::HttpResponse>();
114 } 116 }
115 117
116 if (!test_util::RemovePrefix(absolute_url.path(), 118 if (!test_util::RemovePrefix(absolute_url.path(),
117 "/feeds/default/private/full", 119 "/feeds/default/private/full",
118 &remaining_path)) { 120 &remaining_path)) {
119 return scoped_ptr<net::test_server::HttpResponse>(); 121 return scoped_ptr<net::test_server::HttpResponse>();
120 } 122 }
121 123
122 if (remaining_path.empty()) { 124 if (remaining_path.empty()) {
123 // Process the default feed. 125 // Process the default feed.
124 return test_util::CreateHttpResponseFromFile( 126 scoped_ptr<net::test_server::BasicHttpResponse> result(
125 test_util::GetTestFilePath("chromeos/gdata/root_feed.json")); 127 test_util::CreateHttpResponseFromFile(
128 test_util::GetTestFilePath("chromeos/gdata/root_feed.json")));
129 return result.PassAs<net::test_server::HttpResponse>();
126 } else { 130 } else {
127 // Process a feed for a single resource ID. 131 // Process a feed for a single resource ID.
128 const std::string resource_id = net::UnescapeURLComponent( 132 const std::string resource_id = net::UnescapeURLComponent(
129 remaining_path.substr(1), net::UnescapeRule::URL_SPECIAL_CHARS); 133 remaining_path.substr(1), net::UnescapeRule::URL_SPECIAL_CHARS);
130 if (resource_id == "file:2_file_resource_id") { 134 if (resource_id == "file:2_file_resource_id") {
131 return test_util::CreateHttpResponseFromFile( 135 scoped_ptr<net::test_server::BasicHttpResponse> result(
132 test_util::GetTestFilePath("chromeos/gdata/file_entry.json")); 136 test_util::CreateHttpResponseFromFile(
137 test_util::GetTestFilePath("chromeos/gdata/file_entry.json")));
138 return result.PassAs<net::test_server::HttpResponse>();
133 } else if (resource_id == "folder:root/contents" && 139 } else if (resource_id == "folder:root/contents" &&
134 request.method == net::test_server::METHOD_POST) { 140 request.method == net::test_server::METHOD_POST) {
135 // This is a request for creating a directory in the root directory. 141 // This is a request for creating a directory in the root directory.
136 // TODO(satorux): we should generate valid JSON data for the newly 142 // TODO(satorux): we should generate valid JSON data for the newly
137 // created directory but for now, just return "directory_entry.json" 143 // created directory but for now, just return "directory_entry.json"
138 return test_util::CreateHttpResponseFromFile( 144 scoped_ptr<net::test_server::BasicHttpResponse> result(
139 test_util::GetTestFilePath("chromeos/gdata/directory_entry.json")); 145 test_util::CreateHttpResponseFromFile(
146 test_util::GetTestFilePath(
147 "chromeos/gdata/directory_entry.json")));
148 return result.PassAs<net::test_server::HttpResponse>();
140 } else if (resource_id == 149 } else if (resource_id ==
141 "folder:root/contents/file:2_file_resource_id" && 150 "folder:root/contents/file:2_file_resource_id" &&
142 request.method == net::test_server::METHOD_DELETE) { 151 request.method == net::test_server::METHOD_DELETE) {
143 // This is a request for deleting a file from the root directory. 152 // This is a request for deleting a file from the root directory.
144 // TODO(satorux): Investigate what's returned from the server, and 153 // TODO(satorux): Investigate what's returned from the server, and
145 // copy it. For now, just return a random file, as the contents don't 154 // copy it. For now, just return a random file, as the contents don't
146 // matter. 155 // matter.
147 return test_util::CreateHttpResponseFromFile( 156 scoped_ptr<net::test_server::BasicHttpResponse> result(
148 test_util::GetTestFilePath("chromeos/gdata/testfile.txt")); 157 test_util::CreateHttpResponseFromFile(
158 test_util::GetTestFilePath("chromeos/gdata/testfile.txt")));
159 return result.PassAs<net::test_server::HttpResponse>();
149 } else if (resource_id == "invalid_resource_id") { 160 } else if (resource_id == "invalid_resource_id") {
150 // Check if this is an authorization request for an app. 161 // Check if this is an authorization request for an app.
151 // This emulates to return invalid formatted result from the server. 162 // This emulates to return invalid formatted result from the server.
152 if (request.method == net::test_server::METHOD_PUT && 163 if (request.method == net::test_server::METHOD_PUT &&
153 request.content.find("<docs:authorizedApp>") != std::string::npos) { 164 request.content.find("<docs:authorizedApp>") != std::string::npos) {
154 return test_util::CreateHttpResponseFromFile( 165 scoped_ptr<net::test_server::BasicHttpResponse> result(
155 test_util::GetTestFilePath("chromeos/gdata/testfile.txt")); 166 test_util::CreateHttpResponseFromFile(
167 test_util::GetTestFilePath("chromeos/gdata/testfile.txt")));
168 return result.PassAs<net::test_server::HttpResponse>();
156 } 169 }
157 } 170 }
158 } 171 }
159 172
160 return scoped_ptr<net::test_server::HttpResponse>(); 173 return scoped_ptr<net::test_server::HttpResponse>();
161 } 174 }
162 175
163 // Handles a request for fetching a metadata feed. 176 // Handles a request for fetching a metadata feed.
164 scoped_ptr<net::test_server::HttpResponse> HandleMetadataRequest( 177 scoped_ptr<net::test_server::HttpResponse> HandleMetadataRequest(
165 const net::test_server::HttpRequest& request) { 178 const net::test_server::HttpRequest& request) {
166 http_request_ = request; 179 http_request_ = request;
167 180
168 const GURL absolute_url = test_server_.GetURL(request.relative_url); 181 const GURL absolute_url = test_server_.GetURL(request.relative_url);
169 if (absolute_url.path() != "/feeds/metadata/default") 182 if (absolute_url.path() != "/feeds/metadata/default")
170 return scoped_ptr<net::test_server::HttpResponse>(); 183 return scoped_ptr<net::test_server::HttpResponse>();
171 184
172 scoped_ptr<net::test_server::HttpResponse> result( 185 scoped_ptr<net::test_server::BasicHttpResponse> result(
173 test_util::CreateHttpResponseFromFile( 186 test_util::CreateHttpResponseFromFile(
174 test_util::GetTestFilePath( 187 test_util::GetTestFilePath(
175 "chromeos/gdata/account_metadata.json"))); 188 "chromeos/gdata/account_metadata.json")));
176 if (absolute_url.query().find("include-installed-apps=true") == 189 if (absolute_url.query().find("include-installed-apps=true") ==
177 string::npos) { 190 string::npos) {
178 // Exclude the list of installed apps. 191 // Exclude the list of installed apps.
179 scoped_ptr<base::Value> parsed_content( 192 scoped_ptr<base::Value> parsed_content(
180 base::JSONReader::Read(result->content(), base::JSON_PARSE_RFC)); 193 base::JSONReader::Read(result->content(), base::JSON_PARSE_RFC));
181 CHECK(parsed_content); 194 CHECK(parsed_content);
182 195
183 // Remove the install apps node. 196 // Remove the install apps node.
184 base::DictionaryValue* dictionary_value; 197 base::DictionaryValue* dictionary_value;
185 CHECK(parsed_content->GetAsDictionary(&dictionary_value)); 198 CHECK(parsed_content->GetAsDictionary(&dictionary_value));
186 dictionary_value->Remove("entry.docs$installedApp", NULL); 199 dictionary_value->Remove("entry.docs$installedApp", NULL);
187 200
188 // Write back it as the content of the result. 201 // Write back it as the content of the result.
189 std::string content; 202 std::string content;
190 base::JSONWriter::Write(parsed_content.get(), &content); 203 base::JSONWriter::Write(parsed_content.get(), &content);
191 result->set_content(content); 204 result->set_content(content);
192 } 205 }
193 206
194 return result.Pass(); 207 return result.PassAs<net::test_server::HttpResponse>();
195 } 208 }
196 209
197 // Handles a request for creating a session for uploading. 210 // Handles a request for creating a session for uploading.
198 scoped_ptr<net::test_server::HttpResponse> HandleCreateSessionRequest( 211 scoped_ptr<net::test_server::HttpResponse> HandleCreateSessionRequest(
199 const net::test_server::HttpRequest& request) { 212 const net::test_server::HttpRequest& request) {
200 http_request_ = request; 213 http_request_ = request;
201 214
202 const GURL absolute_url = test_server_.GetURL(request.relative_url); 215 const GURL absolute_url = test_server_.GetURL(request.relative_url);
203 if (StartsWithASCII(absolute_url.path(), 216 if (StartsWithASCII(absolute_url.path(),
204 "/feeds/upload/create-session/default/private/full", 217 "/feeds/upload/create-session/default/private/full",
205 true)) { // case sensitive 218 true)) { // case sensitive
206 // This is an initiating upload URL. 219 // This is an initiating upload URL.
207 scoped_ptr<net::test_server::HttpResponse> http_response( 220 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
208 new net::test_server::HttpResponse); 221 new net::test_server::BasicHttpResponse);
209 222
210 // Check an ETag. 223 // Check an ETag.
211 std::map<std::string, std::string>::const_iterator found = 224 std::map<std::string, std::string>::const_iterator found =
212 request.headers.find("If-Match"); 225 request.headers.find("If-Match");
213 if (found != request.headers.end() && 226 if (found != request.headers.end() &&
214 found->second != "*" && 227 found->second != "*" &&
215 found->second != kTestETag) { 228 found->second != kTestETag) {
216 http_response->set_code(net::test_server::PRECONDITION); 229 http_response->set_code(net::test_server::PRECONDITION);
217 return http_response.Pass(); 230 return http_response.PassAs<net::test_server::HttpResponse>();
218 } 231 }
219 232
220 // Check if the X-Upload-Content-Length is present. If yes, store the 233 // Check if the X-Upload-Content-Length is present. If yes, store the
221 // length of the file. 234 // length of the file.
222 found = request.headers.find("X-Upload-Content-Length"); 235 found = request.headers.find("X-Upload-Content-Length");
223 if (found == request.headers.end() || 236 if (found == request.headers.end() ||
224 !base::StringToInt64(found->second, &content_length_)) { 237 !base::StringToInt64(found->second, &content_length_)) {
225 return scoped_ptr<net::test_server::HttpResponse>(); 238 return scoped_ptr<net::test_server::HttpResponse>();
226 } 239 }
227 received_bytes_ = 0; 240 received_bytes_ = 0;
228 241
229 http_response->set_code(net::test_server::SUCCESS); 242 http_response->set_code(net::test_server::SUCCESS);
230 GURL upload_url; 243 GURL upload_url;
231 // POST is used for a new file, and PUT is used for an existing file. 244 // POST is used for a new file, and PUT is used for an existing file.
232 if (request.method == net::test_server::METHOD_POST) { 245 if (request.method == net::test_server::METHOD_POST) {
233 upload_url = test_server_.GetURL("/upload_new_file"); 246 upload_url = test_server_.GetURL("/upload_new_file");
234 } else if (request.method == net::test_server::METHOD_PUT) { 247 } else if (request.method == net::test_server::METHOD_PUT) {
235 upload_url = test_server_.GetURL("/upload_existing_file"); 248 upload_url = test_server_.GetURL("/upload_existing_file");
236 } else { 249 } else {
237 return scoped_ptr<net::test_server::HttpResponse>(); 250 return scoped_ptr<net::test_server::HttpResponse>();
238 } 251 }
239 http_response->AddCustomHeader("Location", upload_url.spec()); 252 http_response->AddCustomHeader("Location", upload_url.spec());
240 return http_response.Pass(); 253 return http_response.PassAs<net::test_server::HttpResponse>();
241 } 254 }
242 255
243 return scoped_ptr<net::test_server::HttpResponse>(); 256 return scoped_ptr<net::test_server::HttpResponse>();
244 } 257 }
245 258
246 // Handles a request for uploading content. 259 // Handles a request for uploading content.
247 scoped_ptr<net::test_server::HttpResponse> HandleUploadRequest( 260 scoped_ptr<net::test_server::HttpResponse> HandleUploadRequest(
248 const net::test_server::HttpRequest& request) { 261 const net::test_server::HttpRequest& request) {
249 http_request_ = request; 262 http_request_ = request;
250 263
251 const GURL absolute_url = test_server_.GetURL(request.relative_url); 264 const GURL absolute_url = test_server_.GetURL(request.relative_url);
252 if (absolute_url.path() != "/upload_new_file" && 265 if (absolute_url.path() != "/upload_new_file" &&
253 absolute_url.path() != "/upload_existing_file") { 266 absolute_url.path() != "/upload_existing_file") {
254 return scoped_ptr<net::test_server::HttpResponse>(); 267 return scoped_ptr<net::test_server::HttpResponse>();
255 } 268 }
256 269
257 // TODO(satorux): We should create a correct JSON data for the uploaded 270 // TODO(satorux): We should create a correct JSON data for the uploaded
258 // file, but for now, just return file_entry.json. 271 // file, but for now, just return file_entry.json.
259 scoped_ptr<net::test_server::HttpResponse> response = 272 scoped_ptr<net::test_server::BasicHttpResponse> response =
260 test_util::CreateHttpResponseFromFile( 273 test_util::CreateHttpResponseFromFile(
261 test_util::GetTestFilePath("chromeos/gdata/file_entry.json")); 274 test_util::GetTestFilePath("chromeos/gdata/file_entry.json"));
262 // response.code() is set to SUCCESS. Change it to CREATED if it's a new 275 // response.code() is set to SUCCESS. Change it to CREATED if it's a new
263 // file. 276 // file.
264 if (absolute_url.path() == "/upload_new_file") 277 if (absolute_url.path() == "/upload_new_file")
265 response->set_code(net::test_server::CREATED); 278 response->set_code(net::test_server::CREATED);
266 279
267 // Check if the Content-Range header is present. This must be present if 280 // Check if the Content-Range header is present. This must be present if
268 // the request body is not empty. 281 // the request body is not empty.
269 if (!request.content.empty()) { 282 if (!request.content.empty()) {
(...skipping 22 matching lines...) Expand all
292 if (received_bytes_ > 0) { 305 if (received_bytes_ > 0) {
293 response->AddCustomHeader( 306 response->AddCustomHeader(
294 "Range", 307 "Range",
295 "bytes=0-" + base::Int64ToString(received_bytes_ - 1)); 308 "bytes=0-" + base::Int64ToString(received_bytes_ - 1));
296 } 309 }
297 310
298 // Change the code to RESUME_INCOMPLETE if upload is not complete. 311 // Change the code to RESUME_INCOMPLETE if upload is not complete.
299 if (received_bytes_ < content_length_) 312 if (received_bytes_ < content_length_)
300 response->set_code(net::test_server::RESUME_INCOMPLETE); 313 response->set_code(net::test_server::RESUME_INCOMPLETE);
301 314
302 return response.Pass(); 315 return response.PassAs<net::test_server::HttpResponse>();
303 } 316 }
304 317
305 MessageLoopForUI message_loop_; 318 MessageLoopForUI message_loop_;
306 content::TestBrowserThread ui_thread_; 319 content::TestBrowserThread ui_thread_;
307 content::TestBrowserThread file_thread_; 320 content::TestBrowserThread file_thread_;
308 content::TestBrowserThread io_thread_; 321 content::TestBrowserThread io_thread_;
309 net::test_server::EmbeddedTestServer test_server_; 322 net::test_server::EmbeddedTestServer test_server_;
310 scoped_ptr<TestingProfile> profile_; 323 scoped_ptr<TestingProfile> profile_;
311 scoped_ptr<OperationRunner> operation_runner_; 324 scoped_ptr<OperationRunner> operation_runner_;
312 scoped_ptr<GDataWapiUrlGenerator> url_generator_; 325 scoped_ptr<GDataWapiUrlGenerator> url_generator_;
(...skipping 1182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), 1508 EXPECT_EQ(base::Int64ToString(kUploadContent.size()),
1496 http_request_.headers["X-Upload-Content-Length"]); 1509 http_request_.headers["X-Upload-Content-Length"]);
1497 // For updating an existing file, an empty body should be attached (PUT 1510 // For updating an existing file, an empty body should be attached (PUT
1498 // requires a body) 1511 // requires a body)
1499 EXPECT_TRUE(http_request_.has_content); 1512 EXPECT_TRUE(http_request_.has_content);
1500 EXPECT_EQ("", http_request_.content); 1513 EXPECT_EQ("", http_request_.content);
1501 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); 1514 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]);
1502 } 1515 }
1503 1516
1504 } // namespace google_apis 1517 } // namespace google_apis
OLDNEW
« no previous file with comments | « chrome/browser/google_apis/drive_api_operations_unittest.cc ('k') | chrome/browser/google_apis/test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698