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/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 "google_apis/drive/gdata_wapi_parser.h"
20 #include "google_apis/drive/gdata_wapi_requests.h"
21 #include "net/test/embedded_test_server/http_request.h"
22 #include "net/test/embedded_test_server/http_response.h"
25 namespace google_apis
{
28 bool RemovePrefix(const std::string
& input
,
29 const std::string
& prefix
,
30 std::string
* output
) {
31 if (!StartsWithASCII(input
, prefix
, true /* case sensitive */))
34 *output
= input
.substr(prefix
.size());
38 base::FilePath
GetTestFilePath(const std::string
& relative_path
) {
40 if (!PathService::Get(base::DIR_SOURCE_ROOT
, &path
))
41 return base::FilePath();
42 path
= path
.AppendASCII("chrome")
45 .Append(base::FilePath::FromUTF8Unsafe(relative_path
));
49 GURL
GetBaseUrlForTesting(int port
) {
50 return GURL(base::StringPrintf("http://127.0.0.1:%d/", port
));
53 void RunAndQuit(base::RunLoop
* run_loop
, const base::Closure
& closure
) {
58 bool WriteStringToFile(const base::FilePath
& file_path
,
59 const std::string
& content
) {
60 int result
= base::WriteFile(
61 file_path
, content
.data(), static_cast<int>(content
.size()));
62 return content
.size() == static_cast<size_t>(result
);
65 bool CreateFileOfSpecifiedSize(const base::FilePath
& temp_dir
,
69 if (!base::CreateTemporaryFileInDir(temp_dir
, path
))
73 // Note: RandBytesAsString doesn't support generating an empty string.
78 *data
= base::RandBytesAsString(size
);
79 return WriteStringToFile(*path
, *data
);
82 scoped_ptr
<base::Value
> LoadJSONFile(const std::string
& relative_path
) {
83 base::FilePath path
= GetTestFilePath(relative_path
);
86 JSONFileValueSerializer
serializer(path
);
87 scoped_ptr
<base::Value
> value(serializer
.Deserialize(NULL
, &error
));
88 LOG_IF(WARNING
, !value
.get()) << "Failed to parse " << path
.value()
93 // Returns a HttpResponse created from the given file path.
94 scoped_ptr
<net::test_server::BasicHttpResponse
> CreateHttpResponseFromFile(
95 const base::FilePath
& file_path
) {
97 if (!base::ReadFileToString(file_path
, &content
))
98 return scoped_ptr
<net::test_server::BasicHttpResponse
>();
100 std::string content_type
= "text/plain";
101 if (EndsWith(file_path
.AsUTF8Unsafe(), ".json", true /* case sensitive */))
102 content_type
= "application/json";
104 scoped_ptr
<net::test_server::BasicHttpResponse
> http_response(
105 new net::test_server::BasicHttpResponse
);
106 http_response
->set_code(net::HTTP_OK
);
107 http_response
->set_content(content
);
108 http_response
->set_content_type(content_type
);
109 return http_response
.Pass();
112 scoped_ptr
<net::test_server::HttpResponse
> HandleDownloadFileRequest(
113 const GURL
& base_url
,
114 net::test_server::HttpRequest
* out_request
,
115 const net::test_server::HttpRequest
& request
) {
116 *out_request
= request
;
118 GURL absolute_url
= base_url
.Resolve(request
.relative_url
);
119 std::string remaining_path
;
120 if (!RemovePrefix(absolute_url
.path(), "/files/", &remaining_path
))
121 return scoped_ptr
<net::test_server::HttpResponse
>();
122 return CreateHttpResponseFromFile(
123 GetTestFilePath(remaining_path
)).PassAs
<net::test_server::HttpResponse
>();
126 bool ParseContentRangeHeader(const std::string
& value
,
127 int64
* start_position
,
130 DCHECK(start_position
);
131 DCHECK(end_position
);
134 std::string remaining
;
135 if (!RemovePrefix(value
, "bytes ", &remaining
))
138 std::vector
<std::string
> parts
;
139 base::SplitString(remaining
, '/', &parts
);
140 if (parts
.size() != 2U)
143 const std::string range
= parts
[0];
144 if (!base::StringToInt64(parts
[1], length
))
148 base::SplitString(range
, '-', &parts
);
149 if (parts
.size() != 2U)
152 return (base::StringToInt64(parts
[0], start_position
) &&
153 base::StringToInt64(parts
[1], end_position
));
156 void AppendProgressCallbackResult(std::vector
<ProgressInfo
>* progress_values
,
159 progress_values
->push_back(ProgressInfo(progress
, total
));
162 TestGetContentCallback::TestGetContentCallback()
163 : callback_(base::Bind(&TestGetContentCallback::OnGetContent
,
164 base::Unretained(this))) {
167 TestGetContentCallback::~TestGetContentCallback() {
170 std::string
TestGetContentCallback::GetConcatenatedData() const {
172 for (size_t i
= 0; i
< data_
.size(); ++i
) {
178 void TestGetContentCallback::OnGetContent(google_apis::GDataErrorCode error
,
179 scoped_ptr
<std::string
> data
) {
180 data_
.push_back(data
.release());
183 } // namespace test_util
184 } // namespace google_apis