StaticTest.php 14 KB

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