1 // Copyright (c) 2013 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/drive_api_url_generator.h"
7 #include "google_apis/drive/test_util.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 #include "url/url_util.h"
12 namespace google_apis
{
14 // The URLs used for production may be different for Chromium OS and Chrome
15 // OS, so use testing base urls.
16 const char kBaseUrlForTesting
[] = "https://www.example.com";
17 const char kBaseDownloadUrlForTesting
[] = "https://download.example.com/p/";
20 class DriveApiUrlGeneratorTest
: public testing::Test
{
22 DriveApiUrlGeneratorTest()
23 : url_generator_(GURL(kBaseUrlForTesting
),
24 GURL(kBaseDownloadUrlForTesting
)) {}
27 DriveApiUrlGenerator url_generator_
;
30 // Make sure the hard-coded urls are returned.
31 TEST_F(DriveApiUrlGeneratorTest
, GetAboutGetUrl
) {
32 EXPECT_EQ("https://www.example.com/drive/v2/about",
33 url_generator_
.GetAboutGetUrl().spec());
36 TEST_F(DriveApiUrlGeneratorTest
, GetAppsListUrl
) {
37 const bool use_internal_url
= true;
38 EXPECT_EQ("https://www.example.com/drive/v2internal/apps",
39 url_generator_
.GetAppsListUrl(use_internal_url
).spec());
40 EXPECT_EQ("https://www.example.com/drive/v2/apps",
41 url_generator_
.GetAppsListUrl(!use_internal_url
).spec());
44 TEST_F(DriveApiUrlGeneratorTest
, GetAppsDeleteUrl
) {
45 EXPECT_EQ("https://www.example.com/drive/v2internal/apps/0ADK06pfg",
46 url_generator_
.GetAppsDeleteUrl("0ADK06pfg").spec());
49 TEST_F(DriveApiUrlGeneratorTest
, GetFilesGetUrl
) {
50 // |file_id| should be embedded into the url.
51 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg",
52 url_generator_
.GetFilesGetUrl("0ADK06pfg", false, GURL()).spec());
53 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074",
54 url_generator_
.GetFilesGetUrl("0Bz0bd074", false, GURL()).spec());
56 "https://www.example.com/drive/v2/files/file%3Afile_id",
57 url_generator_
.GetFilesGetUrl("file:file_id", false, GURL()).spec());
59 // If |use_internal_endpoint| is true, the generated url should point to the
61 EXPECT_EQ("https://www.example.com/drive/v2internal/files/0ADK06pfg",
62 url_generator_
.GetFilesGetUrl("0ADK06pfg", true, GURL()).spec());
64 // If |embed_origin| is not empty, it should be added as a query parameter.
65 url::AddStandardScheme("chrome-extension");
67 "https://www.example.com/drive/v2/files/0ADK06pfg"
68 "?embedOrigin=chrome-extension%3A%2F%2Ftest",
69 url_generator_
.GetFilesGetUrl("0ADK06pfg", false,
70 GURL("chrome-extension://test")).spec());
72 "https://www.example.com/drive/v2internal/files/0ADK06pfg"
73 "?embedOrigin=chrome-extension%3A%2F%2Ftest",
74 url_generator_
.GetFilesGetUrl("0ADK06pfg", true,
75 GURL("chrome-extension://test")).spec());
78 TEST_F(DriveApiUrlGeneratorTest
, GetFilesAuthorizeUrl
) {
80 "https://www.example.com/drive/v2internal/files/aa/authorize?appId=bb",
81 url_generator_
.GetFilesAuthorizeUrl("aa", "bb").spec());
84 TEST_F(DriveApiUrlGeneratorTest
, GetFilesInsertUrl
) {
85 EXPECT_EQ("https://www.example.com/drive/v2/files",
86 url_generator_
.GetFilesInsertUrl().spec());
89 TEST_F(DriveApiUrlGeneratorTest
, GetFilePatchUrl
) {
91 bool set_modified_date
;
92 bool update_viewed_date
;
93 const std::string expected_query
;
95 const TestPattern kTestPatterns
[] = {
97 { true, true, "?setModifiedDate=true" },
98 { false, false, "?updateViewedDate=false" },
99 { true, false, "?setModifiedDate=true&updateViewedDate=false" },
102 for (size_t i
= 0; i
< arraysize(kTestPatterns
); ++i
) {
104 "https://www.example.com/drive/v2/files/0ADK06pfg" +
105 kTestPatterns
[i
].expected_query
,
106 url_generator_
.GetFilesPatchUrl(
107 "0ADK06pfg", kTestPatterns
[i
].set_modified_date
,
108 kTestPatterns
[i
].update_viewed_date
).spec());
111 "https://www.example.com/drive/v2/files/0Bz0bd074" +
112 kTestPatterns
[i
].expected_query
,
113 url_generator_
.GetFilesPatchUrl(
114 "0Bz0bd074", kTestPatterns
[i
].set_modified_date
,
115 kTestPatterns
[i
].update_viewed_date
).spec());
118 "https://www.example.com/drive/v2/files/file%3Afile_id" +
119 kTestPatterns
[i
].expected_query
,
120 url_generator_
.GetFilesPatchUrl(
121 "file:file_id", kTestPatterns
[i
].set_modified_date
,
122 kTestPatterns
[i
].update_viewed_date
).spec());
126 TEST_F(DriveApiUrlGeneratorTest
, GetFilesCopyUrl
) {
127 // |file_id| should be embedded into the url.
128 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg/copy",
129 url_generator_
.GetFilesCopyUrl("0ADK06pfg").spec());
130 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074/copy",
131 url_generator_
.GetFilesCopyUrl("0Bz0bd074").spec());
132 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afile_id/copy",
133 url_generator_
.GetFilesCopyUrl("file:file_id").spec());
136 TEST_F(DriveApiUrlGeneratorTest
, GetFilesListUrl
) {
139 const std::string page_token
;
141 const std::string expected_query
;
143 const TestPattern kTestPatterns
[] = {
145 { 150, "", "", "?maxResults=150" },
146 { 10, "", "", "?maxResults=10" },
147 { 100, "token", "", "?pageToken=token" },
148 { 150, "token", "", "?maxResults=150&pageToken=token" },
149 { 10, "token", "", "?maxResults=10&pageToken=token" },
150 { 100, "", "query", "?q=query" },
151 { 150, "", "query", "?maxResults=150&q=query" },
152 { 10, "", "query", "?maxResults=10&q=query" },
153 { 100, "token", "query", "?pageToken=token&q=query" },
154 { 150, "token", "query", "?maxResults=150&pageToken=token&q=query" },
155 { 10, "token", "query", "?maxResults=10&pageToken=token&q=query" },
158 for (size_t i
= 0; i
< arraysize(kTestPatterns
); ++i
) {
159 EXPECT_EQ("https://www.example.com/drive/v2/files" +
160 kTestPatterns
[i
].expected_query
,
161 url_generator_
.GetFilesListUrl(kTestPatterns
[i
].max_results
,
162 kTestPatterns
[i
].page_token
,
163 kTestPatterns
[i
].q
).spec());
167 TEST_F(DriveApiUrlGeneratorTest
, GetFilesDeleteUrl
) {
168 // |file_id| should be embedded into the url.
169 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg",
170 url_generator_
.GetFilesDeleteUrl("0ADK06pfg").spec());
171 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074",
172 url_generator_
.GetFilesDeleteUrl("0Bz0bd074").spec());
173 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afile_id",
174 url_generator_
.GetFilesDeleteUrl("file:file_id").spec());
177 TEST_F(DriveApiUrlGeneratorTest
, GetFilesTrashUrl
) {
178 // |file_id| should be embedded into the url.
179 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg/trash",
180 url_generator_
.GetFilesTrashUrl("0ADK06pfg").spec());
181 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074/trash",
182 url_generator_
.GetFilesTrashUrl("0Bz0bd074").spec());
183 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afile_id/trash",
184 url_generator_
.GetFilesTrashUrl("file:file_id").spec());
187 TEST_F(DriveApiUrlGeneratorTest
, GetChangesListUrl
) {
189 bool include_deleted
;
191 const std::string page_token
;
192 int64 start_change_id
;
193 const std::string expected_query
;
195 const TestPattern kTestPatterns
[] = {
196 { true, 100, "", 0, "" },
197 { false, 100, "", 0, "?includeDeleted=false" },
198 { true, 150, "", 0, "?maxResults=150" },
199 { false, 150, "", 0, "?includeDeleted=false&maxResults=150" },
200 { true, 10, "", 0, "?maxResults=10" },
201 { false, 10, "", 0, "?includeDeleted=false&maxResults=10" },
203 { true, 100, "token", 0, "?pageToken=token" },
204 { false, 100, "token", 0, "?includeDeleted=false&pageToken=token" },
205 { true, 150, "token", 0, "?maxResults=150&pageToken=token" },
206 { false, 150, "token", 0,
207 "?includeDeleted=false&maxResults=150&pageToken=token" },
208 { true, 10, "token", 0, "?maxResults=10&pageToken=token" },
209 { false, 10, "token", 0,
210 "?includeDeleted=false&maxResults=10&pageToken=token" },
212 { true, 100, "", 12345, "?startChangeId=12345" },
213 { false, 100, "", 12345, "?includeDeleted=false&startChangeId=12345" },
214 { true, 150, "", 12345, "?maxResults=150&startChangeId=12345" },
215 { false, 150, "", 12345,
216 "?includeDeleted=false&maxResults=150&startChangeId=12345" },
217 { true, 10, "", 12345, "?maxResults=10&startChangeId=12345" },
218 { false, 10, "", 12345,
219 "?includeDeleted=false&maxResults=10&startChangeId=12345" },
221 { true, 100, "token", 12345, "?pageToken=token&startChangeId=12345" },
222 { false, 100, "token", 12345,
223 "?includeDeleted=false&pageToken=token&startChangeId=12345" },
224 { true, 150, "token", 12345,
225 "?maxResults=150&pageToken=token&startChangeId=12345" },
226 { false, 150, "token", 12345,
227 "?includeDeleted=false&maxResults=150&pageToken=token"
228 "&startChangeId=12345" },
229 { true, 10, "token", 12345,
230 "?maxResults=10&pageToken=token&startChangeId=12345" },
231 { false, 10, "token", 12345,
232 "?includeDeleted=false&maxResults=10&pageToken=token"
233 "&startChangeId=12345" },
236 for (size_t i
= 0; i
< arraysize(kTestPatterns
); ++i
) {
237 EXPECT_EQ("https://www.example.com/drive/v2/changes" +
238 kTestPatterns
[i
].expected_query
,
239 url_generator_
.GetChangesListUrl(kTestPatterns
[i
].include_deleted
,
240 kTestPatterns
[i
].max_results
,
241 kTestPatterns
[i
].page_token
,
242 kTestPatterns
[i
].start_change_id
)
247 TEST_F(DriveApiUrlGeneratorTest
, GetChildrenInsertUrl
) {
248 // |file_id| should be embedded into the url.
249 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg/children",
250 url_generator_
.GetChildrenInsertUrl("0ADK06pfg").spec());
251 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074/children",
252 url_generator_
.GetChildrenInsertUrl("0Bz0bd074").spec());
253 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afolder_id/children",
254 url_generator_
.GetChildrenInsertUrl("file:folder_id").spec());
257 TEST_F(DriveApiUrlGeneratorTest
, GetChildrenDeleteUrl
) {
258 // |file_id| should be embedded into the url.
260 "https://www.example.com/drive/v2/files/0ADK06pfg/children/0Bz0bd074",
261 url_generator_
.GetChildrenDeleteUrl("0Bz0bd074", "0ADK06pfg").spec());
263 "https://www.example.com/drive/v2/files/0Bz0bd074/children/0ADK06pfg",
264 url_generator_
.GetChildrenDeleteUrl("0ADK06pfg", "0Bz0bd074").spec());
266 "https://www.example.com/drive/v2/files/file%3Afolder_id/children"
268 url_generator_
.GetChildrenDeleteUrl("file:child_id", "file:folder_id")
272 TEST_F(DriveApiUrlGeneratorTest
, GetInitiateUploadNewFileUrl
) {
273 const bool kSetModifiedDate
= true;
276 "https://www.example.com/upload/drive/v2/files?uploadType=resumable",
277 url_generator_
.GetInitiateUploadNewFileUrl(!kSetModifiedDate
).spec());
279 "https://www.example.com/upload/drive/v2/files?uploadType=resumable&"
280 "setModifiedDate=true",
281 url_generator_
.GetInitiateUploadNewFileUrl(kSetModifiedDate
).spec());
284 TEST_F(DriveApiUrlGeneratorTest
, GetInitiateUploadExistingFileUrl
) {
285 const bool kSetModifiedDate
= true;
287 // |resource_id| should be embedded into the url.
289 "https://www.example.com/upload/drive/v2/files/0ADK06pfg"
290 "?uploadType=resumable",
291 url_generator_
.GetInitiateUploadExistingFileUrl(
292 "0ADK06pfg", !kSetModifiedDate
).spec());
294 "https://www.example.com/upload/drive/v2/files/0Bz0bd074"
295 "?uploadType=resumable",
296 url_generator_
.GetInitiateUploadExistingFileUrl(
297 "0Bz0bd074", !kSetModifiedDate
).spec());
299 "https://www.example.com/upload/drive/v2/files/file%3Afile_id"
300 "?uploadType=resumable",
301 url_generator_
.GetInitiateUploadExistingFileUrl(
302 "file:file_id", !kSetModifiedDate
).spec());
304 "https://www.example.com/upload/drive/v2/files/file%3Afile_id"
305 "?uploadType=resumable&setModifiedDate=true",
306 url_generator_
.GetInitiateUploadExistingFileUrl("file:file_id",
307 kSetModifiedDate
).spec());
310 TEST_F(DriveApiUrlGeneratorTest
, GetMultipartUploadNewFileUrl
) {
311 const bool kSetModifiedDate
= true;
314 "https://www.example.com/upload/drive/v2/files?uploadType=multipart",
315 url_generator_
.GetMultipartUploadNewFileUrl(!kSetModifiedDate
).spec());
317 "https://www.example.com/upload/drive/v2/files?uploadType=multipart&"
318 "setModifiedDate=true",
319 url_generator_
.GetMultipartUploadNewFileUrl(kSetModifiedDate
).spec());
322 TEST_F(DriveApiUrlGeneratorTest
, GetMultipartUploadExistingFileUrl
) {
323 const bool kSetModifiedDate
= true;
325 // |resource_id| should be embedded into the url.
327 "https://www.example.com/upload/drive/v2/files/0ADK06pfg"
328 "?uploadType=multipart",
329 url_generator_
.GetMultipartUploadExistingFileUrl(
330 "0ADK06pfg", !kSetModifiedDate
).spec());
332 "https://www.example.com/upload/drive/v2/files/0Bz0bd074"
333 "?uploadType=multipart",
334 url_generator_
.GetMultipartUploadExistingFileUrl(
335 "0Bz0bd074", !kSetModifiedDate
).spec());
337 "https://www.example.com/upload/drive/v2/files/file%3Afile_id"
338 "?uploadType=multipart",
339 url_generator_
.GetMultipartUploadExistingFileUrl(
340 "file:file_id", !kSetModifiedDate
).spec());
342 "https://www.example.com/upload/drive/v2/files/file%3Afile_id"
343 "?uploadType=multipart&setModifiedDate=true",
344 url_generator_
.GetMultipartUploadExistingFileUrl(
345 "file:file_id", kSetModifiedDate
).spec());
348 TEST_F(DriveApiUrlGeneratorTest
, GenerateDownloadFileUrl
) {
349 EXPECT_EQ("https://download.example.com/p/host/resourceId",
350 url_generator_
.GenerateDownloadFileUrl("resourceId").spec());
351 EXPECT_EQ("https://download.example.com/p/host/file%3AresourceId",
352 url_generator_
.GenerateDownloadFileUrl("file:resourceId").spec());
355 TEST_F(DriveApiUrlGeneratorTest
, GeneratePermissionsInsertUrl
) {
356 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg/permissions",
357 url_generator_
.GetPermissionsInsertUrl("0ADK06pfg").spec());
360 TEST_F(DriveApiUrlGeneratorTest
, GenerateThumbnailUrl
) {
362 "https://download.example.com/p/thumb/0ADK06pfg?width=500&height=500",
363 url_generator_
.GetThumbnailUrl("0ADK06pfg", 500, 500).spec());
366 } // namespace google_apis