19
19
import junit .framework .TestCase ;
20
20
import org .junit .Assert ;
21
21
22
+ import java .net .MalformedURLException ;
23
+ import java .net .URI ;
24
+ import java .net .URISyntaxException ;
25
+ import java .net .URL ;
22
26
import java .util .ArrayList ;
23
27
import java .util .Arrays ;
24
28
import java .util .Collection ;
@@ -150,8 +154,8 @@ public TestUrl(String encodedUrl) {
150
154
151
155
private static String FULL =
152
156
"https://www.google.com:223/m8/feeds/contacts/someone=%23%25&%20%3F%3Co%3E%7B%7D@gmail.com/"
153
- + "full?" + "foo=bar&" + "alt=json&" + "max-results=3&" + "prettyprint=true&"
154
- + "q=Go%3D%23/%25%26%20?%3Co%3Egle" ;
157
+ + "full?" + "foo=bar&" + "alt=json&" + "max-results=3&" + "prettyprint=true&"
158
+ + "q=Go%3D%23/%25%26%20?%3Co%3Egle#DOWNLOADING " ;
155
159
156
160
private static List <String > FULL_PARTS =
157
161
Arrays .asList ("" , "m8" , "feeds" , "contacts" , "someone=#%& ?<o>{}@gmail.com" , "full" );
@@ -162,14 +166,47 @@ public void testBuild_full() {
162
166
url .setHost ("www.google.com" );
163
167
url .setPort (223 );
164
168
url .setPathParts (FULL_PARTS );
165
- url .set ("alt" , "json" ).set ("max-results" , 3 ).set ("prettyprint" , true ).set ("q" , "Go=#/%& ?<o>gle" );
169
+ url .set ("alt" , "json" )
170
+ .set ("max-results" , 3 ).set ("prettyprint" , true ).set ("q" , "Go=#/%& ?<o>gle" );
166
171
url .foo = "bar" ;
167
172
url .hidden = "invisible" ;
173
+ url .setFragment ("DOWNLOADING" );
168
174
assertEquals (FULL , url .build ());
169
175
}
170
176
171
177
public void testParse_full () {
172
178
TestUrl url = new TestUrl (FULL );
179
+ subtestFull (url );
180
+ assertNull (url .hidden );
181
+ assertEquals ("bar" , url .get ("foo" ));
182
+ assertEquals ("bar" , url .foo );
183
+ }
184
+
185
+ public void testConstructor_url () throws MalformedURLException {
186
+ GenericUrl url = new GenericUrl (new URL (FULL ));
187
+ subtestFull (url );
188
+ }
189
+
190
+ public void testConstructor_uri () throws URISyntaxException {
191
+ GenericUrl url = new GenericUrl (new URI (FULL ));
192
+ subtestFull (url );
193
+ }
194
+
195
+ public void testConstructor_string () {
196
+ GenericUrl url = new GenericUrl (FULL );
197
+ subtestFull (url );
198
+ }
199
+
200
+ public void testConstructor_schemeToLowerCase () throws URISyntaxException , MalformedURLException {
201
+ GenericUrl url = new GenericUrl ("HTTps://www.google.com:223" );
202
+ assertEquals ("https" , url .getScheme ());
203
+ url = new GenericUrl (new URI ("HTTPS://www.google.com:223" ));
204
+ assertEquals ("https" , url .getScheme ());
205
+ url = new GenericUrl (new URL ("hTTPs://www.google.com:223" ));
206
+ assertEquals ("https" , url .getScheme ());
207
+ }
208
+
209
+ private void subtestFull (GenericUrl url ) {
173
210
assertEquals ("https" , url .getScheme ());
174
211
assertEquals ("www.google.com" , url .getHost ());
175
212
assertEquals (223 , url .getPort ());
@@ -178,10 +215,8 @@ public void testParse_full() {
178
215
assertEquals ("3" , url .getFirst ("max-results" ));
179
216
assertEquals ("true" , url .getFirst ("prettyprint" ));
180
217
assertEquals ("Go=#/%& ?<o>gle" , url .getFirst ("q" ));
181
- assertNull (url .hidden );
182
- assertEquals ("bar" , url .foo );
183
- assertEquals ("bar" , url .get ("foo" ));
184
218
assertEquals ("bar" , url .getFirst ("foo" ));
219
+ assertEquals ("DOWNLOADING" , url .getFragment ());
185
220
}
186
221
187
222
public static class FieldTypesUrl extends GenericUrl {
@@ -303,7 +338,8 @@ public void testBuildAuthority_exception() {
303
338
try {
304
339
url .buildAuthority ();
305
340
Assert .fail ("no exception was thrown" );
306
- } catch (NullPointerException expected ) {}
341
+ } catch (NullPointerException expected ) {
342
+ }
307
343
308
344
// Test without a host.
309
345
url = new GenericUrl ();
@@ -312,7 +348,8 @@ public void testBuildAuthority_exception() {
312
348
try {
313
349
url .buildAuthority ();
314
350
Assert .fail ("no exception was thrown" );
315
- } catch (NullPointerException expected ) {}
351
+ } catch (NullPointerException expected ) {
352
+ }
316
353
}
317
354
318
355
public void testBuildAuthority_simple () {
@@ -377,7 +414,7 @@ public void testBuildRelativeUrl_onlyQuery() {
377
414
private static final String FULL_PATH = "/some/path/someone%2Fis%2F@gmail.com/test/?one=1&two=2" ;
378
415
379
416
public void testBuildRelativeUrl_full () {
380
- GenericUrl url = new GenericUrl (BASE_URL + FULL_PATH );
417
+ GenericUrl url = new GenericUrl (BASE_URL + FULL_PATH );
381
418
assertEquals (FULL_PATH , url .buildRelativeUrl ());
382
419
}
383
420
@@ -497,4 +534,34 @@ public void testClone() {
497
534
GenericUrl clone = url .clone ();
498
535
assertEquals ("http://www.google.com" , clone .build ());
499
536
}
537
+
538
+ public void testToUrl_relative () {
539
+ // relative redirect
540
+ testRedirectUtility ("http://www.google.com/test" , "http://www.google.com" , "/test" );
541
+ testRedirectUtility ("http://www.google.com/test" , "http://www.google.com/foo/bar/" , "/test" );
542
+
543
+ testRedirectUtility ("http://www.google.com/test" , "http://www.google.com" , "test" );
544
+ testRedirectUtility ("http://www.google.com/test" , "http://www.google.com/foo" , "test" );
545
+ testRedirectUtility ("http://www.google.com/foo/test" , "http://www.google.com/foo/" , "test" );
546
+ testRedirectUtility ("http://www.google.com/foo/test" , "http://www.google.com/foo/bar" , "test" );
547
+
548
+ testRedirectUtility (
549
+ "http://www.google.com/foo/test/" , "http://www.google.com/foo/bar" , "test/" );
550
+ testRedirectUtility (
551
+ "http://www.google.com/foo/test/sub" , "http://www.google.com/foo/bar" , "test/sub" );
552
+
553
+ // absolute redirect
554
+ testRedirectUtility (
555
+ "https://example.com/test" , "http://www.google.com/foo" , "https://example.com/test" );
556
+ testRedirectUtility (
557
+ "https://example.com/test" , "http://www.google.com" , "https://example.com/test" );
558
+ testRedirectUtility (
559
+ "https://example.com/test" , "http://www.google.com/" , "https://example.com/test" );
560
+ }
561
+
562
+ private void testRedirectUtility (String expectedResult , String url , String relative ) {
563
+ GenericUrl gu = new GenericUrl (url );
564
+ GenericUrl redirectedUrl = new GenericUrl (gu .toURL (relative ));
565
+ assertEquals (expectedResult , redirectedUrl .toString ());
<
3BCE
tr class="diff-line-row">
566
+ }
500
567
}
0 commit comments