001: //========================================================================
002: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: //Licensed under the Apache License, Version 2.0 (the "License");
005: //you may not use this file except in compliance with the License.
006: //You may obtain a copy of the License at
007: //http://www.apache.org/licenses/LICENSE-2.0
008: //Unless required by applicable law or agreed to in writing, software
009: //distributed under the License is distributed on an "AS IS" BASIS,
010: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: //See the License for the specific language governing permissions and
012: //limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty;
016:
017: import junit.framework.TestCase;
018:
019: public class HttpURITest extends TestCase {
020: String[][] partial_tests = {
021: /* 0*/{ "/path/info", null, null, null, null,
022: "/path/info", null, null, null },
023: /* 1*/{ "/path/info#fragment", null, null, null, null,
024: "/path/info", null, null, "fragment" },
025: /* 2*/{ "/path/info?query", null, null, null, null,
026: "/path/info", null, "query", null },
027: /* 3*/{ "/path/info?query#fragment", null, null, null,
028: null, "/path/info", null, "query", "fragment" },
029: /* 4*/{ "/path/info;param", null, null, null, null,
030: "/path/info", "param", null, null },
031: /* 5*/{ "/path/info;param#fragment", null, null, null,
032: null, "/path/info", "param", null, "fragment" },
033: /* 6*/{ "/path/info;param?query", null, null, null, null,
034: "/path/info", "param", "query", null },
035: /* 7*/{ "/path/info;param?query#fragment", null, null,
036: null, null, "/path/info", "param", "query",
037: "fragment" },
038: /* 8*/{ "//host/path/info", null, "//host", "host", null,
039: "/path/info", null, null, null },
040: /* 9*/{ "//user@host/path/info", null, "//user@host",
041: "host", null, "/path/info", null, null, null },
042: /*10*/{ "//user@host:8080/path/info", null,
043: "//user@host:8080", "host", "8080", "/path/info",
044: null, null, null },
045: /*11*/{ "//host:8080/path/info", null, "//host:8080",
046: "host", "8080", "/path/info", null, null, null },
047: /*12*/{ "http:/path/info", "http", null, null, null,
048: "/path/info", null, null, null },
049: /*13*/{ "http:/path/info#fragment", "http", null, null,
050: null, "/path/info", null, null, "fragment" },
051: /*14*/{ "http:/path/info?query", "http", null, null, null,
052: "/path/info", null, "query", null },
053: /*15*/{ "http:/path/info?query#fragment", "http", null,
054: null, null, "/path/info", null, "query", "fragment" },
055: /*16*/{ "http:/path/info;param", "http", null, null, null,
056: "/path/info", "param", null, null },
057: /*17*/{ "http:/path/info;param#fragment", "http", null,
058: null, null, "/path/info", "param", null, "fragment" },
059: /*18*/{ "http:/path/info;param?query", "http", null, null,
060: null, "/path/info", "param", "query", null },
061: /*19*/{ "http:/path/info;param?query#fragment", "http",
062: null, null, null, "/path/info", "param", "query",
063: "fragment" },
064: /*20*/{
065: "http://user@host:8080/path/info;param?query#fragment",
066: "http", "//user@host:8080", "host", "8080",
067: "/path/info", "param", "query", "fragment" },
068: /*21*/{
069: "xxxxx://user@host:8080/path/info;param?query#fragment",
070: "xxxxx", "//user@host:8080", "host", "8080",
071: "/path/info", "param", "query", "fragment" },
072: /*22*/{ "http:///;?#", "http", "//", null, null, "/", "",
073: "", "" },
074: /*23*/{ "/path/info?a=?query", null, null, null, null,
075: "/path/info", null, "a=?query", null },
076: /*24*/{ "/path/info?a=;query", null, null, null, null,
077: "/path/info", null, "a=;query", null },
078: /*25*/{ "//host:8080//", null, "//host:8080", "host",
079: "8080", "//", null, null, null },
080: /*26*/{ "file:///path/info", "file", "//", null, null,
081: "/path/info", null, null, null },
082: /*27*/{ "//", null, "//", null, null, null, null, null,
083: null },
084: /*28*/{ "/;param", null, null, null, null, "/", "param",
085: null, null },
086: /*29*/{ "/?x=y", null, null, null, null, "/", null, "x=y",
087: null },
088: /*30*/{ "/?abc=test", null, null, null, null, "/", null,
089: "abc=test", null },
090: /*31*/{ "/#fragment", null, null, null, null, "/", null,
091: null, "fragment" }, };
092:
093: public void testPartialURIs() throws Exception {
094: HttpURI uri = new HttpURI(true);
095:
096: for (int t = 0; t < partial_tests.length; t++) {
097: uri.parse(partial_tests[t][0].getBytes(), 0,
098: partial_tests[t][0].length());
099: assertEquals(t + " " + partial_tests[t][0],
100: partial_tests[t][1], uri.getScheme());
101: assertEquals(t + " " + partial_tests[t][0],
102: partial_tests[t][2], uri.getAuthority());
103: assertEquals(t + " " + partial_tests[t][0],
104: partial_tests[t][3], uri.getHost());
105: assertEquals(t + " " + partial_tests[t][0],
106: partial_tests[t][4] == null ? -1 : Integer
107: .parseInt(partial_tests[t][4]), uri
108: .getPort());
109: assertEquals(t + " " + partial_tests[t][0],
110: partial_tests[t][5], uri.getPath());
111: assertEquals(t + " " + partial_tests[t][0],
112: partial_tests[t][6], uri.getParam());
113: assertEquals(t + " " + partial_tests[t][0],
114: partial_tests[t][7], uri.getQuery());
115: assertEquals(t + " " + partial_tests[t][0],
116: partial_tests[t][8], uri.getFragment());
117: assertEquals(partial_tests[t][0], uri.toString());
118: }
119:
120: }
121:
122: String[][] path_tests = {
123: /* 0*/{ "/path/info", null, null, null, null,
124: "/path/info", null, null, null },
125: /* 1*/{ "/path/info#fragment", null, null, null, null,
126: "/path/info", null, null, "fragment" },
127: /* 2*/{ "/path/info?query", null, null, null, null,
128: "/path/info", null, "query", null },
129: /* 3*/{ "/path/info?query#fragment", null, null, null,
130: null, "/path/info", null, "query", "fragment" },
131: /* 4*/{ "/path/info;param", null, null, null, null,
132: "/path/info", "param", null, null },
133: /* 5*/{ "/path/info;param#fragment", null, null, null,
134: null, "/path/info", "param", null, "fragment" },
135: /* 6*/{ "/path/info;param?query", null, null, null, null,
136: "/path/info", "param", "query", null },
137: /* 7*/{ "/path/info;param?query#fragment", null, null,
138: null, null, "/path/info", "param", "query",
139: "fragment" },
140: /* 8*/{ "//host/path/info", null, null, null, null,
141: "//host/path/info", null, null, null },
142: /* 9*/{ "//user@host/path/info", null, null, null, null,
143: "//user@host/path/info", null, null, null },
144: /*10*/{ "//user@host:8080/path/info", null, null, null,
145: null, "//user@host:8080/path/info", null, null,
146: null },
147: /*11*/{ "//host:8080/path/info", null, null, null, null,
148: "//host:8080/path/info", null, null, null },
149: /*12*/{ "http:/path/info", "http", null, null, null,
150: "/path/info", null, null, null },
151: /*13*/{ "http:/path/info#fragment", "http", null, null,
152: null, "/path/info", null, null, "fragment" },
153: /*14*/{ "http:/path/info?query", "http", null, null, null,
154: "/path/info", null, "query", null },
155: /*15*/{ "http:/path/info?query#fragment", "http", null,
156: null, null, "/path/info", null, "query", "fragment" },
157: /*16*/{ "http:/path/info;param", "http", null, null, null,
158: "/path/info", "param", null, null },
159: /*17*/{ "http:/path/info;param#fragment", "http", null,
160: null, null, "/path/info", "param", null, "fragment" },
161: /*18*/{ "http:/path/info;param?query", "http", null, null,
162: null, "/path/info", "param", "query", null },
163: /*19*/{ "http:/path/info;param?query#fragment", "http",
164: null, null, null, "/path/info", "param", "query",
165: "fragment" },
166: /*20*/{
167: "http://user@host:8080/path/info;param?query#fragment",
168: "http", "//user@host:8080", "host", "8080",
169: "/path/info", "param", "query", "fragment" },
170: /*21*/{
171: "xxxxx://user@host:8080/path/info;param?query#fragment",
172: "xxxxx", "//user@host:8080", "host", "8080",
173: "/path/info", "param", "query", "fragment" },
174: /*22*/{ "http:///;?#", "http", "//", null, null, "/", "",
175: "", "" },
176: /*23*/{ "/path/info?a=?query", null, null, null, null,
177: "/path/info", null, "a=?query", null },
178: /*24*/{ "/path/info?a=;query", null, null, null, null,
179: "/path/info", null, "a=;query", null },
180: /*25*/{ "//host:8080//", null, null, null, null,
181: "//host:8080//", null, null, null },
182: /*26*/{ "file:///path/info", "file", "//", null, null,
183: "/path/info", null, null, null },
184: /*27*/{ "//", null, null, null, null, "//", null, null,
185: null },
186: /*28*/{ "http://localhost/", "http", "//localhost",
187: "localhost", null, "/", null, null, null },
188: /*29*/{ "http://localhost:8080/", "http",
189: "//localhost:8080", "localhost", "8080", "/", null,
190: null, null },
191: /*30*/{ "http://localhost/?x=y", "http", "//localhost",
192: "localhost", null, "/", null, "x=y", null },
193: /*31*/{ "/;param", null, null, null, null, "/", "param",
194: null, null },
195: /*32*/{ "/?x=y", null, null, null, null, "/", null, "x=y",
196: null },
197: /*33*/{ "/?abc=test", null, null, null, null, "/", null,
198: "abc=test", null },
199: /*34*/{ "/#fragment", null, null, null, null, "/", null,
200: null, "fragment" }, };
201:
202: public void testPathURIs() throws Exception {
203: HttpURI uri = new HttpURI();
204:
205: for (int t = 0; t < path_tests.length; t++) {
206: uri.parse(path_tests[t][0].getBytes(), 0, path_tests[t][0]
207: .length());
208: assertEquals(t + " " + path_tests[t][0], path_tests[t][1],
209: uri.getScheme());
210: assertEquals(t + " " + path_tests[t][0], path_tests[t][2],
211: uri.getAuthority());
212: assertEquals(t + " " + path_tests[t][0], path_tests[t][3],
213: uri.getHost());
214: assertEquals(t + " " + path_tests[t][0],
215: path_tests[t][4] == null ? -1 : Integer
216: .parseInt(path_tests[t][4]), uri.getPort());
217: assertEquals(t + " " + path_tests[t][0], path_tests[t][5],
218: uri.getPath());
219: assertEquals(t + " " + path_tests[t][0], path_tests[t][6],
220: uri.getParam());
221: assertEquals(t + " " + path_tests[t][0], path_tests[t][7],
222: uri.getQuery());
223: assertEquals(t + " " + path_tests[t][0], path_tests[t][8],
224: uri.getFragment());
225: assertEquals(path_tests[t][0], uri.toString());
226: }
227:
228: }
229:
230: }
|