001: /*
002: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024: package com.sun.midp.io;
025:
026: import com.sun.midp.i3test.TestCase;
027:
028: public class TestHttpUrl extends TestCase {
029: /**
030: * Runs all the tests.
031: */
032: public void runTests() throws Throwable {
033: declare("testAbsUrl");
034: testAbsUrl();
035: declare("testRelUrl");
036: testRelUrl();
037: }
038:
039: /**
040: * Tests parsing of different pieces of the absolute URL.
041: */
042: void testAbsUrl() {
043: HttpUrl url;
044:
045: info("scheme://machine.domain:8080/path?query#fragment");
046: url = new HttpUrl(
047: "scheme://machine.domain:8080/path?query#fragment");
048: assertEquals("scheme", "scheme", url.scheme);
049: assertEquals("authority", "machine.domain:8080", url.authority);
050: assertEquals("host", "machine.domain", url.host);
051: assertEquals("machine name", "machine", url.machine);
052: assertEquals("domain", "domain", url.domain);
053: assertEquals("port", 8080, url.port);
054: assertEquals("path", "/path", url.path);
055: assertEquals("query", "query", url.query);
056: assertEquals("fragment", "fragment", url.fragment);
057:
058: info("scheme://machine.domain:8080/path?query#");
059: url = new HttpUrl("scheme://machine.domain:8080/path?query#");
060: assertEquals("scheme", "scheme", url.scheme);
061: assertEquals("authority", "machine.domain:8080", url.authority);
062: assertEquals("host", "machine.domain", url.host);
063: assertEquals("machine name", "machine", url.machine);
064: assertEquals("domain", "domain", url.domain);
065: assertEquals("port", 8080, url.port);
066: assertEquals("path", "/path", url.path);
067: assertEquals("query", "query", url.query);
068: assertEquals("fragment", null, url.fragment);
069:
070: info("scheme://machine.domain:8080/path");
071: url = new HttpUrl("scheme://machine.domain:8080/path");
072: assertEquals("scheme", "scheme", url.scheme);
073: assertEquals("authority", "machine.domain:8080", url.authority);
074: assertEquals("host", "machine.domain", url.host);
075: assertEquals("machine name", "machine", url.machine);
076: assertEquals("domain", "domain", url.domain);
077: assertEquals("port", 8080, url.port);
078: assertEquals("path", "/path", url.path);
079: assertEquals("query", null, url.query);
080: assertEquals("fragment", null, url.fragment);
081:
082: info("scheme://machine.domain:8080/");
083: url = new HttpUrl("scheme://machine.domain:8080/");
084: assertEquals("scheme", "scheme", url.scheme);
085: assertEquals("authority", "machine.domain:8080", url.authority);
086: assertEquals("host", "machine.domain", url.host);
087: assertEquals("machine name", "machine", url.machine);
088: assertEquals("domain", "domain", url.domain);
089: assertEquals("port", 8080, url.port);
090: assertEquals("path", "/", url.path);
091: assertEquals("query", null, url.query);
092: assertEquals("fragment", null, url.fragment);
093:
094: info("scheme://machine.domain:8080");
095: url = new HttpUrl("scheme://machine.domain:8080");
096: assertEquals("scheme", "scheme", url.scheme);
097: assertEquals("authority", "machine.domain:8080", url.authority);
098: assertEquals("host", "machine.domain", url.host);
099: assertEquals("machine name", "machine", url.machine);
100: assertEquals("domain", "domain", url.domain);
101: assertEquals("port", 8080, url.port);
102: assertEquals("path", null, url.path);
103: assertEquals("query", null, url.query);
104: assertEquals("fragment", null, url.fragment);
105:
106: info("scheme://machine.domain");
107: url = new HttpUrl("scheme://machine.domain");
108: assertEquals("scheme", "scheme", url.scheme);
109: assertEquals("authority", "machine.domain", url.authority);
110: assertEquals("host", "machine.domain", url.host);
111: assertEquals("machine name", "machine", url.machine);
112: assertEquals("domain", "domain", url.domain);
113: assertEquals("port", -1, url.port);
114: assertEquals("path", null, url.path);
115: assertEquals("query", null, url.query);
116: assertEquals("fragment", null, url.fragment);
117:
118: info("scheme://machine");
119: url = new HttpUrl("scheme://machine");
120: assertEquals("scheme", "scheme", url.scheme);
121: assertEquals("authority", "machine", url.authority);
122: assertEquals("host", "machine", url.host);
123: assertEquals("machine name", "machine", url.machine);
124: assertEquals("domain", null, url.domain);
125: assertEquals("port", -1, url.port);
126: assertEquals("path", null, url.path);
127: assertEquals("query", null, url.query);
128: assertEquals("fragment", null, url.fragment);
129:
130: info("scheme://");
131: url = new HttpUrl("scheme://");
132: assertEquals("scheme", "scheme", url.scheme);
133: assertEquals("authority", null, url.authority);
134: assertEquals("host", null, url.host);
135: assertEquals("machine name", null, url.machine);
136: assertEquals("domain", null, url.domain);
137: assertEquals("port", -1, url.port);
138: assertEquals("path", null, url.path);
139: assertEquals("query", null, url.query);
140: assertEquals("fragment", null, url.fragment);
141:
142: info("scheme://machine.subdomain.domain");
143: url = new HttpUrl("scheme://machine.subdomain.domain");
144: assertEquals("authority", "machine.subdomain.domain",
145: url.authority);
146: assertEquals("host", "machine.subdomain.domain", url.host);
147: assertEquals("machine name", "machine", url.machine);
148: assertEquals("domain", "subdomain.domain", url.domain);
149:
150: info("scheme://123.domain");
151: url = new HttpUrl("scheme://123.domain");
152: assertEquals("authority", "123.domain", url.authority);
153: assertEquals("host", "123.domain", url.host);
154: assertEquals("machine name", "123", url.machine);
155: assertEquals("domain", "domain", url.domain);
156:
157: info("scheme://1234.5678.901.2345");
158: url = new HttpUrl("scheme://1234.5678.901.2345");
159: assertEquals("authority", "1234.5678.901.2345", url.authority);
160: assertEquals("host", "1234.5678.901.2345", url.host);
161: assertEquals("machine name", null, url.machine);
162: assertEquals("domain", null, url.domain);
163:
164: info("scheme://123");
165: url = new HttpUrl("scheme://1234");
166: assertEquals("authority", "1234", url.authority);
167: assertEquals("host", "1234", url.host);
168: assertEquals("machine name", "1234", url.machine);
169: assertEquals("domain", null, url.domain);
170:
171: // IP v4 address
172: info("scheme://123.123");
173: url = new HttpUrl("scheme://123.123");
174: assertEquals("authority", "123.123", url.authority);
175: assertEquals("host", "123.123", url.host);
176: assertEquals("machine name", null, url.machine);
177: assertEquals("domain", null, url.domain);
178:
179: // IP v6 address
180: info("scheme://[123]");
181: url = new HttpUrl("scheme://[123]");
182: assertEquals("authority", "[123]", url.authority);
183: assertEquals("host", "[123]", url.host);
184: assertEquals("machine name", null, url.machine);
185: assertEquals("domain", null, url.domain);
186:
187: info("scheme://authority/");
188: url = new HttpUrl("scheme://authority/");
189: assertEquals("path", "/", url.path);
190: }
191:
192: /**
193: * Tests parsing of different pieces of the relative URL.
194: */
195: void testRelUrl() {
196: HttpUrl url;
197:
198: url = new HttpUrl("//authority/path?query#fragment");
199: assertEquals("scheme", null, url.scheme);
200: assertEquals("authority", "authority", url.authority);
201: assertEquals("path", "/path", url.path);
202: assertEquals("query", "query", url.query);
203: assertEquals("fragment", "fragment", url.fragment);
204:
205: url = new HttpUrl("//authority/path?query");
206: assertEquals("scheme", null, url.scheme);
207: assertEquals("authority", "authority", url.authority);
208: assertEquals("path", "/path", url.path);
209: assertEquals("query", "query", url.query);
210: assertEquals("fragment", null, url.fragment);
211:
212: url = new HttpUrl("//authority/path");
213: assertEquals("scheme", null, url.scheme);
214: assertEquals("authority", "authority", url.authority);
215: assertEquals("path", "/path", url.path);
216: assertEquals("query", null, url.query);
217:
218: url = new HttpUrl("//authority/");
219: assertEquals("scheme", null, url.scheme);
220: assertEquals("authority", "authority", url.authority);
221: assertEquals("path", "/", url.path);
222:
223: url = new HttpUrl("//authority");
224: assertEquals("scheme", null, url.scheme);
225: assertEquals("authority", "authority", url.authority);
226: assertEquals("path", null, url.path);
227:
228: url = new HttpUrl("/path");
229: assertEquals("scheme", null, url.scheme);
230: assertEquals("authority", null, url.authority);
231: assertEquals("path", "/path", url.path);
232: assertEquals("query", null, url.query);
233:
234: url = new HttpUrl("/");
235: assertEquals("scheme", null, url.scheme);
236: assertEquals("authority", null, url.authority);
237: assertEquals("path", "/", url.path);
238:
239: url = new HttpUrl("path/subpath");
240: assertEquals("scheme", null, url.scheme);
241: assertEquals("authority", null, url.authority);
242: assertEquals("path", "path/subpath", url.path);
243:
244: url = new HttpUrl("path");
245: assertEquals("scheme", null, url.scheme);
246: assertEquals("authority", null, url.authority);
247: assertEquals("path", "path", url.path);
248: }
249: }
|