HttpTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Uri
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Uri
  24. */
  25. require_once 'Zend/Uri.php';
  26. /**
  27. * @see Zend_Uri_Http
  28. */
  29. require_once 'Zend/Uri/Http.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Uri
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Uri
  37. */
  38. class Zend_Uri_HttpTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function setup()
  41. {
  42. Zend_Uri::setConfig(array('allow_unwise' => false));
  43. }
  44. /**
  45. * Tests for proper URI decomposition
  46. */
  47. public function testSimple()
  48. {
  49. $this->_testValidUri('http://www.zend.com');
  50. }
  51. /**
  52. * Test that fromString() works proprerly for simple valid URLs
  53. *
  54. */
  55. public function testSimpleFromString()
  56. {
  57. $tests = array(
  58. 'http://www.zend.com',
  59. 'https://www.zend.com',
  60. 'http://www.zend.com/path',
  61. 'http://www.zend.com/path?query=value'
  62. );
  63. foreach ($tests as $uri) {
  64. $obj = Zend_Uri_Http::fromString($uri);
  65. $this->assertEquals($uri, $obj->getUri(),
  66. "getUri() returned value that differs from input for $uri");
  67. }
  68. }
  69. /**
  70. * Make sure an exception is thrown when trying to use fromString() with a
  71. * non-HTTP scheme
  72. *
  73. * @group ZF-4395
  74. *
  75. * @expectedException Zend_Uri_Exception
  76. */
  77. public function testFromStringInvalidScheme()
  78. {
  79. Zend_Uri_Http::fromString('ftp://example.com/file');
  80. }
  81. /**
  82. * Make sure an exception is thrown when trying to use fromString() with a variable that is not
  83. * a string.
  84. *
  85. */
  86. public function testFromStringWithInvalidVariableType()
  87. {
  88. $this->setExpectedException('Zend_Uri_Exception');
  89. Zend_Uri_Http::fromString(0);
  90. }
  91. public function testAllParts()
  92. {
  93. $this->_testValidUri('http://andi:password@www.zend.com:8080/path/to/file?a=1&b=2#top');
  94. }
  95. public function testUsernamePortPathQueryFragment()
  96. {
  97. $this->_testValidUri('http://andi@www.zend.com:8080/path/to/file?a=1&b=2#top');
  98. }
  99. public function testPortPathQueryFragment()
  100. {
  101. $this->_testValidUri('http://www.zend.com:8080/path/to/file?a=1&b=2#top');
  102. }
  103. public function testPathQueryFragment()
  104. {
  105. $this->_testValidUri('http://www.zend.com/path/to/file?a=1&b=2#top');
  106. }
  107. public function testQueryFragment()
  108. {
  109. $this->_testValidUri('http://www.zend.com/?a=1&b=2#top');
  110. }
  111. public function testFragment()
  112. {
  113. $this->_testValidUri('http://www.zend.com/#top');
  114. }
  115. public function testUsernamePassword()
  116. {
  117. $this->_testValidUri('http://andi:password@www.zend.com');
  118. }
  119. public function testUsernamePasswordColon()
  120. {
  121. $this->_testValidUri('http://an:di:password@www.zend.com');
  122. }
  123. public function testUsernamePasswordValidCharacters()
  124. {
  125. $this->_testValidUri('http://a_.!~*\'(-)n0123Di%25%26:pass;:&=+$,word@www.zend.com');
  126. }
  127. public function testUsernameInvalidCharacter()
  128. {
  129. $this->_testInvalidUri('http://an`di:password@www.zend.com');
  130. }
  131. public function testNoUsernamePassword()
  132. {
  133. $this->_testInvalidUri('http://:password@www.zend.com');
  134. }
  135. public function testPasswordInvalidCharacter()
  136. {
  137. $this->_testInvalidUri('http://andi:pass%word@www.zend.com');
  138. }
  139. public function testHostAsIP()
  140. {
  141. $this->_testValidUri('http://127.0.0.1');
  142. }
  143. public function testLocalhost()
  144. {
  145. $this->_testValidUri('http://localhost');
  146. }
  147. public function testLocalhostLocaldomain()
  148. {
  149. $this->_testValidUri('http://localhost.localdomain');
  150. }
  151. public function testSquareBrackets()
  152. {
  153. $this->_testValidUri('https://example.com/foo/?var[]=1&var[]=2&some[thing]=3');
  154. }
  155. /**
  156. * Ensures that successive slashes are considered valid
  157. *
  158. * @return void
  159. */
  160. public function testSuccessiveSlashes()
  161. {
  162. $this->_testValidUri('http://example.com//');
  163. $this->_testValidUri('http://example.com///');
  164. $this->_testValidUri('http://example.com/foo//');
  165. $this->_testValidUri('http://example.com/foo///');
  166. $this->_testValidUri('http://example.com/foo//bar');
  167. $this->_testValidUri('http://example.com/foo///bar');
  168. $this->_testValidUri('http://example.com/foo//bar/baz//fob/');
  169. }
  170. /**
  171. * Test that setQuery() can handle unencoded query parameters (as other
  172. * browsers do), ZF-1934
  173. *
  174. * @group ZF-1934
  175. * @return void
  176. */
  177. public function testUnencodedQueryParameters()
  178. {
  179. $uri = Zend_Uri::factory('http://foo.com/bar');
  180. // First, make sure no exceptions are thrown
  181. try {
  182. $uri->setQuery('id=123&url=http://example.com/?bar=foo baz');
  183. } catch (Exception $e) {
  184. $this->fail('setQuery() was expected to handle unencoded parameters, but failed');
  185. }
  186. // Second, make sure the query string was properly encoded
  187. $parts = parse_url($uri->getUri());
  188. $this->assertEquals('id=123&url=http%3A%2F%2Fexample.com%2F%3Fbar%3Dfoo+baz', $parts['query']);
  189. }
  190. /**
  191. * Test that unwise characters in the query string are not valid
  192. *
  193. */
  194. public function testExceptionUnwiseQueryString()
  195. {
  196. $unwise = array(
  197. 'http://example.com/?q={',
  198. 'http://example.com/?q=}',
  199. 'http://example.com/?q=\\',
  200. 'http://example.com/?q=^',
  201. 'http://example.com/?q=`',
  202. );
  203. foreach ($unwise as $uri) {
  204. $this->assertFalse(Zend_Uri::check($uri), "failed for URI $uri");
  205. }
  206. }
  207. /**
  208. * Test that after setting 'allow_unwise' to true unwise characters are
  209. * accepted
  210. *
  211. */
  212. public function testAllowUnwiseQueryString()
  213. {
  214. $unwise = array(
  215. 'http://example.com/?q={',
  216. 'http://example.com/?q=}',
  217. 'http://example.com/?q=\\',
  218. 'http://example.com/?q=^',
  219. 'http://example.com/?q=`',
  220. );
  221. Zend_Uri::setConfig(array('allow_unwise' => true));
  222. foreach ($unwise as $uri) {
  223. $this->assertTrue(Zend_Uri::check($uri), "failed for URI $uri");
  224. }
  225. Zend_Uri::setConfig(array('allow_unwise' => false));
  226. }
  227. /**
  228. * Test that an extremely long URI does not break things up
  229. *
  230. * @group ZF-3712
  231. * @group ZF-7840
  232. */
  233. public function testVeryLongUriZF3712()
  234. {
  235. if(!defined('TESTS_ZEND_URI_CRASH_TEST_ENABLED') || constant('TESTS_ZEND_URI_CRASH_TEST_ENABLED') == false) {
  236. $this->markTestSkipped('The constant TESTS_ZEND_URI_CRASH_TEST_ENABLED has to be defined and true to allow the test to work.');
  237. }
  238. $uri = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  239. '_files' . DIRECTORY_SEPARATOR . 'testVeryLongUriZF3712.txt');
  240. $this->_testValidUri($uri);
  241. }
  242. /**
  243. * Test a known valid URI
  244. *
  245. * @param string $uri
  246. */
  247. protected function _testValidUri($uri)
  248. {
  249. $obj = Zend_Uri::factory($uri);
  250. $this->assertEquals($uri, $obj->getUri(), 'getUri() returned value that differs from input');
  251. }
  252. /**
  253. * Test a known invalid URI
  254. *
  255. * @param string $uri
  256. */
  257. protected function _testInvalidUri($uri)
  258. {
  259. try {
  260. $obj = Zend_Uri::factory($uri);
  261. $this->fail('Zend_Uri_Exception was expected but not thrown');
  262. } catch (Zend_Uri_Exception $e) {
  263. }
  264. }
  265. public function testSetGetUsername()
  266. {
  267. $uri = Zend_Uri::factory('http://example.com');
  268. $username = 'alice';
  269. $this->assertFalse($uri->getUsername());
  270. $uri->setUsername($username);
  271. $this->assertSame($username, $uri->getUsername());
  272. }
  273. public function testSetGetPassword()
  274. {
  275. $uri = Zend_Uri::factory('http://example.com');
  276. $username = 'alice';
  277. $password = 'secret';
  278. $this->assertFalse($uri->getPassword());
  279. $uri->setUsername($username);
  280. $uri->setPassword($password);
  281. $this->assertSame($password, $uri->getPassword());
  282. }
  283. public function testUriWithAllParts()
  284. {
  285. $uri = Zend_Uri::factory('http://alice:secret@example.com:8080/path/script.php?foo=bar&bar=foo#123');
  286. $this->assertSame('http', $uri->getScheme());
  287. $this->assertSame('alice', $uri->getUsername());
  288. $this->assertSame('secret', $uri->getPassword());
  289. $this->assertSame('example.com', $uri->getHost());
  290. $this->assertEquals(8080, $uri->getPort());
  291. $this->assertSame('/path/script.php', $uri->getPath());
  292. $this->assertSame('foo=bar&bar=foo', $uri->getQuery());
  293. $this->assertSame('123', $uri->getFragment());
  294. }
  295. public function testBuildCompleteUriFromScratch()
  296. {
  297. $uri = Zend_Uri::factory('http');
  298. $uri->setUsername('alice');
  299. $uri->setPassword('secret');
  300. $uri->setHost('example.com');
  301. $uri->setPort(8080);
  302. $uri->setPath('/path/script.php');
  303. $uri->setQuery('foo=bar&bar=foo');
  304. $uri->setFragment('123');
  305. $this->assertSame('http://alice:secret@example.com:8080/path/script.php?foo=bar&bar=foo#123', $uri->getUri());
  306. }
  307. public function testSetInvalidUsername()
  308. {
  309. $uri = Zend_Uri::factory('http://example.com');
  310. $this->setExpectedException('Zend_Uri_Exception');
  311. $uri->setUsername('alice?');
  312. }
  313. public function testSetInvalidPassword()
  314. {
  315. $uri = Zend_Uri::factory('http://example.com');
  316. $this->setExpectedException('Zend_Uri_Exception');
  317. $uri->setUsername('alice');
  318. $uri->setPassword('secret?');
  319. }
  320. public function testSetEmptyHost()
  321. {
  322. $uri = Zend_Uri::factory('http://example.com');
  323. $host = '';
  324. $this->setExpectedException('Zend_Uri_Exception');
  325. $uri->setHost($host);
  326. }
  327. public function testSetInvalidHost()
  328. {
  329. $uri = Zend_Uri::factory('http://example.com');
  330. $host = 'example,com';
  331. $this->setExpectedException('Zend_Uri_Exception');
  332. $uri->setHost($host);
  333. }
  334. /**
  335. * @group ZF-1480
  336. */
  337. public function testGetQueryAsArrayReturnsCorrectArray()
  338. {
  339. $uri = Zend_Uri_Http::fromString('http://example.com/foo/?test=a&var[]=1&var[]=2&some[thing]=3');
  340. $this->assertEquals(array(
  341. 'test' => 'a',
  342. 'var' => array(1, 2),
  343. 'some' => array('thing' => 3)
  344. ), $uri->getQueryAsArray());
  345. }
  346. /**
  347. * @group ZF-1480
  348. */
  349. public function testAddReplaceQueryParametersModifiesQueryAndReturnsOldQuery()
  350. {
  351. $uri = Zend_Uri_Http::fromString('http://example.com/foo/?a=1&b=2&c=3');
  352. $this->assertEquals('a=1&b=2&c=3', $uri->addReplaceQueryParameters(array(
  353. 'b' => 4,
  354. 'd' => -1
  355. )));
  356. $this->assertEquals(array(
  357. 'a' => 1,
  358. 'b' => 4,
  359. 'c' => 3,
  360. 'd' => -1
  361. ), $uri->getQueryAsArray());
  362. $this->assertEquals('a=1&b=4&c=3&d=-1', $uri->getQuery());
  363. }
  364. /**
  365. * @group ZF-1480
  366. */
  367. public function testRemoveQueryParametersModifiesQueryAndReturnsOldQuery()
  368. {
  369. $uri = Zend_Uri_Http::fromString('http://example.com/foo/?a=1&b=2&c=3&d=4');
  370. $this->assertEquals('a=1&b=2&c=3&d=4', $uri->removeQueryParameters(array('b', 'd', 'e')));
  371. $this->assertEquals(array(
  372. 'a' => 1,
  373. 'c' => 3
  374. ), $uri->getQueryAsArray());
  375. $this->assertEquals('a=1&c=3', $uri->getQuery());
  376. }
  377. /**
  378. * @group ZF-11060
  379. */
  380. public function testAcceptsPipeCharacterAsPartOfUri()
  381. {
  382. $uri = Zend_Uri_Http::fromString('http://www.example.com/map?apistyle=a|b|c');
  383. $this->assertType('Zend_Uri_Http', $uri);
  384. $this->assertEquals('apistyle=a|b|c', $uri->getQuery());
  385. }
  386. /**
  387. * @group ZF-11188
  388. * @see http://www.ietf.org/rfc/rfc2732.txt
  389. */
  390. public function testParserSupportsLiteralIpv6AddressesInUri()
  391. {
  392. $this->assertTrue(Zend_Uri_Http::fromString('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html')->valid());
  393. $this->assertTrue(Zend_Uri_Http::fromString('http://[1080:0:0:0:8:800:200C:417A]/index.html')->valid());
  394. $this->assertTrue(Zend_Uri_Http::fromString('http://[3ffe:2a00:100:7031::1]')->valid());
  395. $this->assertTrue(Zend_Uri_Http::fromString('http://[1080::8:800:200C:417A]/foo')->valid());
  396. $this->assertTrue(Zend_Uri_Http::fromString('http://[::192.9.5.5]/ipng')->valid());
  397. $this->assertTrue(Zend_Uri_Http::fromString('http://[::FFFF:129.144.52.38]:80/index.html')->valid());
  398. $this->assertTrue(Zend_Uri_Http::fromString('http://[2010:836B:4179::836B:4179]')->valid());
  399. }
  400. }