forked from googleapis/google-http-java-client
/
GenericUrlTest.java
594 lines (502 loc) · 18.4 KB
/
GenericUrlTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
* Copyright (c) 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.api.client.http;
import com.google.api.client.util.Key;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Assert;
/**
* Tests {@link GenericUrl}.
*
* @author Yaniv Inbar
*/
public class GenericUrlTest extends TestCase {
public GenericUrlTest() {
}
public GenericUrlTest(String name) {
super(name);
}
private static final String MINIMAL = "http://bar";
public void testBuild_minimal() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("bar");
assertEquals(MINIMAL, url.build());
}
public void testParse_minimal() {
GenericUrl url = new GenericUrl(MINIMAL);
assertEquals("http", url.getScheme());
}
private static final String NO_PATH = "http://bar?a=b";
public void testBuild_noPath() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("bar");
url.set("a", "b");
assertEquals(NO_PATH, url.build());
}
public void testBuild_noUserInfo() {
GenericUrl url = new GenericUrl(NO_PATH);
assertNull(url.getUserInfo());
}
public void testBuild_noScheme() {
GenericUrl url = new GenericUrl();
try {
url.build();
fail("expected " + NullPointerException.class);
} catch (NullPointerException e) {
// expected
}
}
public void testBuild_noHost() {
GenericUrl url = new GenericUrl();
try {
url.setScheme("http");
url.build();
fail("expected " + NullPointerException.class);
} catch (NullPointerException e) {
// expected
}
}
public void testParse_noPath() {
GenericUrl url = new GenericUrl(NO_PATH);
assertEquals("http", url.getScheme());
assertEquals("bar", url.getHost());
assertEquals("b", url.getFirst("a"));
assertNull(url.getPathParts());
}
private static final String SHORT_PATH = "http://bar/path?a=b";
private static final List<String> SHORT_PATH_PARTS = Arrays.asList("", "path");
public void testBuild_shortPath() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("bar");
url.setPathParts(SHORT_PATH_PARTS);
url.set("a", "b");
assertEquals(SHORT_PATH, url.build());
}
public void testParse_shortPath() {
GenericUrl url = new GenericUrl(SHORT_PATH);
assertEquals("http", url.getScheme());
assertEquals("bar", url.getHost());
assertEquals(SHORT_PATH_PARTS, url.getPathParts());
assertEquals("b", url.getFirst("a"));
}
private static final String LONG_PATH = "http://bar/path/to/resource?a=b";
private static final List<String> LONG_PATH_PARTS = Arrays.asList("", "path", "to", "resource");
public void testBuild_longPath() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("bar");
url.setPathParts(LONG_PATH_PARTS);
url.set("a", "b");
assertEquals(LONG_PATH, url.build());
}
public void testParse_longPath() {
GenericUrl url = new GenericUrl(LONG_PATH);
assertEquals("http", url.getScheme());
assertEquals("bar", url.getHost());
assertEquals(LONG_PATH_PARTS, url.getPathParts());
assertEquals("b", url.getFirst("a"));
}
public static class TestUrl extends GenericUrl {
@Key
String foo;
public String hidden;
public TestUrl() {
}
public TestUrl(String encodedUrl) {
super(encodedUrl);
}
}
private static final String FULL =
"https://user:%3Cpa&$w%40rd%3E@www.google.com:223/m8/feeds/contacts/"
+ "someone=%23%25&%20%3F%3Co%3E%7B%7D@gmail.com/"
+ "full?" + "foo=bar&" + "alt=json&" + "max-results=3&" + "prettyprint=true&"
+ "q=Go%3D%23/%25%26%20?%3Co%3Egle#%3CD@WNL:ADING%3E";
private static final List<String> FULL_PARTS =
Arrays.asList("", "m8", "feeds", "contacts", "someone=#%& ?<o>{}@gmail.com", "full");
private static final String USER_INFO = "user:<pa&$w@rd>";
private static final String FRAGMENT = "<D@WNL:ADING>";
public void testBuild_full() {
TestUrl url = new TestUrl();
url.setScheme("https");
url.setHost("www.google.com");
url.setPort(223);
url.setPathParts(FULL_PARTS);
url.set("alt", "json")
.set("max-results", 3).set("prettyprint", true).set("q", "Go=#/%& ?<o>gle");
url.foo = "bar";
url.hidden = "invisible";
url.setFragment(FRAGMENT);
url.setUserInfo(USER_INFO);
assertEquals(FULL, url.build());
}
public void testParse_full() {
TestUrl url = new TestUrl(FULL);
subtestFull(url);
assertNull(url.hidden);
assertEquals("bar", url.get("foo"));
assertEquals("bar", url.foo);
}
public void testConstructor_url() throws MalformedURLException {
GenericUrl url = new GenericUrl(new URL(FULL));
subtestFull(url);
}
public void testConstructor_uri() throws URISyntaxException {
GenericUrl url = new GenericUrl(new URI(FULL));
subtestFull(url);
}
public void testConstructor_string() {
GenericUrl url = new GenericUrl(FULL);
subtestFull(url);
}
public void testConstructor_schemeToLowerCase() throws URISyntaxException, MalformedURLException {
GenericUrl url = new GenericUrl("HTTps://www.google.com:223");
assertEquals("https", url.getScheme());
url = new GenericUrl(new URI("HTTPS://www.google.com:223"));
assertEquals("https", url.getScheme());
url = new GenericUrl(new URL("hTTPs://www.google.com:223"));
assertEquals("https", url.getScheme());
}
private void subtestFull(GenericUrl url) {
assertEquals("https", url.getScheme());
assertEquals("www.google.com", url.getHost());
assertEquals(223, url.getPort());
assertEquals(FULL_PARTS, url.getPathParts());
assertEquals("json", url.getFirst("alt"));
assertEquals("3", url.getFirst("max-results"));
assertEquals("true", url.getFirst("prettyprint"));
assertEquals("Go=#/%& ?<o>gle", url.getFirst("q"));
assertEquals("bar", url.getFirst("foo"));
assertEquals(FRAGMENT, url.getFragment());
assertEquals(USER_INFO, url.getUserInfo());
}
public static class FieldTypesUrl extends GenericUrl {
@Key
Boolean B;
@Key
Double D;
@Key
Integer I;
@Key
boolean b;
@Key
double d;
@Key
int i;
@Key
String s;
String hidden;
FieldTypesUrl() {
}
FieldTypesUrl(String encodedUrl) {
super(encodedUrl);
}
@Override
public FieldTypesUrl set(String fieldName, Object value) {
return (FieldTypesUrl) super.set(fieldName, value);
}
}
private static final String FIELD_TYPES =
"http://bar?B=true&D=-3.14&I=-3&b=true&d=-3.14&i=-3&s=a&a=b";
public void testBuild_fieldTypes() {
FieldTypesUrl url = new FieldTypesUrl();
url.setScheme("http");
url.setHost("bar");
url.set("a", "b");
url.B = true;
url.D = -3.14;
url.I = -3;
url.b = true;
url.d = -3.14;
url.i = -3;
url.s = "a";
url.hidden = "notHere";
assertEquals(FIELD_TYPES, url.build());
}
public void testParse_fieldTypes() {
FieldTypesUrl url = new FieldTypesUrl(FIELD_TYPES);
assertEquals("http", url.getScheme());
assertEquals("bar", url.getHost());
assertEquals("b", url.getFirst("a"));
assertNull(url.hidden);
assertEquals(true, url.b);
assertEquals(Boolean.TRUE, url.B);
assertEquals(-3.14d, url.d, 1e-5d);
assertEquals(-3.14d, url.D.doubleValue(), 1e-5d);
assertEquals(-3, url.i);
assertEquals(-3, url.I.intValue());
assertEquals("a", url.s);
}
private static final String FRAGMENT1 =
"http://bar/path/to/resource#fragme=%23/%25&%20?%3Co%3Ent";
public void testBuild_fragment1() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("bar");
url.setPathParts(LONG_PATH_PARTS);
url.setFragment("fragme=#/%& ?<o>nt");
assertEquals(FRAGMENT1, url.build());
}
public void testParse_fragment1() {
GenericUrl url = new GenericUrl(FRAGMENT1);
assertEquals("http", url.getScheme());
assertEquals("bar", url.getHost());
assertEquals(LONG_PATH_PARTS, url.getPathParts());
assertEquals("fragme=#/%& ?<o>nt", url.getFragment());
}
private static final String FRAGMENT2 = "http://bar/path/to/resource?a=b#fragment";
public void testBuild_fragment2() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("bar");
url.setPathParts(LONG_PATH_PARTS);
url.set("a", "b");
url.setFragment("fragment");
assertEquals(FRAGMENT2, url.build());
}
public void testParse_fragment2() {
GenericUrl url = new GenericUrl(FRAGMENT2);
assertEquals("http", url.getScheme());
assertEquals("bar", url.getHost());
assertEquals(LONG_PATH_PARTS, url.getPathParts());
assertEquals("b", url.getFirst("a"));
assertEquals("fragment", url.getFragment());
}
public void testBuildAuthority_exception() {
// Test without a scheme.
GenericUrl url = new GenericUrl();
url.setHost("example.com");
try {
url.buildAuthority();
Assert.fail("no exception was thrown");
} catch (NullPointerException expected) {
}
// Test without a host.
url = new GenericUrl();
url.setScheme("https");
try {
url.buildAuthority();
Assert.fail("no exception was thrown");
} catch (NullPointerException expected) {
}
}
public void testBuildAuthority_simple() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("example.com");
assertEquals("http://example.com", url.buildAuthority());
}
public void testBuildAuthority_withPort() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("example.com");
url.setPort(1234);
assertEquals("http://example.com:1234", url.buildAuthority());
}
public void testBuildAuthority_withUserInfo() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("www.example.com");
url.setUserInfo("first.last:pa@@");
assertEquals("http://first.last:pa%40%40@www.example.com", url.buildAuthority());
}
public void testBuildRelativeUrl_empty() {
GenericUrl url = new GenericUrl();
url.setScheme("foo");
url.setHost("bar");
url.setRawPath("");
assertEquals("", url.buildRelativeUrl());
}
public void testBuildRelativeUrl_simple() {
GenericUrl url = new GenericUrl();
url.setScheme("foo");
url.setHost("bar");
url.setRawPath("/example");
assertEquals("/example", url.buildRelativeUrl());
}
public void testBuildRelativeUrl_simpleQuery() {
GenericUrl url = new GenericUrl();
url.setScheme("foo");
url.setHost("bar");
url.setRawPath("/example");
url.put("key", "value");
assertEquals("/example?key=value", url.buildRelativeUrl());
}
public void testBuildRelativeUrl_fragment() {
GenericUrl url = new GenericUrl();
url.setScheme("foo");
url.setHost("bar");
url.setRawPath("/example");
url.setFragment("test");
assertEquals("/example#test", url.buildRelativeUrl());
}
public void testBuildRelativeUrl_onlyQuery() {
GenericUrl url = new GenericUrl();
url.setScheme("foo");
url.setHost("bar");
url.setRawPath("");
url.put("key", "value");
assertEquals("?key=value", url.buildRelativeUrl());
}
private static final String BASE_URL = "http://google.com";
private static final String FULL_PATH = "/some/path/someone%2Fis%2F@gmail.com/test/?one=1&two=2";
public void testBuildRelativeUrl_full() {
GenericUrl url = new GenericUrl(BASE_URL + FULL_PATH);
assertEquals(FULL_PATH, url.buildRelativeUrl());
}
private static final String PATH_WITH_SLASH =
"http://www.google.com/m8/feeds/contacts/someone%2Fis%2F@gmail.com/full/";
private static final List<String> PATH_WITH_SLASH_PARTS =
Arrays.asList("", "m8", "feeds", "contacts", "someone/is/@gmail.com", "full", "");
public void testBuild_pathWithSlash() {
GenericUrl url = new GenericUrl();
url.setScheme("http");
url.setHost("www.google.com");
url.setPathParts(PATH_WITH_SLASH_PARTS);
assertEquals(PATH_WITH_SLASH, url.build());
}
public void testConstructorUnderscore() {
String url = "http://url_with_underscore.google.com";
GenericUrl parsed = new GenericUrl(url);
assertEquals("url_with_underscore.google.com", parsed.getHost());
}
public void testParse_pathWithSlash() {
GenericUrl url = new GenericUrl(PATH_WITH_SLASH);
assertEquals("http", url.getScheme());
assertEquals("www.google.com", url.getHost());
assertEquals(PATH_WITH_SLASH_PARTS, url.getPathParts());
}
public void testToPathParts() {
subtestToPathParts(null, (String[]) null);
subtestToPathParts(null, "");
subtestToPathParts("/", "", "");
subtestToPathParts("a", "a");
subtestToPathParts("/a", "", "a");
subtestToPathParts("/a/", "", "a", "");
subtestToPathParts("path/to/resource", "path", "to", "resource");
subtestToPathParts("/path/to/resource", "", "path", "to", "resource");
subtestToPathParts("/path/to/resource/", "", "path", "to", "resource", "");
subtestToPathParts("/Go%3D%23%2F%25%26%20?%3Co%3Egle/2nd", "", "Go=#/%& ?<o>gle", "2nd");
subtestToPathParts("/path+with+plus/2nd", "", "path+with+plus", "2nd");
subtestToPathParts("/path%2Bwith+plus/2nd", "", "path+with+plus", "2nd");
}
private void subtestToPathParts(String encodedPath, String... expectedDecodedParts) {
List<String> result = GenericUrl.toPathParts(encodedPath);
if (encodedPath == null) {
assertNull(result);
} else {
assertEquals(Arrays.asList(expectedDecodedParts), result);
}
}
public void testAppendPath() {
GenericUrl url = new GenericUrl("http://google.com");
assertNull(url.getPathParts());
url.appendRawPath(null);
assertNull(url.getPathParts());
url.appendRawPath("");
assertNull(url.getPathParts());
url.appendRawPath("/");
assertEquals(Arrays.asList("", ""), url.getPathParts());
url.appendRawPath("/");
assertEquals(Arrays.asList("", "", ""), url.getPathParts());
url.appendRawPath("/a");
assertEquals(Arrays.asList("", "", "", "a"), url.getPathParts());
url.appendRawPath("b");
assertEquals(Arrays.asList("", "", "", "ab"), url.getPathParts());
url.appendRawPath("c/d");
assertEquals(Arrays.asList("", "", "", "abc", "d"), url.getPathParts());
url.appendRawPath("/e");
assertEquals(Arrays.asList("", "", "", "abc", "d", "e"), url.getPathParts());
url.appendRawPath("/");
assertEquals(Arrays.asList("", "", "", "abc", "d", "e", ""), url.getPathParts());
}
private static final String PREFIX = "https://www.googleapis.com";
private static final String REPEATED_PARAM_PATH = "/latitude/v1/location";
private static final List<String> REPEATED_PARAM_PATH_PARTS =
Arrays.asList("", "latitude", "v1", "location");
private static final String REPEATED_PARAM = PREFIX + REPEATED_PARAM_PATH + "?q=c&q=a&q=b&s=e";
public void testRepeatedParam_build() {
GenericUrl url = new GenericUrl();
url.setScheme("https");
url.setHost("www.googleapis.com");
url.setPathParts(REPEATED_PARAM_PATH_PARTS);
url.set("q", Arrays.asList("c", "a", "b"));
url.set("s", "e");
assertEquals(REPEATED_PARAM, url.build());
}
public void testRepeatedParam_parse() {
GenericUrl url = new GenericUrl(REPEATED_PARAM);
assertEquals("https", url.getScheme());
assertEquals("www.googleapis.com", url.getHost());
assertEquals(REPEATED_PARAM_PATH_PARTS, url.getPathParts());
assertEquals("c", url.getFirst("q"));
assertEquals("e", url.getFirst("s"));
assertEquals(Arrays.asList("e"), url.get("s"));
Collection<?> q = (Collection<?>) url.get("q");
Iterator<?> i = q.iterator();
assertEquals("c", i.next());
assertEquals("a", i.next());
assertEquals("b", i.next());
assertFalse(i.hasNext());
assertEquals(Arrays.asList("c", "a", "b"), new ArrayList<Object>(url.getAll("q")));
}
public void testBuild_noValue() {
GenericUrl url = new GenericUrl();
url.setScheme("https");
url.setHost("www.googleapis.com");
url.setRawPath(REPEATED_PARAM_PATH);
url.set("noval", "");
assertEquals(PREFIX + REPEATED_PARAM_PATH + "?noval", url.build());
}
public void testClone() {
GenericUrl url = new GenericUrl("http://www.google.com");
GenericUrl clone = url.clone();
assertEquals("http://www.google.com", clone.build());
}
public void testToUrl_relative() {
// relative redirect
testRedirectUtility("http://www.google.com/test", "http://www.google.com", "/test");
testRedirectUtility("http://www.google.com/test", "http://www.google.com/foo/bar/", "/test");
testRedirectUtility("http://www.google.com/test", "http://www.google.com", "test");
testRedirectUtility("http://www.google.com/test", "http://www.google.com/foo", "test");
testRedirectUtility("http://www.google.com/foo/test", "http://www.google.com/foo/", "test");
testRedirectUtility("http://www.google.com/foo/test", "http://www.google.com/foo/bar", "test");
testRedirectUtility(
"http://www.google.com/foo/test/", "http://www.google.com/foo/bar", "test/");
testRedirectUtility(
"http://www.google.com/foo/test/sub", "http://www.google.com/foo/bar", "test/sub");
// absolute redirect
testRedirectUtility(
"https://example.com/test", "http://www.google.com/foo", "https://example.com/test");
testRedirectUtility(
"https://example.com/test", "http://www.google.com", "https://example.com/test");
testRedirectUtility(
"https://example.com/test", "http://www.google.com/", "https://example.com/test");
}
private void testRedirectUtility(String expectedResult, String url, String relative) {
GenericUrl gu = new GenericUrl(url);
GenericUrl redirectedUrl = new GenericUrl(gu.toURL(relative));
assertEquals(expectedResult, redirectedUrl.toString());
}
}