| 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_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 void CopyResultFromResumeUploadCallbackAndQuit( | 64 void CopyResultFromResumeUploadCallbackAndQuit( |
| 65 ResumeUploadResponse* out_response, | 65 ResumeUploadResponse* out_response, |
| 66 scoped_ptr<ResourceEntry>* out_new_entry, | 66 scoped_ptr<ResourceEntry>* out_new_entry, |
| 67 const ResumeUploadResponse& response, | 67 const ResumeUploadResponse& response, |
| 68 scoped_ptr<ResourceEntry> new_entry) { | 68 scoped_ptr<ResourceEntry> new_entry) { |
| 69 *out_response = response; | 69 *out_response = response; |
| 70 *out_new_entry = new_entry.Pass(); | 70 *out_new_entry = new_entry.Pass(); |
| 71 MessageLoop::current()->Quit(); | 71 MessageLoop::current()->Quit(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Removes |prefix| from |input| and stores the result in |output|. Returns | |
| 75 // true if the prefix is removed. | |
| 76 bool RemovePrefix(const std::string& input, | |
| 77 const std::string& prefix, | |
| 78 std::string* output) { | |
| 79 if (!StartsWithASCII(input, prefix, true /* case sensitive */)) | |
| 80 return false; | |
| 81 | |
| 82 *output = input.substr(prefix.size()); | |
| 83 return true; | |
| 84 } | |
| 85 | |
| 86 // Parses a value of Content-Range header, which looks like | 74 // Parses a value of Content-Range header, which looks like |
| 87 // "bytes <start_position>-<end_position>/<length>". | 75 // "bytes <start_position>-<end_position>/<length>". |
| 88 // Returns true on success. | 76 // Returns true on success. |
| 89 bool ParseContentRangeHeader(const std::string& value, | 77 bool ParseContentRangeHeader(const std::string& value, |
| 90 int64* start_position, | 78 int64* start_position, |
| 91 int64* end_position, | 79 int64* end_position, |
| 92 int64* length) { | 80 int64* length) { |
| 93 DCHECK(start_position); | 81 DCHECK(start_position); |
| 94 DCHECK(end_position); | 82 DCHECK(end_position); |
| 95 DCHECK(length); | 83 DCHECK(length); |
| 96 | 84 |
| 97 std::string remaining; | 85 std::string remaining; |
| 98 if (!RemovePrefix(value, "bytes ", &remaining)) | 86 if (!test_util::RemovePrefix(value, "bytes ", &remaining)) |
| 99 return false; | 87 return false; |
| 100 | 88 |
| 101 std::vector<std::string> parts; | 89 std::vector<std::string> parts; |
| 102 base::SplitString(remaining, '/', &parts); | 90 base::SplitString(remaining, '/', &parts); |
| 103 if (parts.size() != 2U) | 91 if (parts.size() != 2U) |
| 104 return false; | 92 return false; |
| 105 | 93 |
| 106 const std::string range = parts[0]; | 94 const std::string range = parts[0]; |
| 107 if (!base::StringToInt64(parts[1], length)) | 95 if (!base::StringToInt64(parts[1], length)) |
| 108 return false; | 96 return false; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 128 file_thread_.Start(); | 116 file_thread_.Start(); |
| 129 io_thread_.StartIOThread(); | 117 io_thread_.StartIOThread(); |
| 130 profile_.reset(new TestingProfile); | 118 profile_.reset(new TestingProfile); |
| 131 | 119 |
| 132 request_context_getter_ = new net::TestURLRequestContextGetter( | 120 request_context_getter_ = new net::TestURLRequestContextGetter( |
| 133 content::BrowserThread::GetMessageLoopProxyForThread( | 121 content::BrowserThread::GetMessageLoopProxyForThread( |
| 134 content::BrowserThread::IO)); | 122 content::BrowserThread::IO)); |
| 135 | 123 |
| 136 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | 124 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
| 137 test_server_.RegisterRequestHandler( | 125 test_server_.RegisterRequestHandler( |
| 138 base::Bind(&GDataWapiOperationsTest::HandleDownloadRequest, | 126 base::Bind(&test_util::HandleDownloadRequest, |
| 139 base::Unretained(this))); | 127 test_server_.base_url(), |
| 128 base::Unretained(&http_request_))); |
| 140 test_server_.RegisterRequestHandler( | 129 test_server_.RegisterRequestHandler( |
| 141 base::Bind(&GDataWapiOperationsTest::HandleResourceFeedRequest, | 130 base::Bind(&GDataWapiOperationsTest::HandleResourceFeedRequest, |
| 142 base::Unretained(this))); | 131 base::Unretained(this))); |
| 143 test_server_.RegisterRequestHandler( | 132 test_server_.RegisterRequestHandler( |
| 144 base::Bind(&GDataWapiOperationsTest::HandleMetadataFeedRequest, | 133 base::Bind(&GDataWapiOperationsTest::HandleMetadataFeedRequest, |
| 145 base::Unretained(this))); | 134 base::Unretained(this))); |
| 146 test_server_.RegisterRequestHandler( | 135 test_server_.RegisterRequestHandler( |
| 147 base::Bind(&GDataWapiOperationsTest::HandleCreateSessionRequest, | 136 base::Bind(&GDataWapiOperationsTest::HandleCreateSessionRequest, |
| 148 base::Unretained(this))); | 137 base::Unretained(this))); |
| 149 test_server_.RegisterRequestHandler( | 138 test_server_.RegisterRequestHandler( |
| 150 base::Bind(&GDataWapiOperationsTest::HandleUploadRequest, | 139 base::Bind(&GDataWapiOperationsTest::HandleUploadRequest, |
| 151 base::Unretained(this))); | 140 base::Unretained(this))); |
| 152 | 141 |
| 153 url_generator_.reset(new GDataWapiUrlGenerator( | 142 url_generator_.reset(new GDataWapiUrlGenerator( |
| 154 test_util::GetBaseUrlForTesting(test_server_.port()))); | 143 test_util::GetBaseUrlForTesting(test_server_.port()))); |
| 155 } | 144 } |
| 156 | 145 |
| 157 virtual void TearDown() OVERRIDE { | 146 virtual void TearDown() OVERRIDE { |
| 158 test_server_.ShutdownAndWaitUntilComplete(); | 147 test_server_.ShutdownAndWaitUntilComplete(); |
| 159 request_context_getter_ = NULL; | 148 request_context_getter_ = NULL; |
| 160 } | 149 } |
| 161 | 150 |
| 162 protected: | 151 protected: |
| 163 // Returns a temporary file path suitable for storing the cache file. | 152 // Returns a temporary file path suitable for storing the cache file. |
| 164 FilePath GetTestCachedFilePath(const FilePath& file_name) { | 153 FilePath GetTestCachedFilePath(const FilePath& file_name) { |
| 165 return profile_->GetPath().Append(file_name); | 154 return profile_->GetPath().Append(file_name); |
| 166 } | 155 } |
| 167 | 156 |
| 168 // Handles a request for downloading a file. Reads a file from the test | |
| 169 // directory and returns the content. | |
| 170 scoped_ptr<test_server::HttpResponse> HandleDownloadRequest( | |
| 171 const test_server::HttpRequest& request) { | |
| 172 http_request_ = request; | |
| 173 | |
| 174 const GURL absolute_url = test_server_.GetURL(request.relative_url); | |
| 175 std::string remaining_path; | |
| 176 if (!RemovePrefix(absolute_url.path(), "/files/", &remaining_path)) | |
| 177 return scoped_ptr<test_server::HttpResponse>(); | |
| 178 | |
| 179 return test_util::CreateHttpResponseFromFile( | |
| 180 test_util::GetTestFilePath(remaining_path)); | |
| 181 } | |
| 182 | |
| 183 // Handles a request for fetching a resource feed. | 157 // Handles a request for fetching a resource feed. |
| 184 scoped_ptr<test_server::HttpResponse> HandleResourceFeedRequest( | 158 scoped_ptr<test_server::HttpResponse> HandleResourceFeedRequest( |
| 185 const test_server::HttpRequest& request) { | 159 const test_server::HttpRequest& request) { |
| 186 http_request_ = request; | 160 http_request_ = request; |
| 187 | 161 |
| 188 const GURL absolute_url = test_server_.GetURL(request.relative_url); | 162 const GURL absolute_url = test_server_.GetURL(request.relative_url); |
| 189 std::string remaining_path; | 163 std::string remaining_path; |
| 190 if (absolute_url.path() == "/feeds/default/private/full" && | 164 if (absolute_url.path() == "/feeds/default/private/full" && |
| 191 request.method == test_server::METHOD_POST) { | 165 request.method == test_server::METHOD_POST) { |
| 192 // This is a request for copying a document. | 166 // This is a request for copying a document. |
| 193 // TODO(satorux): we should generate valid JSON data for the newly | 167 // TODO(satorux): we should generate valid JSON data for the newly |
| 194 // copied document but for now, just return "file_entry.json" | 168 // copied document but for now, just return "file_entry.json" |
| 195 return test_util::CreateHttpResponseFromFile( | 169 return test_util::CreateHttpResponseFromFile( |
| 196 test_util::GetTestFilePath("gdata/file_entry.json")); | 170 test_util::GetTestFilePath("gdata/file_entry.json")); |
| 197 } | 171 } |
| 198 | 172 |
| 199 if (!RemovePrefix(absolute_url.path(), | 173 if (!test_util::RemovePrefix(absolute_url.path(), |
| 200 "/feeds/default/private/full/", | 174 "/feeds/default/private/full/", |
| 201 &remaining_path)) { | 175 &remaining_path)) { |
| 202 return scoped_ptr<test_server::HttpResponse>(); | 176 return scoped_ptr<test_server::HttpResponse>(); |
| 203 } | 177 } |
| 204 | 178 |
| 205 if (remaining_path == "-/mine") { | 179 if (remaining_path == "-/mine") { |
| 206 // Process the default feed. | 180 // Process the default feed. |
| 207 return test_util::CreateHttpResponseFromFile( | 181 return test_util::CreateHttpResponseFromFile( |
| 208 test_util::GetTestFilePath("gdata/root_feed.json")); | 182 test_util::GetTestFilePath("gdata/root_feed.json")); |
| 209 } else { | 183 } else { |
| 210 // Process a feed for a single resource ID. | 184 // Process a feed for a single resource ID. |
| 211 const std::string resource_id = net::UnescapeURLComponent( | 185 const std::string resource_id = net::UnescapeURLComponent( |
| (...skipping 1196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1408 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), | 1382 EXPECT_EQ(base::Int64ToString(kUploadContent.size()), |
| 1409 http_request_.headers["X-Upload-Content-Length"]); | 1383 http_request_.headers["X-Upload-Content-Length"]); |
| 1410 // For updating an existing file, an empty body should be attached (PUT | 1384 // For updating an existing file, an empty body should be attached (PUT |
| 1411 // requires a body) | 1385 // requires a body) |
| 1412 EXPECT_TRUE(http_request_.has_content); | 1386 EXPECT_TRUE(http_request_.has_content); |
| 1413 EXPECT_EQ("", http_request_.content); | 1387 EXPECT_EQ("", http_request_.content); |
| 1414 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); | 1388 EXPECT_EQ(kWrongETag, http_request_.headers["If-Match"]); |
| 1415 } | 1389 } |
| 1416 | 1390 |
| 1417 } // namespace google_apis | 1391 } // namespace google_apis |
| OLD | NEW |