1 //
2 // URITest.cpp
3 //
4 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5 // and Contributors.
6 //
7 // SPDX-License-Identifier: BSL-1.0
8 //
9
10
11 #include "URITest.h"
12 #include "CppUnit/TestCaller.h"
13 #include "CppUnit/TestSuite.h"
14 #include "Poco/URI.h"
15 #include "Poco/Path.h"
16
17
18 using Poco::URI;
19 using Poco::Path;
20
21
URITest(const std::string & name)22 URITest::URITest(const std::string& name): CppUnit::TestCase(name)
23 {
24 }
25
26
~URITest()27 URITest::~URITest()
28 {
29 }
30
31
testConstruction()32 void URITest::testConstruction()
33 {
34 URI uri;
35 assertTrue (uri.getScheme().empty());
36 assertTrue (uri.getAuthority().empty());
37 assertTrue (uri.getUserInfo().empty());
38 assertTrue (uri.getHost().empty());
39 assertTrue (uri.getPort() == 0);
40 assertTrue (uri.getPath().empty());
41 assertTrue (uri.getQuery().empty());
42 assertTrue (uri.getFragment().empty());
43
44 uri.setScheme("ftp");
45 assertTrue (uri.getScheme() == "ftp");
46 assertTrue (uri.getPort() == 21);
47
48 uri.setScheme("HTTP");
49 assertTrue (uri.getScheme() == "http");
50
51 uri.setAuthority("www.appinf.com");
52 assertTrue (uri.getAuthority() == "www.appinf.com");
53 assertTrue (uri.getPort() == 80);
54 assertTrue (uri.getSpecifiedPort() == 0);
55
56 uri.setAuthority("user@services.appinf.com:8000");
57 assertTrue (uri.getUserInfo() == "user");
58 assertTrue (uri.getHost() == "services.appinf.com");
59 assertTrue (uri.getPort() == 8000);
60 assertTrue (uri.getSpecifiedPort() == 8000);
61
62 uri.setPath("/index.html");
63 assertTrue (uri.getPath() == "/index.html");
64
65 uri.setPath("/file%20with%20spaces.html");
66 assertTrue (uri.getPath() == "/file with spaces.html");
67
68 uri.setPathEtc("/query.cgi?query=foo");
69 assertTrue (uri.getPath() == "/query.cgi");
70 assertTrue (uri.getQuery() == "query=foo");
71 assertTrue (uri.getFragment().empty());
72 assertTrue (uri.getPathEtc() == "/query.cgi?query=foo");
73 assertTrue (uri.getPathAndQuery() == "/query.cgi?query=foo");
74
75 uri.setPathEtc("/query.cgi?query=bar#frag");
76 assertTrue (uri.getPath() == "/query.cgi");
77 assertTrue (uri.getQuery() == "query=bar");
78 assertTrue (uri.getFragment() == "frag");
79 assertTrue (uri.getPathEtc() == "/query.cgi?query=bar#frag");
80 assertTrue (uri.getPathAndQuery() == "/query.cgi?query=bar");
81
82 uri.setQuery("query=test");
83 assertTrue (uri.getQuery() == "query=test");
84
85 uri.setFragment("result");
86 assertTrue (uri.getFragment() == "result");
87
88 URI uri2("file", "/home/guenter/foo.bar");
89 assertTrue (uri2.getScheme() == "file");
90 assertTrue (uri2.getPath() == "/home/guenter/foo.bar");
91
92 URI uri3("http", "www.appinf.com", "/index.html");
93 assertTrue (uri3.getScheme() == "http");
94 assertTrue (uri3.getAuthority() == "www.appinf.com");
95 assertTrue (uri3.getPath() == "/index.html");
96
97 URI uri4("http", "www.appinf.com:8000", "/index.html");
98 assertTrue (uri4.getScheme() == "http");
99 assertTrue (uri4.getAuthority() == "www.appinf.com:8000");
100 assertTrue (uri4.getPath() == "/index.html");
101
102 URI uri5("http", "user@www.appinf.com:8000", "/index.html");
103 assertTrue (uri5.getScheme() == "http");
104 assertTrue (uri5.getUserInfo() == "user");
105 assertTrue (uri5.getHost() == "www.appinf.com");
106 assertTrue (uri5.getPort() == 8000);
107 assertTrue (uri5.getAuthority() == "user@www.appinf.com:8000");
108 assertTrue (uri5.getPath() == "/index.html");
109
110 URI uri6("http", "user@www.appinf.com:80", "/index.html");
111 assertTrue (uri6.getScheme() == "http");
112 assertTrue (uri6.getUserInfo() == "user");
113 assertTrue (uri6.getHost() == "www.appinf.com");
114 assertTrue (uri6.getPort() == 80);
115 assertTrue (uri6.getSpecifiedPort() == 80);
116 assertTrue (uri6.getAuthority() == "user@www.appinf.com");
117 assertTrue (uri6.getPath() == "/index.html");
118
119 URI uri7("http", "user@www.appinf.com:", "/index.html");
120 assertTrue (uri7.getScheme() == "http");
121 assertTrue (uri7.getUserInfo() == "user");
122 assertTrue (uri7.getHost() == "www.appinf.com");
123 assertTrue (uri7.getPort() == 80);
124 assertTrue (uri7.getSpecifiedPort() == 0);
125 assertTrue (uri7.getAuthority() == "user@www.appinf.com");
126 assertTrue (uri7.getPath() == "/index.html");
127
128 URI uri8("http", "www.appinf.com", "/index.html", "query=test");
129 assertTrue (uri8.getScheme() == "http");
130 assertTrue (uri8.getAuthority() == "www.appinf.com");
131 assertTrue (uri8.getPath() == "/index.html");
132 assertTrue (uri8.getQuery() == "query=test");
133
134 URI uri9("http", "www.appinf.com", "/index.html", "query=test", "fragment");
135 assertTrue (uri9.getScheme() == "http");
136 assertTrue (uri9.getAuthority() == "www.appinf.com");
137 assertTrue (uri9.getPath() == "/index.html");
138 assertTrue (uri9.getPathEtc() == "/index.html?query=test#fragment");
139 assertTrue (uri9.getQuery() == "query=test");
140 assertTrue (uri9.getFragment() == "fragment");
141
142 uri9.clear();
143 assertTrue (uri9.getScheme().empty());
144 assertTrue (uri9.getAuthority().empty());
145 assertTrue (uri9.getUserInfo().empty());
146 assertTrue (uri9.getHost().empty());
147 assertTrue (uri9.getPort() == 0);
148 assertTrue (uri9.getPath().empty());
149 assertTrue (uri9.getQuery().empty());
150 assertTrue (uri9.getFragment().empty());
151
152 URI uri10("ldap", "[2001:db8::7]", "/c=GB?objectClass?one");
153 assertTrue (uri10.getScheme() == "ldap");
154 assertTrue (uri10.getUserInfo().empty());
155 assertTrue (uri10.getHost() == "2001:db8::7");
156 assertTrue (uri10.getPort() == 389);
157 assertTrue (uri10.getSpecifiedPort() == 0);
158 assertTrue (uri10.getAuthority() == "[2001:db8::7]");
159 assertTrue (uri10.getPathEtc() == "/c=GB?objectClass?one");
160
161 URI uri11("http", "www.appinf.com", "/index.html?query=test#fragment");
162 assertTrue (uri11.getScheme() == "http");
163 assertTrue (uri11.getAuthority() == "www.appinf.com");
164 assertTrue (uri11.getPath() == "/index.html");
165 assertTrue (uri11.getPathEtc() == "/index.html?query=test#fragment");
166 assertTrue (uri11.getQuery() == "query=test");
167 assertTrue (uri11.getFragment() == "fragment");
168 }
169
170
testParse()171 void URITest::testParse()
172 {
173 URI uri("http://www.appinf.com");
174 assertTrue (uri.getScheme() == "http");
175 assertTrue (uri.getAuthority() == "www.appinf.com");
176 assertTrue (uri.getPath().empty());
177 assertTrue (uri.getQuery().empty());
178 assertTrue (uri.getFragment().empty());
179 assertTrue (!uri.isRelative());
180
181 uri = "http://www.appinf.com/";
182 assertTrue (uri.getScheme() == "http");
183 assertTrue (uri.getAuthority() == "www.appinf.com");
184 assertTrue (uri.getPath() == "/");
185 assertTrue (uri.getQuery().empty());
186 assertTrue (uri.getFragment().empty());
187 assertTrue (!uri.isRelative());
188
189 uri = "ftp://anonymous@ftp.appinf.com/pub/";
190 assertTrue (uri.getScheme() == "ftp");
191 assertTrue (uri.getUserInfo() == "anonymous");
192 assertTrue (uri.getHost() == "ftp.appinf.com");
193 assertTrue (uri.getPort() == 21);
194 assertTrue (uri.getAuthority() == "anonymous@ftp.appinf.com");
195 assertTrue (uri.getPath() == "/pub/");
196 assertTrue (uri.getQuery().empty());
197 assertTrue (uri.getFragment().empty());
198 assertTrue (!uri.isRelative());
199 assertTrue (!uri.isRelative());
200
201 uri = "https://www.appinf.com/index.html#top";
202 assertTrue (uri.getScheme() == "https");
203 assertTrue (uri.getHost() == "www.appinf.com");
204 assertTrue (uri.getPort() == 443);
205 assertTrue (uri.getPath() == "/index.html");
206 assertTrue (uri.getQuery().empty());
207 assertTrue (uri.getFragment() == "top");
208 assertTrue (!uri.isRelative());
209
210 uri = "http://www.appinf.com/search.cgi?keyword=test&scope=all";
211 assertTrue (uri.getScheme() == "http");
212 assertTrue (uri.getHost() == "www.appinf.com");
213 assertTrue (uri.getPort() == 80);
214 assertTrue (uri.getPath() == "/search.cgi");
215 assertTrue (uri.getQuery() == "keyword=test&scope=all");
216 assertTrue (uri.getFragment().empty());
217 assertTrue (!uri.isRelative());
218
219 uri = "http://www.appinf.com/search.cgi?keyword=test&scope=all#result";
220 assertTrue (uri.getScheme() == "http");
221 assertTrue (uri.getHost() == "www.appinf.com");
222 assertTrue (uri.getPort() == 80);
223 assertTrue (uri.getPath() == "/search.cgi");
224 assertTrue (uri.getQuery() == "keyword=test&scope=all");
225 assertTrue (uri.getFragment() == "result");
226 assertTrue (!uri.isRelative());
227
228 uri = "http://www.appinf.com/search.cgi?keyword=test%20encoded&scope=all#result";
229 assertTrue (uri.getScheme() == "http");
230 assertTrue (uri.getHost() == "www.appinf.com");
231 assertTrue (uri.getPort() == 80);
232 assertTrue (uri.getPath() == "/search.cgi");
233 assertTrue (uri.getQuery() == "keyword=test encoded&scope=all");
234 assertTrue (uri.getFragment() == "result");
235 assertTrue (!uri.isRelative());
236
237 uri = "ldap://[2001:db8::7]/c=GB?objectClass?one";
238 assertTrue (uri.getScheme() == "ldap");
239 assertTrue (uri.getUserInfo().empty());
240 assertTrue (uri.getHost() == "2001:db8::7");
241 assertTrue (uri.getPort() == 389);
242 assertTrue (uri.getAuthority() == "[2001:db8::7]");
243 assertTrue (uri.getPath() == "/c=GB");
244 assertTrue (uri.getQuery() == "objectClass?one");
245 assertTrue (uri.getFragment().empty());
246
247 uri = "mailto:John.Doe@example.com";
248 assertTrue (uri.getScheme() == "mailto");
249 assertTrue (uri.getUserInfo().empty());
250 assertTrue (uri.getHost().empty());
251 assertTrue (uri.getPort() == 0);
252 assertTrue (uri.getAuthority().empty());
253 assertTrue (uri.getPath() == "John.Doe@example.com");
254 assertTrue (uri.getQuery().empty());
255 assertTrue (uri.getFragment().empty());
256
257 uri = "tel:+1-816-555-1212";
258 assertTrue (uri.getScheme() == "tel");
259 assertTrue (uri.getUserInfo().empty());
260 assertTrue (uri.getHost().empty());
261 assertTrue (uri.getPort() == 0);
262 assertTrue (uri.getAuthority().empty());
263 assertTrue (uri.getPath() == "+1-816-555-1212");
264 assertTrue (uri.getQuery().empty());
265 assertTrue (uri.getFragment().empty());
266
267 uri = "telnet://192.0.2.16:80";
268 assertTrue (uri.getScheme() == "telnet");
269 assertTrue (uri.getUserInfo().empty());
270 assertTrue (uri.getHost() == "192.0.2.16");
271 assertTrue (uri.getPort() == 80);
272 assertTrue (uri.getAuthority() == "192.0.2.16:80");
273 assertTrue (uri.getPath().empty());
274 assertTrue (uri.getQuery().empty());
275 assertTrue (uri.getFragment().empty());
276
277 uri = "urn:oasis:names:specification:docbook:dtd:xml:4.1.2";
278 assertTrue (uri.getScheme() == "urn");
279 assertTrue (uri.getUserInfo().empty());
280 assertTrue (uri.getHost().empty());
281 assertTrue (uri.getPort() == 0);
282 assertTrue (uri.getAuthority().empty());
283 assertTrue (uri.getPath() == "oasis:names:specification:docbook:dtd:xml:4.1.2");
284 assertTrue (uri.getQuery().empty());
285 assertTrue (uri.getFragment().empty());
286
287 uri = "";
288 assertTrue (uri.getScheme().empty());
289 assertTrue (uri.getAuthority().empty());
290 assertTrue (uri.getUserInfo().empty());
291 assertTrue (uri.getHost().empty());
292 assertTrue (uri.getPort() == 0);
293 assertTrue (uri.getPath().empty());
294 assertTrue (uri.getQuery().empty());
295 assertTrue (uri.getFragment().empty());
296 assertTrue (uri.empty());
297
298 // relative references
299
300 uri = "/foo/bar";
301 assertTrue (uri.getScheme().empty());
302 assertTrue (uri.getAuthority().empty());
303 assertTrue (uri.getUserInfo().empty());
304 assertTrue (uri.getHost().empty());
305 assertTrue (uri.getPort() == 0);
306 assertTrue (uri.getPath() == "/foo/bar");
307 assertTrue (uri.getQuery().empty());
308 assertTrue (uri.getFragment().empty());
309 assertTrue (uri.isRelative());
310
311 uri = "./foo/bar";
312 assertTrue (uri.getScheme().empty());
313 assertTrue (uri.getAuthority().empty());
314 assertTrue (uri.getUserInfo().empty());
315 assertTrue (uri.getHost().empty());
316 assertTrue (uri.getPort() == 0);
317 assertTrue (uri.getPath() == "./foo/bar");
318 assertTrue (uri.getQuery().empty());
319 assertTrue (uri.getFragment().empty());
320 assertTrue (uri.isRelative());
321
322 uri = "../foo/bar";
323 assertTrue (uri.getScheme().empty());
324 assertTrue (uri.getAuthority().empty());
325 assertTrue (uri.getUserInfo().empty());
326 assertTrue (uri.getHost().empty());
327 assertTrue (uri.getPort() == 0);
328 assertTrue (uri.getPath() == "../foo/bar");
329 assertTrue (uri.getQuery().empty());
330 assertTrue (uri.getFragment().empty());
331 assertTrue (uri.isRelative());
332
333 uri = "index.html";
334 assertTrue (uri.getScheme().empty());
335 assertTrue (uri.getAuthority().empty());
336 assertTrue (uri.getUserInfo().empty());
337 assertTrue (uri.getHost().empty());
338 assertTrue (uri.getPort() == 0);
339 assertTrue (uri.getPath() == "index.html");
340 assertTrue (uri.getQuery().empty());
341 assertTrue (uri.getFragment().empty());
342 assertTrue (uri.isRelative());
343
344 uri = "index.html#frag";
345 assertTrue (uri.getScheme().empty());
346 assertTrue (uri.getAuthority().empty());
347 assertTrue (uri.getUserInfo().empty());
348 assertTrue (uri.getHost().empty());
349 assertTrue (uri.getPort() == 0);
350 assertTrue (uri.getPath() == "index.html");
351 assertTrue (uri.getQuery().empty());
352 assertTrue (uri.getFragment() == "frag");
353 assertTrue (uri.isRelative());
354
355 uri = "?query=test";
356 assertTrue (uri.getScheme().empty());
357 assertTrue (uri.getAuthority().empty());
358 assertTrue (uri.getUserInfo().empty());
359 assertTrue (uri.getHost().empty());
360 assertTrue (uri.getPort() == 0);
361 assertTrue (uri.getPath().empty());
362 assertTrue (uri.getQuery() == "query=test");
363 assertTrue (uri.getFragment().empty());
364 assertTrue (uri.isRelative());
365
366 uri = "?query=test#frag";
367 assertTrue (uri.getScheme().empty());
368 assertTrue (uri.getAuthority().empty());
369 assertTrue (uri.getUserInfo().empty());
370 assertTrue (uri.getHost().empty());
371 assertTrue (uri.getPort() == 0);
372 assertTrue (uri.getPath().empty());
373 assertTrue (uri.getQuery() == "query=test");
374 assertTrue (uri.getFragment() == "frag");
375 assertTrue (uri.isRelative());
376
377 uri = "#frag";
378 assertTrue (uri.getScheme().empty());
379 assertTrue (uri.getAuthority().empty());
380 assertTrue (uri.getUserInfo().empty());
381 assertTrue (uri.getHost().empty());
382 assertTrue (uri.getPort() == 0);
383 assertTrue (uri.getPath().empty());
384 assertTrue (uri.getQuery().empty());
385 assertTrue (uri.getFragment() == "frag");
386 assertTrue (uri.isRelative());
387
388 uri = "#";
389 assertTrue (uri.getScheme().empty());
390 assertTrue (uri.getAuthority().empty());
391 assertTrue (uri.getUserInfo().empty());
392 assertTrue (uri.getHost().empty());
393 assertTrue (uri.getPort() == 0);
394 assertTrue (uri.getPath().empty());
395 assertTrue (uri.getQuery().empty());
396 assertTrue (uri.getFragment().empty());
397 assertTrue (uri.isRelative());
398
399 uri = "file:///a/b/c";
400 assertTrue (uri.getScheme() == "file");
401 assertTrue (uri.getAuthority().empty());
402 assertTrue (uri.getUserInfo().empty());
403 assertTrue (uri.getHost().empty());
404 assertTrue (uri.getPort() == 0);
405 assertTrue (uri.getPath() == "/a/b/c");
406 assertTrue (uri.getQuery().empty());
407 assertTrue (uri.getFragment().empty());
408 assertTrue (!uri.isRelative());
409
410 uri = "file://localhost/a/b/c";
411 assertTrue (uri.getScheme() == "file");
412 assertTrue (uri.getAuthority() == "localhost");
413 assertTrue (uri.getUserInfo().empty());
414 assertTrue (uri.getHost() == "localhost");
415 assertTrue (uri.getPort() == 0);
416 assertTrue (uri.getPath() == "/a/b/c");
417 assertTrue (uri.getQuery().empty());
418 assertTrue (uri.getFragment().empty());
419 assertTrue (!uri.isRelative());
420
421 uri = "file:///c:/Windows/system32/";
422 assertTrue (uri.getScheme() == "file");
423 assertTrue (uri.getAuthority().empty());
424 assertTrue (uri.getUserInfo().empty());
425 assertTrue (uri.getHost().empty());
426 assertTrue (uri.getPort() == 0);
427 assertTrue (uri.getPath() == "/c:/Windows/system32/");
428 assertTrue (uri.getQuery().empty());
429 assertTrue (uri.getFragment().empty());
430 assertTrue (!uri.isRelative());
431
432 uri = "./c:/Windows/system32/";
433 assertTrue (uri.getScheme().empty());
434 assertTrue (uri.getAuthority().empty());
435 assertTrue (uri.getUserInfo().empty());
436 assertTrue (uri.getHost().empty());
437 assertTrue (uri.getPort() == 0);
438 assertTrue (uri.getPath() == "./c:/Windows/system32/");
439 assertTrue (uri.getQuery().empty());
440 assertTrue (uri.getFragment().empty());
441 assertTrue (uri.isRelative());
442
443 uri = "ws://www.appinf.com/ws";
444 assertTrue (uri.getScheme() == "ws");
445 assertTrue (uri.getPort() == 80);
446
447 uri = "wss://www.appinf.com/ws";
448 assertTrue (uri.getScheme() == "wss");
449 assertTrue (uri.getPort() == 443);
450 }
451
452
testToString()453 void URITest::testToString()
454 {
455 URI uri("http://www.appinf.com");
456 assertTrue (uri.toString() == "http://www.appinf.com");
457
458 uri = "http://www.appinf.com/";
459 assertTrue (uri.toString() == "http://www.appinf.com/");
460
461 uri = "ftp://anonymous@ftp.appinf.com/pub/";
462 assertTrue (uri.toString() == "ftp://anonymous@ftp.appinf.com/pub/");
463
464 uri = "https://www.appinf.com/index.html#top";
465 assertTrue (uri.toString() == "https://www.appinf.com/index.html#top");
466
467 uri = "http://www.appinf.com/search.cgi?keyword=test&scope=all";
468 assertTrue (uri.toString() == "http://www.appinf.com/search.cgi?keyword=test&scope=all");
469
470 uri = "http://www.appinf.com/search.cgi?keyword=test&scope=all#result";
471 assertTrue (uri.toString() == "http://www.appinf.com/search.cgi?keyword=test&scope=all#result");
472
473 uri = "http://www.appinf.com/search.cgi?keyword=test%20encoded&scope=all#result";
474 assertTrue (uri.toString() == "http://www.appinf.com/search.cgi?keyword=test%20encoded&scope=all#result");
475
476 uri = "ldap://[2001:db8::7]/c=GB?objectClass?one";
477 assertTrue (uri.toString() == "ldap://[2001:db8::7]/c=GB?objectClass?one");
478
479 uri = "mailto:John.Doe@example.com";
480 assertTrue (uri.toString() == "mailto:John.Doe@example.com");
481
482 uri = "tel:+1-816-555-1212";
483 assertTrue (uri.toString() == "tel:+1-816-555-1212");
484
485 uri = "telnet://192.0.2.16:80";
486 assertTrue (uri.toString() == "telnet://192.0.2.16:80");
487
488 uri = "urn:oasis:names:specification:docbook:dtd:xml:4.1.2";
489 assertTrue (uri.toString() == "urn:oasis:names:specification:docbook:dtd:xml:4.1.2");
490
491 uri = "";
492 assertTrue (uri.toString() == "");
493
494 // relative references
495
496 uri = "/foo/bar";
497 assertTrue (uri.toString() == "/foo/bar");
498
499 uri = "./foo/bar";
500 assertTrue (uri.toString() == "./foo/bar");
501
502 uri = "../foo/bar";
503 assertTrue (uri.toString() == "../foo/bar");
504
505 uri = "//foo/bar";
506 assertTrue (uri.toString() == "//foo/bar");
507
508 uri = "index.html";
509 assertTrue (uri.toString() == "index.html");
510
511 uri = "index.html#frag";
512 assertTrue (uri.toString() == "index.html#frag");
513
514 uri = "?query=test";
515 assertTrue (uri.toString() == "?query=test");
516
517 uri = "?query=test#frag";
518 assertTrue (uri.toString() == "?query=test#frag");
519
520 uri = "#frag";
521 assertTrue (uri.toString() == "#frag");
522
523 uri = "#";
524 assertTrue (uri.toString() == "");
525
526 uri = "file:///a/b/c";
527 assertTrue (uri.toString() == "file:///a/b/c");
528
529 uri = "file://localhost/a/b/c";
530 assertTrue (uri.toString() == "file://localhost/a/b/c");
531
532 uri = "file:///c:/Windows/system32/";
533 assertTrue (uri.toString() == "file:///c:/Windows/system32/");
534
535 uri = "./c:/Windows/system32/";
536 assertTrue (uri.toString() == "./c:/Windows/system32/");
537
538 uri = "http://www.appinf.com";
539 uri.setRawQuery("query=test");
540 assertTrue (uri.toString() == "http://www.appinf.com/?query=test");
541 }
542
543
testCompare()544 void URITest::testCompare()
545 {
546 URI uri1("http://www.appinf.com");
547 URI uri2("HTTP://www.appinf.com:80");
548 assertTrue (uri1 == uri2);
549 assertTrue (uri1 == "http://www.appinf.com:");
550 assertTrue (uri1 != "http://www.google.com");
551
552 uri1 = "/foo/bar";
553 assertTrue (uri1 == "/foo/bar");
554 assertTrue (uri1 != "/foo/baz");
555
556 uri1 = "?query";
557 assertTrue (uri1 == "?query");
558 assertTrue (uri1 != "?query2");
559
560 uri1 = "#frag";
561 assertTrue (uri1 == "#frag");
562 assertTrue (uri1 != "#frag2");
563
564 uri1 = "/index.html#frag";
565 assertTrue (uri1 == "/index.html#frag");
566 assertTrue (uri1 != "/index.html");
567 }
568
569
testNormalize()570 void URITest::testNormalize()
571 {
572 URI uri("http://www.appinf.com");
573 uri.normalize();
574 assertTrue (uri.toString() == "http://www.appinf.com");
575
576 uri = "http://www.appinf.com/";
577 uri.normalize();
578 assertTrue (uri.toString() == "http://www.appinf.com/");
579
580 uri = "http://www.appinf.com/foo/bar/./index.html";
581 uri.normalize();
582 assertTrue (uri.toString() == "http://www.appinf.com/foo/bar/index.html");
583
584 uri = "http://www.appinf.com/foo/bar/../index.html";
585 uri.normalize();
586 assertTrue (uri.toString() == "http://www.appinf.com/foo/index.html");
587
588 uri = "http://www.appinf.com/foo/./bar/../index.html";
589 uri.normalize();
590 assertTrue (uri.toString() == "http://www.appinf.com/foo/index.html");
591
592 uri = "http://www.appinf.com/foo/./bar/../index.html";
593 uri.normalize();
594 assertTrue (uri.toString() == "http://www.appinf.com/foo/index.html");
595
596 uri = "http://www.appinf.com/foo/bar/../../index.html";
597 uri.normalize();
598 assertTrue (uri.toString() == "http://www.appinf.com/index.html");
599
600 uri = "http://www.appinf.com/foo/bar/../../../index.html";
601 uri.normalize();
602 assertTrue (uri.toString() == "http://www.appinf.com/index.html");
603
604 uri = "http://www.appinf.com/foo/bar/.././../index.html";
605 uri.normalize();
606 assertTrue (uri.toString() == "http://www.appinf.com/index.html");
607
608 uri = "http://www.appinf.com/./foo/bar/index.html";
609 uri.normalize();
610 assertTrue (uri.toString() == "http://www.appinf.com/foo/bar/index.html");
611
612 uri = "http://www.appinf.com/../foo/bar/index.html";
613 uri.normalize();
614 assertTrue (uri.toString() == "http://www.appinf.com/foo/bar/index.html");
615
616 uri = "http://www.appinf.com/../foo/bar/";
617 uri.normalize();
618 assertTrue (uri.toString() == "http://www.appinf.com/foo/bar/");
619
620 uri = "http://www.appinf.com/../foo/../";
621 uri.normalize();
622 assertTrue (uri.toString() == "http://www.appinf.com/");
623
624 uri = "file:///c:/Windows/system32/";
625 uri.normalize();
626 assertTrue (uri.toString() == "file:///c:/Windows/system32/");
627
628 uri.clear();
629 uri.setPath("c:/windows/system32/");
630 uri.normalize();
631 assertTrue (uri.toString() == "./c:/windows/system32/");
632 }
633
634
testResolve()635 void URITest::testResolve()
636 {
637 URI uri("http://www.appinf.com");
638
639 uri.resolve("/index.html");
640 assertTrue (uri.toString() == "http://www.appinf.com/index.html");
641
642 uri.resolve("#frag");
643 assertTrue (uri.toString() == "http://www.appinf.com/index.html#frag");
644
645 uri = "http://www.appinf.com/html";
646 uri.resolve("../images/foo.gif");
647 assertTrue (uri.toString() == "http://www.appinf.com/images/foo.gif");
648
649 uri = "http://www.appinf.com/html/";
650 uri.resolve(".");
651 assertTrue (uri.toString() == "http://www.appinf.com/html/");
652
653 uri = "http://www.appinf.com/html/";
654 uri.resolve(".");
655 assertTrue (uri.toString() == "http://www.appinf.com/html/");
656
657 uri = "http://www.appinf.com/html/";
658 uri.resolve("..");
659 assertTrue (uri.toString() == "http://www.appinf.com/");
660
661 uri = "http://www.appinf.com/html/";
662 uri.resolve("index.html");
663 assertTrue (uri.toString() == "http://www.appinf.com/html/index.html");
664
665 uri = "http://www.appinf.com/html/";
666 uri.resolve("/index.html");
667 assertTrue (uri.toString() == "http://www.appinf.com/index.html");
668
669 uri = "/a/b/c/d/e";
670 uri.resolve("./../../f/./g");
671 assertTrue (uri.toString() == "/a/b/f/g");
672
673 uri = "/a/b/../c/";
674 uri.resolve("../d");
675 assertTrue (uri.toString() == "/a/d");
676
677 uri = "/a/b/../c/";
678 uri.resolve("../d/");
679 assertTrue (uri.toString() == "/a/d/");
680
681 uri = "/a/b/c/";
682 uri.resolve("../../../../d/");
683 assertTrue (uri.toString() == "/d/");
684
685 uri = "/a/b/c/";
686 uri.resolve("%2e%2e/%2e%2e/%2e%2e/../d/");
687 assertTrue (uri.toString() == "/d/");
688
689 uri = "/a/b/c/";
690 uri.resolve("");
691 assertTrue (uri.toString() == "/a/b/c/");
692
693 uri = "/a/b/c/";
694 uri.resolve("/d/");
695 assertTrue (uri.toString() == "/d/");
696
697 uri = "/a/b/c";
698 uri.resolve("");
699 assertTrue (uri.toString() == "/a/b/c");
700
701 uri = "/a/b/c";
702 uri.resolve("?query=test");
703 assertTrue (uri.toString() == "/a/b/c?query=test");
704
705 uri = "/a/b/c";
706 uri.resolve("#frag");
707 assertTrue (uri.toString() == "/a/b/c#frag");
708
709 uri = "http://www.appinf.com/html/";
710 uri.resolve("http://www.google.com/");
711 assertTrue (uri.toString() == "http://www.google.com/");
712
713 uri = "http://www.appinf.com/";
714 URI uri2(uri, "index.html");
715 assertTrue (uri2.toString() == "http://www.appinf.com/index.html");
716
717 uri = "index.html";
718 URI uri3(uri, "search.html");
719 assertTrue (uri3.toString() == "search.html");
720 }
721
722
testSwap()723 void URITest::testSwap()
724 {
725 URI uri1("http://www.appinf.com/search.cgi?keyword=test%20encoded&scope=all#result");
726 URI uri2("mailto:John.Doe@example.com");
727
728 uri1.swap(uri2);
729 assertTrue (uri1.toString() == "mailto:John.Doe@example.com");
730 assertTrue (uri2.toString() == "http://www.appinf.com/search.cgi?keyword=test%20encoded&scope=all#result");
731 }
732
733
testOther()734 void URITest::testOther()
735 {
736 // The search string is "hello%world"; google happens to ignore the '%'
737 // character, so it finds lots of hits for "hello world" programs. This is
738 // a convenient reproduction case, and a URL that actually works.
739 Poco::URI uri("http://google.com/search?q=hello%25world#frag%20ment");
740
741 assertTrue (uri.getHost() == "google.com");
742 assertTrue (uri.getPath() == "/search");
743 assertTrue (uri.getQuery() == "q=hello%world");
744 assertTrue (uri.getRawQuery() == "q=hello%25world");
745 assertTrue (uri.getFragment() == "frag ment");
746 assertTrue (uri.toString() == "http://google.com/search?q=hello%25world#frag%20ment");
747 assertTrue (uri.getPathEtc() == "/search?q=hello%25world#frag%20ment");
748
749 uri.setQuery("q=foo&bar");
750 assertTrue (uri.getQuery() == "q=foo&bar");
751 assertTrue (uri.getRawQuery() == "q=foo&bar");
752 assertTrue (uri.toString() == "http://google.com/search?q=foo&bar#frag%20ment");
753 assertTrue (uri.getPathEtc() == "/search?q=foo&bar#frag%20ment");
754
755 uri.setQuery("q=foo/bar");
756 assertTrue (uri.getQuery() == "q=foo/bar");
757 assertTrue (uri.getRawQuery() == "q=foo%2Fbar");
758 assertTrue (uri.toString() == "http://google.com/search?q=foo%2Fbar#frag%20ment");
759 assertTrue (uri.getPathEtc() == "/search?q=foo%2Fbar#frag%20ment");
760
761 uri.setQuery("q=goodbye cruel world");
762 assertTrue (uri.getQuery() == "q=goodbye cruel world");
763 assertTrue (uri.getRawQuery() == "q=goodbye%20cruel%20world");
764 assertTrue (uri.toString() == "http://google.com/search?q=goodbye%20cruel%20world#frag%20ment");
765 assertTrue (uri.getPathEtc() == "/search?q=goodbye%20cruel%20world#frag%20ment");
766
767 uri.setRawQuery("q=pony%7eride");
768 assertTrue (uri.getQuery() == "q=pony~ride");
769 assertTrue (uri.getRawQuery() == "q=pony%7eride");
770 assertTrue (uri.toString() == "http://google.com/search?q=pony%7eride#frag%20ment");
771 assertTrue (uri.getPathEtc() == "/search?q=pony%7eride#frag%20ment");
772
773 uri.addQueryParameter("pa=ra&m1");
774 assertTrue (uri.getRawQuery() == "q=pony%7eride&pa%3Dra%26m1=");
775 assertTrue (uri.getQuery() == "q=pony~ride&pa=ra&m1=");
776 uri.addQueryParameter("pa=ra&m2", "val&ue");
777 assertTrue (uri.getRawQuery() == "q=pony%7eride&pa%3Dra%26m1=&pa%3Dra%26m2=val%26ue");
778 assertTrue (uri.getQuery() == "q=pony~ride&pa=ra&m1=&pa=ra&m2=val&ue");
779
780 uri = "http://google.com/search?q=hello+world#frag%20ment";
781 assertTrue (uri.getHost() == "google.com");
782 assertTrue (uri.getPath() == "/search");
783 assertTrue (uri.getQuery() == "q=hello+world");
784 assertTrue (uri.getRawQuery() == "q=hello+world");
785 assertTrue (uri.getFragment() == "frag ment");
786 assertTrue (uri.toString() == "http://google.com/search?q=hello+world#frag%20ment");
787 assertTrue (uri.getPathEtc() == "/search?q=hello+world#frag%20ment");
788 }
789
790
testEncodeDecode()791 void URITest::testEncodeDecode()
792 {
793 std::string str;
794 URI::encode("http://google.com/search?q=hello+world#frag ment", "+#?", str);
795 assertTrue (str == "http://google.com/search%3Fq=hello%2Bworld%23frag%20ment");
796
797 str = "";
798 URI::encode("http://google.com/search?q=hello+world#frag ment", "", str);
799 assertTrue (str == "http://google.com/search?q=hello+world#frag%20ment");
800
801 str = "";
802 URI::decode("http://google.com/search?q=hello+world#frag%20ment", str, true);
803 assertTrue (str == "http://google.com/search?q=hello world#frag ment");
804
805 str = "";
806 URI::decode("http://google.com/search?q=hello%2Bworld#frag%20ment", str);
807 assertTrue (str == "http://google.com/search?q=hello+world#frag ment");
808 }
809
810
testFromPath()811 void URITest::testFromPath()
812 {
813 Path path1("/var/www/site/index.html", Path::PATH_UNIX);
814 URI uri1(path1);
815 assertTrue (uri1.toString() == "file:///var/www/site/index.html");
816
817 Path path2("/var/www/site/with space.html", Path::PATH_UNIX);
818 URI uri2(path2);
819 assertTrue (uri2.toString() == "file:///var/www/site/with%20space.html");
820
821 Path path3("c:\\www\\index.html", Path::PATH_WINDOWS);
822 URI uri3(path3);
823 assertTrue (uri3.toString() == "file:///c:/www/index.html");
824 }
825
826
testQueryParameters()827 void URITest::testQueryParameters()
828 {
829 Poco::URI uri("http://google.com/search?q=hello+world&client=safari");
830 URI::QueryParameters params = uri.getQueryParameters();
831 assertTrue (params.size() == 2);
832 assertTrue (params[0].first == "q");
833 assertTrue (params[0].second == "hello world");
834 assertTrue (params[1].first == "client");
835 assertTrue (params[1].second == "safari");
836
837 uri.setQueryParameters(params);
838 assertTrue (uri.toString() == "http://google.com/search?q=hello%20world&client=safari");
839
840 uri = "http://google.com/search?q=&client&";
841 params = uri.getQueryParameters();
842 assertTrue (params.size() == 2);
843 assertTrue (params[0].first == "q");
844 assertTrue (params[0].second == "");
845 assertTrue (params[1].first == "client");
846 assertTrue (params[1].second == "");
847
848 uri.setQueryParameters(params);
849 assertTrue (uri.toString() == "http://google.com/search?q=&client=");
850
851 params[0].second = "foo/bar?";
852 uri.setQueryParameters(params);
853 assertTrue (uri.toString() == "http://google.com/search?q=foo%2Fbar%3F&client=");
854
855 params[0].second = "foo&bar";
856 uri.setQueryParameters(params);
857 assertTrue (uri.toString() == "http://google.com/search?q=foo%26bar&client=");
858 }
859
860
setUp()861 void URITest::setUp()
862 {
863 }
864
865
tearDown()866 void URITest::tearDown()
867 {
868 }
869
870
suite()871 CppUnit::Test* URITest::suite()
872 {
873 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("URITest");
874
875 CppUnit_addTest(pSuite, URITest, testConstruction);
876 CppUnit_addTest(pSuite, URITest, testParse);
877 CppUnit_addTest(pSuite, URITest, testToString);
878 CppUnit_addTest(pSuite, URITest, testCompare);
879 CppUnit_addTest(pSuite, URITest, testNormalize);
880 CppUnit_addTest(pSuite, URITest, testResolve);
881 CppUnit_addTest(pSuite, URITest, testSwap);
882 CppUnit_addTest(pSuite, URITest, testEncodeDecode);
883 CppUnit_addTest(pSuite, URITest, testOther);
884 CppUnit_addTest(pSuite, URITest, testFromPath);
885 CppUnit_addTest(pSuite, URITest, testQueryParameters);
886
887 return pSuite;
888 }
889