HttpTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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-2009 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. * PHPUnit test case
  32. */
  33. require_once 'PHPUnit/Framework/TestCase.php';
  34. /**
  35. * @category Zend
  36. * @package Zend_Uri
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Uri
  41. */
  42. class Zend_Uri_HttpTest extends PHPUnit_Framework_TestCase
  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. * @see http://framework.zend.com/issues/browse/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. * @link http://framework.zend.com/issues/browse/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. 'http://example.com/?q=`',
  203. );
  204. foreach ($unwise as $uri) {
  205. $this->assertFalse(Zend_Uri::check($uri), "failed for URI $uri");
  206. }
  207. }
  208. /**
  209. * Test that after setting 'allow_unwise' to true unwise characters are
  210. * accepted
  211. *
  212. */
  213. public function testAllowUnwiseQueryString()
  214. {
  215. $unwise = array(
  216. 'http://example.com/?q={',
  217. 'http://example.com/?q=}',
  218. 'http://example.com/?q=|',
  219. 'http://example.com/?q=\\',
  220. 'http://example.com/?q=^',
  221. 'http://example.com/?q=`',
  222. );
  223. Zend_Uri::setConfig(array('allow_unwise' => true));
  224. foreach ($unwise as $uri) {
  225. $this->assertTrue(Zend_Uri::check($uri), "failed for URI $uri");
  226. }
  227. Zend_Uri::setConfig(array('allow_unwise' => false));
  228. }
  229. /**
  230. * Test that an extremely long URI does not break things up
  231. *
  232. * @link http://framework.zend.com/issues/browse/ZF-3712
  233. */
  234. public function testVeryLongUriZF3712()
  235. {
  236. $uri = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  237. '_files' . DIRECTORY_SEPARATOR . 'testVeryLongUriZF3712.txt');
  238. $this->_testValidUri($uri);
  239. }
  240. /**
  241. * Test a known valid URI
  242. *
  243. * @param string $uri
  244. */
  245. protected function _testValidUri($uri)
  246. {
  247. $obj = Zend_Uri::factory($uri);
  248. $this->assertEquals($uri, $obj->getUri(), 'getUri() returned value that differs from input');
  249. }
  250. /**
  251. * Test a known invalid URI
  252. *
  253. * @param string $uri
  254. */
  255. protected function _testInvalidUri($uri)
  256. {
  257. try {
  258. $obj = Zend_Uri::factory($uri);
  259. $this->fail('Zend_Uri_Exception was expected but not thrown');
  260. } catch (Zend_Uri_Exception $e) {
  261. }
  262. }
  263. public function testSetGetUsername()
  264. {
  265. $uri = Zend_Uri::factory('http://example.com');
  266. $username = 'alice';
  267. $this->assertFalse($uri->getUsername());
  268. $uri->setUsername($username);
  269. $this->assertSame($username, $uri->getUsername());
  270. }
  271. public function testSetGetPassword()
  272. {
  273. $uri = Zend_Uri::factory('http://example.com');
  274. $username = 'alice';
  275. $password = 'secret';
  276. $this->assertFalse($uri->getPassword());
  277. $uri->setUsername($username);
  278. $uri->setPassword($password);
  279. $this->assertSame($password, $uri->getPassword());
  280. }
  281. public function testUriWithAllParts()
  282. {
  283. $uri = Zend_Uri::factory('http://alice:secret@example.com:8080/path/script.php?foo=bar&bar=foo#123');
  284. $this->assertSame('http', $uri->getScheme());
  285. $this->assertSame('alice', $uri->getUsername());
  286. $this->assertSame('secret', $uri->getPassword());
  287. $this->assertSame('example.com', $uri->getHost());
  288. $this->assertEquals(8080, $uri->getPort());
  289. $this->assertSame('/path/script.php', $uri->getPath());
  290. $this->assertSame('foo=bar&bar=foo', $uri->getQuery());
  291. $this->assertSame('123', $uri->getFragment());
  292. }
  293. public function testBuildCompleteUriFromScratch()
  294. {
  295. $uri = Zend_Uri::factory('http');
  296. $uri->setUsername('alice');
  297. $uri->setPassword('secret');
  298. $uri->setHost('example.com');
  299. $uri->setPort(8080);
  300. $uri->setPath('/path/script.php');
  301. $uri->setQuery('foo=bar&bar=foo');
  302. $uri->setFragment('123');
  303. $this->assertSame('http://alice:secret@example.com:8080/path/script.php?foo=bar&bar=foo#123', $uri->getUri());
  304. }
  305. public function testSetInvalidUsername()
  306. {
  307. $uri = Zend_Uri::factory('http://example.com');
  308. $this->setExpectedException('Zend_Uri_Exception');
  309. $uri->setUsername('alice?');
  310. }
  311. public function testSetInvalidPassword()
  312. {
  313. $uri = Zend_Uri::factory('http://example.com');
  314. $this->setExpectedException('Zend_Uri_Exception');
  315. $uri->setUsername('alice');
  316. $uri->setPassword('secret?');
  317. }
  318. public function testSetEmptyHost()
  319. {
  320. $uri = Zend_Uri::factory('http://example.com');
  321. $host = '';
  322. $this->setExpectedException('Zend_Uri_Exception');
  323. $uri->setHost($host);
  324. }
  325. public function testSetInvalidHost()
  326. {
  327. $uri = Zend_Uri::factory('http://example.com');
  328. $host = 'example,com';
  329. $this->setExpectedException('Zend_Uri_Exception');
  330. $uri->setHost($host);
  331. }
  332. }