URIParserTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. class HTMLPurifier_URIParserTest extends HTMLPurifier_Harness
  3. {
  4. protected function assertParsing(
  5. $uri, $scheme, $userinfo, $host, $port, $path, $query, $fragment, $config = null, $context = null
  6. ) {
  7. $this->prepareCommon($config, $context);
  8. $parser = new HTMLPurifier_URIParser();
  9. $result = $parser->parse($uri, $config, $context);
  10. $expect = new HTMLPurifier_URI($scheme, $userinfo, $host, $port, $path, $query, $fragment);
  11. $this->assertEqual($result, $expect);
  12. }
  13. function testPercentNormalization() {
  14. $this->assertParsing(
  15. '%G',
  16. null, null, null, null, '%25G', null, null
  17. );
  18. }
  19. function testRegular() {
  20. $this->assertParsing(
  21. 'http://www.example.com/webhp?q=foo#result2',
  22. 'http', null, 'www.example.com', null, '/webhp', 'q=foo', 'result2'
  23. );
  24. }
  25. function testPortAndUsername() {
  26. $this->assertParsing(
  27. 'http://user@authority.part:80/now/the/path?query#fragment',
  28. 'http', 'user', 'authority.part', 80, '/now/the/path', 'query', 'fragment'
  29. );
  30. }
  31. function testPercentEncoding() {
  32. $this->assertParsing(
  33. 'http://en.wikipedia.org/wiki/Clich%C3%A9',
  34. 'http', null, 'en.wikipedia.org', null, '/wiki/Clich%C3%A9', null, null
  35. );
  36. }
  37. function testEmptyQuery() {
  38. $this->assertParsing(
  39. 'http://www.example.com/?#',
  40. 'http', null, 'www.example.com', null, '/', '', null
  41. );
  42. }
  43. function testEmptyPath() {
  44. $this->assertParsing(
  45. 'http://www.example.com',
  46. 'http', null, 'www.example.com', null, '', null, null
  47. );
  48. }
  49. function testOpaqueURI() {
  50. $this->assertParsing(
  51. 'mailto:bob@example.com',
  52. 'mailto', null, null, null, 'bob@example.com', null, null
  53. );
  54. }
  55. function testIPv4Address() {
  56. $this->assertParsing(
  57. 'http://192.0.34.166/',
  58. 'http', null, '192.0.34.166', null, '/', null, null
  59. );
  60. }
  61. function testFakeIPv4Address() {
  62. $this->assertParsing(
  63. 'http://333.123.32.123/',
  64. 'http', null, '333.123.32.123', null, '/', null, null
  65. );
  66. }
  67. function testIPv6Address() {
  68. $this->assertParsing(
  69. 'http://[2001:db8::7]/c=GB?objectClass?one',
  70. 'http', null, '[2001:db8::7]', null, '/c=GB', 'objectClass?one', null
  71. );
  72. }
  73. function testInternationalizedDomainName() {
  74. $this->assertParsing(
  75. "http://t\xC5\xABdali\xC5\x86.lv",
  76. 'http', null, "t\xC5\xABdali\xC5\x86.lv", null, '', null, null
  77. );
  78. }
  79. function testInvalidPort() {
  80. $this->assertParsing(
  81. 'http://example.com:foobar',
  82. 'http', null, 'example.com', null, '', null, null
  83. );
  84. }
  85. function testPathAbsolute() {
  86. $this->assertParsing(
  87. 'http:/this/is/path',
  88. 'http', null, null, null, '/this/is/path', null, null
  89. );
  90. }
  91. function testPathRootless() {
  92. // this should not be used but is allowed
  93. $this->assertParsing(
  94. 'http:this/is/path',
  95. 'http', null, null, null, 'this/is/path', null, null
  96. );
  97. }
  98. function testPathEmpty() {
  99. $this->assertParsing(
  100. 'http:',
  101. 'http', null, null, null, '', null, null
  102. );
  103. }
  104. function testRelativeURI() {
  105. $this->assertParsing(
  106. '/a/b',
  107. null, null, null, null, '/a/b', null, null
  108. );
  109. }
  110. function testMalformedTag() {
  111. $this->assertParsing(
  112. 'http://www.example.com/>',
  113. 'http', null, 'www.example.com', null, '/', null, null
  114. );
  115. }
  116. function testEmpty() {
  117. $this->assertParsing(
  118. '',
  119. null, null, null, null, '', null, null
  120. );
  121. }
  122. }
  123. // vim: et sw=4 sts=4