1 16 17 18 package org.apache.catalina.util; 19 20 21 import java.net.MalformedURLException ; 22 import junit.framework.Test; 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 26 27 33 34 public class URLTestCase extends TestCase { 35 36 37 39 40 42 43 48 public URLTestCase(String name) { 49 50 super(name); 51 52 } 53 54 55 57 58 61 public void setUp() { 62 63 ; 65 } 66 67 68 71 public static Test suite() { 72 73 return (new TestSuite(URLTestCase.class)); 74 75 } 76 77 78 81 public void tearDown() { 82 83 ; 85 } 86 87 88 90 91 95 public void testNegativeAbsolute() { 96 97 negative("index.html"); 98 negative("index.html#ref"); 99 negative("index.html?name=value"); 100 negative("index.html?name=value#ref"); 101 102 negative("/index.html"); 103 negative("/index.html#ref"); 104 negative("/index.html?name=value"); 105 negative("/index.html?name=value#ref"); 106 107 } 108 109 110 114 public void testNegativeNormalize() { 115 116 normalize("http://localhost/.."); 117 normalize("http://localhost/..#ref"); 118 normalize("http://localhost/..?name=value"); 119 normalize("http://localhost/..?name=value#ref"); 120 121 normalize("http://localhost:8080/.."); 122 normalize("http://localhost:8080/..#ref"); 123 normalize("http://localhost:8080/..?name=value"); 124 normalize("http://localhost:8080/..?name=value#ref"); 125 126 normalize("http://localhost/../"); 127 normalize("http://localhost/../#ref"); 128 normalize("http://localhost/../?name=value"); 129 normalize("http://localhost/../?name=value#ref"); 130 131 normalize("http://localhost:8080/../"); 132 normalize("http://localhost:8080/../#ref"); 133 normalize("http://localhost:8080/../?name=value"); 134 normalize("http://localhost:8080/../?name=value#ref"); 135 136 normalize("http://localhost/index.html/../../foo.html"); 137 normalize("http://localhost/index.html/../../foo.html#ref"); 138 normalize("http://localhost/index.html/../../foo.html?name=value"); 139 normalize("http://localhost/index.html/../../foo.html?name=value#ref"); 140 141 normalize("http://localhost:8080/index.html/../../foo.html"); 142 normalize("http://localhost:8080/index.html/../../foo.html#ref"); 143 normalize("http://localhost:8080/index.html/../../foo.html?name=value"); 144 normalize("http://localhost:8080/index.html/../../foo.html?name=value#ref"); 145 146 } 147 148 149 153 public void testNegativeRelative() { 154 155 159 } 160 161 162 165 public void testPositiveAbsolute() { 166 167 positive("http://a/b/c/d;p?q"); 168 169 positive("http://localhost/index.html"); 170 positive("http://localhost/index.html#ref"); 171 positive("http://localhost/index.html?name=value"); 172 positive("http://localhost/index.html?name=value#ref"); 173 174 positive("http://localhost:8080/index.html"); 175 positive("http://localhost:8080/index.html#ref"); 176 positive("http://localhost:8080/index.html?name=value"); 177 positive("http://localhost:8080/index.html?name=value#ref"); 178 179 positive("http://localhost/index.html/."); 180 positive("http://localhost/index.html/.#ref"); 181 positive("http://localhost/index.html/.?name=value"); 182 positive("http://localhost/index.html/.?name=value#ref"); 183 184 positive("http://localhost:8080/index.html/."); 185 positive("http://localhost:8080/index.html/.#ref"); 186 positive("http://localhost:8080/index.html/.?name=value"); 187 positive("http://localhost:8080/index.html/.?name=value#ref"); 188 189 positive("http://localhost/index.html/foo/.."); 190 positive("http://localhost/index.html/foo/..#ref"); 191 positive("http://localhost/index.html/foo/..?name=value"); 192 positive("http://localhost/index.html/foo/..?name=value#ref"); 193 194 positive("http://localhost:8080/index.html/foo/.."); 195 positive("http://localhost:8080/index.html/foo/..#ref"); 196 positive("http://localhost:8080/index.html/foo/..?name=value"); 197 positive("http://localhost:8080/index.html/foo/..?name=value#ref"); 198 199 positive("http://localhost/index.html/../foo.html"); 200 positive("http://localhost/index.html/../foo.html#ref"); 201 positive("http://localhost/index.html/../foo.html?name=value"); 202 positive("http://localhost/index.html/../foo.html?name=value#ref"); 203 204 positive("http://localhost:8080/index.html/../foo.html"); 205 positive("http://localhost:8080/index.html/../foo.html#ref"); 206 positive("http://localhost:8080/index.html/../foo.html?name=value"); 207 positive("http://localhost:8080/index.html/../foo.html?name=value#ref"); 208 209 positive("http://localhost"); 210 positive("http://localhost#ref"); 211 positive("http://localhost?name=value"); 212 positive("http://localhost?name=value#ref"); 213 214 positive("http://localhost:8080"); 215 positive("http://localhost:8080#ref"); 216 positive("http://localhost:8080?name=value"); 217 positive("http://localhost:8080?name=value#ref"); 218 219 positive("http://localhost/"); 220 positive("http://localhost/#ref"); 221 positive("http://localhost/?name=value"); 222 positive("http://localhost/?name=value#ref"); 223 224 positive("http://localhost:8080/"); 225 positive("http://localhost:8080/#ref"); 226 positive("http://localhost:8080/?name=value"); 227 positive("http://localhost:8080/?name=value#ref"); 228 229 } 230 231 232 235 public void testPositiveNormalize() { 236 237 normalize("http://a/b/c/d;p?q", 238 "http://a/b/c/d;p?q"); 239 240 normalize("http://localhost/index.html", 241 "http://localhost/index.html"); 242 normalize("http://localhost/index.html#ref", 243 "http://localhost/index.html#ref"); 244 normalize("http://localhost/index.html?name=value", 245 "http://localhost/index.html?name=value"); 246 normalize("http://localhost/index.html?name=value#ref", 247 "http://localhost/index.html?name=value#ref"); 248 249 normalize("http://localhost:8080/index.html", 250 "http://localhost:8080/index.html"); 251 normalize("http://localhost:8080/index.html#ref", 252 "http://localhost:8080/index.html#ref"); 253 normalize("http://localhost:8080/index.html?name=value", 254 "http://localhost:8080/index.html?name=value"); 255 normalize("http://localhost:8080/index.html?name=value#ref", 256 "http://localhost:8080/index.html?name=value#ref"); 257 258 normalize("http://localhost/./index.html", 259 "http://localhost/index.html"); 260 normalize("http://localhost/./index.html#ref", 261 "http://localhost/index.html#ref"); 262 normalize("http://localhost/./index.html?name=value", 263 "http://localhost/index.html?name=value"); 264 normalize("http://localhost/./index.html?name=value#ref", 265 "http://localhost/index.html?name=value#ref"); 266 267 normalize("http://localhost:8080/./index.html", 268 "http://localhost:8080/index.html"); 269 normalize("http://localhost:8080/./index.html#ref", 270 "http://localhost:8080/index.html#ref"); 271 normalize("http://localhost:8080/./index.html?name=value", 272 "http://localhost:8080/index.html?name=value"); 273 normalize("http://localhost:8080/./index.html?name=value#ref", 274 "http://localhost:8080/index.html?name=value#ref"); 275 276 normalize("http://localhost/index.html/.", 277 "http://localhost/index.html/"); 278 normalize("http://localhost/index.html/.#ref", 279 "http://localhost/index.html/#ref"); 280 normalize("http://localhost/index.html/.?name=value", 281 "http://localhost/index.html/?name=value"); 282 normalize("http://localhost/index.html/.?name=value#ref", 283 "http://localhost/index.html/?name=value#ref"); 284 285 normalize("http://localhost:8080/index.html/.", 286 "http://localhost:8080/index.html/"); 287 normalize("http://localhost:8080/index.html/.#ref", 288 "http://localhost:8080/index.html/#ref"); 289 normalize("http://localhost:8080/index.html/.?name=value", 290 "http://localhost:8080/index.html/?name=value"); 291 normalize("http://localhost:8080/index.html/.?name=value#ref", 292 "http://localhost:8080/index.html/?name=value#ref"); 293 294 normalize("http://localhost/index.html/./", 295 "http://localhost/index.html/"); 296 normalize("http://localhost/index.html/./#ref", 297 "http://localhost/index.html/#ref"); 298 normalize("http://localhost/index.html/./?name=value", 299 "http://localhost/index.html/?name=value"); 300 normalize("http://localhost/index.html/./?name=value#ref", 301 "http://localhost/index.html/?name=value#ref"); 302 303 normalize("http://localhost:8080/index.html/./", 304 "http://localhost:8080/index.html/"); 305 normalize("http://localhost:8080/index.html/./#ref", 306 "http://localhost:8080/index.html/#ref"); 307 normalize("http://localhost:8080/index.html/./?name=value", 308 "http://localhost:8080/index.html/?name=value"); 309 normalize("http://localhost:8080/index.html/./?name=value#ref", 310 "http://localhost:8080/index.html/?name=value#ref"); 311 312 normalize("http://localhost/foo.html/../index.html", 313 "http://localhost/index.html"); 314 normalize("http://localhost/foo.html/../index.html#ref", 315 "http://localhost/index.html#ref"); 316 normalize("http://localhost/foo.html/../index.html?name=value", 317 "http://localhost/index.html?name=value"); 318 normalize("http://localhost/foo.html/../index.html?name=value#ref", 319 "http://localhost/index.html?name=value#ref"); 320 321 normalize("http://localhost:8080/foo.html/../index.html", 322 "http://localhost:8080/index.html"); 323 normalize("http://localhost:8080/foo.html/../index.html#ref", 324 "http://localhost:8080/index.html#ref"); 325 normalize("http://localhost:8080/foo.html/../index.html?name=value", 326 "http://localhost:8080/index.html?name=value"); 327 normalize("http://localhost:8080/foo.html/../index.html?name=value#ref", 328 "http://localhost:8080/index.html?name=value#ref"); 329 330 normalize("http://localhost/index.html/foo.html/..", 331 "http://localhost/index.html/"); 332 normalize("http://localhost/index.html/foo.html/..#ref", 333 "http://localhost/index.html/#ref"); 334 normalize("http://localhost/index.html/foo.html/..?name=value", 335 "http://localhost/index.html/?name=value"); 336 normalize("http://localhost/index.html/foo.html/..?name=value#ref", 337 "http://localhost/index.html/?name=value#ref"); 338 339 normalize("http://localhost:8080/index.html/foo.html/..", 340 "http://localhost:8080/index.html/"); 341 normalize("http://localhost:8080/index.html/foo.html/..#ref", 342 "http://localhost:8080/index.html/#ref"); 343 normalize("http://localhost:8080/index.html/foo.html/..?name=value", 344 "http://localhost:8080/index.html/?name=value"); 345 normalize("http://localhost:8080/index.html/foo.html/..?name=value#ref", 346 "http://localhost:8080/index.html/?name=value#ref"); 347 348 normalize("http://localhost/index.html/foo.html/../", 349 "http://localhost/index.html/"); 350 normalize("http://localhost/index.html/foo.html/../#ref", 351 "http://localhost/index.html/#ref"); 352 normalize("http://localhost/index.html/foo.html/../?name=value", 353 "http://localhost/index.html/?name=value"); 354 normalize("http://localhost/index.html/foo.html/../?name=value#ref", 355 "http://localhost/index.html/?name=value#ref"); 356 357 normalize("http://localhost:8080/index.html/foo.html/../", 358 "http://localhost:8080/index.html/"); 359 normalize("http://localhost:8080/index.html/foo.html/../#ref", 360 "http://localhost:8080/index.html/#ref"); 361 normalize("http://localhost:8080/index.html/foo.html/../?name=value", 362 "http://localhost:8080/index.html/?name=value"); 363 normalize("http://localhost:8080/index.html/foo.html/../?name=value#ref", 364 "http://localhost:8080/index.html/?name=value#ref"); 365 366 } 367 368 369 372 public void testPositiveRelative() { 373 374 positive("http://a/b/c/d;p?q", "http:h"); 376 positive("http://a/b/c/d;p?q", "g"); 377 positive("http://a/b/c/d;p?q", "./g"); 378 positive("http://a/b/c/d;p?q", "g/"); 379 positive("http://a/b/c/d;p?q", "/g"); 380 positive("http://a/b/c/d;p?q", "?y"); 382 positive("http://a/b/c/d;p?q", "g?y"); 383 positive("http://a/b/c/d;p?q", "g#s"); 385 positive("http://a/b/c/d;p?q", "g?y#s"); 386 positive("http://a/b/c/d;p?q", ";x"); 387 positive("http://a/b/c/d;p?q", "g;x"); 388 positive("http://a/b/c/d;p?q", "g;x?y#s"); 389 positive("http://a/b/c/d;p?q", "."); 390 positive("http://a/b/c/d;p?q", "./"); 391 positive("http://a/b/c/d;p?q", ".."); 392 positive("http://a/b/c/d;p?q", "../"); 393 positive("http://a/b/c/d;p?q", "../g"); 394 positive("http://a/b/c/d;p?q", "../.."); 395 positive("http://a/b/c/d;p?q", "../../"); 396 positive("http://a/b/c/d;p?q", "../../g"); 397 positive("http://a/b/c/d;p?q", "g."); 400 positive("http://a/b/c/d;p?q", ".g"); 401 positive("http://a/b/c/d;p?q", "g.."); 402 positive("http://a/b/c/d;p?q", "..g"); 403 positive("http://a/b/c/d;p?q", "./../g"); 404 positive("http://a/b/c/d;p?q", "./g/."); 405 positive("http://a/b/c/d;p?q", "g/./h"); 406 positive("http://a/b/c/d;p?q", "g/../h"); 407 positive("http://a/b/c/d;p?q", "g;x=1/./y"); 408 positive("http://a/b/c/d;p?q", "g;x=1/../y"); 409 positive("http://a/b/c/d;p?q", "g?y/./x"); 410 positive("http://a/b/c/d;p?q", "g?y/../x"); 411 positive("http://a/b/c/d;p?q", "g#s/./x"); 412 positive("http://a/b/c/d;p?q", "g#s/../x"); 413 positive("http://a/b", "c"); 414 positive("http://a/b/", "c"); 415 416 } 417 418 419 421 422 428 private void negative(String spec) { 429 430 try { 431 java.net.URL url = new java.net.URL (spec); 432 fail(spec + " should have failed on java.net.URL " + 433 "but returned " + url.toExternalForm()); 434 } catch (MalformedURLException e) { 435 ; } 437 438 try { 439 URL url = new URL(spec); 440 fail(spec + " should have failed on tested URL " + 441 "but returned " + url.toExternalForm()); 442 } catch (MalformedURLException e) { 443 ; } 445 446 } 447 448 449 457 private void negative(String abs, String rel) { 458 459 java.net.URL baseNet = null; 460 URL baseUrl = null; 461 462 try { 463 baseNet = new java.net.URL (abs); 464 } catch (MalformedURLException e) { 465 fail(abs + " net URL threw " + e); 466 } 467 468 try { 469 baseUrl = new URL(abs); 470 } catch (MalformedURLException e) { 471 fail(abs + " url URL threw " + e); 472 } 473 474 try { 475 java.net.URL url = new java.net.URL (baseNet, rel); 476 fail(rel + " should have failed on java.net.URL " + 477 "but returned " + url.toExternalForm()); 478 } catch (MalformedURLException e) { 479 ; } 481 482 try { 483 URL url = new URL(baseUrl, rel); 484 fail(rel + " should have failed on tested URL " + 485 "but returned " + url.toExternalForm()); 486 } catch (MalformedURLException e) { 487 ; } 489 490 } 491 492 493 499 private void normalize(String spec) { 500 501 URL url = null; 502 try { 503 url = new URL(spec); 504 } catch (Throwable t) { 505 fail(spec + " should not have thrown " + t); 506 } 507 508 try { 509 url.normalize(); 510 fail(spec + ".normalize() should have thrown MUE"); 511 } catch (MalformedURLException e) { 512 ; } 514 515 } 516 517 518 525 private void normalize(String spec, String norm) { 526 527 try { 528 URL url = new URL(spec); 529 url.normalize(); 530 assertEquals(spec + ".normalize()", norm, url.toExternalForm()); 531 } catch (Throwable t) { 532 fail(spec + ".normalize() threw " + t); 533 } 534 535 } 536 537 538 544 private void positive(String spec) { 545 546 try { 548 URL url = new URL(spec); 549 java.net.URL net = new java.net.URL (spec); 550 assertEquals(spec + " toExternalForm()", 551 net.toExternalForm(), 552 url.toExternalForm()); 553 assertEquals(spec + ".getAuthority()", 554 net.getAuthority(), 555 url.getAuthority()); 556 assertEquals(spec + ".getFile()", 557 net.getFile(), 558 url.getFile()); 559 assertEquals(spec + ".getHost()", 560 net.getHost(), 561 url.getHost()); 562 assertEquals(spec + ".getPath()", 563 net.getPath(), 564 url.getPath()); 565 assertEquals(spec + ".getPort()", 566 net.getPort(), 567 url.getPort()); 568 assertEquals(spec + ".getProtocol()", 569 net.getProtocol(), 570 url.getProtocol()); 571 assertEquals(spec + ".getQuery()", 572 net.getQuery(), 573 url.getQuery()); 574 assertEquals(spec + ".getRef()", 575 net.getRef(), 576 url.getRef()); 577 assertEquals(spec + ".getUserInfo()", 578 net.getUserInfo(), 579 url.getUserInfo()); 580 } catch (Throwable t) { 581 fail(spec + " positive test threw " + t); 582 } 583 584 } 585 586 587 594 private void positive(String abs, String rel) { 595 596 try { 598 URL urlBase = new URL(abs); 599 java.net.URL netBase = new java.net.URL (abs); 600 URL url = new URL(urlBase, rel); 601 java.net.URL net = new java.net.URL (netBase, rel); 602 assertEquals(rel + " toExternalForm()", 603 net.toExternalForm(), 604 url.toExternalForm()); 605 assertEquals(rel + ".getAuthority()", 606 net.getAuthority(), 607 url.getAuthority()); 608 assertEquals(rel + ".getFile()", 609 net.getFile(), 610 url.getFile()); 611 assertEquals(rel + ".getHost()", 612 net.getHost(), 613 url.getHost()); 614 assertEquals(rel + ".getPath()", 615 net.getPath(), 616 url.getPath()); 617 assertEquals(rel + ".getPort()", 618 net.getPort(), 619 url.getPort()); 620 assertEquals(rel + ".getProtocol()", 621 net.getProtocol(), 622 url.getProtocol()); 623 assertEquals(rel + ".getQuery()", 624 net.getQuery(), 625 url.getQuery()); 626 assertEquals(rel + ".getRef()", 627 net.getRef(), 628 url.getRef()); 629 assertEquals(rel + ".getUserInfo()", 630 net.getUserInfo(), 631 url.getUserInfo()); 632 } catch (Throwable t) { 633 fail(rel + " positive test threw " + t); 634 } 635 636 } 637 638 639 } 640 | Popular Tags |