xref: /haiku/src/tests/kits/net/service/UrlTest.cpp (revision b31cb92f29fe89eaca84d173d0f70d38bf0c6a3d)
1 /*
2  * Copyright 2010, Christophe Huriaux
3  * Copyright 2014, Haiku, inc.
4  * Distributed under the terms of the MIT licence
5  */
6 
7 
8 #include "UrlTest.h"
9 
10 
11 #include <cstdlib>
12 #include <cstring>
13 #include <cstdio>
14 
15 #include <NetworkKit.h>
16 
17 #include <cppunit/TestCaller.h>
18 #include <cppunit/TestSuite.h>
19 
20 
21 UrlTest::UrlTest()
22 {
23 }
24 
25 
26 UrlTest::~UrlTest()
27 {
28 }
29 
30 
31 // Test that parsing a valid URL and converting back to string doesn't alter it
32 void UrlTest::ParseTest()
33 {
34 	uint8 testIndex;
35 	BUrl testUrl;
36 
37 	const char* kTestLength[] =
38 	{
39 		"http://user:pass@www.foo.com:80/path?query#fragment",
40 		"http://user:pass@www.foo.com:80/path?query#",
41 		"http://user:pass@www.foo.com:80/path?query",
42 		"http://user:pass@www.foo.com:80/path?",
43 		"http://user:pass@www.foo.com:80/path",
44 		"http://user:pass@www.foo.com:80/",
45 		"http://user:pass@www.foo.com",
46 		"http://user:pass@",
47 		"http://www.foo.com",
48 		"http://",
49 		"http:"
50 	};
51 
52 	for (testIndex = 0; testIndex < sizeof(kTestLength) / sizeof(const char*);
53 		testIndex++)
54 	{
55 		NextSubTest();
56 
57 		testUrl.SetUrlString(kTestLength[testIndex]);
58 		CPPUNIT_ASSERT_EQUAL(BString(kTestLength[testIndex]),
59 			testUrl.UrlString());
60 	}
61 }
62 
63 
64 void UrlTest::TestIsValid()
65 {
66 	BUrl url("http:");
67 	CPPUNIT_ASSERT_MESSAGE("Created with a scheme but no hierarchical segment.",
68 		!url.IsValid());
69 
70 	url.SetHost("<invalid>");
71 	CPPUNIT_ASSERT_MESSAGE("Set to an invalid host", !url.IsValid());
72 
73 	url.SetUrlString("");
74 	url.SetProtocol("\t \n");
75 	CPPUNIT_ASSERT_MESSAGE("Set a protocol with whitespace", !url.IsValid());
76 	url.SetProtocol("123");
77 	CPPUNIT_ASSERT_MESSAGE("Set an all-digits protocol", !url.IsValid());
78 
79 	url.SetUserName("user");
80 	CPPUNIT_ASSERT_MESSAGE("Retain invalid state on user change",
81 		!url.IsValid());
82 	url.SetPassword("pass");
83 	CPPUNIT_ASSERT_MESSAGE("Retain invalid state on password change",
84 		!url.IsValid());
85 
86 	url.SetProtocol("http");
87 	url.SetFragment("fragment");
88 	CPPUNIT_ASSERT_MESSAGE("Only protocol and fragment are set",
89 		!url.IsValid());
90 	url.SetFragment("fragment");
91 	url.SetProtocol("http");
92 	CPPUNIT_ASSERT_MESSAGE("Only protocol and fragment are set",
93 		!url.IsValid());
94 }
95 
96 
97 void UrlTest::TestGettersSetters()
98 {
99 	BUrl url;
100 	url.SetProtocol("http");
101 	url.SetUserName("user");
102 	url.SetPassword("password");
103 	url.SetHost("example.com");
104 	url.SetPort(8080);
105 	url.SetPath("/path");
106 	url.SetRequest("query=value");
107 	url.SetFragment("fragment");
108 
109 	CPPUNIT_ASSERT_EQUAL(BString("http"), url.Protocol());
110 	CPPUNIT_ASSERT_EQUAL(BString("user"), url.UserName());
111 	CPPUNIT_ASSERT_EQUAL(BString("password"), url.Password());
112 	CPPUNIT_ASSERT_EQUAL(BString("user:password"), url.UserInfo());
113 	CPPUNIT_ASSERT_EQUAL(BString("example.com"), url.Host());
114 	CPPUNIT_ASSERT_EQUAL(BString("user:password@example.com:8080"),
115 		url.Authority());
116 	CPPUNIT_ASSERT_EQUAL(8080, url.Port());
117 	CPPUNIT_ASSERT_EQUAL(BString("/path"), url.Path());
118 	CPPUNIT_ASSERT_EQUAL(BString("query=value"), url.Request());
119 	CPPUNIT_ASSERT_EQUAL(BString("fragment"), url.Fragment());
120 	CPPUNIT_ASSERT_EQUAL(BString(
121 			"http://user:password@example.com:8080/path?query=value#fragment"),
122 		url.UrlString());
123 }
124 
125 
126 void UrlTest::TestNullity()
127 {
128 	BUrl url;
129 	url.SetProtocol("http");
130 	url.SetHost("example.com");
131 
132 	CPPUNIT_ASSERT(url.HasAuthority());
133 	CPPUNIT_ASSERT(url.HasHost());
134 
135 	CPPUNIT_ASSERT(!url.HasUserName());
136 	CPPUNIT_ASSERT(!url.HasPassword());
137 	CPPUNIT_ASSERT(!url.HasUserInfo());
138 	CPPUNIT_ASSERT(!url.HasPort());
139 	CPPUNIT_ASSERT(!url.HasPath());
140 	CPPUNIT_ASSERT(!url.HasRequest());
141 	CPPUNIT_ASSERT(!url.HasFragment());
142 }
143 
144 
145 void UrlTest::TestCopy()
146 {
147 	BUrl url1("http://example.com");
148 	BUrl url2(url1);
149 
150 	url2.SetHost("www.example.com");
151 
152 	CPPUNIT_ASSERT_EQUAL(BString("www.example.com"), url2.Host());
153 	CPPUNIT_ASSERT_EQUAL(BString("http://www.example.com"), url2.UrlString());
154 	CPPUNIT_ASSERT_EQUAL(BString("example.com"), url1.Host());
155 	CPPUNIT_ASSERT_EQUAL(BString("http://example.com"), url1.UrlString());
156 }
157 
158 
159 typedef struct
160 {
161 	const char* url;
162 
163 	struct
164 	{
165 		const char* protocol;
166 		const char* userName;
167 		const char* password;
168 		const char* host;
169 		int16		port;
170 		const char* path;
171 		const char* request;
172 		const char* fragment;
173 	} expected;
174 } ExplodeTest;
175 
176 
177 const ExplodeTest	kTestExplode[] =
178 	//  Url
179 	//       Protocol     User  Password  Hostname  Port     Path    Request      Fragment
180 	//       -------- --------- --------- --------- ---- ---------- ---------- ------------
181 	{
182 		{ "http://user:pass@host:80/path?query#fragment",
183 			{ "http",   "user",   "pass",   "host",	 80,   "/path",	  "query",   "fragment" } },
184 		{ "http://www.host.tld/path?query#fragment",
185 			{ "http",   "",        "", "www.host.tld",0,   "/path",	  "query",   "fragment" } },
186 		{ "",
187 			{ "",       "",        "", "",            0,   "",        "",        ""} },
188 		{ "mailto:John.Doe@example.com",
189 			{ "mailto", "",        "", "",            0,   "John.Doe@example.com", "", "" } },
190 		{ "mailto:?to=addr1@an.example,addr2@an.example",
191 			{ "mailto", "",        "", "",            0,   "",        "to=addr1@an.example,addr2@an.example", "" } },
192 		{ "urn:oasis:names:specification:docbook:dtd:xml:4.1.2",
193 			{ "urn",    "",        "", "",            0,   "oasis:names:specification:docbook:dtd:xml:4.1.2", "", "" } },
194 		{ "http://www.goodsearch.com/login?return_path=/",
195 			{ "http",   "",        "", "www.goodsearch.com", 0, "/login", "return_path=/", "" } },
196 		{ "ldap://[2001:db8::7]:389/c=GB?objectClass?one",
197 			{ "ldap",   "",        "", "[2001:db8::7]",389,"/c=GB",   "objectClass?one", "" } },
198 		{ "ldap://[2001:db8::7]/c=GB?objectClass?one",
199 			{ "ldap",   "",        "", "[2001:db8::7]",0,  "/c=GB",   "objectClass?one", "" } },
200 		{ "HTTP://example.com.:80/%70a%74%68?a=%31#1%323",
201 			{ "HTTP",   "",        "", "example.com.",80,  "/%70a%74%68","a=%31","1%323"} },
202 		{ "/boot/home/Desktop/index.html",
203 			{ "",   "",            "", "",             0,  "/boot/home/Desktop/index.html","",""} },
204 		{ "//remote.host/boot/home/Desktop",
205 			{ "",   "",            "", "remote.host",  0,  "/boot/home/Desktop","",""} }
206 	};
207 
208 void UrlTest::ExplodeImplodeTest()
209 {
210 	uint8 testIndex;
211 	BUrl testUrl;
212 
213 	for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++)
214 	{
215 		NextSubTest();
216 		testUrl.SetUrlString(kTestExplode[testIndex].url);
217 
218 		CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].url),
219 			BString(testUrl.UrlString()));
220 		CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.protocol),
221 			BString(testUrl.Protocol()));
222 		CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.userName),
223 			BString(testUrl.UserName()));
224 		CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.password),
225 			BString(testUrl.Password()));
226 		CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.host),
227 			BString(testUrl.Host()));
228 		CPPUNIT_ASSERT_EQUAL(kTestExplode[testIndex].expected.port,
229 			testUrl.Port());
230 		CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.path),
231 			BString(testUrl.Path()));
232 		CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.request),
233 			BString(testUrl.Request()));
234 		CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.fragment),
235 			BString(testUrl.Fragment()));
236 	}
237 }
238 
239 
240 void
241 UrlTest::PathOnly()
242 {
243 	BUrl test = "lol";
244 	CPPUNIT_ASSERT(test.HasPath());
245 	CPPUNIT_ASSERT_EQUAL(BString("lol"), test.Path());
246 	CPPUNIT_ASSERT_EQUAL(BString("lol"), test.UrlString());
247 }
248 
249 
250 void
251 UrlTest::RelativeUriTest()
252 {
253 	// http://skew.org/uri/uri%5Ftests.html
254 	struct RelativeUrl {
255 		const char* base;
256 		const char* relative;
257 		const char* absolute;
258 	};
259 
260 	const RelativeUrl tests[] = {
261 		// The port must be preserved
262 		{"http://host:81/",		"/path",				"http://host:81/path"},
263 
264 		// Tests from http://skew.org/uri/uri_tests.html
265 		{"http://example.com/path?query#frag", "",
266 			"http://example.com/path?query"},
267 			// The fragment must be dropped when changing the path, but the
268 			// query must be preserved
269 		{"foo:a/b",				"../c",					"foo:/c"},
270 			// foo:c would be more intuitive, and is what skew.org tests.
271 			// However, foo:/c is what the RFC says we should get.
272 		{"foo:a",				"foo:.",				"foo:"},
273 		{"zz:abc",				"/foo/../../../bar",	"zz:/bar"},
274 		{"zz:abc",				"/foo/../bar",			"zz:/bar"},
275 		{"zz:abc",				"foo/../../../bar",		"zz:/bar"},
276 			// zz:bar would be more intuitive, ...
277 		{"zz:abc",				"zz:.",					"zz:"},
278 		{"http://a/b/c/d;p?q",	"/.",					"http://a/"},
279 		{"http://a/b/c/d;p?q",	"/.foo",				"http://a/.foo"},
280 		{"http://a/b/c/d;p?q",	".foo",					"http://a/b/c/.foo"},
281 		{"http://a/b/c/d;p?q",	"g:h",					"g:h"},
282 
283 		{"http://a/b/c/d;p?q",	"g:h",					"g:h"},
284 		{"http://a/b/c/d;p?q",	"g",					"http://a/b/c/g"},
285 		{"http://a/b/c/d;p?q",	"./g",					"http://a/b/c/g"},
286 		{"http://a/b/c/d;p?q",	"g/",					"http://a/b/c/g/"},
287 		{"http://a/b/c/d;p?q",	"/g",					"http://a/g"},
288 		{"http://a/b/c/d;p?q",	"//g",					"http://g"},
289 		{"http://a/b/c/d;p?q",	"?y",					"http://a/b/c/d;p?y"},
290 		{"http://a/b/c/d;p?q",	"g?y",					"http://a/b/c/g?y"},
291 		{"http://a/b/c/d;p?q",	"#s",					"http://a/b/c/d;p?q#s"},
292 		{"http://a/b/c/d;p?q",	"g#s",					"http://a/b/c/g#s"},
293 		{"http://a/b/c/d;p?q",	"g?y#s",				"http://a/b/c/g?y#s"},
294 		{"http://a/b/c/d;p?q",	";x",					"http://a/b/c/;x"},
295 		{"http://a/b/c/d;p?q",	"g;x",					"http://a/b/c/g;x"},
296 		{"http://a/b/c/d;p?q",	"g;x?y#s",				"http://a/b/c/g;x?y#s"},
297 		{"http://a/b/c/d;p?q",	"",						"http://a/b/c/d;p?q"},
298 		{"http://a/b/c/d;p?q",	".",					"http://a/b/c/"},
299 		{"http://a/b/c/d;p?q",	"./",					"http://a/b/c/"},
300 		{"http://a/b/c/d;p?q",	"..",					"http://a/b/"},
301 		{"http://a/b/c/d;p?q",	"../",					"http://a/b/"},
302 		{"http://a/b/c/d;p?q",	"../g",					"http://a/b/g"},
303 		{"http://a/b/c/d;p?q",	"../..",				"http://a/"},
304 		{"http://a/b/c/d;p?q",	"../../",				"http://a/"},
305 		{"http://a/b/c/d;p?q",	"../../g",				"http://a/g"},
306 
307 		// Parsers must be careful in handling cases where there are more
308 		// relative path ".." segments than there are hierarchical levels in the
309 		// base URI's path. Note that the ".." syntax cannot be used to change
310 		// the authority component of a URI.
311 		{"http://a/b/c/d;p?q",	"../../../g",			"http://a/g"},
312 		{"http://a/b/c/d;p?q",	"../../../../g",		"http://a/g"},
313 
314 		// Similarly, parsers must remove the dot-segments "." and ".." when
315 		// they are complete components of a path, but not when they are only
316 		// part of a segment.
317 		{"http://a/b/c/d;p?q",	"/./g",					"http://a/g"},
318 		{"http://a/b/c/d;p?q",	"/../g",				"http://a/g"},
319 		{"http://a/b/c/d;p?q",	"g.",					"http://a/b/c/g."},
320 		{"http://a/b/c/d;p?q",	".g",					"http://a/b/c/.g"},
321 		{"http://a/b/c/d;p?q",	"g..",					"http://a/b/c/g.."},
322 		{"http://a/b/c/d;p?q",	"..g",					"http://a/b/c/..g"},
323 
324 		// Less likely are cases where the relative URI reference uses
325 		// unnecessary or nonsensical forms of the "." and ".." complete path
326 		// segments.
327 		{"http://a/b/c/d;p?q",	"./../g",				"http://a/b/g"},
328 		{"http://a/b/c/d;p?q",	"./g/.",				"http://a/b/c/g/"},
329 		{"http://a/b/c/d;p?q",	"g/./h",				"http://a/b/c/g/h"},
330 		{"http://a/b/c/d;p?q",	"g/../h",				"http://a/b/c/h"},
331 		{"http://a/b/c/d;p?q",	"g;x=1/./y",			"http://a/b/c/g;x=1/y"},
332 		{"http://a/b/c/d;p?q",	"g;x=1/../y",			"http://a/b/c/y"},
333 
334 		// Some applications fail to separate the reference's query and/or
335 		// fragment components from a relative path before merging it with the
336 		// base path and removing dot-segments. This error is rarely noticed,
337 		// since typical usage of a fragment never includes the hierarchy ("/")
338 		// character, and the query component is not normally used within
339 		// relative references.
340 		{"http://a/b/c/d;p?q",	"g?y/./x",			"http://a/b/c/g?y/./x"},
341 		{"http://a/b/c/d;p?q",	"g?y/../x",			"http://a/b/c/g?y/../x"},
342 		{"http://a/b/c/d;p?q",	"g#s/./x",			"http://a/b/c/g#s/./x"},
343 		{"http://a/b/c/d;p?q",	"g#s/../x",			"http://a/b/c/g#s/../x"},
344 
345 		// Some parsers allow the scheme name to be present in a relative URI
346 		// reference if it is the same as the base URI scheme. This is
347 		// considered to be a loophole in prior specifications of partial URI
348 		// [RFC1630]. Its use should be avoided, but is allowed for backward
349 		// compatibility.
350 		{"http://a/b/c/d;p?q",	"http:g",			"http:g"},
351 		{"http://a/b/c/d;p?q",	"http:",			"http:"},
352 
353 		{"http://a/b/c/d;p?q",	"./g:h",			"http://a/b/c/g:h"},
354 		{"http://a/b/c/d;p?q",	"/a/b/c/./../../g",	"http://a/a/g"},
355 
356 		{"http://a/b/c/d;p?q=1/2",	"g",			"http://a/b/c/g"},
357 		{"http://a/b/c/d;p?q=1/2",	"./g",			"http://a/b/c/g"},
358 		{"http://a/b/c/d;p?q=1/2",	"g/",			"http://a/b/c/g/"},
359 		{"http://a/b/c/d;p?q=1/2",	"/g",			"http://a/g"},
360 		{"http://a/b/c/d;p?q=1/2",	"//g",			"http://g"},
361 		{"http://a/b/c/d;p?q=1/2",	"?y",			"http://a/b/c/d;p?y"},
362 		{"http://a/b/c/d;p?q=1/2",	"g?y",			"http://a/b/c/g?y"},
363 		{"http://a/b/c/d;p?q=1/2",	"g?y/./x",		"http://a/b/c/g?y/./x"},
364 		{"http://a/b/c/d;p?q=1/2",	"g?y/../x",		"http://a/b/c/g?y/../x"},
365 		{"http://a/b/c/d;p?q=1/2",	"g#s",			"http://a/b/c/g#s"},
366 		{"http://a/b/c/d;p?q=1/2",	"g#s/./x",		"http://a/b/c/g#s/./x"},
367 		{"http://a/b/c/d;p?q=1/2",	"g#s/../x",		"http://a/b/c/g#s/../x"},
368 		{"http://a/b/c/d;p?q=1/2",	"./",			"http://a/b/c/"},
369 		{"http://a/b/c/d;p?q=1/2",	"../",			"http://a/b/"},
370 		{"http://a/b/c/d;p?q=1/2",	"../g",			"http://a/b/g"},
371 		{"http://a/b/c/d;p?q=1/2",	"../../",		"http://a/"},
372 		{"http://a/b/c/d;p?q=1/2",	"../../g",		"http://a/g"},
373 
374 		{"http://a/b/c/d;p=1/2?q",	"g",			"http://a/b/c/d;p=1/g"},
375 		{"http://a/b/c/d;p=1/2?q",	"./g",			"http://a/b/c/d;p=1/g"},
376 		{"http://a/b/c/d;p=1/2?q",	"g/",			"http://a/b/c/d;p=1/g/"},
377 		{"http://a/b/c/d;p=1/2?q",	"g?y",			"http://a/b/c/d;p=1/g?y"},
378 		{"http://a/b/c/d;p=1/2?q",	";x",			"http://a/b/c/d;p=1/;x"},
379 		{"http://a/b/c/d;p=1/2?q",	"g;x",			"http://a/b/c/d;p=1/g;x"},
380 		{"http://a/b/c/d;p=1/2?q", "g;x=1/./y", "http://a/b/c/d;p=1/g;x=1/y"},
381 		{"http://a/b/c/d;p=1/2?q",	"g;x=1/../y",	"http://a/b/c/d;p=1/y"},
382 		{"http://a/b/c/d;p=1/2?q",	"./",			"http://a/b/c/d;p=1/"},
383 		{"http://a/b/c/d;p=1/2?q",	"../",			"http://a/b/c/"},
384 		{"http://a/b/c/d;p=1/2?q",	"../g",			"http://a/b/c/g"},
385 		{"http://a/b/c/d;p=1/2?q",	"../../",		"http://a/b/"},
386 		{"http://a/b/c/d;p=1/2?q",	"../../g",		"http://a/b/g"},
387 
388 		// Empty host and directory
389 		{"fred:///s//a/b/c",	"g:h",					"g:h"},
390 		{"fred:///s//a/b/c",	"g",					"fred:///s//a/b/g"},
391 		{"fred:///s//a/b/c",	"./g",					"fred:///s//a/b/g"},
392 		{"fred:///s//a/b/c",	"g/",					"fred:///s//a/b/g/"},
393 		{"fred:///s//a/b/c",	"/g",					"fred:///g"},
394 		{"fred:///s//a/b/c",	"//g",					"fred://g"},
395 		{"fred:///s//a/b/c",	"//g/x",				"fred://g/x"},
396 		{"fred:///s//a/b/c",	"///g",					"fred:///g"},
397 		{"fred:///s//a/b/c",	"./",					"fred:///s//a/b/"},
398 		{"fred:///s//a/b/c",	"../",					"fred:///s//a/"},
399 		{"fred:///s//a/b/c",	"../g",					"fred:///s//a/g"},
400 		{"fred:///s//a/b/c",	"../..",				"fred:///s//"},
401 		{"fred:///s//a/b/c",	"../../g",				"fred:///s//g"},
402 		{"fred:///s//a/b/c",	"../../../g",			"fred:///s/g"},
403 		{"fred:///s//a/b/c",	"../../../g",			"fred:///s/g"},
404 
405 		{"http:///s//a/b/c",	"g:h",					"g:h"},
406 		{"http:///s//a/b/c",	"g",					"http:///s//a/b/g"},
407 		{"http:///s//a/b/c",	"./g",					"http:///s//a/b/g"},
408 		{"http:///s//a/b/c",	"g/",					"http:///s//a/b/g/"},
409 		{"http:///s//a/b/c",	"/g",					"http:///g"},
410 		{"http:///s//a/b/c",	"//g",					"http://g"},
411 		{"http:///s//a/b/c",	"//g/x",				"http://g/x"},
412 		{"http:///s//a/b/c",	"///g",					"http:///g"},
413 		{"http:///s//a/b/c",	"./",					"http:///s//a/b/"},
414 		{"http:///s//a/b/c",	"../",					"http:///s//a/"},
415 		{"http:///s//a/b/c",	"../g",					"http:///s//a/g"},
416 		{"http:///s//a/b/c",	"../..",				"http:///s//"},
417 		{"http:///s//a/b/c",	"../../g",				"http:///s//g"},
418 		{"http:///s//a/b/c",	"../../../g",			"http:///s/g"},
419 		{"http:///s//a/b/c",	"../../../g",			"http:///s/g"},
420 
421 		{"foo:xyz",				"bar:abc",				"bar:abc"},
422 		{"http://example/x/y/z","../abc",				"http://example/x/abc"},
423 		{"http://example2/x/y/z","http://example/x/abc","http://example/x/abc"},
424 		{"http://ex/x/y/z",		"../r",					"http://ex/x/r"},
425 		{"http://ex/x/y",		"q/r",					"http://ex/x/q/r"},
426 		{"http://ex/x/y",		"q/r#s",				"http://ex/x/q/r#s"},
427 		{"http://ex/x/y",		"q/r#s/t",				"http://ex/x/q/r#s/t"},
428 		{"http://ex/x/y",		"ftp://ex/x/q/r",		"ftp://ex/x/q/r"},
429 		{"http://ex/x/y",		"",						"http://ex/x/y"},
430 		{"http://ex/x/y/",		"",						"http://ex/x/y/"},
431 		{"http://ex/x/y/pdq",	"",						"http://ex/x/y/pdq"},
432 		{"http://ex/x/y/",		"z/",					"http://ex/x/y/z/"},
433 
434 		{"file:/swap/test/animal.rdf", "#Animal",
435 			"file:/swap/test/animal.rdf#Animal"},
436 		{"file:/e/x/y/z",		"../abc",				"file:/e/x/abc"},
437 		{"file:/example2/x/y/z","/example/x/abc",		"file:/example/x/abc"},
438 		{"file:/e/x/y/z",		"../r",					"file:/e/x/r"},
439 		{"file:/e/x/y/z",		"/r",					"file:/r"},
440 		{"file:/e/x/y",			"q/r",					"file:/e/x/q/r"},
441 		{"file:/e/x/y",			"q/r#s",				"file:/e/x/q/r#s"},
442 		{"file:/e/x/y",			"q/r#",					"file:/e/x/q/r#"},
443 		{"file:/e/x/y",			"q/r#s/t",				"file:/e/x/q/r#s/t"},
444 		{"file:/e/x/y",			"ftp://ex/x/q/r",		"ftp://ex/x/q/r"},
445 		{"file:/e/x/y",			"",						"file:/e/x/y"},
446 		{"file:/e/x/y/",		"",						"file:/e/x/y/"},
447 		{"file:/e/x/y/pdq",		"",						"file:/e/x/y/pdq"},
448 		{"file:/e/x/y/",		"z/",					"file:/e/x/y/z/"},
449 		{"file:/devel/WWW/2000/10/swap/test/reluri-1.n3",
450 			"file://meetings.example.com/cal#m1",
451 			"file://meetings.example.com/cal#m1"},
452 		{"file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3",
453 			"file://meetings.example.com/cal#m1",
454 			"file://meetings.example.com/cal#m1"},
455 		{"file:/some/dir/foo",		"./#blort",		"file:/some/dir/#blort"},
456 		{"file:/some/dir/foo",		"./#",			"file:/some/dir/#"},
457 
458 		{"http://example/x/abc.efg", "./",			"http://example/x/"},
459 		{"http://example2/x/y/z", "//example/x/abc","http://example/x/abc"},
460 		{"http://ex/x/y/z",			"/r",			"http://ex/r"},
461 		{"http://ex/x/y",			"./q:r",		"http://ex/x/q:r"},
462 		{"http://ex/x/y",			"./p=q:r",		"http://ex/x/p=q:r"},
463 		{"http://ex/x/y?pp/qq",		"?pp/rr",		"http://ex/x/y?pp/rr"},
464 		{"http://ex/x/y?pp/qq",		"y/z",			"http://ex/x/y/z"},
465 
466 		{"mailto:local", "local/qual@domain.org#frag",
467 			"mailto:local/qual@domain.org#frag"},
468 		{"mailto:local/qual1@domain1.org", "more/qual2@domain2.org#frag",
469 			"mailto:local/more/qual2@domain2.org#frag"},
470 
471 		{"http://ex/x/y?q",		"y?q",			"http://ex/x/y?q"},
472 		{"http://ex?p",			"x/y?q",		"http://ex/x/y?q"},
473 		{"foo:a/b",				"c/d",			"foo:a/c/d"},
474 		{"foo:a/b",				"/c/d",			"foo:/c/d"},
475 		{"foo:a/b?c#d",			"",				"foo:a/b?c"},
476 		{"foo:a",				"b/c",			"foo:b/c"},
477 		{"foo:/a/y/z",			"../b/c",		"foo:/a/b/c"},
478 		{"foo:a",				"./b/c",		"foo:b/c"},
479 		{"foo:a",				"/./b/c",		"foo:/b/c"},
480 		{"foo://a//b/c",		"../../d",		"foo://a/d"},
481 		{"foo:a",				".",			"foo:"},
482 		{"foo:a",				"..",			"foo:"},
483 
484 		{"http://example/x/y%2Fz", "abc",			"http://example/x/abc"},
485 		{"http://example/a/x/y/z", "../../x%2Fabc", "http://example/a/x%2Fabc"},
486 		{"http://example/a/x/y%2Fz", "../x%2Fabc", "http://example/a/x%2Fabc"},
487 		{"http://example/x%2Fy/z", "abc", "http://example/x%2Fy/abc"},
488 		{"http://ex/x/y", "q%3Ar", "http://ex/x/q%3Ar"},
489 		{"http://example/x/y%2Fz", "/x%2Fabc", "http://example/x%2Fabc"},
490 		{"http://example/x/y/z", "/x%2Fabc", "http://example/x%2Fabc"},
491 
492 		{"mailto:local1@domain1?query1", "local2@domain2",
493 			"mailto:local2@domain2"},
494 		{"mailto:local1@domain1", "local2@domain2?query2",
495 			"mailto:local2@domain2?query2"},
496 		{"mailto:local1@domain1?query1", "local2@domain2?query2",
497 			"mailto:local2@domain2?query2"},
498 		{"mailto:local@domain?query1", "?query2",
499 			"mailto:local@domain?query2"},
500 		{"mailto:?query1", "local2@domain2?query2",
501 			"mailto:local2@domain2?query2"},
502 		{"mailto:local1@domain1?query1", "?query2",
503 			"mailto:local1@domain1?query2"},
504 
505 		{"foo:bar", "http://example/a/b?c/../d", "http://example/a/b?c/../d"},
506 		{"foo:bar", "http://example/a/b#c/../d", "http://example/a/b#c/../d"},
507 		{"http://example.org/base/uri", "http:this", "http:this"},
508 		{"http:base", "http:this", "http:this"},
509 		{"f:/a", ".//g", "f://g"},
510 		{"f://example.org/base/a", "b/c//d/e", "f://example.org/base/b/c//d/e"},
511 		{"mid:m@example.ord/c@example.org", "m2@example.ord/c2@example.org",
512 			"mid:m@example.ord/m2@example.ord/c2@example.org"},
513 		{"file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/", "mini1.xml",
514 			"file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/mini1.xml"},
515 		{"foo:a/y/z", "../b/c", "foo:a/b/c"},
516 		{"foo:", "b", "foo:b"},
517 		{"foo://a", "b", "foo://a/b"},
518 		{"foo://a?q", "b", "foo://a/b"},
519 		{"foo://a", "b?q", "foo://a/b?q"},
520 		{"foo://a?r", "b?q", "foo://a/b?q"},
521 	};
522 
523 	BString message(" Base: ");
524 	for (unsigned int index = 0; index < sizeof(tests) / sizeof(RelativeUrl);
525 		index++)
526 	{
527 		NextSubTest();
528 
529 		BUrl baseUrl(tests[index].base);
530 
531 		message.Truncate(7, true);
532 		message << tests[index].base;
533 		message << " Relative: ";
534 		message << tests[index].relative;
535 
536 		CPPUNIT_ASSERT_EQUAL_MESSAGE(message.String(),
537 			BString(tests[index].absolute),
538 			BUrl(baseUrl, tests[index].relative).UrlString());
539 	}
540 }
541 
542 
543 void
544 UrlTest::IDNTest()
545 {
546 	// http://www.w3.org/2004/04/uri-rel-test.html
547 	// TODO We need to decide wether to store them as UTF-8 or IDNA/punycode.
548 
549 	struct Test {
550 		const char* escaped;
551 		const char* decoded;
552 	};
553 
554 	Test tests[] = {
555 		{ "http://www.w%33.org", "http://www.w3.org" },
556 		{ "http://r%C3%A4ksm%C3%B6rg%C3%A5s.josefsson.org",
557 			"http://xn--rksmrgs-5wao1o.josefsson.org" },
558 		{ "http://%E7%B4%8D%E8%B1%86.w3.mag.keio.ac.jp",
559 			"http://xn--99zt52a.w3.mag.keio.ac.jp" },
560 		{ "http://www.%E3%81%BB%E3%82%93%E3%81%A8%E3%81%86%E3%81%AB%E3%81%AA"
561 			"%E3%81%8C%E3%81%84%E3%82%8F%E3%81%91%E3%81%AE%E3%82%8F%E3%81%8B%E3"
562 			"%82%89%E3%81%AA%E3%81%84%E3%81%A9%E3%82%81%E3%81%84%E3%82%93%E3%82"
563 			"%81%E3%81%84%E3%81%AE%E3%82%89%E3%81%B9%E3%82%8B%E3%81%BE%E3%81%A0"
564 			"%E3%81%AA%E3%81%8C%E3%81%8F%E3%81%97%E3%81%AA%E3%81%84%E3%81%A8%E3"
565 			"%81%9F%E3%82%8A%E3%81%AA%E3%81%84.w3.mag.keio.ac.jp/",
566 			"http://www.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3fg11amb5gzdb4wi9b"
567 			"ya3kc6lra.w3.mag.keio.ac.jp/" },
568 
569 		{ "http://%E3%81%BB%E3%82%93%E3%81%A8%E3%81%86%E3%81%AB%E3%81%AA%E3"
570 			"%81%8C%E3%81%84%E3%82%8F%E3%81%91%E3%81%AE%E3%82%8F%E3%81%8B%E3%82"
571 			"%89%E3%81%AA%E3%81%84%E3%81%A9%E3%82%81%E3%81%84%E3%82%93%E3%82%81"
572 			"%E3%81%84%E3%81%AE%E3%82%89%E3%81%B9%E3%82%8B%E3%81%BE%E3%81%A0%E3"
573 			"%81%AA%E3%81%8C%E3%81%8F%E3%81%97%E3%81%AA%E3%81%84%E3%81%A8%E3%81"
574 			"%9F%E3%82%8A%E3%81%AA%E3%81%84.%E3%81%BB%E3%82%93%E3%81%A8%E3%81"
575 			"%86%E3%81%AB%E3%81%AA%E3%81%8C%E3%81%84%E3%82%8F%E3%81%91%E3%81%AE"
576 			"%E3%82%8F%E3%81%8B%E3%82%89%E3%81%AA%E3%81%84%E3%81%A9%E3%82%81%E3"
577 			"%81%84%E3%82%93%E3%82%81%E3%81%84%E3%81%AE%E3%82%89%E3%81%B9%E3%82"
578 			"%8B%E3%81%BE%E3%81%A0%E3%81%AA%E3%81%8C%E3%81%8F%E3%81%97%E3%81%AA"
579 			"%E3%81%84%E3%81%A8%E3%81%9F%E3%82%8A%E3%81%AA%E3%81%84.%E3%81%BB"
580 			"%E3%82%93%E3%81%A8%E3%81%86%E3%81%AB%E3%81%AA%E3%81%8C%E3%81%84%E3"
581 			"%82%8F%E3%81%91%E3%81%AE%E3%82%8F%E3%81%8B%E3%82%89%E3%81%AA%E3%81"
582 			"%84%E3%81%A9%E3%82%81%E3%81%84%E3%82%93%E3%82%81%E3%81%84%E3%81%AE"
583 			"%E3%82%89%E3%81%B9%E3%82%8B%E3%81%BE%E3%81%A0%E3%81%AA%E3%81%8C%E3"
584 			"%81%8F%E3%81%97%E3%81%AA%E3%81%84%E3%81%A8%E3%81%9F%E3%82%8A%E3%81"
585 			"%AA%E3%81%84.w3.mag.keio.ac.jp/",
586 			"http://xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3fg11amb5gzdb4wi9bya3k"
587 			"c6lra.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3fg11amb5gzdb4wi9bya3kc6"
588 			"lra.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3fg11amb5gzdb4wi9bya3kc6lr"
589 			"a.w3.mag.keio.ac.jp/" },
590 		{ NULL, NULL }
591 	};
592 
593 	for (int i = 0; tests[i].escaped != NULL; i++)
594 	{
595 		NextSubTest();
596 
597 		BUrl url(tests[i].escaped);
598 		url.UrlDecode();
599 
600 		BUrl idn(tests[i].decoded);
601 		status_t success = idn.IDNAToUnicode();
602 
603 		CPPUNIT_ASSERT_EQUAL(B_OK, success);
604 		CPPUNIT_ASSERT_EQUAL(url.UrlString(), idn.UrlString());
605 	}
606 }
607 
608 
609 /* static */ void
610 UrlTest::AddTests(BTestSuite& parent)
611 {
612 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("UrlTest");
613 
614 	suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::ParseTest",
615 		&UrlTest::ParseTest));
616 	suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::TestIsValid",
617 		&UrlTest::TestIsValid));
618 	suite.addTest(new CppUnit::TestCaller<UrlTest>(
619 		"UrlTest::TestGettersSetters", &UrlTest::TestGettersSetters));
620 	suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::TestNullity",
621 		&UrlTest::TestNullity));
622 	suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::TestCopy",
623 		&UrlTest::TestCopy));
624 	suite.addTest(new CppUnit::TestCaller<UrlTest>(
625 		"UrlTest::ExplodeImplodeTest", &UrlTest::ExplodeImplodeTest));
626 	suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::PathOnly",
627 		&UrlTest::PathOnly));
628 	suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::RelativeUriTest",
629 		&UrlTest::RelativeUriTest));
630 	suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::IDNTest",
631 		&UrlTest::IDNTest));
632 
633 	parent.addTest("UrlTest", &suite);
634 }
635