StaticTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. * Test that when the encodecookies flag is set to FALSE, cookies captured
  257. * from a response by Zend_Http_CookieJar are not encoded
  258. *
  259. * @group ZF-1850
  260. */
  261. public function testCaptureCookiesNoEncodeZF1850()
  262. {
  263. $cookieName = "cookieWithSpecialChars";
  264. $cookieValue = "HID=XXXXXX&UN=XXXXXXX&UID=XXXXX";
  265. $adapter = new Zend_Http_Client_Adapter_Test();
  266. $adapter->setResponse(
  267. "HTTP/1.0 200 OK\r\n" .
  268. "Content-type: text/plain\r\n" .
  269. "Content-length: 2\r\n" .
  270. "Connection: close\r\n" .
  271. "Set-Cookie: $cookieName=$cookieValue; path=/\r\n" .
  272. "\r\n" .
  273. "OK"
  274. );
  275. $this->_client->setUri('http://example.example/test');
  276. $this->_client->setConfig(array(
  277. 'adapter' => $adapter,
  278. 'encodecookies' => false
  279. ));
  280. $this->_client->setCookieJar();
  281. // First request is expected to set the cookie
  282. $this->_client->request();
  283. // Next request should contain the cookie
  284. $this->_client->request();
  285. $request = $this->_client->getLastRequest();
  286. if (! preg_match("/^Cookie: $cookieName=([^;]+)/m", $request, $match)) {
  287. $this->fail("Could not find cookie in request");
  288. }
  289. $this->assertEquals($cookieValue, $match[1]);
  290. }
  291. /**
  292. * Configuration Handling
  293. */
  294. /**
  295. * Test that we can set a valid configuration array with some options
  296. *
  297. */
  298. public function testConfigSetAsArray()
  299. {
  300. $config = array(
  301. 'timeout' => 500,
  302. 'someoption' => 'hasvalue'
  303. );
  304. $this->_client->setConfig($config);
  305. $hasConfig = $this->_client->config;
  306. foreach($config as $k => $v) {
  307. $this->assertEquals($v, $hasConfig[$k]);
  308. }
  309. }
  310. /**
  311. * Test that a Zend_Config object can be used to set configuration
  312. *
  313. * @link http://framework.zend.com/issues/browse/ZF-5577
  314. */
  315. public function testConfigSetAsZendConfig()
  316. {
  317. require_once 'Zend/Config.php';
  318. $config = new Zend_Config(array(
  319. 'timeout' => 400,
  320. 'nested' => array(
  321. 'item' => 'value',
  322. )
  323. ));
  324. $this->_client->setConfig($config);
  325. $hasConfig = $this->_client->config;
  326. $this->assertEquals($config->timeout, $hasConfig['timeout']);
  327. $this->assertEquals($config->nested->item, $hasConfig['nested']['item']);
  328. }
  329. /**
  330. * Test that passing invalid variables to setConfig() causes an exception
  331. *
  332. * @dataProvider invalidConfigProvider
  333. * @expectedException Zend_Http_Client_Exception
  334. */
  335. public function testConfigSetInvalid($config)
  336. {
  337. $this->_client->setConfig($config);
  338. }
  339. /**
  340. * Test that configuration options are passed to the adapter after the
  341. * adapter is instantiated
  342. *
  343. * @link http://framework.zend.com/issues/browse/ZF-4557
  344. */
  345. public function testConfigPassToAdapterZF4557()
  346. {
  347. $adapter = new Zend_Http_Client_StaticTest_TestAdapter_Mock();
  348. // test that config passes when we set the adapter
  349. $this->_client->setConfig(array('param' => 'value1'));
  350. $this->_client->setAdapter($adapter);
  351. $adapterCfg = $adapter->config;
  352. $this->assertEquals('value1', $adapterCfg['param']);
  353. // test that adapter config value changes when we set client config
  354. $this->_client->setConfig(array('param' => 'value2'));
  355. $adapterCfg = $adapter->config;
  356. $this->assertEquals('value2', $adapterCfg['param']);
  357. }
  358. /**
  359. * Other Tests
  360. */
  361. /**
  362. * Test the getLastResponse() method actually returns the last response
  363. *
  364. */
  365. public function testGetLastResponse()
  366. {
  367. // First, make sure we get null before the request
  368. $this->assertEquals(null, $this->_client->getLastResponse(),
  369. 'getLastResponse() is still expected to return null');
  370. // Now, test we get a proper response after the request
  371. $this->_client->setUri('http://example.com/foo/bar');
  372. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  373. $response = $this->_client->request();
  374. $this->assertTrue(($response === $this->_client->getLastResponse()),
  375. 'Response is expected to be identical to the result of getLastResponse()');
  376. }
  377. /**
  378. * Test that getLastResponse returns null when not storing
  379. *
  380. */
  381. public function testGetLastResponseWhenNotStoring()
  382. {
  383. // Now, test we get a proper response after the request
  384. $this->_client->setUri('http://example.com/foo/bar');
  385. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  386. $this->_client->setConfig(array('storeresponse' => false));
  387. $response = $this->_client->request();
  388. $this->assertNull($this->_client->getLastResponse(),
  389. 'getLastResponse is expected to be null when not storing');
  390. }
  391. /**
  392. * Check we get an exception when trying to send a POST request with an
  393. * invalid content-type header
  394. *
  395. * @expectedException Zend_Http_Client_Exception
  396. */
  397. public function testInvalidPostContentType()
  398. {
  399. $this->_client->setEncType('x-foo/something-fake');
  400. $this->_client->setParameterPost('parameter', 'value');
  401. // This should throw an exception
  402. $this->_client->request('POST');
  403. }
  404. /**
  405. * Check we get an exception if there's an error in the socket
  406. *
  407. * @expectedException Zend_Http_Client_Adapter_Exception
  408. */
  409. public function testSocketErrorException()
  410. {
  411. // Try to connect to an invalid host
  412. $this->_client->setUri('http://255.255.255.255');
  413. // Reduce timeout to 3 seconds to avoid waiting
  414. $this->_client->setConfig(array('timeout' => 3));
  415. // This call should cause an exception
  416. $this->_client->request();
  417. }
  418. /**
  419. * Check that we can set methods which are not documented in the RFC.
  420. *
  421. * @dataProvider validMethodProvider
  422. */
  423. public function testSettingExtendedMethod($method)
  424. {
  425. try {
  426. $this->_client->setMethod($method);
  427. } catch (Exception $e) {
  428. $this->fail("An unexpected exception was thrown when setting request method to '{$method}'");
  429. }
  430. }
  431. /**
  432. * Check that an exception is thrown if non-word characters are used in
  433. * the request method.
  434. *
  435. * @dataProvider invalidMethodProvider
  436. * @expectedException Zend_Http_Client_Exception
  437. */
  438. public function testSettingInvalidMethodThrowsException($method)
  439. {
  440. $this->_client->setMethod($method);
  441. }
  442. /**
  443. * Test that POST data with mutli-dimentional array is properly encoded as
  444. * multipart/form-data
  445. *
  446. */
  447. public function testFormDataEncodingWithMultiArrayZF7038()
  448. {
  449. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  450. $this->_client->setUri('http://example.com');
  451. $this->_client->setEncType(Zend_Http_Client::ENC_FORMDATA);
  452. $this->_client->setParameterPost('test', array(
  453. 'v0.1',
  454. 'v0.2',
  455. 'k1' => 'v1.0',
  456. 'k2' => array(
  457. 'v2.1',
  458. 'k2.1' => 'v2.1.0'
  459. )
  460. ));
  461. $this->_client->request('POST');
  462. $expectedLines = file(dirname(__FILE__) . '/_files/ZF7038-multipartarrayrequest.txt');
  463. $gotLines = explode("\n", $this->_client->getLastRequest());
  464. $this->assertEquals(count($expectedLines), count($gotLines));
  465. while (($expected = array_shift($expectedLines)) &&
  466. ($got = array_shift($gotLines))) {
  467. $expected = trim($expected);
  468. $got = trim($got);
  469. $this->assertRegExp("/^$expected$/", $got);
  470. }
  471. }
  472. /**
  473. * Test that we properly calculate the content-length of multibyte-encoded
  474. * request body
  475. *
  476. * This may file in case that mbstring overloads the substr and strlen
  477. * functions, and the mbstring internal encoding is a multibyte encoding.
  478. *
  479. * @link http://framework.zend.com/issues/browse/ZF-2098
  480. */
  481. public function testMultibyteRawPostDataZF2098()
  482. {
  483. $this->_client->setAdapter('Zend_Http_Client_Adapter_Test');
  484. $this->_client->setUri('http://example.com');
  485. $bodyFile = dirname(__FILE__) . '/_files/ZF2098-multibytepostdata.txt';
  486. $this->_client->setRawData(file_get_contents($bodyFile), 'text/plain');
  487. $this->_client->request('POST');
  488. $request = $this->_client->getLastRequest();
  489. if (! preg_match('/^content-length:\s+(\d+)/mi', $request, $match)) {
  490. $this->fail("Unable to find content-length header in request");
  491. }
  492. $this->assertEquals(filesize($bodyFile), (int) $match[1]);
  493. }
  494. /**
  495. * @group ZF-8057
  496. */
  497. public function testSetDisabledAuthBeforSettingUriBug()
  498. {
  499. $client = new Zend_Http_Client_StaticTest_Mock();
  500. // if the bug exists this call should creates a fatal error
  501. $client->setAuth(false);
  502. }
  503. /**
  504. * Testing if the connection isn't closed
  505. *
  506. * @group ZF-9685
  507. */
  508. public function testOpenTempStreamWithValidFileDoesntThrowsException()
  509. {
  510. $url = 'http://www.example.com';
  511. $config = array (
  512. 'output_stream' => realpath(dirname(__FILE__) . '/_files/zend_http_client_stream.file'),
  513. );
  514. $client = new Zend_Http_Client($url, $config);
  515. try {
  516. $result = $client->request();
  517. } catch (Zend_Http_Client_Exception $e) {
  518. $this->fail('Unexpected exception was thrown');
  519. }
  520. // we can safely return until we can verify link is still active
  521. // @todo verify link is still active
  522. return;
  523. }
  524. /**
  525. * Testing if the connection can be closed
  526. *
  527. * @group ZF-9685
  528. */
  529. public function testOpenTempStreamWithBogusFileClosesTheConnection()
  530. {
  531. $url = 'http://www.example.com';
  532. $config = array (
  533. 'output_stream' => '/path/to/bogus/file.ext',
  534. );
  535. $client = new Zend_Http_Client($url, $config);
  536. try {
  537. $result = $client->request();
  538. $this->fail('Expected exception was not thrown');
  539. } catch (Zend_Http_Client_Exception $e) {
  540. // we return since we expect the exception
  541. return;
  542. }
  543. }
  544. /**
  545. * Data providers
  546. */
  547. /**
  548. * Data provider of valid non-standard HTTP methods
  549. *
  550. * @return array
  551. */
  552. static public function validMethodProvider()
  553. {
  554. return array(
  555. array('OPTIONS'),
  556. array('POST'),
  557. array('DOSOMETHING'),
  558. array('PROPFIND'),
  559. array('Some_Characters'),
  560. array('X-MS-ENUMATTS')
  561. );
  562. }
  563. /**
  564. * Data provider of invalid HTTP methods
  565. *
  566. * @return array
  567. */
  568. static public function invalidMethodProvider()
  569. {
  570. return array(
  571. array('N@5TYM3T#0D'),
  572. array('TWO WORDS'),
  573. array('GET http://foo.com/?'),
  574. array("Injected\nnewline")
  575. );
  576. }
  577. /**
  578. * Data provider for invalid configuration containers
  579. *
  580. * @return array
  581. */
  582. static public function invalidConfigProvider()
  583. {
  584. return array(
  585. array(false),
  586. array('foo => bar'),
  587. array(null),
  588. array(new stdClass),
  589. array(55)
  590. );
  591. }
  592. }
  593. class Zend_Http_Client_StaticTest_Mock extends Zend_Http_Client
  594. {
  595. public $config = array(
  596. 'maxredirects' => 5,
  597. 'strictredirects' => false,
  598. 'useragent' => 'Zend_Http_Client',
  599. 'timeout' => 10,
  600. 'adapter' => 'Zend_Http_Client_Adapter_Socket',
  601. 'httpversion' => self::HTTP_1,
  602. 'keepalive' => false,
  603. 'storeresponse' => true,
  604. 'strict' => true,
  605. 'output_stream' => false,
  606. 'encodecookies' => true,
  607. );
  608. }
  609. class Zend_Http_Client_StaticTest_TestAdapter_Mock extends Zend_Http_Client_Adapter_Test
  610. {
  611. public $config = array();
  612. }