StaticTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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_Http_Client
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 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. require_once realpath(dirname(__FILE__) . '/../../../') . '/TestHelper.php';
  23. require_once 'Zend/Http/Client.php';
  24. require_once 'Zend/Http/Client/Adapter/Test.php';
  25. /**
  26. * This Testsuite includes all Zend_Http_Client tests that do not rely
  27. * on performing actual requests to an HTTP server. These tests can be
  28. * executed once, and do not need to be tested with different servers /
  29. * client setups.
  30. *
  31. * @category Zend
  32. * @package Zend_Http_Client
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Http
  37. * @group Zend_Http_Client
  38. */
  39. class Zend_Http_Client_StaticTest extends PHPUnit_Framework_TestCase
  40. {
  41. /**
  42. * Common HTTP client
  43. *
  44. * @var Zend_Http_Client
  45. */
  46. protected $_client = null;
  47. /**
  48. * Set up the test suite before each test
  49. *
  50. */
  51. public function setUp()
  52. {
  53. $this->_client = new Zend_Http_Client_StaticTest_Mock('http://www.example.com');
  54. }
  55. /**
  56. * Clean up after running a test
  57. *
  58. */
  59. public function tearDown()
  60. {
  61. $this->_client = null;
  62. }
  63. /**
  64. * URI Tests
  65. */
  66. /**
  67. * Test we can SET and GET a URI as string
  68. *
  69. */
  70. public function testSetGetUriString()
  71. {
  72. $uristr = 'http://www.zend.com:80/';
  73. $this->_client->setUri($uristr);
  74. $uri = $this->_client->getUri();
  75. $this->assertTrue($uri instanceof Zend_Uri_Http, 'Returned value is not a Uri object as expected');
  76. $this->assertEquals($uri->__toString(), $uristr, 'Returned Uri object does not hold the expected URI');
  77. $uri = $this->_client->getUri(true);
  78. $this->assertTrue(is_string($uri), 'Returned value expected to be a string, ' . gettype($uri) . ' returned');
  79. $this->assertEquals($uri, $uristr, 'Returned string is not the expected URI');
  80. }
  81. /**
  82. * Test we can SET and GET a URI as object
  83. *
  84. */
  85. public function testSetGetUriObject()
  86. {
  87. $uriobj = Zend_Uri::factory('http://www.zend.com:80/');
  88. $this->_client->setUri($uriobj);
  89. $uri = $this->_client->getUri();
  90. $this->assertTrue($uri instanceof Zend_Uri_Http, 'Returned value is not a Uri object as expected');
  91. $this->assertEquals($uri, $uriobj, 'Returned object is not the excepted Uri object');
  92. }
  93. /**
  94. * Test that passing an invalid URI string throws an exception
  95. *
  96. * @expectedException Zend_Uri_Exception
  97. */
  98. public function testInvalidUriStringException()
  99. {
  100. $this->_client->setUri('httpp://__invalid__.com');
  101. }
  102. /**
  103. * Test that passing an invalid URI object throws an exception
  104. *
  105. */
  106. public function testInvalidUriObjectException()
  107. {
  108. try {
  109. $uri = Zend_Uri::factory('mailto:nobody@example.com');
  110. $this->_client->setUri($uri);
  111. $this->fail('Excepted invalid URI object exception was not thrown');
  112. } catch (Zend_Http_Client_Exception $e) {
  113. // We're good
  114. } catch (Zend_Uri_Exception $e) {
  115. // URI is currently unimplemented
  116. $this->markTestIncomplete('Zend_Uri_Mailto is not implemented yet');
  117. }
  118. }
  119. /**
  120. * Test that setting the same parameter twice in the query string does not
  121. * get reduced to a single value only.
  122. *
  123. */
  124. public function testDoubleGetParameter()
  125. {
  126. $qstr = 'foo=bar&foo=baz';
  127. $this->_client->setUri('http://example.com/test/?' . $qstr);
  128. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  129. $res = $this->_client->request('GET');
  130. $this->assertContains($qstr, $this->_client->getLastRequest(),
  131. 'Request is expected to contain the entire query string');
  132. }
  133. /**
  134. * Header Tests
  135. */
  136. /**
  137. * Make sure an exception is thrown if an invalid header name is used
  138. *
  139. * @expectedException Zend_Http_Client_Exception
  140. */
  141. public function testInvalidHeaderExcept()
  142. {
  143. $this->_client->setHeaders('Ina_lid* Hea%der', 'is not good');
  144. }
  145. /**
  146. * Make sure non-strict mode disables header name validation
  147. *
  148. */
  149. public function testInvalidHeaderNonStrictMode()
  150. {
  151. // Disable strict validation
  152. $this->_client->setConfig(array('strict' => false));
  153. try {
  154. $this->_client->setHeaders('Ina_lid* Hea%der', 'is not good');
  155. } catch (Zend_Http_Client_Exception $e) {
  156. $this->fail('Invalid header names should be allowed in non-strict mode');
  157. }
  158. }
  159. /**
  160. * Test we can get already set headers
  161. *
  162. */
  163. public function testGetHeader()
  164. {
  165. $this->_client->setHeaders(array(
  166. 'Accept-encoding' => 'gzip,deflate',
  167. 'Accept-language' => 'en,de,*',
  168. ));
  169. $this->assertEquals($this->_client->getHeader('Accept-encoding'), 'gzip,deflate', 'Returned value of header is not as expected');
  170. $this->assertEquals($this->_client->getHeader('X-Fake-Header'), null, 'Non-existing header should not return a value');
  171. }
  172. public function testUnsetHeader()
  173. {
  174. $this->_client->setHeaders('Accept-Encoding', 'gzip,deflate');
  175. $this->_client->setHeaders('Accept-Encoding', null);
  176. $this->assertNull($this->_client->getHeader('Accept-encoding'), 'Returned value of header is expected to be null');
  177. }
  178. /**
  179. * Authentication tests
  180. */
  181. /**
  182. * Test setAuth (dynamic method) fails when trying to use an unsupported
  183. * authentication scheme
  184. *
  185. * @expectedException Zend_Http_Client_Exception
  186. */
  187. public function testExceptUnsupportedAuthDynamic()
  188. {
  189. $this->_client->setAuth('shahar', '1234', 'SuperStrongAlgo');
  190. }
  191. /**
  192. * Test encodeAuthHeader (static method) fails when trying to use an
  193. * unsupported authentication scheme
  194. *
  195. * @expectedException Zend_Http_Client_Exception
  196. */
  197. public function testExceptUnsupportedAuthStatic()
  198. {
  199. Zend_Http_Client::encodeAuthHeader('shahar', '1234', 'SuperStrongAlgo');
  200. }
  201. /**
  202. * Cookie and Cookie Jar tests
  203. */
  204. /**
  205. * Test we can properly set a new cookie jar
  206. *
  207. */
  208. public function testSetNewCookieJar()
  209. {
  210. $this->_client->setCookieJar();
  211. $this->_client->setCookie('cookie', 'value');
  212. $this->_client->setCookie('chocolate', 'chips');
  213. $jar = $this->_client->getCookieJar();
  214. // Check we got the right cookiejar
  215. $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of Zend_Http_CookieJar as expected');
  216. $this->assertEquals(count($jar->getAllCookies()), 2, '$jar does not contain 2 cookies as expected');
  217. }
  218. /**
  219. * Test we can properly set an existing cookie jar
  220. *
  221. */
  222. public function testSetReadyCookieJar()
  223. {
  224. $jar = new Zend_Http_CookieJar();
  225. $jar->addCookie('cookie=value', 'http://www.example.com');
  226. $jar->addCookie('chocolate=chips; path=/foo', 'http://www.example.com');
  227. $this->_client->setCookieJar($jar);
  228. // Check we got the right cookiejar
  229. $this->assertEquals($jar, $this->_client->getCookieJar(), '$jar is not the client\'s cookie jar as expected');
  230. }
  231. /**
  232. * Test we can unset a cookie jar
  233. *
  234. */
  235. public function testUnsetCookieJar()
  236. {
  237. // Set the cookie jar just like in testSetNewCookieJar
  238. $this->_client->setCookieJar();
  239. $this->_client->setCookie('cookie', 'value');
  240. $this->_client->setCookie('chocolate', 'chips');
  241. $jar = $this->_client->getCookieJar();
  242. // Try unsetting the cookiejar
  243. $this->_client->setCookieJar(null);
  244. $this->assertNull($this->_client->getCookieJar(), 'Cookie jar is expected to be null but it is not');
  245. }
  246. /**
  247. * Make sure using an invalid cookie jar object throws an exception
  248. *
  249. * @expectedException Zend_Http_Client_Exception
  250. */
  251. public function testSetInvalidCookieJar()
  252. {
  253. $this->_client->setCookieJar('cookiejar');
  254. }
  255. /**
  256. * Configuration Handling
  257. */
  258. /**
  259. * Test that we can set a valid configuration array with some options
  260. *
  261. */
  262. public function testConfigSetAsArray()
  263. {
  264. $config = array(
  265. 'timeout' => 500,
  266. 'someoption' => 'hasvalue'
  267. );
  268. $this->_client->setConfig($config);
  269. $hasConfig = $this->_client->config;
  270. foreach($config as $k => $v) {
  271. $this->assertEquals($v, $hasConfig[$k]);
  272. }
  273. }
  274. /**
  275. * Test that a Zend_Config object can be used to set configuration
  276. *
  277. * @link http://framework.zend.com/issues/browse/ZF-5577
  278. */
  279. public function testConfigSetAsZendConfig()
  280. {
  281. require_once 'Zend/Config.php';
  282. $config = new Zend_Config(array(
  283. 'timeout' => 400,
  284. 'nested' => array(
  285. 'item' => 'value',
  286. )
  287. ));
  288. $this->_client->setConfig($config);
  289. $hasConfig = $this->_client->config;
  290. $this->assertEquals($config->timeout, $hasConfig['timeout']);
  291. $this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
  292. }
  293. /**
  294. * Test that passing invalid variables to setConfig() causes an exception
  295. *
  296. * @dataProvider invalidConfigProvider
  297. * @expectedException Zend_Http_Client_Exception
  298. */
  299. public function testConfigSetInvalid($config)
  300. {
  301. $this->_client->setConfig($config);
  302. }
  303. /**
  304. * Test that configuration options are passed to the adapter after the
  305. * adapter is instantiated
  306. *
  307. * @link http://framework.zend.com/issues/browse/ZF-4557
  308. */
  309. public function testConfigPassToAdapterZF4557()
  310. {
  311. $adapter = new Zend_Http_Client_StaticTest_TestAdapter_Mock();
  312. // test that config passes when we set the adapter
  313. $this->_client->setConfig(array('param' => 'value1'));
  314. $this->_client->setAdapter($adapter);
  315. $adapterCfg = $adapter->config;
  316. $this->assertEquals('value1', $adapterCfg['param']);
  317. // test that adapter config value changes when we set client config
  318. $this->_client->setConfig(array('param' => 'value2'));
  319. $adapterCfg = $adapter->config;
  320. $this->assertEquals('value2', $adapterCfg['param']);
  321. }
  322. /**
  323. * Other Tests
  324. */
  325. /**
  326. * Test the getLastResponse() method actually returns the last response
  327. *
  328. */
  329. public function testGetLastResponse()
  330. {
  331. // First, make sure we get null before the request
  332. $this->assertEquals(null, $this->_client->getLastResponse(),
  333. 'getLastResponse() is still expected to return null');
  334. // Now, test we get a proper response after the request
  335. $this->_client->setUri('http://example.com/foo/bar');
  336. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  337. $response = $this->_client->request();
  338. $this->assertTrue(($response === $this->_client->getLastResponse()),
  339. 'Response is expected to be identical to the result of getLastResponse()');
  340. }
  341. /**
  342. * Test that getLastResponse returns null when not storing
  343. *
  344. */
  345. public function testGetLastResponseWhenNotStoring()
  346. {
  347. // Now, test we get a proper response after the request
  348. $this->_client->setUri('http://example.com/foo/bar');
  349. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  350. $this->_client->setConfig(array('storeresponse' => false));
  351. $response = $this->_client->request();
  352. $this->assertNull($this->_client->getLastResponse(),
  353. 'getLastResponse is expected to be null when not storing');
  354. }
  355. /**
  356. * Check we get an exception when trying to send a POST request with an
  357. * invalid content-type header
  358. *
  359. * @expectedException Zend_Http_Client_Exception
  360. */
  361. public function testInvalidPostContentType()
  362. {
  363. $this->_client->setEncType('x-foo/something-fake');
  364. $this->_client->setParameterPost('parameter', 'value');
  365. // This should throw an exception
  366. $this->_client->request('POST');
  367. }
  368. /**
  369. * Check we get an exception if there's an error in the socket
  370. *
  371. * @expectedException Zend_Http_Client_Adapter_Exception
  372. */
  373. public function testSocketErrorException()
  374. {
  375. // Try to connect to an invalid host
  376. $this->_client->setUri('http://255.255.255.255');
  377. // Reduce timeout to 3 seconds to avoid waiting
  378. $this->_client->setConfig(array('timeout' => 3));
  379. // This call should cause an exception
  380. $this->_client->request();
  381. }
  382. /**
  383. * Check that we can set methods which are not documented in the RFC.
  384. *
  385. * @dataProvider validMethodProvider
  386. */
  387. public function testSettingExtendedMethod($method)
  388. {
  389. try {
  390. $this->_client->setMethod($method);
  391. } catch (Exception $e) {
  392. $this->fail("An unexpected exception was thrown when setting request method to '{$method}'");
  393. }
  394. }
  395. /**
  396. * Check that an exception is thrown if non-word characters are used in
  397. * the request method.
  398. *
  399. * @dataProvider invalidMethodProvider
  400. * @expectedException Zend_Http_Client_Exception
  401. */
  402. public function testSettingInvalidMethodThrowsException($method)
  403. {
  404. $this->_client->setMethod($method);
  405. }
  406. /**
  407. * Test that POST data with mutli-dimentional array is properly encoded as
  408. * multipart/form-data
  409. *
  410. */
  411. public function testFormDataEncodingWithMultiArrayZF7038()
  412. {
  413. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  414. $this->_client->setUri('http://example.com');
  415. $this->_client->setEncType(Zend_Http_Client::ENC_FORMDATA);
  416. $this->_client->setParameterPost('test', array(
  417. 'v0.1',
  418. 'v0.2',
  419. 'k1' => 'v1.0',
  420. 'k2' => array(
  421. 'v2.1',
  422. 'k2.1' => 'v2.1.0'
  423. )
  424. ));
  425. $this->_client->request('POST');
  426. $expectedLines = file(dirname(__FILE__) . '/_files/ZF7038-multipartarrayrequest.txt');
  427. $gotLines = explode("\n", $this->_client->getLastRequest());
  428. $this->assertEquals(count($expectedLines), count($gotLines));
  429. while (($expected = array_shift($expectedLines)) &&
  430. ($got = array_shift($gotLines))) {
  431. $expected = trim($expected);
  432. $got = trim($got);
  433. $this->assertRegExp("/^$expected$/", $got);
  434. }
  435. }
  436. /**
  437. * Test that we properly calculate the content-length of multibyte-encoded
  438. * request body
  439. *
  440. * This may file in case that mbstring overloads the substr and strlen
  441. * functions, and the mbstring internal encoding is a multibyte encoding.
  442. *
  443. * @link http://framework.zend.com/issues/browse/ZF-2098
  444. */
  445. public function testMultibyteRawPostDataZF2098()
  446. {
  447. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  448. $this->_client->setUri('http://example.com');
  449. $bodyFile = dirname(__FILE__) . '/_files/ZF2098-multibytepostdata.txt';
  450. $this->_client->setRawData(file_get_contents($bodyFile), 'text/plain');
  451. $this->_client->request('POST');
  452. $request = $this->_client->getLastRequest();
  453. if (! preg_match('/^content-length:\s+(\d+)/mi', $request, $match)) {
  454. $this->fail("Unable to find content-length header in request");
  455. }
  456. $this->assertEquals(filesize($bodyFile), (int) $match[1]);
  457. }
  458. /**
  459. * @group ZF-8057
  460. */
  461. public function testSetDisabledAuthBeforSettingUriBug()
  462. {
  463. $client = new Zend_Http_Client_StaticTest_Mock();
  464. // if the bug exists this call should creates a fatal error
  465. $client->setAuth(false);
  466. }
  467. /**
  468. * Testing if the connection isn't closed
  469. *
  470. * @group ZF-9685
  471. */
  472. public function testOpenTempStreamWithValidFileDoesntThrowsException()
  473. {
  474. $url = 'http://www.example.com';
  475. $config = array (
  476. 'output_stream' => realpath(dirname(__FILE__) . '/_files/zend_http_client_stream.file'),
  477. );
  478. $client = new Zend_Http_Client($url, $config);
  479. try {
  480. $result = $client->request();
  481. } catch (Zend_Http_Client_Exception $e) {
  482. $this->fail('Unexpected exception was thrown');
  483. }
  484. // we can safely return until we can verify link is still active
  485. // @todo verify link is still active
  486. return;
  487. }
  488. /**
  489. * Testing if the connection can be closed
  490. *
  491. * @group ZF-9685
  492. */
  493. public function testOpenTempStreamWithBogusFileClosesTheConnection()
  494. {
  495. $url = 'http://www.example.com';
  496. $config = array (
  497. 'output_stream' => '/path/to/bogus/file.ext',
  498. );
  499. $client = new Zend_Http_Client($url, $config);
  500. try {
  501. $result = $client->request();
  502. $this->fail('Expected exception was not thrown');
  503. } catch (Zend_Http_Client_Exception $e) {
  504. // we return since we expect the exception
  505. return;
  506. }
  507. }
  508. /**
  509. * Data providers
  510. */
  511. /**
  512. * Data provider of valid non-standard HTTP methods
  513. *
  514. * @return array
  515. */
  516. static public function validMethodProvider()
  517. {
  518. return array(
  519. array('OPTIONS'),
  520. array('POST'),
  521. array('DOSOMETHING'),
  522. array('PROPFIND'),
  523. array('Some_Characters'),
  524. array('X-MS-ENUMATTS')
  525. );
  526. }
  527. /**
  528. * Data provider of invalid HTTP methods
  529. *
  530. * @return array
  531. */
  532. static public function invalidMethodProvider()
  533. {
  534. return array(
  535. array('N@5TYM3T#0D'),
  536. array('TWO WORDS'),
  537. array('GET http://foo.com/?'),
  538. array("Injected\nnewline")
  539. );
  540. }
  541. /**
  542. * Data provider for invalid configuration containers
  543. *
  544. * @return array
  545. */
  546. static public function invalidConfigProvider()
  547. {
  548. return array(
  549. array(false),
  550. array('foo => bar'),
  551. array(null),
  552. array(new stdClass),
  553. array(55)
  554. );
  555. }
  556. }
  557. class Zend_Http_Client_StaticTest_Mock extends Zend_Http_Client
  558. {
  559. public $config = array(
  560. 'maxredirects' => 5,
  561. 'strictredirects' => false,
  562. 'useragent' => 'Zend_Http_Client',
  563. 'timeout' => 10,
  564. 'adapter' => 'Zend_Http_Client_Adapter_Socket',
  565. 'httpversion' => self::HTTP_1,
  566. 'keepalive' => false,
  567. 'storeresponse' => true,
  568. 'strict' => true,
  569. 'output_stream' => false,
  570. 'encodecookies' => true,
  571. );
  572. }
  573. class Zend_Http_Client_StaticTest_TestAdapter_Mock extends Zend_Http_Client_Adapter_Test
  574. {
  575. public $config = array();
  576. }