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