HttpTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. public function testAllParts()
  82. {
  83. $this->_testValidUri('http://andi:password@www.zend.com:8080/path/to/file?a=1&b=2#top');
  84. }
  85. public function testUsernamePortPathQueryFragment()
  86. {
  87. $this->_testValidUri('http://andi@www.zend.com:8080/path/to/file?a=1&b=2#top');
  88. }
  89. public function testPortPathQueryFragment()
  90. {
  91. $this->_testValidUri('http://www.zend.com:8080/path/to/file?a=1&b=2#top');
  92. }
  93. public function testPathQueryFragment()
  94. {
  95. $this->_testValidUri('http://www.zend.com/path/to/file?a=1&b=2#top');
  96. }
  97. public function testQueryFragment()
  98. {
  99. $this->_testValidUri('http://www.zend.com/?a=1&b=2#top');
  100. }
  101. public function testFragment()
  102. {
  103. $this->_testValidUri('http://www.zend.com/#top');
  104. }
  105. public function testUsernamePassword()
  106. {
  107. $this->_testValidUri('http://andi:password@www.zend.com');
  108. }
  109. public function testUsernamePasswordColon()
  110. {
  111. $this->_testValidUri('http://an:di:password@www.zend.com');
  112. }
  113. public function testUsernamePasswordValidCharacters()
  114. {
  115. $this->_testValidUri('http://a_.!~*\'(-)n0123Di%25%26:pass;:&=+$,word@www.zend.com');
  116. }
  117. public function testUsernameInvalidCharacter()
  118. {
  119. $this->_testInvalidUri('http://an`di:password@www.zend.com');
  120. }
  121. public function testNoUsernamePassword()
  122. {
  123. $this->_testInvalidUri('http://:password@www.zend.com');
  124. }
  125. public function testPasswordInvalidCharacter()
  126. {
  127. $this->_testInvalidUri('http://andi:pass%word@www.zend.com');
  128. }
  129. public function testHostAsIP()
  130. {
  131. $this->_testValidUri('http://127.0.0.1');
  132. }
  133. public function testLocalhost()
  134. {
  135. $this->_testValidUri('http://localhost');
  136. }
  137. public function testLocalhostLocaldomain()
  138. {
  139. $this->_testValidUri('http://localhost.localdomain');
  140. }
  141. public function testSquareBrackets()
  142. {
  143. $this->_testValidUri('https://example.com/foo/?var[]=1&var[]=2&some[thing]=3');
  144. }
  145. /**
  146. * Ensures that successive slashes are considered valid
  147. *
  148. * @return void
  149. */
  150. public function testSuccessiveSlashes()
  151. {
  152. $this->_testValidUri('http://example.com//');
  153. $this->_testValidUri('http://example.com///');
  154. $this->_testValidUri('http://example.com/foo//');
  155. $this->_testValidUri('http://example.com/foo///');
  156. $this->_testValidUri('http://example.com/foo//bar');
  157. $this->_testValidUri('http://example.com/foo///bar');
  158. $this->_testValidUri('http://example.com/foo//bar/baz//fob/');
  159. }
  160. /**
  161. * Test that setQuery() can handle unencoded query parameters (as other
  162. * browsers do), ZF-1934
  163. *
  164. * @link http://framework.zend.com/issues/browse/ZF-1934
  165. * @return void
  166. */
  167. public function testUnencodedQueryParameters()
  168. {
  169. $uri = Zend_Uri::factory('http://foo.com/bar');
  170. // First, make sure no exceptions are thrown
  171. try {
  172. $uri->setQuery('id=123&url=http://example.com/?bar=foo baz');
  173. } catch (Exception $e) {
  174. $this->fail('setQuery() was expected to handle unencoded parameters, but failed');
  175. }
  176. // Second, make sure the query string was properly encoded
  177. $parts = parse_url($uri->getUri());
  178. $this->assertEquals('id=123&url=http%3A%2F%2Fexample.com%2F%3Fbar%3Dfoo+baz', $parts['query']);
  179. }
  180. /**
  181. * Test that unwise characters in the query string are not valid
  182. *
  183. */
  184. public function testExceptionUnwiseQueryString()
  185. {
  186. $unwise = array(
  187. 'http://example.com/?q={',
  188. 'http://example.com/?q=}',
  189. 'http://example.com/?q=|',
  190. 'http://example.com/?q=\\',
  191. 'http://example.com/?q=^',
  192. 'http://example.com/?q=`',
  193. );
  194. foreach ($unwise as $uri) {
  195. $this->assertFalse(Zend_Uri::check($uri), "failed for URI $uri");
  196. }
  197. }
  198. /**
  199. * Test that after setting 'allow_unwise' to true unwise characters are
  200. * accepted
  201. *
  202. */
  203. public function testAllowUnwiseQueryString()
  204. {
  205. $unwise = array(
  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. 'http://example.com/?q=`',
  212. );
  213. Zend_Uri::setConfig(array('allow_unwise' => true));
  214. foreach ($unwise as $uri) {
  215. $this->assertTrue(Zend_Uri::check($uri), "failed for URI $uri");
  216. }
  217. Zend_Uri::setConfig(array('allow_unwise' => false));
  218. }
  219. /**
  220. * Test that an extremely long URI does not break things up
  221. *
  222. * @link http://framework.zend.com/issues/browse/ZF-3712
  223. */
  224. public function testVeryLongUriZF3712()
  225. {
  226. $uri = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  227. '_files' . DIRECTORY_SEPARATOR . 'testVeryLongUriZF3712.txt');
  228. $this->_testValidUri($uri);
  229. }
  230. /**
  231. * Test a known valid URI
  232. *
  233. * @param string $uri
  234. */
  235. protected function _testValidUri($uri)
  236. {
  237. $obj = Zend_Uri::factory($uri);
  238. $this->assertEquals($uri, $obj->getUri(), 'getUri() returned value that differs from input');
  239. }
  240. /**
  241. * Test a known invalid URI
  242. *
  243. * @param string $uri
  244. */
  245. protected function _testInvalidUri($uri)
  246. {
  247. try {
  248. $obj = Zend_Uri::factory($uri);
  249. $this->fail('Zend_Uri_Exception was expected but not thrown');
  250. } catch (Zend_Uri_Exception $e) {
  251. }
  252. }
  253. }