Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / google_apis / drive / drive_api_url_generator_unittest.cc
blob28e79c45460f117831f95b77af57a537276087e2
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"
9 #include "url/gurl.h"
10 #include "url/url_util.h"
12 namespace google_apis {
13 namespace {
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/";
18 } // namespace
20 class DriveApiUrlGeneratorTest : public testing::Test {
21 public:
22 DriveApiUrlGeneratorTest()
23 : url_generator_(GURL(kBaseUrlForTesting),
24 GURL(kBaseDownloadUrlForTesting)) {}
26 protected:
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());
55 EXPECT_EQ(
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
60 // v2internal.
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", url::SCHEME_WITHOUT_PORT);
66 EXPECT_EQ(
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());
71 EXPECT_EQ(
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) {
79 EXPECT_EQ(
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());
87 EXPECT_EQ("https://www.example.com/drive/v2/files?visibility=DEFAULT",
88 url_generator_.GetFilesInsertUrl("DEFAULT").spec());
89 EXPECT_EQ("https://www.example.com/drive/v2/files?visibility=PRIVATE",
90 url_generator_.GetFilesInsertUrl("PRIVATE").spec());
93 TEST_F(DriveApiUrlGeneratorTest, GetFilePatchUrl) {
94 struct TestPattern {
95 bool set_modified_date;
96 bool update_viewed_date;
97 const std::string expected_query;
99 const TestPattern kTestPatterns[] = {
100 { false, true, "" },
101 { true, true, "?setModifiedDate=true" },
102 { false, false, "?updateViewedDate=false" },
103 { true, false, "?setModifiedDate=true&updateViewedDate=false" },
106 for (size_t i = 0; i < arraysize(kTestPatterns); ++i) {
107 EXPECT_EQ(
108 "https://www.example.com/drive/v2/files/0ADK06pfg" +
109 kTestPatterns[i].expected_query,
110 url_generator_.GetFilesPatchUrl(
111 "0ADK06pfg", kTestPatterns[i].set_modified_date,
112 kTestPatterns[i].update_viewed_date).spec());
114 EXPECT_EQ(
115 "https://www.example.com/drive/v2/files/0Bz0bd074" +
116 kTestPatterns[i].expected_query,
117 url_generator_.GetFilesPatchUrl(
118 "0Bz0bd074", kTestPatterns[i].set_modified_date,
119 kTestPatterns[i].update_viewed_date).spec());
121 EXPECT_EQ(
122 "https://www.example.com/drive/v2/files/file%3Afile_id" +
123 kTestPatterns[i].expected_query,
124 url_generator_.GetFilesPatchUrl(
125 "file:file_id", kTestPatterns[i].set_modified_date,
126 kTestPatterns[i].update_viewed_date).spec());
130 TEST_F(DriveApiUrlGeneratorTest, GetFilesCopyUrl) {
131 // |file_id| should be embedded into the url.
132 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg/copy",
133 url_generator_.GetFilesCopyUrl("0ADK06pfg", "").spec());
134 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074/copy",
135 url_generator_.GetFilesCopyUrl("0Bz0bd074", "").spec());
136 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afile_id/copy",
137 url_generator_.GetFilesCopyUrl("file:file_id", "").spec());
138 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074/copy"
139 "?visibility=DEFAULT",
140 url_generator_.GetFilesCopyUrl("0Bz0bd074", "DEFAULT").spec());
141 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afile_id/copy"
142 "?visibility=PRIVATE",
143 url_generator_.GetFilesCopyUrl("file:file_id", "PRIVATE").spec());
146 TEST_F(DriveApiUrlGeneratorTest, GetFilesListUrl) {
147 struct TestPattern {
148 int max_results;
149 const std::string page_token;
150 const std::string q;
151 const std::string expected_query;
153 const TestPattern kTestPatterns[] = {
154 { 100, "", "", "" },
155 { 150, "", "", "?maxResults=150" },
156 { 10, "", "", "?maxResults=10" },
157 { 100, "token", "", "?pageToken=token" },
158 { 150, "token", "", "?maxResults=150&pageToken=token" },
159 { 10, "token", "", "?maxResults=10&pageToken=token" },
160 { 100, "", "query", "?q=query" },
161 { 150, "", "query", "?maxResults=150&q=query" },
162 { 10, "", "query", "?maxResults=10&q=query" },
163 { 100, "token", "query", "?pageToken=token&q=query" },
164 { 150, "token", "query", "?maxResults=150&pageToken=token&q=query" },
165 { 10, "token", "query", "?maxResults=10&pageToken=token&q=query" },
168 for (size_t i = 0; i < arraysize(kTestPatterns); ++i) {
169 EXPECT_EQ("https://www.example.com/drive/v2/files" +
170 kTestPatterns[i].expected_query,
171 url_generator_.GetFilesListUrl(kTestPatterns[i].max_results,
172 kTestPatterns[i].page_token,
173 kTestPatterns[i].q).spec());
177 TEST_F(DriveApiUrlGeneratorTest, GetFilesDeleteUrl) {
178 // |file_id| should be embedded into the url.
179 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg",
180 url_generator_.GetFilesDeleteUrl("0ADK06pfg").spec());
181 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074",
182 url_generator_.GetFilesDeleteUrl("0Bz0bd074").spec());
183 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afile_id",
184 url_generator_.GetFilesDeleteUrl("file:file_id").spec());
187 TEST_F(DriveApiUrlGeneratorTest, GetFilesTrashUrl) {
188 // |file_id| should be embedded into the url.
189 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg/trash",
190 url_generator_.GetFilesTrashUrl("0ADK06pfg").spec());
191 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074/trash",
192 url_generator_.GetFilesTrashUrl("0Bz0bd074").spec());
193 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afile_id/trash",
194 url_generator_.GetFilesTrashUrl("file:file_id").spec());
197 TEST_F(DriveApiUrlGeneratorTest, GetChangesListUrl) {
198 struct TestPattern {
199 bool include_deleted;
200 int max_results;
201 const std::string page_token;
202 int64 start_change_id;
203 const std::string expected_query;
205 const TestPattern kTestPatterns[] = {
206 { true, 100, "", 0, "" },
207 { false, 100, "", 0, "?includeDeleted=false" },
208 { true, 150, "", 0, "?maxResults=150" },
209 { false, 150, "", 0, "?includeDeleted=false&maxResults=150" },
210 { true, 10, "", 0, "?maxResults=10" },
211 { false, 10, "", 0, "?includeDeleted=false&maxResults=10" },
213 { true, 100, "token", 0, "?pageToken=token" },
214 { false, 100, "token", 0, "?includeDeleted=false&pageToken=token" },
215 { true, 150, "token", 0, "?maxResults=150&pageToken=token" },
216 { false, 150, "token", 0,
217 "?includeDeleted=false&maxResults=150&pageToken=token" },
218 { true, 10, "token", 0, "?maxResults=10&pageToken=token" },
219 { false, 10, "token", 0,
220 "?includeDeleted=false&maxResults=10&pageToken=token" },
222 { true, 100, "", 12345, "?startChangeId=12345" },
223 { false, 100, "", 12345, "?includeDeleted=false&startChangeId=12345" },
224 { true, 150, "", 12345, "?maxResults=150&startChangeId=12345" },
225 { false, 150, "", 12345,
226 "?includeDeleted=false&maxResults=150&startChangeId=12345" },
227 { true, 10, "", 12345, "?maxResults=10&startChangeId=12345" },
228 { false, 10, "", 12345,
229 "?includeDeleted=false&maxResults=10&startChangeId=12345" },
231 { true, 100, "token", 12345, "?pageToken=token&startChangeId=12345" },
232 { false, 100, "token", 12345,
233 "?includeDeleted=false&pageToken=token&startChangeId=12345" },
234 { true, 150, "token", 12345,
235 "?maxResults=150&pageToken=token&startChangeId=12345" },
236 { false, 150, "token", 12345,
237 "?includeDeleted=false&maxResults=150&pageToken=token"
238 "&startChangeId=12345" },
239 { true, 10, "token", 12345,
240 "?maxResults=10&pageToken=token&startChangeId=12345" },
241 { false, 10, "token", 12345,
242 "?includeDeleted=false&maxResults=10&pageToken=token"
243 "&startChangeId=12345" },
246 for (size_t i = 0; i < arraysize(kTestPatterns); ++i) {
247 EXPECT_EQ("https://www.example.com/drive/v2/changes" +
248 kTestPatterns[i].expected_query,
249 url_generator_.GetChangesListUrl(kTestPatterns[i].include_deleted,
250 kTestPatterns[i].max_results,
251 kTestPatterns[i].page_token,
252 kTestPatterns[i].start_change_id)
253 .spec());
257 TEST_F(DriveApiUrlGeneratorTest, GetChildrenInsertUrl) {
258 // |file_id| should be embedded into the url.
259 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg/children",
260 url_generator_.GetChildrenInsertUrl("0ADK06pfg").spec());
261 EXPECT_EQ("https://www.example.com/drive/v2/files/0Bz0bd074/children",
262 url_generator_.GetChildrenInsertUrl("0Bz0bd074").spec());
263 EXPECT_EQ("https://www.example.com/drive/v2/files/file%3Afolder_id/children",
264 url_generator_.GetChildrenInsertUrl("file:folder_id").spec());
267 TEST_F(DriveApiUrlGeneratorTest, GetChildrenDeleteUrl) {
268 // |file_id| should be embedded into the url.
269 EXPECT_EQ(
270 "https://www.example.com/drive/v2/files/0ADK06pfg/children/0Bz0bd074",
271 url_generator_.GetChildrenDeleteUrl("0Bz0bd074", "0ADK06pfg").spec());
272 EXPECT_EQ(
273 "https://www.example.com/drive/v2/files/0Bz0bd074/children/0ADK06pfg",
274 url_generator_.GetChildrenDeleteUrl("0ADK06pfg", "0Bz0bd074").spec());
275 EXPECT_EQ(
276 "https://www.example.com/drive/v2/files/file%3Afolder_id/children"
277 "/file%3Achild_id",
278 url_generator_.GetChildrenDeleteUrl("file:child_id", "file:folder_id")
279 .spec());
282 TEST_F(DriveApiUrlGeneratorTest, GetInitiateUploadNewFileUrl) {
283 const bool kSetModifiedDate = true;
285 EXPECT_EQ(
286 "https://www.example.com/upload/drive/v2/files?uploadType=resumable",
287 url_generator_.GetInitiateUploadNewFileUrl(!kSetModifiedDate).spec());
288 EXPECT_EQ(
289 "https://www.example.com/upload/drive/v2/files?uploadType=resumable&"
290 "setModifiedDate=true",
291 url_generator_.GetInitiateUploadNewFileUrl(kSetModifiedDate).spec());
294 TEST_F(DriveApiUrlGeneratorTest, GetInitiateUploadExistingFileUrl) {
295 const bool kSetModifiedDate = true;
297 // |resource_id| should be embedded into the url.
298 EXPECT_EQ(
299 "https://www.example.com/upload/drive/v2/files/0ADK06pfg"
300 "?uploadType=resumable",
301 url_generator_.GetInitiateUploadExistingFileUrl(
302 "0ADK06pfg", !kSetModifiedDate).spec());
303 EXPECT_EQ(
304 "https://www.example.com/upload/drive/v2/files/0Bz0bd074"
305 "?uploadType=resumable",
306 url_generator_.GetInitiateUploadExistingFileUrl(
307 "0Bz0bd074", !kSetModifiedDate).spec());
308 EXPECT_EQ(
309 "https://www.example.com/upload/drive/v2/files/file%3Afile_id"
310 "?uploadType=resumable",
311 url_generator_.GetInitiateUploadExistingFileUrl(
312 "file:file_id", !kSetModifiedDate).spec());
313 EXPECT_EQ(
314 "https://www.example.com/upload/drive/v2/files/file%3Afile_id"
315 "?uploadType=resumable&setModifiedDate=true",
316 url_generator_.GetInitiateUploadExistingFileUrl("file:file_id",
317 kSetModifiedDate).spec());
320 TEST_F(DriveApiUrlGeneratorTest, GetMultipartUploadNewFileUrl) {
321 const bool kSetModifiedDate = true;
323 EXPECT_EQ(
324 "https://www.example.com/upload/drive/v2/files?uploadType=multipart",
325 url_generator_.GetMultipartUploadNewFileUrl(!kSetModifiedDate).spec());
326 EXPECT_EQ(
327 "https://www.example.com/upload/drive/v2/files?uploadType=multipart&"
328 "setModifiedDate=true",
329 url_generator_.GetMultipartUploadNewFileUrl(kSetModifiedDate).spec());
332 TEST_F(DriveApiUrlGeneratorTest, GetMultipartUploadExistingFileUrl) {
333 const bool kSetModifiedDate = true;
335 // |resource_id| should be embedded into the url.
336 EXPECT_EQ(
337 "https://www.example.com/upload/drive/v2/files/0ADK06pfg"
338 "?uploadType=multipart",
339 url_generator_.GetMultipartUploadExistingFileUrl(
340 "0ADK06pfg", !kSetModifiedDate).spec());
341 EXPECT_EQ(
342 "https://www.example.com/upload/drive/v2/files/0Bz0bd074"
343 "?uploadType=multipart",
344 url_generator_.GetMultipartUploadExistingFileUrl(
345 "0Bz0bd074", !kSetModifiedDate).spec());
346 EXPECT_EQ(
347 "https://www.example.com/upload/drive/v2/files/file%3Afile_id"
348 "?uploadType=multipart",
349 url_generator_.GetMultipartUploadExistingFileUrl(
350 "file:file_id", !kSetModifiedDate).spec());
351 EXPECT_EQ(
352 "https://www.example.com/upload/drive/v2/files/file%3Afile_id"
353 "?uploadType=multipart&setModifiedDate=true",
354 url_generator_.GetMultipartUploadExistingFileUrl(
355 "file:file_id", kSetModifiedDate).spec());
358 TEST_F(DriveApiUrlGeneratorTest, GenerateDownloadFileUrl) {
359 EXPECT_EQ("https://download.example.com/p/host/resourceId",
360 url_generator_.GenerateDownloadFileUrl("resourceId").spec());
361 EXPECT_EQ("https://download.example.com/p/host/file%3AresourceId",
362 url_generator_.GenerateDownloadFileUrl("file:resourceId").spec());
365 TEST_F(DriveApiUrlGeneratorTest, GeneratePermissionsInsertUrl) {
366 EXPECT_EQ("https://www.example.com/drive/v2/files/0ADK06pfg/permissions",
367 url_generator_.GetPermissionsInsertUrl("0ADK06pfg").spec());
370 TEST_F(DriveApiUrlGeneratorTest, GenerateThumbnailUrl) {
371 EXPECT_EQ(
372 "https://download.example.com/p/thumb/0ADK06pfg?width=500&height=500",
373 url_generator_.GetThumbnailUrl("0ADK06pfg", 500, 500, false).spec());
375 EXPECT_EQ(
376 "https://download.example.com/p/thumb/"
377 "0ADK06pfg?width=360&height=360&crop=true",
378 url_generator_.GetThumbnailUrl("0ADK06pfg", 360, 360, true).spec());
381 TEST_F(DriveApiUrlGeneratorTest, BatchUploadUrl) {
382 EXPECT_EQ("https://www.example.com/upload/drive",
383 url_generator_.GetBatchUploadUrl().spec());
386 } // namespace google_apis