StaticTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. require_once realpath(dirname(__FILE__) . '/../../../') . '/TestHelper.php';
  3. require_once 'Zend/Http/Client.php';
  4. /**
  5. * This Testsuite includes all Zend_Http_Client tests that do not rely
  6. * on performing actual requests to an HTTP server. These tests can be
  7. * executed once, and do not need to be tested with different servers /
  8. * client setups.
  9. *
  10. * @category Zend
  11. * @package Zend_Http_Client
  12. * @subpackage UnitTests
  13. * @version $Id$
  14. * @copyright
  15. * @license http://framework.zend.com/license/new-bsd New BSD License
  16. */
  17. class Zend_Http_Client_StaticTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Common HTTP client
  21. *
  22. * @var Zend_Http_Client
  23. */
  24. protected $_client = null;
  25. /**
  26. * Set up the test suite before each test
  27. *
  28. */
  29. public function setUp()
  30. {
  31. $this->_client = new Zend_Http_Client('http://www.example.com');
  32. }
  33. /**
  34. * Clean up after running a test
  35. *
  36. */
  37. public function tearDown()
  38. {
  39. $this->_client = null;
  40. }
  41. /**
  42. * URI Tests
  43. */
  44. /**
  45. * Test we can SET and GET a URI as string
  46. *
  47. */
  48. public function testSetGetUriString()
  49. {
  50. $uristr = 'http://www.zend.com:80/';
  51. $this->_client->setUri($uristr);
  52. $uri = $this->_client->getUri();
  53. $this->assertTrue($uri instanceof Zend_Uri_Http, 'Returned value is not a Uri object as expected');
  54. $this->assertEquals($uri->__toString(), $uristr, 'Returned Uri object does not hold the expected URI');
  55. $uri = $this->_client->getUri(true);
  56. $this->assertTrue(is_string($uri), 'Returned value expected to be a string, ' . gettype($uri) . ' returned');
  57. $this->assertEquals($uri, $uristr, 'Returned string is not the expected URI');
  58. }
  59. /**
  60. * Test we can SET and GET a URI as object
  61. *
  62. */
  63. public function testSetGetUriObject()
  64. {
  65. $uriobj = Zend_Uri::factory('http://www.zend.com:80/');
  66. $this->_client->setUri($uriobj);
  67. $uri = $this->_client->getUri();
  68. $this->assertTrue($uri instanceof Zend_Uri_Http, 'Returned value is not a Uri object as expected');
  69. $this->assertEquals($uri, $uriobj, 'Returned object is not the excepted Uri object');
  70. }
  71. /**
  72. * Test that passing an invalid URI string throws an exception
  73. *
  74. * @expectedException Zend_Uri_Exception
  75. */
  76. public function testInvalidUriStringException()
  77. {
  78. $this->_client->setUri('httpp://__invalid__.com');
  79. }
  80. /**
  81. * Test that passing an invalid URI object throws an exception
  82. *
  83. */
  84. public function testInvalidUriObjectException()
  85. {
  86. try {
  87. $uri = Zend_Uri::factory('mailto:nobody@example.com');
  88. $this->_client->setUri($uri);
  89. $this->fail('Excepted invalid URI object exception was not thrown');
  90. } catch (Zend_Http_Client_Exception $e) {
  91. // We're good
  92. } catch (Zend_Uri_Exception $e) {
  93. // URI is currently unimplemented
  94. $this->markTestIncomplete('Zend_Uri_Mailto is not implemented yet');
  95. }
  96. }
  97. /**
  98. * Header Tests
  99. */
  100. /**
  101. * Make sure an exception is thrown if an invalid header name is used
  102. *
  103. * @expectedException Zend_Http_Client_Exception
  104. */
  105. public function testInvalidHeaderExcept()
  106. {
  107. $this->_client->setHeaders('Ina_lid* Hea%der', 'is not good');
  108. }
  109. /**
  110. * Make sure non-strict mode disables header name validation
  111. *
  112. */
  113. public function testInvalidHeaderNonStrictMode()
  114. {
  115. // Disable strict validation
  116. $this->_client->setConfig(array('strict' => false));
  117. try {
  118. $this->_client->setHeaders('Ina_lid* Hea%der', 'is not good');
  119. } catch (Zend_Http_Client_Exception $e) {
  120. $this->fail('Invalid header names should be allowed in non-strict mode');
  121. }
  122. }
  123. /**
  124. * Test we can get already set headers
  125. *
  126. */
  127. public function testGetHeader()
  128. {
  129. $this->_client->setHeaders(array(
  130. 'Accept-encoding' => 'gzip,deflate',
  131. 'Accept-language' => 'en,de,*',
  132. ));
  133. $this->assertEquals($this->_client->getHeader('Accept-encoding'), 'gzip,deflate', 'Returned value of header is not as expected');
  134. $this->assertEquals($this->_client->getHeader('X-Fake-Header'), null, 'Non-existing header should not return a value');
  135. }
  136. public function testUnsetHeader()
  137. {
  138. $this->_client->setHeaders('Accept-Encoding', 'gzip,deflate');
  139. $this->_client->setHeaders('Accept-Encoding', null);
  140. $this->assertNull($this->_client->getHeader('Accept-encoding'), 'Returned value of header is expected to be null');
  141. }
  142. /**
  143. * Authentication tests
  144. */
  145. /**
  146. * Test setAuth (dynamic method) fails when trying to use an unsupported
  147. * authentication scheme
  148. *
  149. * @expectedException Zend_Http_Client_Exception
  150. */
  151. public function testExceptUnsupportedAuthDynamic()
  152. {
  153. $this->_client->setAuth('shahar', '1234', 'SuperStrongAlgo');
  154. }
  155. /**
  156. * Test encodeAuthHeader (static method) fails when trying to use an
  157. * unsupported authentication scheme
  158. *
  159. * @expectedException Zend_Http_Client_Exception
  160. */
  161. public function testExceptUnsupportedAuthStatic()
  162. {
  163. Zend_Http_Client::encodeAuthHeader('shahar', '1234', 'SuperStrongAlgo');
  164. }
  165. /**
  166. * Cookie and Cookie Jar tests
  167. */
  168. /**
  169. * Test we can properly set a new cookie jar
  170. *
  171. */
  172. public function testSetNewCookieJar()
  173. {
  174. $this->_client->setCookieJar();
  175. $this->_client->setCookie('cookie', 'value');
  176. $this->_client->setCookie('chocolate', 'chips');
  177. $jar = $this->_client->getCookieJar();
  178. // Check we got the right cookiejar
  179. $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of Zend_Http_CookieJar as expected');
  180. $this->assertEquals(count($jar->getAllCookies()), 2, '$jar does not contain 2 cookies as expected');
  181. }
  182. /**
  183. * Test we can properly set an existing cookie jar
  184. *
  185. */
  186. public function testSetReadyCookieJar()
  187. {
  188. $jar = new Zend_Http_CookieJar();
  189. $jar->addCookie('cookie=value', 'http://www.example.com');
  190. $jar->addCookie('chocolate=chips; path=/foo', 'http://www.example.com');
  191. $this->_client->setCookieJar($jar);
  192. // Check we got the right cookiejar
  193. $this->assertEquals($jar, $this->_client->getCookieJar(), '$jar is not the client\'s cookie jar as expected');
  194. }
  195. /**
  196. * Test we can unset a cookie jar
  197. *
  198. */
  199. public function testUnsetCookieJar()
  200. {
  201. // Set the cookie jar just like in testSetNewCookieJar
  202. $this->_client->setCookieJar();
  203. $this->_client->setCookie('cookie', 'value');
  204. $this->_client->setCookie('chocolate', 'chips');
  205. $jar = $this->_client->getCookieJar();
  206. // Try unsetting the cookiejar
  207. $this->_client->setCookieJar(null);
  208. $this->assertNull($this->_client->getCookieJar(), 'Cookie jar is expected to be null but it is not');
  209. }
  210. /**
  211. * Make sure using an invalid cookie jar object throws an exception
  212. *
  213. * @expectedException Zend_Http_Client_Exception
  214. */
  215. public function testSetInvalidCookieJar()
  216. {
  217. $this->_client->setCookieJar('cookiejar');
  218. }
  219. /**
  220. * Other Tests
  221. */
  222. /**
  223. * Check we get an exception when trying to send a POST request with an
  224. * invalid content-type header
  225. *
  226. * @expectedException Zend_Http_Client_Exception
  227. */
  228. public function testInvalidPostContentType()
  229. {
  230. $this->_client->setEncType('x-foo/something-fake');
  231. $this->_client->setParameterPost('parameter', 'value');
  232. // This should throw an exception
  233. $this->_client->request('POST');
  234. }
  235. /**
  236. * Check we get an exception if there's an error in the socket
  237. *
  238. * @expectedException Zend_Http_Client_Adapter_Exception
  239. */
  240. public function testSocketErrorException()
  241. {
  242. // Try to connect to an invalid host
  243. $this->_client->setUri('http://255.255.255.255');
  244. // Reduce timeout to 3 seconds to avoid waiting
  245. $this->_client->setConfig(array('timeout' => 3));
  246. // This call should cause an exception
  247. $this->_client->request();
  248. }
  249. /**
  250. * Check that we can set methods which are not documented in the RFC.
  251. *
  252. * @dataProvider validMethodProvider
  253. */
  254. public function testSettingExtendedMethod($method)
  255. {
  256. try {
  257. $this->_client->setMethod($method);
  258. } catch (Exception $e) {
  259. $this->fail("An unexpected exception was thrown when setting request method to '{$method}'");
  260. }
  261. }
  262. /**
  263. * Check that an exception is thrown if non-word characters are used in
  264. * the request method.
  265. *
  266. * @dataProvider invalidMethodProvider
  267. * @expectedException Zend_Http_Client_Exception
  268. */
  269. public function testSettingInvalidMethodThrowsException($method)
  270. {
  271. $this->_client->setMethod($method);
  272. }
  273. /**
  274. * Test that configuration options are passed to the adapter after the
  275. * adapter is instantiated
  276. *
  277. * @link http://framework.zend.com/issues/browse/ZF-4557
  278. */
  279. public function testConfigPassToAdapterZF4557()
  280. {
  281. require_once 'Zend/Http/Client/Adapter/Test.php';
  282. $adapter = new Zend_Http_Client_Adapter_Test();
  283. // test that config passes when we set the adapter
  284. $this->_client->setConfig(array('param' => 'value1'));
  285. $this->_client->setAdapter($adapter);
  286. $adapterCfg = $this->getObjectAttribute($adapter, 'config');
  287. $this->assertEquals('value1', $adapterCfg['param']);
  288. // test that adapter config value changes when we set client config
  289. $this->_client->setConfig(array('param' => 'value2'));
  290. $adapterCfg = $this->getObjectAttribute($adapter, 'config');
  291. $this->assertEquals('value2', $adapterCfg['param']);
  292. }
  293. /**
  294. * Data providers
  295. */
  296. /**
  297. * Data provider of valid non-standard HTTP methods
  298. *
  299. * @return array
  300. */
  301. static public function validMethodProvider()
  302. {
  303. return array(
  304. array('OPTIONS'),
  305. array('POST'),
  306. array('DOSOMETHING'),
  307. array('PROPFIND'),
  308. array('Some_Characters'),
  309. array('X-MS-ENUMATTS')
  310. );
  311. }
  312. /**
  313. * Data provider of invalid HTTP methods
  314. *
  315. * @return array
  316. */
  317. static public function invalidMethodProvider()
  318. {
  319. return array(
  320. array('N@5TYM3T#0D'),
  321. array('TWO WORDS'),
  322. array('GET http://foo.com/?'),
  323. array("Injected\nnewline")
  324. );
  325. }
  326. }