HttpTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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-2015 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-2015 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 testMissingDomainParts()
  140. {
  141. $this->_testInvalidUri('https://www.zend..com');
  142. }
  143. public function testHostAsIP()
  144. {
  145. $this->_testValidUri('http://127.0.0.1');
  146. }
  147. public function testLocalhost()
  148. {
  149. $this->_testValidUri('http://localhost');
  150. }
  151. public function testLocalhostLocaldomain()
  152. {
  153. $this->_testValidUri('http://localhost.localdomain');
  154. }
  155. public function testSquareBrackets()
  156. {
  157. $this->_testValidUri('https://example.com/foo/?var[]=1&var[]=2&some[thing]=3');
  158. }
  159. /**
  160. * Ensures that successive slashes are considered valid
  161. *
  162. * @return void
  163. */
  164. public function testSuccessiveSlashes()
  165. {
  166. $this->_testValidUri('http://example.com//');
  167. $this->_testValidUri('http://example.com///');
  168. $this->_testValidUri('http://example.com/foo//');
  169. $this->_testValidUri('http://example.com/foo///');
  170. $this->_testValidUri('http://example.com/foo//bar');
  171. $this->_testValidUri('http://example.com/foo///bar');
  172. $this->_testValidUri('http://example.com/foo//bar/baz//fob/');
  173. }
  174. /**
  175. * Test that setQuery() can handle unencoded query parameters (as other
  176. * browsers do), ZF-1934
  177. *
  178. * @group ZF-1934
  179. * @return void
  180. */
  181. public function testUnencodedQueryParameters()
  182. {
  183. $uri = Zend_Uri::factory('http://foo.com/bar');
  184. // First, make sure no exceptions are thrown
  185. try {
  186. $uri->setQuery('id=123&url=http://example.com/?bar=foo baz');
  187. } catch (Exception $e) {
  188. $this->fail('setQuery() was expected to handle unencoded parameters, but failed');
  189. }
  190. // Second, make sure the query string was properly encoded
  191. $parts = parse_url($uri->getUri());
  192. $this->assertEquals('id=123&url=http%3A%2F%2Fexample.com%2F%3Fbar%3Dfoo+baz', $parts['query']);
  193. }
  194. /**
  195. * Test that unwise characters in the query string are not valid
  196. *
  197. */
  198. public function testExceptionUnwiseQueryString()
  199. {
  200. $unwise = array(
  201. 'http://example.com/?q={',
  202. 'http://example.com/?q=}',
  203. 'http://example.com/?q=|',
  204. 'http://example.com/?q=\\',
  205. 'http://example.com/?q=^',
  206. 'http://example.com/?q=`',
  207. );
  208. foreach ($unwise as $uri) {
  209. $this->assertFalse(Zend_Uri::check($uri), "failed for URI $uri");
  210. }
  211. }
  212. /**
  213. * Test that after setting 'allow_unwise' to true unwise characters are
  214. * accepted
  215. *
  216. */
  217. public function testAllowUnwiseQueryString()
  218. {
  219. $unwise = array(
  220. 'http://example.com/?q={',
  221. 'http://example.com/?q=}',
  222. 'http://example.com/?q=|',
  223. 'http://example.com/?q=\\',
  224. 'http://example.com/?q=^',
  225. 'http://example.com/?q=`',
  226. );
  227. Zend_Uri::setConfig(array('allow_unwise' => true));
  228. foreach ($unwise as $uri) {
  229. $this->assertTrue(Zend_Uri::check($uri), "failed for URI $uri");
  230. }
  231. Zend_Uri::setConfig(array('allow_unwise' => false));
  232. }
  233. /**
  234. * Test that an extremely long URI does not break things up
  235. *
  236. * @group ZF-3712
  237. * @group ZF-7840
  238. */
  239. public function testVeryLongUriZF3712()
  240. {
  241. if(!defined('TESTS_ZEND_URI_CRASH_TEST_ENABLED') || constant('TESTS_ZEND_URI_CRASH_TEST_ENABLED') == false) {
  242. $this->markTestSkipped('The constant TESTS_ZEND_URI_CRASH_TEST_ENABLED has to be defined and true to allow the test to work.');
  243. }
  244. $uri = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  245. '_files' . DIRECTORY_SEPARATOR . 'testVeryLongUriZF3712.txt');
  246. $this->_testValidUri($uri);
  247. }
  248. /**
  249. * Test a known valid URI
  250. *
  251. * @param string $uri
  252. */
  253. protected function _testValidUri($uri)
  254. {
  255. $obj = Zend_Uri::factory($uri);
  256. $this->assertEquals($uri, $obj->getUri(), 'getUri() returned value that differs from input');
  257. }
  258. /**
  259. * Test a known invalid URI
  260. *
  261. * @param string $uri
  262. */
  263. protected function _testInvalidUri($uri)
  264. {
  265. try {
  266. $obj = Zend_Uri::factory($uri);
  267. $this->fail('Zend_Uri_Exception was expected but not thrown');
  268. } catch (Zend_Uri_Exception $e) {
  269. }
  270. }
  271. public function testSetGetUsername()
  272. {
  273. $uri = Zend_Uri::factory('http://example.com');
  274. $username = 'alice';
  275. $this->assertFalse($uri->getUsername());
  276. $uri->setUsername($username);
  277. $this->assertSame($username, $uri->getUsername());
  278. }
  279. public function testSetGetPassword()
  280. {
  281. $uri = Zend_Uri::factory('http://example.com');
  282. $username = 'alice';
  283. $password = 'secret';
  284. $this->assertFalse($uri->getPassword());
  285. $uri->setUsername($username);
  286. $uri->setPassword($password);
  287. $this->assertSame($password, $uri->getPassword());
  288. }
  289. public function testUriWithAllParts()
  290. {
  291. $uri = Zend_Uri::factory('http://alice:secret@example.com:8080/path/script.php?foo=bar&bar=foo#123');
  292. $this->assertSame('http', $uri->getScheme());
  293. $this->assertSame('alice', $uri->getUsername());
  294. $this->assertSame('secret', $uri->getPassword());
  295. $this->assertSame('example.com', $uri->getHost());
  296. $this->assertEquals(8080, $uri->getPort());
  297. $this->assertSame('/path/script.php', $uri->getPath());
  298. $this->assertSame('foo=bar&bar=foo', $uri->getQuery());
  299. $this->assertSame('123', $uri->getFragment());
  300. }
  301. public function testBuildCompleteUriFromScratch()
  302. {
  303. $uri = Zend_Uri::factory('http');
  304. $uri->setUsername('alice');
  305. $uri->setPassword('secret');
  306. $uri->setHost('example.com');
  307. $uri->setPort(8080);
  308. $uri->setPath('/path/script.php');
  309. $uri->setQuery('foo=bar&bar=foo');
  310. $uri->setFragment('123');
  311. $this->assertSame('http://alice:secret@example.com:8080/path/script.php?foo=bar&bar=foo#123', $uri->getUri());
  312. }
  313. public function testSetInvalidUsername()
  314. {
  315. $uri = Zend_Uri::factory('http://example.com');
  316. $this->setExpectedException('Zend_Uri_Exception');
  317. $uri->setUsername('alice?');
  318. }
  319. public function testSetInvalidPassword()
  320. {
  321. $uri = Zend_Uri::factory('http://example.com');
  322. $this->setExpectedException('Zend_Uri_Exception');
  323. $uri->setUsername('alice');
  324. $uri->setPassword('secret?');
  325. }
  326. public function testSetEmptyHost()
  327. {
  328. $uri = Zend_Uri::factory('http://example.com');
  329. $host = '';
  330. $this->setExpectedException('Zend_Uri_Exception');
  331. $uri->setHost($host);
  332. }
  333. public function testSetInvalidHost()
  334. {
  335. $uri = Zend_Uri::factory('http://example.com');
  336. $host = 'example§com';
  337. $this->setExpectedException('Zend_Uri_Exception');
  338. $uri->setHost($host);
  339. }
  340. /**
  341. * @group ZF-1480
  342. */
  343. public function testGetQueryAsArrayReturnsCorrectArray()
  344. {
  345. $uri = Zend_Uri_Http::fromString('http://example.com/foo/?test=a&var[]=1&var[]=2&some[thing]=3');
  346. $this->assertEquals(array(
  347. 'test' => 'a',
  348. 'var' => array(1, 2),
  349. 'some' => array('thing' => 3)
  350. ), $uri->getQueryAsArray());
  351. }
  352. /**
  353. * @group ZF-1480
  354. */
  355. public function testAddReplaceQueryParametersModifiesQueryAndReturnsOldQuery()
  356. {
  357. $uri = Zend_Uri_Http::fromString('http://example.com/foo/?a=1&b=2&c=3');
  358. $this->assertEquals('a=1&b=2&c=3', $uri->addReplaceQueryParameters(array(
  359. 'b' => 4,
  360. 'd' => -1
  361. )));
  362. $this->assertEquals(array(
  363. 'a' => 1,
  364. 'b' => 4,
  365. 'c' => 3,
  366. 'd' => -1
  367. ), $uri->getQueryAsArray());
  368. $this->assertEquals('a=1&b=4&c=3&d=-1', $uri->getQuery());
  369. }
  370. /**
  371. * @group ZF-1480
  372. */
  373. public function testRemoveQueryParametersModifiesQueryAndReturnsOldQuery()
  374. {
  375. $uri = Zend_Uri_Http::fromString('http://example.com/foo/?a=1&b=2&c=3&d=4');
  376. $this->assertEquals('a=1&b=2&c=3&d=4', $uri->removeQueryParameters(array('b', 'd', 'e')));
  377. $this->assertEquals(array(
  378. 'a' => 1,
  379. 'c' => 3
  380. ), $uri->getQueryAsArray());
  381. $this->assertEquals('a=1&c=3', $uri->getQuery());
  382. }
  383. /**
  384. * @group ZF-11188
  385. * @see http://www.ietf.org/rfc/rfc2732.txt
  386. */
  387. public function testParserSupportsLiteralIpv6AddressesInUri()
  388. {
  389. $this->assertTrue(Zend_Uri_Http::fromString('http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html')->valid());
  390. $this->assertTrue(Zend_Uri_Http::fromString('http://[1080:0:0:0:8:800:200C:417A]/index.html')->valid());
  391. $this->assertTrue(Zend_Uri_Http::fromString('http://[3ffe:2a00:100:7031::1]')->valid());
  392. $this->assertTrue(Zend_Uri_Http::fromString('http://[1080::8:800:200C:417A]/foo')->valid());
  393. $this->assertTrue(Zend_Uri_Http::fromString('http://[::192.9.5.5]/ipng')->valid());
  394. $this->assertTrue(Zend_Uri_Http::fromString('http://[::FFFF:129.144.52.38]:80/index.html')->valid());
  395. $this->assertTrue(Zend_Uri_Http::fromString('http://[2010:836B:4179::836B:4179]')->valid());
  396. }
  397. }