View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.util;
16  
17  import static java.nio.charset.StandardCharsets.ISO_8859_1;
18  
19  import java.net.InetAddress;
20  import java.net.URL;
21  
22  import org.htmlunit.SimpleWebTestCase;
23  import org.junit.Test;
24  
25  /**
26   * Tests for {@link UrlUtils}.
27   *
28   * @author Daniel Gredler
29   * @author Martin Tamme
30   * @author Sudhan Moghe
31   * @author Ahmed Ashour
32   * @author Ronald Brill
33   * @author Hartmut Arlt
34   */
35  public class UrlUtilsTest extends SimpleWebTestCase {
36  
37      /**
38       * @throws Exception if the test fails
39       */
40      @Test
41      public void toUrlSafe() throws Exception {
42          URL url = UrlUtils.toUrlSafe("http://my.home.com/index.html?query#ref");
43          assertEquals("http://my.home.com/index.html?query#ref", url.toExternalForm());
44  
45          url = UrlUtils.toUrlSafe("about:blank");
46          assertEquals(UrlUtils.URL_ABOUT_BLANK, url);
47  
48          url = UrlUtils.toUrlSafe("about:Blank");
49          assertEquals(UrlUtils.URL_ABOUT_BLANK, url);
50  
51          url = UrlUtils.toUrlSafe("about:config");
52          assertEquals("about:config", url.toExternalForm());
53      }
54  
55      /**
56       * @throws Exception if the test fails
57       */
58      @Test
59      public void getUrlWithNewProtocol() throws Exception {
60          final URL a = new URL("http://my.home.com/index.html?query#ref");
61          final URL b = UrlUtils.getUrlWithNewProtocol(a, "ftp");
62          assertEquals("ftp://my.home.com/index.html?query#ref", b.toExternalForm());
63      }
64  
65      /**
66       * @throws Exception if the test fails
67       */
68      @Test
69      public void getUrlWithNewHost() throws Exception {
70          URL a = new URL("http://my.home.com/index.html?query#ref");
71          URL b = UrlUtils.getUrlWithNewHost(a, "your.home.com");
72          assertEquals("http://your.home.com/index.html?query#ref", b.toExternalForm());
73  
74          // preserve user info
75          a = new URL("http://john.smith:secret@my.home.com/index.html?query#ref");
76          b = UrlUtils.getUrlWithNewHost(a, "your.home.com");
77          assertEquals("http://john.smith:secret@your.home.com/index.html?query#ref", b.toExternalForm());
78  
79          a = new URL("http://john.smith:@my.home.com/index.html?query#ref");
80          b = UrlUtils.getUrlWithNewHost(a, "your.home.com");
81          assertEquals("http://john.smith:@your.home.com/index.html?query#ref", b.toExternalForm());
82  
83          a = new URL("http://john.smith@my.home.com/index.html?query#ref");
84          b = UrlUtils.getUrlWithNewHost(a, "your.home.com");
85          assertEquals("http://john.smith@your.home.com/index.html?query#ref", b.toExternalForm());
86  
87          a = new URL("http://@my.home.com/index.html?query#ref");
88          b = UrlUtils.getUrlWithNewHost(a, "your.home.com");
89          assertEquals("http://@your.home.com/index.html?query#ref", b.toExternalForm());
90      }
91  
92      /**
93       * @throws Exception if the test fails
94       */
95      @Test
96      public void getUrlWithNewHostAndPort() throws Exception {
97          URL a = new URL("http://my.home.com/index.html?query#ref");
98          URL b = UrlUtils.getUrlWithNewHostAndPort(a, "your.home.com", 4711);
99          assertEquals("http://your.home.com:4711/index.html?query#ref", b.toExternalForm());
100 
101         b = UrlUtils.getUrlWithNewHostAndPort(a, "your.home.com", -1);
102         assertEquals("http://your.home.com/index.html?query#ref", b.toExternalForm());
103 
104         a = new URL("http://john.smith:secret@my.home.com/index.html?query#ref");
105         b = UrlUtils.getUrlWithNewHostAndPort(a, "your.home.com", -1);
106         assertEquals("http://john.smith:secret@your.home.com/index.html?query#ref", b.toExternalForm());
107     }
108 
109     /**
110      * @throws Exception if the test fails
111      */
112     @Test
113     public void getUrlWithNewPort() throws Exception {
114         URL a = new URL("http://my.home.com/index.html?query#ref");
115         URL b = UrlUtils.getUrlWithNewPort(a, 8080);
116         assertEquals("http://my.home.com:8080/index.html?query#ref", b.toExternalForm());
117 
118         b = UrlUtils.getUrlWithNewPort(a, -1);
119         assertEquals("http://my.home.com/index.html?query#ref", b.toExternalForm());
120 
121         a = new URL("http://john.smith:secret@my.home.com/index.html?query#ref");
122         b = UrlUtils.getUrlWithNewPort(a, 8080);
123         assertEquals("http://john.smith:secret@my.home.com:8080/index.html?query#ref", b.toExternalForm());
124     }
125 
126     /**
127      * @throws Exception if the test fails
128      */
129     @Test
130     public void getUrlWithNewPath() throws Exception {
131         final URL a = new URL("http://my.home.com/index.html?query#ref");
132         final URL b = UrlUtils.getUrlWithNewPath(a, "/es/indice.html");
133         assertEquals("http://my.home.com/es/indice.html?query#ref", b.toExternalForm());
134     }
135 
136     /**
137      * @throws Exception if the test fails
138      */
139     @Test
140     public void getUrlWithNewRef() throws Exception {
141         URL a = new URL("http://my.home.com/index.html?query#ref");
142         URL b = UrlUtils.getUrlWithNewRef(a, "abc");
143         assertEquals("http://my.home.com/index.html?query#abc", b.toExternalForm());
144 
145         a = new URL("http://my.home.com/#ref");
146         b = UrlUtils.getUrlWithNewRef(a, "xyz");
147         assertEquals("http://my.home.com/#xyz", b.toExternalForm());
148 
149         a = new URL("http://my.home.com#ref");
150         b = UrlUtils.getUrlWithNewRef(a, "xyz");
151         assertEquals("http://my.home.com#xyz", b.toExternalForm());
152 
153         a = new URL("http://my.home.com");
154         b = UrlUtils.getUrlWithNewRef(a, "xyz");
155         assertEquals("http://my.home.com#xyz", b.toExternalForm());
156 
157         a = new URL("http://my.home.com");
158         b = UrlUtils.getUrlWithNewRef(a, null);
159         assertEquals("http://my.home.com", b.toExternalForm());
160 
161         a = new URL("http://my.home.com");
162         b = UrlUtils.getUrlWithNewRef(a, "");
163         assertEquals("http://my.home.com#", b.toExternalForm());
164     }
165 
166     /**
167      * @throws Exception if the test fails
168      */
169     @Test
170     public void getUrlWithNewQuery() throws Exception {
171         URL a = new URL("http://my.home.com/index.html?query#ref");
172         URL b = UrlUtils.getUrlWithNewQuery(a, "xyz");
173         assertEquals("http://my.home.com/index.html?xyz#ref", b.toExternalForm());
174 
175         // DOS
176         a = new URL("file://c:/index.html?query");
177         b = UrlUtils.getUrlWithNewQuery(a, "xyz");
178         assertEquals("file://c:/index.html?xyz", b.toExternalForm());
179         // UNIX
180         a = new URL("file:///index.html?query");
181         b = UrlUtils.getUrlWithNewQuery(a, "xyz");
182         assertEquals("file:/index.html?xyz", b.toExternalForm());
183     }
184 
185     /**
186      * @throws Exception if the test fails
187      */
188     @Test
189     public void getUrlWithProtocolAndAuthority() throws Exception {
190         URL a = new URL("http://my.home.com/index.html?query#ref");
191         URL b = UrlUtils.getUrlWithProtocolAndAuthority(a);
192         assertEquals("http://my.home.com", b.toExternalForm());
193 
194         a = new URL("http://my.home.com/folder/index.html?query#ref");
195         b = UrlUtils.getUrlWithProtocolAndAuthority(a);
196         assertEquals("http://my.home.com", b.toExternalForm());
197 
198         a = new URL("http://my.home.com/folder/subfolder/index.html");
199         b = UrlUtils.getUrlWithProtocolAndAuthority(a);
200         assertEquals("http://my.home.com", b.toExternalForm());
201 
202         a = new URL("http://my.home.com");
203         b = UrlUtils.getUrlWithProtocolAndAuthority(a);
204         assertEquals("http://my.home.com", b.toExternalForm());
205 
206         a = new URL("http://my.home.com?query");
207         b = UrlUtils.getUrlWithProtocolAndAuthority(a);
208         assertEquals("http://my.home.com", b.toExternalForm());
209 
210         a = new URL("http://my.home.com/");
211         b = UrlUtils.getUrlWithProtocolAndAuthority(a);
212         assertEquals("http://my.home.com", b.toExternalForm());
213 
214         a = new URL("http://my.home.com/#href");
215         b = UrlUtils.getUrlWithProtocolAndAuthority(a);
216         assertEquals("http://my.home.com", b.toExternalForm());
217     }
218 
219     /**
220      * @throws Exception if the test fails
221      */
222     @Test
223     public void getUrlWithNewUserName() throws Exception {
224         URL a = new URL("http://my.home.com/index.html?query");
225         URL b = UrlUtils.getUrlWithNewUserName(a, "abc");
226         assertEquals("http://abc@my.home.com/index.html?query", b.toExternalForm());
227 
228         a = new URL("http://my.home.com");
229         b = UrlUtils.getUrlWithNewUserName(a, "xyz");
230         assertEquals("http://xyz@my.home.com", b.toExternalForm());
231 
232         a = new URL("http://user:pw@my.home.com");
233         b = UrlUtils.getUrlWithNewUserName(a, "xyz");
234         assertEquals("http://xyz:pw@my.home.com", b.toExternalForm());
235 
236         a = new URL("http://user:@my.home.com");
237         b = UrlUtils.getUrlWithNewUserName(a, "xyz");
238         assertEquals("http://xyz:@my.home.com", b.toExternalForm());
239 
240         a = new URL("http://user@my.home.com");
241         b = UrlUtils.getUrlWithNewUserName(a, "xyz");
242         assertEquals("http://xyz@my.home.com", b.toExternalForm());
243 
244         a = new URL("http://:pw@my.home.com");
245         b = UrlUtils.getUrlWithNewUserName(a, "xyz");
246         assertEquals("http://xyz:pw@my.home.com", b.toExternalForm());
247     }
248 
249     /**
250      * @throws Exception if the test fails
251      */
252     @Test
253     public void getUrlWithNewUserPassword() throws Exception {
254         URL a = new URL("http://my.home.com/index.html?query");
255         URL b = UrlUtils.getUrlWithNewUserPassword(a, "abc");
256         assertEquals("http://:abc@my.home.com/index.html?query", b.toExternalForm());
257 
258         a = new URL("http://my.home.com");
259         b = UrlUtils.getUrlWithNewUserPassword(a, "xyz");
260         assertEquals("http://:xyz@my.home.com", b.toExternalForm());
261 
262         a = new URL("http://user:pw@my.home.com");
263         b = UrlUtils.getUrlWithNewUserPassword(a, "xyz");
264         assertEquals("http://user:xyz@my.home.com", b.toExternalForm());
265 
266         a = new URL("http://user:@my.home.com");
267         b = UrlUtils.getUrlWithNewUserPassword(a, "xyz");
268         assertEquals("http://user:xyz@my.home.com", b.toExternalForm());
269 
270         a = new URL("http://user@my.home.com");
271         b = UrlUtils.getUrlWithNewUserPassword(a, "xyz");
272         assertEquals("http://user:xyz@my.home.com", b.toExternalForm());
273 
274         a = new URL("http://:pw@my.home.com");
275         b = UrlUtils.getUrlWithNewUserPassword(a, "xyz");
276         assertEquals("http://:xyz@my.home.com", b.toExternalForm());
277     }
278 
279     /**
280      * Test {@link UrlUtils#resolveUrl(String, String)} with the normal examples taken from
281      * <a href="http://www.faqs.org/rfcs/rfc1808.html">RFC1808</a> Section 5.1.
282      */
283     @Test
284     public void resolveUrlWithNormalExamples() {
285         final String baseUrl = "http://a/b/c/d;p?q#f";
286 
287         assertEquals("g:h",                  UrlUtils.resolveUrl(baseUrl, "g:h"));
288         assertEquals("http://a/b/c/g",       UrlUtils.resolveUrl(baseUrl, "g"));
289         assertEquals("http://a/b/c/g",       UrlUtils.resolveUrl(baseUrl, "./g"));
290         assertEquals("http://a/b/c/g/",      UrlUtils.resolveUrl(baseUrl, "g/"));
291         assertEquals("http://a/g",           UrlUtils.resolveUrl(baseUrl, "/g"));
292         assertEquals("http://g",             UrlUtils.resolveUrl(baseUrl, "//g"));
293         assertEquals("http://a/b/c/d;p?y",   UrlUtils.resolveUrl(baseUrl, "?y"));
294         assertEquals("http://a/b/c/g?y",     UrlUtils.resolveUrl(baseUrl, "g?y"));
295         assertEquals("http://a/b/c/g?y/./x", UrlUtils.resolveUrl(baseUrl, "g?y/./x"));
296         assertEquals("http://a/b/c/d;p?q#s", UrlUtils.resolveUrl(baseUrl, "#s"));
297         assertEquals("http://a/b/c/g#s",     UrlUtils.resolveUrl(baseUrl, "g#s"));
298         assertEquals("http://a/b/c/g#s/./x", UrlUtils.resolveUrl(baseUrl, "g#s/./x"));
299         assertEquals("http://a/b/c/g?y#s",   UrlUtils.resolveUrl(baseUrl, "g?y#s"));
300         assertEquals("http://a/b/c/d;x",     UrlUtils.resolveUrl(baseUrl, ";x"));
301         assertEquals("http://a/b/c/g;x",     UrlUtils.resolveUrl(baseUrl, "g;x"));
302         assertEquals("http://a/b/c/g;x?y#s", UrlUtils.resolveUrl(baseUrl, "g;x?y#s"));
303         assertEquals("http://a/b/c/",        UrlUtils.resolveUrl(baseUrl, "."));
304         assertEquals("http://a/b/c/",        UrlUtils.resolveUrl(baseUrl, "./"));
305         assertEquals("http://a/b/",          UrlUtils.resolveUrl(baseUrl, ".."));
306         assertEquals("http://a/b/",          UrlUtils.resolveUrl(baseUrl, "../"));
307         assertEquals("http://a/b/g",         UrlUtils.resolveUrl(baseUrl, "../g"));
308         assertEquals("http://a/",            UrlUtils.resolveUrl(baseUrl, "../.."));
309         assertEquals("http://a/",            UrlUtils.resolveUrl(baseUrl, "../../"));
310         assertEquals("http://a/g",           UrlUtils.resolveUrl(baseUrl, "../../g"));
311 
312         //Following two cases were failing when original implementation was modified to handle
313         //the cases given in RFC 1808. Lots of other test cases failed because of that.
314         assertEquals(URL_FIRST + "foo.xml", UrlUtils.resolveUrl(URL_FIRST, "/foo.xml"));
315         assertEquals(URL_FIRST + "foo.xml", UrlUtils.resolveUrl(URL_FIRST, "foo.xml"));
316     }
317 
318     /**
319      * Test {@link UrlUtils#resolveUrl(String, String)} with the abnormal examples taken from
320      * <a href="http://www.faqs.org/rfcs/rfc1808.html">RFC1808</a> Section 5.2.
321      */
322     @Test
323     public void resolveUrlWithAbnormalExamples() {
324         final String baseUrl = "http://a/b/c/d;p?q#f";
325 
326         assertEquals("http://a/b/c/d;p?q#f", UrlUtils.resolveUrl(baseUrl, ""));
327 
328 // These examples are fine... but it's not what browsers do (see below)
329 //        assertEquals("http://a/../g",        UrlUtils.resolveUrl(baseUrl, "../../../g"));
330 //        assertEquals("http://a/../../g",     UrlUtils.resolveUrl(baseUrl, "../../../../g"));
331 //        assertEquals("http://a/./g",         UrlUtils.resolveUrl(baseUrl, "/./g"));
332 //        assertEquals("http://a/../g",        UrlUtils.resolveUrl(baseUrl, "/../g"));
333         assertEquals("http://a/g",        UrlUtils.resolveUrl(baseUrl, "../../../g"));
334         assertEquals("http://a/g",     UrlUtils.resolveUrl(baseUrl, "../../../../g"));
335         assertEquals("http://a/./g",         UrlUtils.resolveUrl(baseUrl, "/./g"));
336         assertEquals("http://a/g",        UrlUtils.resolveUrl(baseUrl, "/../g"));
337 
338         assertEquals("http://a/g",           UrlUtils.resolveUrl(baseUrl, "/../../g"));
339         assertEquals("http://a/.g",          UrlUtils.resolveUrl(baseUrl, "/.g"));
340         assertEquals("http://a/..g",         UrlUtils.resolveUrl(baseUrl, "/..g"));
341         assertEquals("http://a/...g",        UrlUtils.resolveUrl(baseUrl, "/...g"));
342 
343         assertEquals("http://a/b/c/g.",      UrlUtils.resolveUrl(baseUrl, "g."));
344         assertEquals("http://a/b/c/.g",      UrlUtils.resolveUrl(baseUrl, ".g"));
345         assertEquals("http://a/b/c/g..",     UrlUtils.resolveUrl(baseUrl, "g.."));
346         assertEquals("http://a/b/c/..g",     UrlUtils.resolveUrl(baseUrl, "..g"));
347         assertEquals("http://a/b/g",         UrlUtils.resolveUrl(baseUrl, "./../g"));
348         assertEquals("http://a/b/c/g/",      UrlUtils.resolveUrl(baseUrl, "./g/."));
349         assertEquals("http://a/b/c/g/h",     UrlUtils.resolveUrl(baseUrl, "g/./h"));
350         assertEquals("http://a/b/c/h",       UrlUtils.resolveUrl(baseUrl, "g/../h"));
351         assertEquals("http:g",               UrlUtils.resolveUrl(baseUrl, "http:g"));
352         assertEquals("http:",                UrlUtils.resolveUrl(baseUrl, "http:"));
353     }
354 
355     /**
356      * Test {@link UrlUtils#resolveUrl(String, String)} with extra examples.
357      */
358     @Test
359     public void resolveUrlWithExtraExamples() {
360         final String baseUrl = "http://a/b/c/d;p?q#f";
361 
362         assertEquals("http://a/b/c/d;",      UrlUtils.resolveUrl(baseUrl, ";"));
363         assertEquals("http://a/b/c/d;p?",    UrlUtils.resolveUrl(baseUrl, "?"));
364         assertEquals("http://a/b/c/d;p?q#",  UrlUtils.resolveUrl(baseUrl, "#"));
365         assertEquals("http://a/b/c/d;p?q#s", UrlUtils.resolveUrl(baseUrl, "#s"));
366 
367         assertEquals("http://a/f.html", UrlUtils.resolveUrl("http://a/otherFile.html", "../f.html"));
368         assertEquals("http://a/f.html", UrlUtils.resolveUrl("http://a/otherFile.html", "../../f.html"));
369     }
370 
371     /**
372      * Test {@link UrlUtils#resolveUrl(String, String)} with extra examples.
373      */
374     @Test
375     public void resolveUrlWithWhitespace() {
376         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlunit.org\t", "/x"));
377         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlunit.or\tg", "/x"));
378         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlu\tnit.org", "/x"));
379         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://\twww.htmlunit.org", "/x"));
380         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http:\t//www.htmlunit.org", "/x"));
381         assertEquals("http://www.htmlunit.org/x",     UrlUtils.resolveUrl("http\t://www.htmlunit.org", "/x"));
382         assertEquals("http://www.htmlunit.org/x",     UrlUtils.resolveUrl("\thttp://www.htmlunit.org", "/x"));
383 
384         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlunit.org\n", "/x"));
385         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlunit.or\ng", "/x"));
386         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlu\nnit.org", "/x"));
387         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://\nwww.htmlunit.org", "/x"));
388         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http:\n//www.htmlunit.org", "/x"));
389         assertEquals("http://www.htmlunit.org/x",     UrlUtils.resolveUrl("http\n://www.htmlunit.org", "/x"));
390         assertEquals("http://www.htmlunit.org/x",     UrlUtils.resolveUrl("\nhttp://www.htmlunit.org", "/x"));
391 
392         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlunit.org\r", "/x"));
393         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlunit.or\rg", "/x"));
394         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlu\rnit.org", "/x"));
395         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://\rwww.htmlunit.org", "/x"));
396         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http:\r//www.htmlunit.org", "/x"));
397         assertEquals("http://www.htmlunit.org/x",     UrlUtils.resolveUrl("http\r://www.htmlunit.org", "/x"));
398         assertEquals("http://www.htmlunit.org/x",     UrlUtils.resolveUrl("\rhttp://www.htmlunit.org", "/x"));
399     }
400 
401     /**
402      * Test {@link UrlUtils#resolveUrl(String, String)} with extra examples.
403      */
404     @Test
405     public void resolveUrlWithLeadingControl() {
406         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("\u0005http://www.htmlunit.org", "/x"));
407         assertEquals("http://www.htmlunit.org/x",
408                 UrlUtils.resolveUrl("\u0005\u0004\u0003\u0002\u0000http://www.htmlunit.org", "/x"));
409 
410         assertEquals("http://www.htmlunit.o\u0005rg/x",    UrlUtils.resolveUrl("http://www.htmlunit.o\u0005rg", "/x"));
411         assertEquals("http://www.htmlunit.o\u0005rg/x",
412                 UrlUtils.resolveUrl("\u0011http://www.htmlunit.o\u0005rg", "/x"));
413     }
414 
415     /**
416      * Test {@link UrlUtils#resolveUrl(String, String)} with extra examples.
417      */
418     @Test
419     public void resolveUrlWithTrailingControl() {
420         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlunit.org\u0005", "/x"));
421         assertEquals("http://www.htmlunit.org/x",
422                 UrlUtils.resolveUrl("http://www.htmlunit.org\u0005\u0004\u0003\u0002\u0000", "/x"));
423 
424         assertEquals("http://www.htmlunit.o\u0005rg/x",    UrlUtils.resolveUrl("http://www.htmlunit.o\u0005rg", "/x"));
425         assertEquals("http://www.htmlunit.o\u0005rg/x",
426                 UrlUtils.resolveUrl("http://www.htmlunit.o\u0005rg\u0011", "/x"));
427     }
428 
429     /**
430      * Test {@link UrlUtils#resolveUrl(String, String)} with extra examples.
431      */
432     @Test
433     public void resolveUrlWithTrailingSpace() {
434         assertEquals("http://www.htmlunit.org/x",    UrlUtils.resolveUrl("http://www.htmlunit.org ", "/x"));
435         assertEquals("http://www.htmlunit.org/x",
436                 UrlUtils.resolveUrl("http://www.htmlunit.org    ", "/x"));
437 
438         assertEquals("http://www.htmlunit.o rg/x",    UrlUtils.resolveUrl("http://www.htmlunit.o rg", "/x"));
439         assertEquals("http://www.htmlunit.o rg/x",
440                 UrlUtils.resolveUrl("http://www.htmlunit.o rg ", "/x"));
441     }
442 
443     /**
444      * @throws Exception if the test fails
445      */
446     @Test
447     public void percent() throws Exception {
448         URL url = new URL("http://localhost/bug%21.html");
449         assertEquals("http://localhost/bug%21.html",
450                 UrlUtils.encodeUrl(url, ISO_8859_1));
451 
452         url = new URL("http://localhost/bug%0F.html");
453         assertEquals("http://localhost/bug%0F.html",
454                 UrlUtils.encodeUrl(url, ISO_8859_1));
455 
456         url = new URL("http://localhost/bug%ff.html");
457         assertEquals("http://localhost/bug%ff.html",
458                 UrlUtils.encodeUrl(url, ISO_8859_1));
459 
460         url = new URL("http://localhost/bug%AB.html");
461         assertEquals("http://localhost/bug%AB.html",
462                 UrlUtils.encodeUrl(url, ISO_8859_1));
463 
464         url = new URL("http://john.smith:secret@localhost/bug%AB.html");
465         assertEquals("http://john.smith:secret@localhost/bug%AB.html",
466                 UrlUtils.encodeUrl(url, ISO_8859_1));
467     }
468 
469     /**
470      * Tests for #1587.
471      * @throws Exception if the test fails
472      */
473     @Test
474     public void percentEncoding() throws Exception {
475         URL url = new URL("http://localhost/bug%.html");
476         assertEquals("http://localhost/bug%25.html",
477                 UrlUtils.encodeUrl(url, ISO_8859_1));
478 
479         url = new URL("http://localhost/bug%a.html");
480         assertEquals("http://localhost/bug%25a.html",
481                 UrlUtils.encodeUrl(url, ISO_8859_1));
482 
483         url = new URL("http://localhost/bug%ak.html");
484         assertEquals("http://localhost/bug%25ak.html",
485                 UrlUtils.encodeUrl(url, ISO_8859_1));
486 
487         url = new URL("http://localhost/bug.html?namelist=Woman%2g%20Daily");
488         assertEquals("http://localhost/bug.html?namelist=Woman%252g%20Daily",
489                 UrlUtils.encodeUrl(url, ISO_8859_1));
490 
491         url = new URL("http://localhost/bug.html?namelist=Woman%u2122%20Daily");
492         assertEquals("http://localhost/bug.html?namelist=Woman%25u2122%20Daily",
493                 UrlUtils.encodeUrl(url, ISO_8859_1));
494 
495         url = new URL("http://localhost/bug.html?%");
496         assertEquals("http://localhost/bug.html?%25",
497                 UrlUtils.encodeUrl(url, ISO_8859_1));
498 
499         url = new URL("http://localhost/bug.html?%2");
500         assertEquals("http://localhost/bug.html?%252",
501                 UrlUtils.encodeUrl(url, ISO_8859_1));
502 
503         url = new URL("http://localhost/bug.html?%2x");
504         assertEquals("http://localhost/bug.html?%252x",
505                 UrlUtils.encodeUrl(url, ISO_8859_1));
506     }
507 
508     /**
509      * Tests for #1951.
510      * @throws Exception if the test fails
511      */
512     @Test
513     public void percentEncoding2() throws Exception {
514         URL url = new URL("http://localhost/foo%%20bar.html");
515         assertEquals("http://localhost/foo%25%20bar.html",
516                 UrlUtils.encodeUrl(url, ISO_8859_1));
517 
518         url = new URL("http://localhost/foo%20bar.html");
519         assertEquals("http://localhost/foo%20bar.html",
520                 UrlUtils.encodeUrl(url, ISO_8859_1));
521 
522         url = new URL("http://localhost/foo%ar.html");
523         assertEquals("http://localhost/foo%25ar.html",
524                 UrlUtils.encodeUrl(url, ISO_8859_1));
525 
526         url = new URL("http://localhost/foo%%xyz.html");
527         assertEquals("http://localhost/foo%25%25xyz.html",
528                 UrlUtils.encodeUrl(url, ISO_8859_1));
529 
530         url = new URL("http://localhost/foo%20%xyz.html");
531         assertEquals("http://localhost/foo%20%25xyz.html",
532                 UrlUtils.encodeUrl(url, ISO_8859_1));
533 
534         url = new URL("http://localhost/foo%2x%bar.html");
535         assertEquals("http://localhost/foo%252x%bar.html",
536                 UrlUtils.encodeUrl(url, ISO_8859_1));
537     }
538 
539     /**
540      * @throws Exception if the test fails
541      */
542     @Test
543     public void relativeBase() throws Exception {
544         final String baseUrl = "a/a1/a2";
545         assertEquals("b",      UrlUtils.resolveUrl(baseUrl, "../../b"));
546     }
547 
548     /**
549      * @throws Exception if the test fails
550      */
551     @Test
552     public void sameFile() throws Exception {
553         assertTrue(UrlUtils.sameFile(null, null));
554         assertFalse(UrlUtils.sameFile(new URL("http://localhost/bug.htm"), null));
555         assertFalse(UrlUtils.sameFile(null, new URL("http://localhost/bug.html")));
556 
557         assertTrue(UrlUtils.sameFile(new URL("http://localhost/bug.html"), new URL("http://localhost/bug.html")));
558         assertFalse(UrlUtils.sameFile(new URL("http://localhost/bug.htm"), new URL("http://localhost/bug.html")));
559 
560         // assertTrue(new URL("http://localhost/bug.html").sameFile(new URL("http://localhost/test/../bug.html")));
561         assertTrue(UrlUtils.sameFile(new URL("http://localhost/bug.html"),
562                     new URL("http://localhost/test/../bug.html")));
563 
564         assertTrue(UrlUtils.sameFile(new URL("http://localhost"), new URL("http://localhost")));
565         assertTrue(UrlUtils.sameFile(new URL("http://localhost/"), new URL("http://localhost/")));
566         assertTrue(UrlUtils.sameFile(new URL("http://localhost/"), new URL("http://localhost")));
567 
568         assertTrue(UrlUtils.sameFile(new URL("http://localhost/bug.html?test"),
569                         new URL("http://localhost/bug.html?test")));
570         assertFalse(UrlUtils.sameFile(new URL("http://localhost/bug.html?test"),
571                         new URL("http://localhost/bug.html?rest")));
572 
573         assertTrue(UrlUtils.sameFile(new URL("http://localhost/bug.html#test"),
574                 new URL("http://localhost/bug.html#rest")));
575 
576         assertTrue(UrlUtils.sameFile(new URL("https://localhost/bug.html"), new URL("https://localhost/bug.html")));
577         assertFalse(UrlUtils.sameFile(new URL("http://localhost/bug.htm"), new URL("https://localhost/bug.html")));
578 
579         assertTrue(UrlUtils.sameFile(new URL("http://localhost:81/bug.html"), new URL("http://localhost:81/bug.html")));
580         assertFalse(UrlUtils.sameFile(new URL("http://localhost:81/bug.htm"), new URL("http://localhost:80/bug.html")));
581         assertTrue(UrlUtils.sameFile(new URL("http://localhost/bug.html"), new URL("http://localhost:80/bug.html")));
582         assertTrue(UrlUtils.sameFile(new URL("https://localhost:443/bug.html"), new URL("https://localhost/bug.html")));
583 
584         // issue #1787
585         // final URL u1 = new URL("http://sourceforge.net/");
586         // final URL u2 = new URL("http://ch3.sourceforge.net/");
587         final URL u1 = new URL("http://htmlunit-dev.org/");
588         final URL u2 = new URL("http://host1.htmlunit-dev.org/");
589         assertEquals(InetAddress.getByName(u1.getHost()), InetAddress.getByName(u2.getHost()));
590         assertFalse(UrlUtils.sameFile(u1, u2));
591     }
592 
593     /**
594      * @throws Exception if the test fails
595      */
596     @Test
597     public void normalize() throws Exception {
598         assertEquals("http://localhost:80/bug.html", UrlUtils.normalize(new URL("http://localhost/bug.html")));
599         assertEquals("https://localhost:443/bug.html", UrlUtils.normalize(new URL("https://localhost/bug.html")));
600 
601         assertEquals("http://localhost:80/test/bug.html",
602                     UrlUtils.normalize(new URL("http://localhost/test/./bug.html")));
603         assertEquals("http://localhost:80/../bug.html", UrlUtils.normalize(new URL("http://localhost/../bug.html")));
604         assertEquals("http://localhost:80/bug.html", UrlUtils.normalize(new URL("http://localhost/test/../bug.html")));
605 
606         assertEquals("http://localhost:80/", UrlUtils.normalize(new URL("http://localhost")));
607         assertEquals("http://localhost:80/", UrlUtils.normalize(new URL("http://localhost/")));
608 
609         assertEquals("http://localhost:80/bug.html?test",
610                     UrlUtils.normalize(new URL("http://localhost/bug.html?test")));
611         assertEquals("http://localhost:80/bug.html", UrlUtils.normalize(new URL("http://localhost/bug.html#anchor")));
612         assertEquals("http://localhost:80/bug.html?test",
613                     UrlUtils.normalize(new URL("http://localhost/bug.html?test#anchor")));
614 
615         assertEquals("http://localhost:80/bug.html", UrlUtils.normalize(new URL("http://localhost:80/bug.html")));
616         assertEquals("http://localhost:81/bug.html", UrlUtils.normalize(new URL("http://localhost:81/bug.html")));
617 
618         assertEquals("https://localhost:443/bug.html",
619                     UrlUtils.normalize(new URL("https://localhost:443/bug.html")));
620         assertEquals("https://localhost:8443/bug.html",
621                     UrlUtils.normalize(new URL("https://localhost:8443/bug.html")));
622     }
623 }