| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/google_apis/test_util.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/json/json_file_value_serializer.h" | |
| 9 #include "base/json/json_reader.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/rand_util.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/strings/string_split.h" | |
| 16 #include "base/strings/string_util.h" | |
| 17 #include "base/strings/stringprintf.h" | |
| 18 #include "chrome/browser/google_apis/drive_api_parser.h" | |
| 19 #include "chrome/browser/google_apis/gdata_wapi_parser.h" | |
| 20 #include "chrome/browser/google_apis/gdata_wapi_requests.h" | |
| 21 #include "net/test/embedded_test_server/http_request.h" | |
| 22 #include "net/test/embedded_test_server/http_response.h" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 namespace google_apis { | |
| 26 namespace test_util { | |
| 27 | |
| 28 bool RemovePrefix(const std::string& input, | |
| 29 const std::string& prefix, | |
| 30 std::string* output) { | |
| 31 if (!StartsWithASCII(input, prefix, true /* case sensitive */)) | |
| 32 return false; | |
| 33 | |
| 34 *output = input.substr(prefix.size()); | |
| 35 return true; | |
| 36 } | |
| 37 | |
| 38 base::FilePath GetTestFilePath(const std::string& relative_path) { | |
| 39 base::FilePath path; | |
| 40 if (!PathService::Get(base::DIR_SOURCE_ROOT, &path)) | |
| 41 return base::FilePath(); | |
| 42 path = path.AppendASCII("chrome") | |
| 43 .AppendASCII("test") | |
| 44 .AppendASCII("data") | |
| 45 .Append(base::FilePath::FromUTF8Unsafe(relative_path)); | |
| 46 return path; | |
| 47 } | |
| 48 | |
| 49 GURL GetBaseUrlForTesting(int port) { | |
| 50 return GURL(base::StringPrintf("http://127.0.0.1:%d/", port)); | |
| 51 } | |
| 52 | |
| 53 void RunAndQuit(base::RunLoop* run_loop, const base::Closure& closure) { | |
| 54 closure.Run(); | |
| 55 run_loop->Quit(); | |
| 56 } | |
| 57 | |
| 58 bool WriteStringToFile(const base::FilePath& file_path, | |
| 59 const std::string& content) { | |
| 60 int result = file_util::WriteFile(file_path, content.data(), content.size()); | |
| 61 return content.size() == static_cast<size_t>(result); | |
| 62 } | |
| 63 | |
| 64 bool CreateFileOfSpecifiedSize(const base::FilePath& temp_dir, | |
| 65 size_t size, | |
| 66 base::FilePath* path, | |
| 67 std::string* data) { | |
| 68 if (!file_util::CreateTemporaryFileInDir(temp_dir, path)) | |
| 69 return false; | |
| 70 | |
| 71 if (size == 0) { | |
| 72 // Note: RandBytesAsString doesn't support generating an empty string. | |
| 73 data->clear(); | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 *data = base::RandBytesAsString(size); | |
| 78 return WriteStringToFile(*path, *data); | |
| 79 } | |
| 80 | |
| 81 scoped_ptr<base::Value> LoadJSONFile(const std::string& relative_path) { | |
| 82 base::FilePath path = GetTestFilePath(relative_path); | |
| 83 | |
| 84 std::string error; | |
| 85 JSONFileValueSerializer serializer(path); | |
| 86 scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error)); | |
| 87 LOG_IF(WARNING, !value.get()) << "Failed to parse " << path.value() | |
| 88 << ": " << error; | |
| 89 return value.Pass(); | |
| 90 } | |
| 91 | |
| 92 // Returns a HttpResponse created from the given file path. | |
| 93 scoped_ptr<net::test_server::BasicHttpResponse> CreateHttpResponseFromFile( | |
| 94 const base::FilePath& file_path) { | |
| 95 std::string content; | |
| 96 if (!base::ReadFileToString(file_path, &content)) | |
| 97 return scoped_ptr<net::test_server::BasicHttpResponse>(); | |
| 98 | |
| 99 std::string content_type = "text/plain"; | |
| 100 if (EndsWith(file_path.AsUTF8Unsafe(), ".json", true /* case sensitive */)) | |
| 101 content_type = "application/json"; | |
| 102 | |
| 103 scoped_ptr<net::test_server::BasicHttpResponse> http_response( | |
| 104 new net::test_server::BasicHttpResponse); | |
| 105 http_response->set_code(net::HTTP_OK); | |
| 106 http_response->set_content(content); | |
| 107 http_response->set_content_type(content_type); | |
| 108 return http_response.Pass(); | |
| 109 } | |
| 110 | |
| 111 scoped_ptr<net::test_server::HttpResponse> HandleDownloadFileRequest( | |
| 112 const GURL& base_url, | |
| 113 net::test_server::HttpRequest* out_request, | |
| 114 const net::test_server::HttpRequest& request) { | |
| 115 *out_request = request; | |
| 116 | |
| 117 GURL absolute_url = base_url.Resolve(request.relative_url); | |
| 118 std::string remaining_path; | |
| 119 if (!RemovePrefix(absolute_url.path(), "/files/", &remaining_path)) | |
| 120 return scoped_ptr<net::test_server::HttpResponse>(); | |
| 121 return CreateHttpResponseFromFile( | |
| 122 GetTestFilePath(remaining_path)).PassAs<net::test_server::HttpResponse>(); | |
| 123 } | |
| 124 | |
| 125 bool ParseContentRangeHeader(const std::string& value, | |
| 126 int64* start_position, | |
| 127 int64* end_position, | |
| 128 int64* length) { | |
| 129 DCHECK(start_position); | |
| 130 DCHECK(end_position); | |
| 131 DCHECK(length); | |
| 132 | |
| 133 std::string remaining; | |
| 134 if (!RemovePrefix(value, "bytes ", &remaining)) | |
| 135 return false; | |
| 136 | |
| 137 std::vector<std::string> parts; | |
| 138 base::SplitString(remaining, '/', &parts); | |
| 139 if (parts.size() != 2U) | |
| 140 return false; | |
| 141 | |
| 142 const std::string range = parts[0]; | |
| 143 if (!base::StringToInt64(parts[1], length)) | |
| 144 return false; | |
| 145 | |
| 146 parts.clear(); | |
| 147 base::SplitString(range, '-', &parts); | |
| 148 if (parts.size() != 2U) | |
| 149 return false; | |
| 150 | |
| 151 return (base::StringToInt64(parts[0], start_position) && | |
| 152 base::StringToInt64(parts[1], end_position)); | |
| 153 } | |
| 154 | |
| 155 void AppendProgressCallbackResult(std::vector<ProgressInfo>* progress_values, | |
| 156 int64 progress, | |
| 157 int64 total) { | |
| 158 progress_values->push_back(ProgressInfo(progress, total)); | |
| 159 } | |
| 160 | |
| 161 TestGetContentCallback::TestGetContentCallback() | |
| 162 : callback_(base::Bind(&TestGetContentCallback::OnGetContent, | |
| 163 base::Unretained(this))) { | |
| 164 } | |
| 165 | |
| 166 TestGetContentCallback::~TestGetContentCallback() { | |
| 167 } | |
| 168 | |
| 169 std::string TestGetContentCallback::GetConcatenatedData() const { | |
| 170 std::string result; | |
| 171 for (size_t i = 0; i < data_.size(); ++i) { | |
| 172 result += *data_[i]; | |
| 173 } | |
| 174 return result; | |
| 175 } | |
| 176 | |
| 177 void TestGetContentCallback::OnGetContent(google_apis::GDataErrorCode error, | |
| 178 scoped_ptr<std::string> data) { | |
| 179 data_.push_back(data.release()); | |
| 180 } | |
| 181 | |
| 182 } // namespace test_util | |
| 183 } // namespace google_apis | |
| OLD | NEW |