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