URLTest.java revision 7e00db4156e50ce5f20fefb820dca339299134d3
1/*
2 * Copyright (C) 2010 The Android Open Source Project
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 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.net;
18
19import java.net.Inet6Address;
20import java.net.InetAddress;
21import java.net.URL;
22import junit.framework.TestCase;
23
24public class URLTest extends TestCase {
25    // http://code.google.com/p/android/issues/detail?id=12724
26    public void testExplicitPort() throws Exception {
27        URL url = new URL("http://www.google.com:80/example?language[id]=2");
28        assertEquals("www.google.com", url.getHost());
29        assertEquals(80, url.getPort());
30    }
31
32    public void testHostWithSlashInFragment() throws Exception {
33        URL url = new URL("http://www.google.com#foo/bar");
34        assertEquals("www.google.com", url.getHost());
35        assertEquals("foo/bar", url.getRef());
36        assertEquals(-1, url.getPort());
37    }
38
39    public void testHostWithColonAndSlashInFragment() throws Exception {
40        URL url = new URL("http://www.google.com#foo:bar/baz");
41        assertEquals("www.google.com", url.getHost());
42        assertEquals("foo:bar/baz", url.getRef());
43        assertEquals(-1, url.getPort());
44    }
45
46    /**
47     * Android's URL.equals() works as if the network is down. This is different
48     * from the RI, which does potentially slow and inconsistent DNS lookups in
49     * URL.equals.
50     */
51    public void testEqualsDoesNotDoHostnameResolution() throws Exception {
52        for (InetAddress inetAddress : InetAddress.getAllByName("localhost")) {
53            String address = inetAddress.getHostAddress();
54            if (inetAddress instanceof Inet6Address) {
55                address = "[" + address + "]";
56            }
57            URL urlByHostName = new URL("http://localhost/foo?bar=baz#quux");
58            URL urlByAddress = new URL("http://" + address + "/foo?bar=baz#quux");
59            assertFalse("Expected " + urlByHostName + " to not equal " + urlByAddress,
60                    urlByHostName.equals(urlByAddress));
61        }
62    }
63
64    public void testEqualsCaseMapping() throws Exception {
65        assertEquals(new URL("HTTP://localhost/foo?bar=baz#quux"),
66                new URL("HTTP://localhost/foo?bar=baz#quux"));
67        assertTrue(new URL("http://localhost/foo?bar=baz#quux").equals(
68                new URL("http://LOCALHOST/foo?bar=baz#quux")));
69        assertFalse(new URL("http://localhost/foo?bar=baz#quux").equals(
70                new URL("http://localhost/FOO?bar=baz#quux")));
71        assertFalse(new URL("http://localhost/foo?bar=baz#quux").equals(
72                new URL("http://localhost/foo?BAR=BAZ#quux")));
73        assertFalse(new URL("http://localhost/foo?bar=baz#quux").equals(
74                new URL("http://localhost/foo?bar=baz#QUUX")));
75    }
76}
77