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]/c=GB?objectClass?one", 197 { "ldap", "", "", "[2001:db8::7]",389,"/c=GB", "objectClass?one", "" } }, 198 { "HTTP://example.com.:80/%70a%74%68?a=%31#1%323", 199 { "HTTP", "", "", "example.com.",80, "/%70a%74%68","a=%31","1%323"} } 200 }; 201 202 void UrlTest::ExplodeImplodeTest() 203 { 204 uint8 testIndex; 205 BUrl testUrl; 206 207 for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++) 208 { 209 NextSubTest(); 210 testUrl.SetUrlString(kTestExplode[testIndex].url); 211 212 CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].url), 213 BString(testUrl.UrlString())); 214 CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.protocol), 215 BString(testUrl.Protocol())); 216 CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.userName), 217 BString(testUrl.UserName())); 218 CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.password), 219 BString(testUrl.Password())); 220 CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.host), 221 BString(testUrl.Host())); 222 CPPUNIT_ASSERT_EQUAL(kTestExplode[testIndex].expected.port, 223 testUrl.Port()); 224 CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.path), 225 BString(testUrl.Path())); 226 CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.request), 227 BString(testUrl.Request())); 228 CPPUNIT_ASSERT_EQUAL(BString(kTestExplode[testIndex].expected.fragment), 229 BString(testUrl.Fragment())); 230 } 231 } 232 233 234 void 235 UrlTest::PathOnly() 236 { 237 BUrl test = "lol"; 238 CPPUNIT_ASSERT(test.HasPath()); 239 CPPUNIT_ASSERT_EQUAL(BString("lol"), test.Path()); 240 CPPUNIT_ASSERT_EQUAL(BString("lol"), test.UrlString()); 241 } 242 243 244 void 245 UrlTest::RelativeUriTest() 246 { 247 // http://skew.org/uri/uri%5Ftests.html 248 struct RelativeUrl { 249 const char* base; 250 const char* relative; 251 const char* absolute; 252 }; 253 254 const RelativeUrl tests[] = { 255 {"http://example.com/path?query#frag", "", 256 "http://example.com/path?query"}, 257 {"foo:a/b", "../c", "foo:/c"}, 258 // foo:c would be more intuitive, and is what skew.org tests. 259 // However, foo:/c is what the RFC says we should get. 260 {"foo:a", "foo:.", "foo:"}, 261 {"zz:abc", "/foo/../../../bar", "zz:/bar"}, 262 {"zz:abc", "/foo/../bar", "zz:/bar"}, 263 {"zz:abc", "foo/../../../bar", "zz:/bar"}, 264 // zz:bar would be more intuitive, ... 265 {"zz:abc", "zz:.", "zz:"}, 266 {"http://a/b/c/d;p?q", "/.", "http://a/"}, 267 {"http://a/b/c/d;p?q", "/.foo", "http://a/.foo"}, 268 {"http://a/b/c/d;p?q", ".foo", "http://a/b/c/.foo"}, 269 {"http://a/b/c/d;p?q", "g:h", "g:h"}, 270 271 {"http://a/b/c/d;p?q", "g:h", "g:h"}, 272 {"http://a/b/c/d;p?q", "g", "http://a/b/c/g"}, 273 {"http://a/b/c/d;p?q", "./g", "http://a/b/c/g"}, 274 {"http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"}, 275 {"http://a/b/c/d;p?q", "/g", "http://a/g"}, 276 {"http://a/b/c/d;p?q", "//g", "http://g"}, 277 {"http://a/b/c/d;p?q", "?y", "http://a/b/c/d;p?y"}, 278 {"http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"}, 279 {"http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s"}, 280 {"http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"}, 281 {"http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"}, 282 {"http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"}, 283 {"http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"}, 284 {"http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"}, 285 {"http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q"}, 286 {"http://a/b/c/d;p?q", ".", "http://a/b/c/"}, 287 {"http://a/b/c/d;p?q", "./", "http://a/b/c/"}, 288 {"http://a/b/c/d;p?q", "..", "http://a/b/"}, 289 {"http://a/b/c/d;p?q", "../", "http://a/b/"}, 290 {"http://a/b/c/d;p?q", "../g", "http://a/b/g"}, 291 {"http://a/b/c/d;p?q", "../..", "http://a/"}, 292 {"http://a/b/c/d;p?q", "../../", "http://a/"}, 293 {"http://a/b/c/d;p?q", "../../g", "http://a/g"}, 294 295 // Parsers must be careful in handling cases where there are more 296 // relative path ".." segments than there are hierarchical levels in the 297 // base URI's path. Note that the ".." syntax cannot be used to change 298 // the authority component of a URI. 299 {"http://a/b/c/d;p?q", "../../../g", "http://a/g"}, 300 {"http://a/b/c/d;p?q", "../../../../g", "http://a/g"}, 301 302 // Similarly, parsers must remove the dot-segments "." and ".." when 303 // they are complete components of a path, but not when they are only 304 // part of a segment. 305 {"http://a/b/c/d;p?q", "/./g", "http://a/g"}, 306 {"http://a/b/c/d;p?q", "/../g", "http://a/g"}, 307 {"http://a/b/c/d;p?q", "g.", "http://a/b/c/g."}, 308 {"http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"}, 309 {"http://a/b/c/d;p?q", "g..", "http://a/b/c/g.."}, 310 {"http://a/b/c/d;p?q", "..g", "http://a/b/c/..g"}, 311 312 // Less likely are cases where the relative URI reference uses 313 // unnecessary or nonsensical forms of the "." and ".." complete path 314 // segments. 315 {"http://a/b/c/d;p?q", "./../g", "http://a/b/g"}, 316 {"http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"}, 317 {"http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"}, 318 {"http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h"}, 319 {"http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"}, 320 {"http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y"}, 321 322 // Some applications fail to separate the reference's query and/or 323 // fragment components from a relative path before merging it with the 324 // base path and removing dot-segments. This error is rarely noticed, 325 // since typical usage of a fragment never includes the hierarchy ("/") 326 // character, and the query component is not normally used within 327 // relative references. 328 {"http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x"}, 329 {"http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x"}, 330 {"http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x"}, 331 {"http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x"}, 332 333 // Some parsers allow the scheme name to be present in a relative URI 334 // reference if it is the same as the base URI scheme. This is 335 // considered to be a loophole in prior specifications of partial URI 336 // [RFC1630]. Its use should be avoided, but is allowed for backward 337 // compatibility. 338 {"http://a/b/c/d;p?q", "http:g", "http:g"}, 339 {"http://a/b/c/d;p?q", "http:", "http:"}, 340 341 {"http://a/b/c/d;p?q", "./g:h", "http://a/b/c/g:h"}, 342 {"http://a/b/c/d;p?q", "/a/b/c/./../../g", "http://a/a/g"}, 343 344 {"http://a/b/c/d;p?q=1/2", "g", "http://a/b/c/g"}, 345 {"http://a/b/c/d;p?q=1/2", "./g", "http://a/b/c/g"}, 346 {"http://a/b/c/d;p?q=1/2", "g/", "http://a/b/c/g/"}, 347 {"http://a/b/c/d;p?q=1/2", "/g", "http://a/g"}, 348 {"http://a/b/c/d;p?q=1/2", "//g", "http://g"}, 349 {"http://a/b/c/d;p?q=1/2", "?y", "http://a/b/c/d;p?y"}, 350 {"http://a/b/c/d;p?q=1/2", "g?y", "http://a/b/c/g?y"}, 351 {"http://a/b/c/d;p?q=1/2", "g?y/./x", "http://a/b/c/g?y/./x"}, 352 {"http://a/b/c/d;p?q=1/2", "g?y/../x", "http://a/b/c/g?y/../x"}, 353 {"http://a/b/c/d;p?q=1/2", "g#s", "http://a/b/c/g#s"}, 354 {"http://a/b/c/d;p?q=1/2", "g#s/./x", "http://a/b/c/g#s/./x"}, 355 {"http://a/b/c/d;p?q=1/2", "g#s/../x", "http://a/b/c/g#s/../x"}, 356 {"http://a/b/c/d;p?q=1/2", "./", "http://a/b/c/"}, 357 {"http://a/b/c/d;p?q=1/2", "../", "http://a/b/"}, 358 {"http://a/b/c/d;p?q=1/2", "../g", "http://a/b/g"}, 359 {"http://a/b/c/d;p?q=1/2", "../../", "http://a/"}, 360 {"http://a/b/c/d;p?q=1/2", "../../g", "http://a/g"}, 361 362 {"http://a/b/c/d;p=1/2?q", "g", "http://a/b/c/d;p=1/g"}, 363 {"http://a/b/c/d;p=1/2?q", "./g", "http://a/b/c/d;p=1/g"}, 364 {"http://a/b/c/d;p=1/2?q", "g/", "http://a/b/c/d;p=1/g/"}, 365 {"http://a/b/c/d;p=1/2?q", "g?y", "http://a/b/c/d;p=1/g?y"}, 366 {"http://a/b/c/d;p=1/2?q", ";x", "http://a/b/c/d;p=1/;x"}, 367 {"http://a/b/c/d;p=1/2?q", "g;x", "http://a/b/c/d;p=1/g;x"}, 368 {"http://a/b/c/d;p=1/2?q", "g;x=1/./y", "http://a/b/c/d;p=1/g;x=1/y"}, 369 {"http://a/b/c/d;p=1/2?q", "g;x=1/../y", "http://a/b/c/d;p=1/y"}, 370 {"http://a/b/c/d;p=1/2?q", "./", "http://a/b/c/d;p=1/"}, 371 {"http://a/b/c/d;p=1/2?q", "../", "http://a/b/c/"}, 372 {"http://a/b/c/d;p=1/2?q", "../g", "http://a/b/c/g"}, 373 {"http://a/b/c/d;p=1/2?q", "../../", "http://a/b/"}, 374 {"http://a/b/c/d;p=1/2?q", "../../g", "http://a/b/g"}, 375 376 // Empty host and directory 377 {"fred:///s//a/b/c", "g:h", "g:h"}, 378 {"fred:///s//a/b/c", "g", "fred:///s//a/b/g"}, 379 {"fred:///s//a/b/c", "./g", "fred:///s//a/b/g"}, 380 {"fred:///s//a/b/c", "g/", "fred:///s//a/b/g/"}, 381 {"fred:///s//a/b/c", "/g", "fred:///g"}, 382 {"fred:///s//a/b/c", "//g", "fred://g"}, 383 {"fred:///s//a/b/c", "//g/x", "fred://g/x"}, 384 {"fred:///s//a/b/c", "///g", "fred:///g"}, 385 {"fred:///s//a/b/c", "./", "fred:///s//a/b/"}, 386 {"fred:///s//a/b/c", "../", "fred:///s//a/"}, 387 {"fred:///s//a/b/c", "../g", "fred:///s//a/g"}, 388 {"fred:///s//a/b/c", "../..", "fred:///s//"}, 389 {"fred:///s//a/b/c", "../../g", "fred:///s//g"}, 390 {"fred:///s//a/b/c", "../../../g", "fred:///s/g"}, 391 {"fred:///s//a/b/c", "../../../g", "fred:///s/g"}, 392 393 {"http:///s//a/b/c", "g:h", "g:h"}, 394 {"http:///s//a/b/c", "g", "http:///s//a/b/g"}, 395 {"http:///s//a/b/c", "./g", "http:///s//a/b/g"}, 396 {"http:///s//a/b/c", "g/", "http:///s//a/b/g/"}, 397 {"http:///s//a/b/c", "/g", "http:///g"}, 398 {"http:///s//a/b/c", "//g", "http://g"}, 399 {"http:///s//a/b/c", "//g/x", "http://g/x"}, 400 {"http:///s//a/b/c", "///g", "http:///g"}, 401 {"http:///s//a/b/c", "./", "http:///s//a/b/"}, 402 {"http:///s//a/b/c", "../", "http:///s//a/"}, 403 {"http:///s//a/b/c", "../g", "http:///s//a/g"}, 404 {"http:///s//a/b/c", "../..", "http:///s//"}, 405 {"http:///s//a/b/c", "../../g", "http:///s//g"}, 406 {"http:///s//a/b/c", "../../../g", "http:///s/g"}, 407 {"http:///s//a/b/c", "../../../g", "http:///s/g"}, 408 409 {"foo:xyz", "bar:abc", "bar:abc"}, 410 {"http://example/x/y/z","../abc", "http://example/x/abc"}, 411 {"http://example2/x/y/z","http://example/x/abc","http://example/x/abc"}, 412 {"http://ex/x/y/z", "../r", "http://ex/x/r"}, 413 {"http://ex/x/y", "q/r", "http://ex/x/q/r"}, 414 {"http://ex/x/y", "q/r#s", "http://ex/x/q/r#s"}, 415 {"http://ex/x/y", "q/r#s/t", "http://ex/x/q/r#s/t"}, 416 {"http://ex/x/y", "ftp://ex/x/q/r", "ftp://ex/x/q/r"}, 417 {"http://ex/x/y", "", "http://ex/x/y"}, 418 {"http://ex/x/y/", "", "http://ex/x/y/"}, 419 {"http://ex/x/y/pdq", "", "http://ex/x/y/pdq"}, 420 {"http://ex/x/y/", "z/", "http://ex/x/y/z/"}, 421 422 {"file:/swap/test/animal.rdf", "#Animal", 423 "file:/swap/test/animal.rdf#Animal"}, 424 {"file:/e/x/y/z", "../abc", "file:/e/x/abc"}, 425 {"file:/example2/x/y/z","/example/x/abc", "file:/example/x/abc"}, 426 {"file:/e/x/y/z", "../r", "file:/e/x/r"}, 427 {"file:/e/x/y/z", "/r", "file:/r"}, 428 {"file:/e/x/y", "q/r", "file:/e/x/q/r"}, 429 {"file:/e/x/y", "q/r#s", "file:/e/x/q/r#s"}, 430 {"file:/e/x/y", "q/r#", "file:/e/x/q/r#"}, 431 {"file:/e/x/y", "q/r#s/t", "file:/e/x/q/r#s/t"}, 432 {"file:/e/x/y", "ftp://ex/x/q/r", "ftp://ex/x/q/r"}, 433 {"file:/e/x/y", "", "file:/e/x/y"}, 434 {"file:/e/x/y/", "", "file:/e/x/y/"}, 435 {"file:/e/x/y/pdq", "", "file:/e/x/y/pdq"}, 436 {"file:/e/x/y/", "z/", "file:/e/x/y/z/"}, 437 {"file:/devel/WWW/2000/10/swap/test/reluri-1.n3", 438 "file://meetings.example.com/cal#m1", 439 "file://meetings.example.com/cal#m1"}, 440 {"file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3", 441 "file://meetings.example.com/cal#m1", 442 "file://meetings.example.com/cal#m1"}, 443 {"file:/some/dir/foo", "./#blort", "file:/some/dir/#blort"}, 444 {"file:/some/dir/foo", "./#", "file:/some/dir/#"}, 445 446 {"http://example/x/abc.efg", "./", "http://example/x/"}, 447 {"http://example2/x/y/z", "//example/x/abc","http://example/x/abc"}, 448 {"http://ex/x/y/z", "/r", "http://ex/r"}, 449 {"http://ex/x/y", "./q:r", "http://ex/x/q:r"}, 450 {"http://ex/x/y", "./p=q:r", "http://ex/x/p=q:r"}, 451 {"http://ex/x/y?pp/qq", "?pp/rr", "http://ex/x/y?pp/rr"}, 452 {"http://ex/x/y?pp/qq", "y/z", "http://ex/x/y/z"}, 453 454 {"mailto:local", "local/qual@domain.org#frag", 455 "mailto:local/qual@domain.org#frag"}, 456 {"mailto:local/qual1@domain1.org", "more/qual2@domain2.org#frag", 457 "mailto:local/more/qual2@domain2.org#frag"}, 458 459 {"http://ex/x/y?q", "y?q", "http://ex/x/y?q"}, 460 {"http://ex?p", "x/y?q", "http://ex/x/y?q"}, 461 {"foo:a/b", "c/d", "foo:a/c/d"}, 462 {"foo:a/b", "/c/d", "foo:/c/d"}, 463 {"foo:a/b?c#d", "", "foo:a/b?c"}, 464 {"foo:a", "b/c", "foo:b/c"}, 465 {"foo:/a/y/z", "../b/c", "foo:/a/b/c"}, 466 {"foo:a", "./b/c", "foo:b/c"}, 467 {"foo:a", "/./b/c", "foo:/b/c"}, 468 {"foo://a//b/c", "../../d", "foo://a/d"}, 469 {"foo:a", ".", "foo:"}, 470 {"foo:a", "..", "foo:"}, 471 472 {"http://example/x/y%2Fz", "abc", "http://example/x/abc"}, 473 {"http://example/a/x/y/z", "../../x%2Fabc", "http://example/a/x%2Fabc"}, 474 {"http://example/a/x/y%2Fz", "../x%2Fabc", "http://example/a/x%2Fabc"}, 475 {"http://example/x%2Fy/z", "abc", "http://example/x%2Fy/abc"}, 476 {"http://ex/x/y", "q%3Ar", "http://ex/x/q%3Ar"}, 477 {"http://example/x/y%2Fz", "/x%2Fabc", "http://example/x%2Fabc"}, 478 {"http://example/x/y/z", "/x%2Fabc", "http://example/x%2Fabc"}, 479 480 {"mailto:local1@domain1?query1", "local2@domain2", 481 "mailto:local2@domain2"}, 482 {"mailto:local1@domain1", "local2@domain2?query2", 483 "mailto:local2@domain2?query2"}, 484 {"mailto:local1@domain1?query1", "local2@domain2?query2", 485 "mailto:local2@domain2?query2"}, 486 {"mailto:local@domain?query1", "?query2", 487 "mailto:local@domain?query2"}, 488 {"mailto:?query1", "local2@domain2?query2", 489 "mailto:local2@domain2?query2"}, 490 {"mailto:local1@domain1?query1", "?query2", 491 "mailto:local1@domain1?query2"}, 492 493 {"foo:bar", "http://example/a/b?c/../d", "http://example/a/b?c/../d"}, 494 {"foo:bar", "http://example/a/b#c/../d", "http://example/a/b#c/../d"}, 495 {"http://example.org/base/uri", "http:this", "http:this"}, 496 {"http:base", "http:this", "http:this"}, 497 {"f:/a", ".//g", "f://g"}, 498 {"f://example.org/base/a", "b/c//d/e", "f://example.org/base/b/c//d/e"}, 499 {"mid:m@example.ord/c@example.org", "m2@example.ord/c2@example.org", 500 "mid:m@example.ord/m2@example.ord/c2@example.org"}, 501 {"file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/", "mini1.xml", 502 "file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/mini1.xml"}, 503 {"foo:a/y/z", "../b/c", "foo:a/b/c"}, 504 {"foo:", "b", "foo:b"}, 505 {"foo://a", "b", "foo://a/b"}, 506 {"foo://a?q", "b", "foo://a/b"}, 507 {"foo://a", "b?q", "foo://a/b?q"}, 508 {"foo://a?r", "b?q", "foo://a/b?q"}, 509 }; 510 511 BString message(" Base: "); 512 for (unsigned int index = 0; index < sizeof(tests) / sizeof(RelativeUrl); 513 index++) 514 { 515 NextSubTest(); 516 517 BUrl baseUrl(tests[index].base); 518 519 message.Truncate(7, true); 520 message << tests[index].base; 521 message << " Relative: "; 522 message << tests[index].relative; 523 524 CPPUNIT_ASSERT_EQUAL_MESSAGE(message.String(), 525 BString(tests[index].absolute), 526 BUrl(baseUrl, tests[index].relative).UrlString()); 527 } 528 } 529 530 531 532 void 533 UrlTest::IDNTest() 534 { 535 // http://www.w3.org/2004/04/uri-rel-test.html 536 // TODO these need to be manually UrlDecoded with ou API. 537 // We also need to decide wether to store them as UTF-8 or IDNA/punycode. 538 NextSubTest(); 539 CPPUNIT_ASSERT_EQUAL(BUrl("http://www.w3.org").UrlString(), 540 BUrl("http://www.w%33.org").UrlString()); 541 NextSubTest(); 542 CPPUNIT_ASSERT_EQUAL( 543 BUrl("http://xn--rksmrgs-5wao1o.josefsson.org").UrlString(), 544 BUrl("http://r%C3%A4ksm%C3%B6rg%C3%A5s.josefsson.org").UrlString()); 545 NextSubTest(); 546 CPPUNIT_ASSERT_EQUAL( 547 BUrl("http://xn--rksmrgs-5wao1o.josefsson.org").UrlString(), 548 BUrl("http://%E7%B4%8D%E8%B1%86.w3.mag.keio.ac.jp").UrlString()); 549 NextSubTest(); 550 CPPUNIT_ASSERT_EQUAL( 551 BUrl("http://www.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3fg11amb5gzdb4wi9b" 552 "ya3kc6lra.w3.mag.keio.ac.jp/").UrlString(), 553 BUrl("http://www.%E3%81%BB%E3%82%93%E3%81%A8%E3%81%86%E3%81%AB%E3%81%AA" 554 "%E3%81%8C%E3%81%84%E3%82%8F%E3%81%91%E3%81%AE%E3%82%8F%E3%81%8B%E3" 555 "%82%89%E3%81%AA%E3%81%84%E3%81%A9%E3%82%81%E3%81%84%E3%82%93%E3%82" 556 "%81%E3%81%84%E3%81%AE%E3%82%89%E3%81%B9%E3%82%8B%E3%81%BE%E3%81%A0" 557 "%E3%81%AA%E3%81%8C%E3%81%8F%E3%81%97%E3%81%AA%E3%81%84%E3%81%A8%E3" 558 "%81%9F%E3%82%8A%E3%81%AA%E3%81%84.w3.mag.keio.ac.jp/").UrlString()); 559 NextSubTest(); 560 CPPUNIT_ASSERT_EQUAL( 561 BUrl("http://xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3fg11amb5gzdb4wi9bya3k" 562 "c6lra.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3fg11amb5gzdb4wi9bya3kc6" 563 "lra.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3fg11amb5gzdb4wi9bya3kc6lr" 564 "a.w3.mag.keio.ac.jp/").UrlString(), 565 BUrl("http://%E3%81%BB%E3%82%93%E3%81%A8%E3%81%86%E3%81%AB%E3%81%AA%E3" 566 "%81%8C%E3%81%84%E3%82%8F%E3%81%91%E3%81%AE%E3%82%8F%E3%81%8B%E3%82" 567 "%89%E3%81%AA%E3%81%84%E3%81%A9%E3%82%81%E3%81%84%E3%82%93%E3%82%81" 568 "%E3%81%84%E3%81%AE%E3%82%89%E3%81%B9%E3%82%8B%E3%81%BE%E3%81%A0%E3" 569 "%81%AA%E3%81%8C%E3%81%8F%E3%81%97%E3%81%AA%E3%81%84%E3%81%A8%E3%81" 570 "%9F%E3%82%8A%E3%81%AA%E3%81%84.%E3%81%BB%E3%82%93%E3%81%A8%E3%81" 571 "%86%E3%81%AB%E3%81%AA%E3%81%8C%E3%81%84%E3%82%8F%E3%81%91%E3%81%AE" 572 "%E3%82%8F%E3%81%8B%E3%82%89%E3%81%AA%E3%81%84%E3%81%A9%E3%82%81%E3" 573 "%81%84%E3%82%93%E3%82%81%E3%81%84%E3%81%AE%E3%82%89%E3%81%B9%E3%82" 574 "%8B%E3%81%BE%E3%81%A0%E3%81%AA%E3%81%8C%E3%81%8F%E3%81%97%E3%81%AA" 575 "%E3%81%84%E3%81%A8%E3%81%9F%E3%82%8A%E3%81%AA%E3%81%84.%E3%81%BB" 576 "%E3%82%93%E3%81%A8%E3%81%86%E3%81%AB%E3%81%AA%E3%81%8C%E3%81%84%E3" 577 "%82%8F%E3%81%91%E3%81%AE%E3%82%8F%E3%81%8B%E3%82%89%E3%81%AA%E3%81" 578 "%84%E3%81%A9%E3%82%81%E3%81%84%E3%82%93%E3%82%81%E3%81%84%E3%81%AE" 579 "%E3%82%89%E3%81%B9%E3%82%8B%E3%81%BE%E3%81%A0%E3%81%AA%E3%81%8C%E3" 580 "%81%8F%E3%81%97%E3%81%AA%E3%81%84%E3%81%A8%E3%81%9F%E3%82%8A%E3%81" 581 "%AA%E3%81%84.w3.mag.keio.ac.jp/").UrlString()); 582 } 583 584 585 /* static */ void 586 UrlTest::AddTests(BTestSuite& parent) 587 { 588 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("UrlTest"); 589 590 suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::ParseTest", 591 &UrlTest::ParseTest)); 592 suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::TestIsValid", 593 &UrlTest::TestIsValid)); 594 suite.addTest(new CppUnit::TestCaller<UrlTest>( 595 "UrlTest::TestGettersSetters", &UrlTest::TestGettersSetters)); 596 suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::TestNullity", 597 &UrlTest::TestNullity)); 598 suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::TestCopy", 599 &UrlTest::TestCopy)); 600 suite.addTest(new CppUnit::TestCaller<UrlTest>( 601 "UrlTest::ExplodeImplodeTest", &UrlTest::ExplodeImplodeTest)); 602 suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::PathOnly", 603 &UrlTest::PathOnly)); 604 suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::RelativeUriTest", 605 &UrlTest::RelativeUriTest)); 606 suite.addTest(new CppUnit::TestCaller<UrlTest>("UrlTest::IDNTest", 607 &UrlTest::IDNTest)); 608 609 parent.addTest("UrlTest", &suite); 610 } 611