2
0

StaticTest.php 9.9 KB

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