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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Uri_HttpTest extends PHPUnit_Framework_TestCase
  42. {
  43. /**
  44. * Tests for proper URI decomposition
  45. */
  46. public function testSimple()
  47. {
  48. $this->_testValidUri('http://www.zend.com');
  49. }
  50. /**
  51. * Test that fromString() works proprerly for simple valid URLs
  52. *
  53. */
  54. public function testSimpleFromString()
  55. {
  56. $tests = array(
  57. 'http://www.zend.com',
  58. 'https://www.zend.com',
  59. 'http://www.zend.com/path',
  60. 'http://www.zend.com/path?query=value'
  61. );
  62. foreach ($tests as $uri) {
  63. $obj = Zend_Uri_Http::fromString($uri);
  64. $this->assertEquals($uri, $obj->getUri(),
  65. "getUri() returned value that differs from input for $uri");
  66. }
  67. }
  68. /**
  69. * Make sure an exception is thrown when trying to use fromString() with a
  70. * non-HTTP scheme
  71. *
  72. * @see http://framework.zend.com/issues/browse/ZF-4395
  73. *
  74. * @expectedException Zend_Uri_Exception
  75. */
  76. public function testFromStringInvalidScheme()
  77. {
  78. Zend_Uri_Http::fromString('ftp://example.com/file');
  79. }
  80. public function testAllParts()
  81. {
  82. $this->_testValidUri('http://andi:password@www.zend.com:8080/path/to/file?a=1&b=2#top');
  83. }
  84. public function testUsernamePortPathQueryFragment()
  85. {
  86. $this->_testValidUri('http://andi@www.zend.com:8080/path/to/file?a=1&b=2#top');
  87. }
  88. public function testPortPathQueryFragment()
  89. {
  90. $this->_testValidUri('http://www.zend.com:8080/path/to/file?a=1&b=2#top');
  91. }
  92. public function testPathQueryFragment()
  93. {
  94. $this->_testValidUri('http://www.zend.com/path/to/file?a=1&b=2#top');
  95. }
  96. public function testQueryFragment()
  97. {
  98. $this->_testValidUri('http://www.zend.com/?a=1&b=2#top');
  99. }
  100. public function testFragment()
  101. {
  102. $this->_testValidUri('http://www.zend.com/#top');
  103. }
  104. public function testUsernamePassword()
  105. {
  106. $this->_testValidUri('http://andi:password@www.zend.com');
  107. }
  108. public function testUsernamePasswordColon()
  109. {
  110. $this->_testValidUri('http://an:di:password@www.zend.com');
  111. }
  112. public function testUsernamePasswordValidCharacters()
  113. {
  114. $this->_testValidUri('http://a_.!~*\'(-)n0123Di%25%26:pass;:&=+$,word@www.zend.com');
  115. }
  116. public function testUsernameInvalidCharacter()
  117. {
  118. $this->_testInvalidUri('http://an`di:password@www.zend.com');
  119. }
  120. public function testNoUsernamePassword()
  121. {
  122. $this->_testInvalidUri('http://:password@www.zend.com');
  123. }
  124. public function testPasswordInvalidCharacter()
  125. {
  126. $this->_testInvalidUri('http://andi:pass%word@www.zend.com');
  127. }
  128. public function testHostAsIP()
  129. {
  130. $this->_testValidUri('http://127.0.0.1');
  131. }
  132. public function testLocalhost()
  133. {
  134. $this->_testValidUri('http://localhost');
  135. }
  136. public function testLocalhostLocaldomain()
  137. {
  138. $this->_testValidUri('http://localhost.localdomain');
  139. }
  140. public function testSquareBrackets()
  141. {
  142. $this->_testValidUri('https://example.com/foo/?var[]=1&var[]=2&some[thing]=3');
  143. }
  144. /**
  145. * Ensures that successive slashes are considered valid
  146. *
  147. * @return void
  148. */
  149. public function testSuccessiveSlashes()
  150. {
  151. $this->_testValidUri('http://example.com//');
  152. $this->_testValidUri('http://example.com///');
  153. $this->_testValidUri('http://example.com/foo//');
  154. $this->_testValidUri('http://example.com/foo///');
  155. $this->_testValidUri('http://example.com/foo//bar');
  156. $this->_testValidUri('http://example.com/foo///bar');
  157. $this->_testValidUri('http://example.com/foo//bar/baz//fob/');
  158. }
  159. /**
  160. * Test that setQuery() can handle unencoded query parameters (as other
  161. * browsers do), ZF-1934
  162. *
  163. * @link http://framework.zend.com/issues/browse/ZF-1934
  164. * @return void
  165. */
  166. public function testUnencodedQueryParameters()
  167. {
  168. $uri = Zend_Uri::factory('http://foo.com/bar');
  169. // First, make sure no exceptions are thrown
  170. try {
  171. $uri->setQuery('id=123&url=http://example.com/?bar=foo baz');
  172. } catch (Exception $e) {
  173. $this->fail('setQuery() was expected to handle unencoded parameters, but failed');
  174. }
  175. // Second, make sure the query string was properly encoded
  176. $parts = parse_url($uri->getUri());
  177. $this->assertEquals('id=123&url=http%3A%2F%2Fexample.com%2F%3Fbar%3Dfoo+baz', $parts['query']);
  178. }
  179. /**
  180. * Test that unwise characters in the query string are not valid
  181. *
  182. */
  183. public function testExceptionUnwiseQueryString()
  184. {
  185. $unwise = array(
  186. 'http://example.com/?q={',
  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. );
  193. foreach ($unwise as $uri) {
  194. $this->assertFalse(Zend_Uri::check($uri), "failed for URI $uri");
  195. }
  196. }
  197. /**
  198. * Test that after setting 'allow_unwise' to true unwise characters are
  199. * accepted
  200. *
  201. */
  202. public function testAllowUnwiseQueryString()
  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. Zend_Uri::setConfig(array('allow_unwise' => true));
  213. foreach ($unwise as $uri) {
  214. $this->assertTrue(Zend_Uri::check($uri), "failed for URI $uri");
  215. }
  216. Zend_Uri::setConfig(array('allow_unwise' => false));
  217. }
  218. /**
  219. * Test that an extremely long URI does not break things up
  220. *
  221. * @link http://framework.zend.com/issues/browse/ZF-3712
  222. */
  223. public function testVeryLongUriZF3712()
  224. {
  225. $uri = file_get_contents(dirname(realpath(__FILE__)) . DIRECTORY_SEPARATOR .
  226. '_files' . DIRECTORY_SEPARATOR . 'testVeryLongUriZF3712.txt');
  227. $this->_testValidUri($uri);
  228. }
  229. /**
  230. * Test a known valid URI
  231. *
  232. * @param string $uri
  233. */
  234. protected function _testValidUri($uri)
  235. {
  236. $obj = Zend_Uri::factory($uri);
  237. $this->assertEquals($uri, $obj->getUri(), 'getUri() returned value that differs from input');
  238. }
  239. /**
  240. * Test a known invalid URI
  241. *
  242. * @param string $uri
  243. */
  244. protected function _testInvalidUri($uri)
  245. {
  246. try {
  247. $obj = Zend_Uri::factory($uri);
  248. $this->fail('Zend_Uri_Exception was expected but not thrown');
  249. } catch (Zend_Uri_Exception $e) {
  250. }
  251. }
  252. }