URIParserTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. public function testPercentNormalization()
  14. {
  15. $this->assertParsing(
  16. '%G',
  17. null, null, null, null, '%25G', null, null
  18. );
  19. }
  20. public function testRegular()
  21. {
  22. $this->assertParsing(
  23. 'http://www.example.com/webhp?q=foo#result2',
  24. 'http', null, 'www.example.com', null, '/webhp', 'q=foo', 'result2'
  25. );
  26. }
  27. public function testPortAndUsername()
  28. {
  29. $this->assertParsing(
  30. 'http://user@authority.part:80/now/the/path?query#fragment',
  31. 'http', 'user', 'authority.part', 80, '/now/the/path', 'query', 'fragment'
  32. );
  33. }
  34. public function testPercentEncoding()
  35. {
  36. $this->assertParsing(
  37. 'http://en.wikipedia.org/wiki/Clich%C3%A9',
  38. 'http', null, 'en.wikipedia.org', null, '/wiki/Clich%C3%A9', null, null
  39. );
  40. }
  41. public function testEmptyQuery()
  42. {
  43. $this->assertParsing(
  44. 'http://www.example.com/?#',
  45. 'http', null, 'www.example.com', null, '/', '', null
  46. );
  47. }
  48. public function testEmptyPath()
  49. {
  50. $this->assertParsing(
  51. 'http://www.example.com',
  52. 'http', null, 'www.example.com', null, '', null, null
  53. );
  54. }
  55. public function testOpaqueURI()
  56. {
  57. $this->assertParsing(
  58. 'mailto:bob@example.com',
  59. 'mailto', null, null, null, 'bob@example.com', null, null
  60. );
  61. }
  62. public function testTelURI()
  63. {
  64. $this->assertParsing(
  65. 'tel:+1 (555) 555-5555',
  66. 'tel', null, null, null, '+1 (555) 555-5555', null, null
  67. );
  68. $this->assertParsing(
  69. 'tel:+1%20(555)%20555-5555',
  70. 'tel', null, null, null, '+1%20(555)%20555-5555', null, null
  71. );
  72. }
  73. public function testIPv4Address()
  74. {
  75. $this->assertParsing(
  76. 'http://192.0.34.166/',
  77. 'http', null, '192.0.34.166', null, '/', null, null
  78. );
  79. }
  80. public function testFakeIPv4Address()
  81. {
  82. $this->assertParsing(
  83. 'http://333.123.32.123/',
  84. 'http', null, '333.123.32.123', null, '/', null, null
  85. );
  86. }
  87. public function testIPv6Address()
  88. {
  89. $this->assertParsing(
  90. 'http://[2001:db8::7]/c=GB?objectClass?one',
  91. 'http', null, '[2001:db8::7]', null, '/c=GB', 'objectClass?one', null
  92. );
  93. }
  94. public function testInternationalizedDomainName()
  95. {
  96. $this->assertParsing(
  97. "http://t\xC5\xABdali\xC5\x86.lv",
  98. 'http', null, "t\xC5\xABdali\xC5\x86.lv", null, '', null, null
  99. );
  100. }
  101. public function testInvalidPort()
  102. {
  103. $this->assertParsing(
  104. 'http://example.com:foobar',
  105. 'http', null, 'example.com', null, '', null, null
  106. );
  107. }
  108. public function testPathAbsolute()
  109. {
  110. $this->assertParsing(
  111. 'http:/this/is/path',
  112. 'http', null, null, null, '/this/is/path', null, null
  113. );
  114. }
  115. public function testPathRootless()
  116. {
  117. // this should not be used but is allowed
  118. $this->assertParsing(
  119. 'http:this/is/path',
  120. 'http', null, null, null, 'this/is/path', null, null
  121. );
  122. }
  123. public function testPathEmpty()
  124. {
  125. $this->assertParsing(
  126. 'http:',
  127. 'http', null, null, null, '', null, null
  128. );
  129. }
  130. public function testRelativeURI()
  131. {
  132. $this->assertParsing(
  133. '/a/b',
  134. null, null, null, null, '/a/b', null, null
  135. );
  136. }
  137. public function testMalformedTag()
  138. {
  139. $this->assertParsing(
  140. 'http://www.example.com/>',
  141. 'http', null, 'www.example.com', null, '/', null, null
  142. );
  143. }
  144. public function testEmpty()
  145. {
  146. $this->assertParsing(
  147. '',
  148. null, null, null, null, '', null, null
  149. );
  150. }
  151. public function testEmbeddedColon()
  152. {
  153. $this->assertParsing(
  154. '{:test:}',
  155. null, null, null, null, '{:test:}', null, null
  156. );
  157. }
  158. }
  159. // vim: et sw=4 sts=4