Add yunsik.jang@lge.com to AUTHORS
[chromium-blink-merge.git] / google_apis / drive / test_util.cc
blobdb14c9d7bb43b37e67b71a1fef7ce7a0547cfa90
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.
5 #include "google_apis/drive/test_util.h"
7 #include "base/files/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 "google_apis/drive/drive_api_parser.h"
19 #include "net/test/embedded_test_server/http_request.h"
20 #include "net/test/embedded_test_server/http_response.h"
21 #include "url/gurl.h"
23 namespace google_apis {
24 namespace test_util {
26 bool RemovePrefix(const std::string& input,
27 const std::string& prefix,
28 std::string* output) {
29 if (!StartsWithASCII(input, prefix, true /* case sensitive */))
30 return false;
32 *output = input.substr(prefix.size());
33 return true;
36 base::FilePath GetTestFilePath(const std::string& relative_path) {
37 base::FilePath path;
38 if (!PathService::Get(base::DIR_SOURCE_ROOT, &path))
39 return base::FilePath();
40 path = path.AppendASCII("chrome")
41 .AppendASCII("test")
42 .AppendASCII("data")
43 .Append(base::FilePath::FromUTF8Unsafe(relative_path));
44 return path;
47 GURL GetBaseUrlForTesting(int port) {
48 return GURL(base::StringPrintf("http://127.0.0.1:%d/", port));
51 void RunAndQuit(base::RunLoop* run_loop, const base::Closure& closure) {
52 closure.Run();
53 run_loop->Quit();
56 bool WriteStringToFile(const base::FilePath& file_path,
57 const std::string& content) {
58 int result = base::WriteFile(
59 file_path, content.data(), static_cast<int>(content.size()));
60 return content.size() == static_cast<size_t>(result);
63 bool CreateFileOfSpecifiedSize(const base::FilePath& temp_dir,
64 size_t size,
65 base::FilePath* path,
66 std::string* data) {
67 if (!base::CreateTemporaryFileInDir(temp_dir, path))
68 return false;
70 if (size == 0) {
71 // Note: RandBytesAsString doesn't support generating an empty string.
72 data->clear();
73 return true;
76 *data = base::RandBytesAsString(size);
77 return WriteStringToFile(*path, *data);
80 scoped_ptr<base::Value> LoadJSONFile(const std::string& relative_path) {
81 base::FilePath path = GetTestFilePath(relative_path);
83 std::string error;
84 JSONFileValueDeserializer deserializer(path);
85 scoped_ptr<base::Value> value(deserializer.Deserialize(NULL, &error));
86 LOG_IF(WARNING, !value.get()) << "Failed to parse " << path.value()
87 << ": " << error;
88 return value.Pass();
91 // Returns a HttpResponse created from the given file path.
92 scoped_ptr<net::test_server::BasicHttpResponse> CreateHttpResponseFromFile(
93 const base::FilePath& file_path) {
94 std::string content;
95 if (!base::ReadFileToString(file_path, &content))
96 return scoped_ptr<net::test_server::BasicHttpResponse>();
98 std::string content_type = "text/plain";
99 if (EndsWith(file_path.AsUTF8Unsafe(), ".json", true /* case sensitive */))
100 content_type = "application/json";
102 scoped_ptr<net::test_server::BasicHttpResponse> http_response(
103 new net::test_server::BasicHttpResponse);
104 http_response->set_code(net::HTTP_OK);
105 http_response->set_content(content);
106 http_response->set_content_type(content_type);
107 return http_response.Pass();
110 scoped_ptr<net::test_server::HttpResponse> HandleDownloadFileRequest(
111 const GURL& base_url,
112 net::test_server::HttpRequest* out_request,
113 const net::test_server::HttpRequest& request) {
114 *out_request = request;
116 GURL absolute_url = base_url.Resolve(request.relative_url);
117 std::string remaining_path;
118 if (!RemovePrefix(absolute_url.path(), "/files/", &remaining_path))
119 return scoped_ptr<net::test_server::HttpResponse>();
120 return CreateHttpResponseFromFile(GetTestFilePath(remaining_path));
123 bool ParseContentRangeHeader(const std::string& value,
124 int64* start_position,
125 int64* end_position,
126 int64* length) {
127 DCHECK(start_position);
128 DCHECK(end_position);
129 DCHECK(length);
131 std::string remaining;
132 if (!RemovePrefix(value, "bytes ", &remaining))
133 return false;
135 std::vector<std::string> parts;
136 base::SplitString(remaining, '/', &parts);
137 if (parts.size() != 2U)
138 return false;
140 const std::string range = parts[0];
141 if (!base::StringToInt64(parts[1], length))
142 return false;
144 parts.clear();
145 base::SplitString(range, '-', &parts);
146 if (parts.size() != 2U)
147 return false;
149 return (base::StringToInt64(parts[0], start_position) &&
150 base::StringToInt64(parts[1], end_position));
153 void AppendProgressCallbackResult(std::vector<ProgressInfo>* progress_values,
154 int64 progress,
155 int64 total) {
156 progress_values->push_back(ProgressInfo(progress, total));
159 TestGetContentCallback::TestGetContentCallback()
160 : callback_(base::Bind(&TestGetContentCallback::OnGetContent,
161 base::Unretained(this))) {
164 TestGetContentCallback::~TestGetContentCallback() {
167 std::string TestGetContentCallback::GetConcatenatedData() const {
168 std::string result;
169 for (size_t i = 0; i < data_.size(); ++i) {
170 result += *data_[i];
172 return result;
175 void TestGetContentCallback::OnGetContent(google_apis::DriveApiErrorCode error,
176 scoped_ptr<std::string> data) {
177 data_.push_back(data.release());
180 } // namespace test_util
181 } // namespace google_apis