IRI.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /**
  3. * IRI test cases
  4. *
  5. * Copyright (c) 2008-2010 Geoffrey Sneddon.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. *
  18. * * Neither the name of the SimplePie Team nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * @package IRI
  35. * @author Geoffrey Sneddon
  36. * @copyright 2008-2010 Geoffrey Sneddon
  37. * @license http://www.opensource.org/licenses/bsd-license.php
  38. * @link http://hg.gsnedders.com/iri/
  39. *
  40. */
  41. class RequestsTest_IRI extends PHPUnit_Framework_TestCase
  42. {
  43. public static function rfc3986_tests()
  44. {
  45. return array(
  46. // Normal
  47. array('g:h', 'g:h'),
  48. array('g', 'http://a/b/c/g'),
  49. array('./g', 'http://a/b/c/g'),
  50. array('g/', 'http://a/b/c/g/'),
  51. array('/g', 'http://a/g'),
  52. array('//g', 'http://g/'),
  53. array('?y', 'http://a/b/c/d;p?y'),
  54. array('g?y', 'http://a/b/c/g?y'),
  55. array('#s', 'http://a/b/c/d;p?q#s'),
  56. array('g#s', 'http://a/b/c/g#s'),
  57. array('g?y#s', 'http://a/b/c/g?y#s'),
  58. array(';x', 'http://a/b/c/;x'),
  59. array('g;x', 'http://a/b/c/g;x'),
  60. array('g;x?y#s', 'http://a/b/c/g;x?y#s'),
  61. array('', 'http://a/b/c/d;p?q'),
  62. array('.', 'http://a/b/c/'),
  63. array('./', 'http://a/b/c/'),
  64. array('..', 'http://a/b/'),
  65. array('../', 'http://a/b/'),
  66. array('../g', 'http://a/b/g'),
  67. array('../..', 'http://a/'),
  68. array('../../', 'http://a/'),
  69. array('../../g', 'http://a/g'),
  70. // Abnormal
  71. array('../../../g', 'http://a/g'),
  72. array('../../../../g', 'http://a/g'),
  73. array('/./g', 'http://a/g'),
  74. array('/../g', 'http://a/g'),
  75. array('g.', 'http://a/b/c/g.'),
  76. array('.g', 'http://a/b/c/.g'),
  77. array('g..', 'http://a/b/c/g..'),
  78. array('..g', 'http://a/b/c/..g'),
  79. array('./../g', 'http://a/b/g'),
  80. array('./g/.', 'http://a/b/c/g/'),
  81. array('g/./h', 'http://a/b/c/g/h'),
  82. array('g/../h', 'http://a/b/c/h'),
  83. array('g;x=1/./y', 'http://a/b/c/g;x=1/y'),
  84. array('g;x=1/../y', 'http://a/b/c/y'),
  85. array('g?y/./x', 'http://a/b/c/g?y/./x'),
  86. array('g?y/../x', 'http://a/b/c/g?y/../x'),
  87. array('g#s/./x', 'http://a/b/c/g#s/./x'),
  88. array('g#s/../x', 'http://a/b/c/g#s/../x'),
  89. array('http:g', 'http:g'),
  90. );
  91. }
  92. /**
  93. * @dataProvider rfc3986_tests
  94. */
  95. public function testStringRFC3986($relative, $expected)
  96. {
  97. $base = new Requests_IRI('http://a/b/c/d;p?q');
  98. $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)->iri);
  99. $this->assertEquals($expected, (string) Requests_IRI::absolutize($base, $relative));
  100. }
  101. /**
  102. * @dataProvider rfc3986_tests
  103. */
  104. public function testBothStringRFC3986($relative, $expected)
  105. {
  106. $base = 'http://a/b/c/d;p?q';
  107. $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)->iri);
  108. $this->assertEquals($expected, (string) Requests_IRI::absolutize($base, $relative));
  109. }
  110. /**
  111. * @dataProvider rfc3986_tests
  112. */
  113. public function testObjectRFC3986($relative, $expected)
  114. {
  115. $base = new Requests_IRI('http://a/b/c/d;p?q');
  116. $expected = new Requests_IRI($expected);
  117. $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative));
  118. }
  119. public static function sp_tests()
  120. {
  121. return array(
  122. array('http://a/b/c/d', 'f%0o', 'http://a/b/c/f%250o'),
  123. array('http://a/b/', 'c', 'http://a/b/c'),
  124. array('http://a/', 'b', 'http://a/b'),
  125. array('http://a/', '/b', 'http://a/b'),
  126. array('http://a/b', 'c', 'http://a/c'),
  127. array('http://a/b/', "c\x0Ad", 'http://a/b/c%0Ad'),
  128. array('http://a/b/', "c\x0A\x0B", 'http://a/b/c%0A%0B'),
  129. array('http://a/b/c', '//0', 'http://0/'),
  130. array('http://a/b/c', '0', 'http://a/b/0'),
  131. array('http://a/b/c', '?0', 'http://a/b/c?0'),
  132. array('http://a/b/c', '#0', 'http://a/b/c#0'),
  133. array('http://0/b/c', 'd', 'http://0/b/d'),
  134. array('http://a/b/c?0', 'd', 'http://a/b/d'),
  135. array('http://a/b/c#0', 'd', 'http://a/b/d'),
  136. array('http://example.com', '//example.net', 'http://example.net/'),
  137. array('http:g', 'a', 'http:a'),
  138. );
  139. }
  140. /**
  141. * @dataProvider sp_tests
  142. */
  143. public function testStringSP($base, $relative, $expected)
  144. {
  145. $base = new Requests_IRI($base);
  146. $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)->iri);
  147. $this->assertEquals($expected, (string) Requests_IRI::absolutize($base, $relative));
  148. }
  149. /**
  150. * @dataProvider sp_tests
  151. */
  152. public function testObjectSP($base, $relative, $expected)
  153. {
  154. $base = new Requests_IRI($base);
  155. $expected = new Requests_IRI($expected);
  156. $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative));
  157. }
  158. public static function absolutize_tests()
  159. {
  160. return array(
  161. array('http://example.com/', 'foo/111:bar', 'http://example.com/foo/111:bar'),
  162. array('http://example.com/#foo', '', 'http://example.com/'),
  163. );
  164. }
  165. /**
  166. * @dataProvider absolutize_tests
  167. */
  168. public function testAbsolutizeString($base, $relative, $expected)
  169. {
  170. $base = new Requests_IRI($base);
  171. $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative)->iri);
  172. }
  173. /**
  174. * @dataProvider absolutize_tests
  175. */
  176. public function testAbsolutizeObject($base, $relative, $expected)
  177. {
  178. $base = new Requests_IRI($base);
  179. $expected = new Requests_IRI($expected);
  180. $this->assertEquals($expected, Requests_IRI::absolutize($base, $relative));
  181. }
  182. public static function normalization_tests()
  183. {
  184. return array(
  185. array('example://a/b/c/%7Bfoo%7D', 'example://a/b/c/%7Bfoo%7D'),
  186. array('eXAMPLE://a/./b/../b/%63/%7bfoo%7d', 'example://a/b/c/%7Bfoo%7D'),
  187. array('example://%61/', 'example://a/'),
  188. array('example://%41/', 'example://a/'),
  189. array('example://A/', 'example://a/'),
  190. array('example://a/', 'example://a/'),
  191. array('example://%25A/', 'example://%25a/'),
  192. array('HTTP://EXAMPLE.com/', 'http://example.com/'),
  193. array('http://example.com/', 'http://example.com/'),
  194. array('http://example.com:', 'http://example.com/'),
  195. array('http://example.com:80', 'http://example.com/'),
  196. array('http://@example.com', 'http://@example.com/'),
  197. array('http://', 'http:///'),
  198. array('http://example.com?', 'http://example.com/?'),
  199. array('http://example.com#', 'http://example.com/#'),
  200. array('https://example.com/', 'https://example.com/'),
  201. array('https://example.com:', 'https://example.com/'),
  202. array('https://@example.com', 'https://@example.com/'),
  203. array('https://example.com?', 'https://example.com/?'),
  204. array('https://example.com#', 'https://example.com/#'),
  205. array('file://localhost/foobar', 'file:/foobar'),
  206. array('http://[0:0:0:0:0:0:0:1]', 'http://[::1]/'),
  207. array('http://[2001:db8:85a3:0000:0000:8a2e:370:7334]', 'http://[2001:db8:85a3::8a2e:370:7334]/'),
  208. array('http://[0:0:0:0:0:ffff:c0a8:a01]', 'http://[::ffff:c0a8:a01]/'),
  209. array('http://[ffff:0:0:0:0:0:0:0]', 'http://[ffff::]/'),
  210. array('http://[::ffff:192.0.2.128]', 'http://[::ffff:192.0.2.128]/'),
  211. array('http://[invalid]', 'http:'),
  212. array('http://[0:0:0:0:0:0:0:1]:', 'http://[::1]/'),
  213. array('http://[0:0:0:0:0:0:0:1]:80', 'http://[::1]/'),
  214. array('http://[0:0:0:0:0:0:0:1]:1234', 'http://[::1]:1234/'),
  215. // Punycode decoding helps with normalisation of IRIs, but is not
  216. // needed for URIs, so we don't really care about it for Requests
  217. //array('http://xn--tdali-d8a8w.lv', 'http://tūdaliņ.lv/'),
  218. //array('http://t%C5%ABdali%C5%86.lv', 'http://tūdaliņ.lv/'),
  219. array('http://Aa@example.com', 'http://Aa@example.com/'),
  220. array('http://example.com?Aa', 'http://example.com/?Aa'),
  221. array('http://example.com/Aa', 'http://example.com/Aa'),
  222. array('http://example.com#Aa', 'http://example.com/#Aa'),
  223. array('http://[0:0:0:0:0:0:0:0]', 'http://[::]/'),
  224. array('http:.', 'http:'),
  225. array('http:..', 'http:'),
  226. array('http:./', 'http:'),
  227. array('http:../', 'http:'),
  228. array('http://example.com/%3A', 'http://example.com/%3A'),
  229. array('http://example.com/:', 'http://example.com/:'),
  230. array('http://example.com/%C2', 'http://example.com/%C2'),
  231. array('http://example.com/%C2a', 'http://example.com/%C2a'),
  232. array('http://example.com/%C2%00', 'http://example.com/%C2%00'),
  233. array('http://example.com/%C3%A9', 'http://example.com/é'),
  234. array('http://example.com/%C3%A9%00', 'http://example.com/é%00'),
  235. array('http://example.com/%C3%A9cole', 'http://example.com/école'),
  236. array('http://example.com/%FF', 'http://example.com/%FF'),
  237. array("http://example.com/\xF3\xB0\x80\x80", 'http://example.com/%F3%B0%80%80'),
  238. array("http://example.com/\xF3\xB0\x80\x80%00", 'http://example.com/%F3%B0%80%80%00'),
  239. array("http://example.com/\xF3\xB0\x80\x80a", 'http://example.com/%F3%B0%80%80a'),
  240. array("http://example.com?\xF3\xB0\x80\x80", "http://example.com/?\xF3\xB0\x80\x80"),
  241. array("http://example.com?\xF3\xB0\x80\x80%00", "http://example.com/?\xF3\xB0\x80\x80%00"),
  242. array("http://example.com?\xF3\xB0\x80\x80a", "http://example.com/?\xF3\xB0\x80\x80a"),
  243. array("http://example.com/\xEE\x80\x80", 'http://example.com/%EE%80%80'),
  244. array("http://example.com/\xEE\x80\x80%00", 'http://example.com/%EE%80%80%00'),
  245. array("http://example.com/\xEE\x80\x80a", 'http://example.com/%EE%80%80a'),
  246. array("http://example.com?\xEE\x80\x80", "http://example.com/?\xEE\x80\x80"),
  247. array("http://example.com?\xEE\x80\x80%00", "http://example.com/?\xEE\x80\x80%00"),
  248. array("http://example.com?\xEE\x80\x80a", "http://example.com/?\xEE\x80\x80a"),
  249. array("http://example.com/\xC2", 'http://example.com/%C2'),
  250. array("http://example.com/\xC2a", 'http://example.com/%C2a'),
  251. array("http://example.com/\xC2\x00", 'http://example.com/%C2%00'),
  252. array("http://example.com/\xC3\xA9", 'http://example.com/é'),
  253. array("http://example.com/\xC3\xA9\x00", 'http://example.com/é%00'),
  254. array("http://example.com/\xC3\xA9cole", 'http://example.com/école'),
  255. array("http://example.com/\xFF", 'http://example.com/%FF'),
  256. array("http://example.com/\xFF%00", 'http://example.com/%FF%00'),
  257. array("http://example.com/\xFFa", 'http://example.com/%FFa'),
  258. array('http://example.com/%61', 'http://example.com/a'),
  259. array('http://example.com?%26', 'http://example.com/?%26'),
  260. array('http://example.com?%61', 'http://example.com/?a'),
  261. array('///', '///'),
  262. );
  263. }
  264. /**
  265. * @dataProvider normalization_tests
  266. */
  267. public function testStringNormalization($input, $output)
  268. {
  269. $input = new Requests_IRI($input);
  270. $this->assertEquals($output, $input->iri);
  271. $this->assertEquals($output, (string) $input);
  272. }
  273. /**
  274. * @dataProvider normalization_tests
  275. */
  276. public function testObjectNormalization($input, $output)
  277. {
  278. $input = new Requests_IRI($input);
  279. $output = new Requests_IRI($output);
  280. $this->assertEquals($output, $input);
  281. }
  282. public static function equivalence_tests()
  283. {
  284. return array(
  285. array('http://É.com', 'http://%C3%89.com'),
  286. );
  287. }
  288. /**
  289. * @dataProvider equivalence_tests
  290. */
  291. public function testObjectEquivalence($input, $output)
  292. {
  293. $input = new Requests_IRI($input);
  294. $output = new Requests_IRI($output);
  295. $this->assertEquals($output, $input);
  296. }
  297. public static function not_equivalence_tests()
  298. {
  299. return array(
  300. array('http://example.com/foo/bar', 'http://example.com/foo%2Fbar'),
  301. );
  302. }
  303. /**
  304. * @dataProvider not_equivalence_tests
  305. */
  306. public function testObjectNotEquivalence($input, $output)
  307. {
  308. $input = new Requests_IRI($input);
  309. $output = new Requests_IRI($output);
  310. $this->assertNotEquals($output, $input);
  311. }
  312. public function testInvalidAbsolutizeBase()
  313. {
  314. $this->assertFalse(Requests_IRI::absolutize('://not a URL', '../'));
  315. }
  316. public function testFullGamut()
  317. {
  318. $iri = new Requests_IRI();
  319. $iri->scheme = 'http';
  320. $iri->userinfo = 'user:password';
  321. $iri->host = 'example.com';
  322. $iri->path = '/test/';
  323. $iri->fragment = 'test';
  324. $this->assertEquals('http', $iri->scheme);
  325. $this->assertEquals('user:password', $iri->userinfo);
  326. $this->assertEquals('example.com', $iri->host);
  327. $this->assertEquals(80, $iri->port);
  328. $this->assertEquals('/test/', $iri->path);
  329. $this->assertEquals('test', $iri->fragment);
  330. }
  331. public function testReadAliased()
  332. {
  333. $iri = new Requests_IRI();
  334. $iri->scheme = 'http';
  335. $iri->userinfo = 'user:password';
  336. $iri->host = 'example.com';
  337. $iri->path = '/test/';
  338. $iri->fragment = 'test';
  339. $this->assertEquals('http', $iri->ischeme);
  340. $this->assertEquals('user:password', $iri->iuserinfo);
  341. $this->assertEquals('example.com', $iri->ihost);
  342. $this->assertEquals(80, $iri->iport);
  343. $this->assertEquals('/test/', $iri->ipath);
  344. $this->assertEquals('test', $iri->ifragment);
  345. }
  346. public function testWriteAliased()
  347. {
  348. $iri = new Requests_IRI();
  349. $iri->scheme = 'http';
  350. $iri->iuserinfo = 'user:password';
  351. $iri->ihost = 'example.com';
  352. $iri->ipath = '/test/';
  353. $iri->ifragment = 'test';
  354. $this->assertEquals('http', $iri->scheme);
  355. $this->assertEquals('user:password', $iri->userinfo);
  356. $this->assertEquals('example.com', $iri->host);
  357. $this->assertEquals(80, $iri->port);
  358. $this->assertEquals('/test/', $iri->path);
  359. $this->assertEquals('test', $iri->fragment);
  360. }
  361. /**
  362. * @expectedException PHPUnit_Framework_Error_Notice
  363. */
  364. public function testNonexistantProperty()
  365. {
  366. $iri = new Requests_IRI();
  367. $this->assertFalse(isset($iri->nonexistant_prop));
  368. $should_fail = $iri->nonexistant_prop;
  369. }
  370. public function testBlankHost()
  371. {
  372. $iri = new Requests_IRI('http://example.com/a/?b=c#d');
  373. $iri->host = null;
  374. $this->assertEquals(null, $iri->host);
  375. $this->assertEquals('http:/a/?b=c#d', (string) $iri);
  376. }
  377. public function testBadPort()
  378. {
  379. $iri = new Requests_IRI();
  380. $iri->port = 'example';
  381. $this->assertEquals(null, $iri->port);
  382. }
  383. }