HttpTest.php 13 KB

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