PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/branches/release-1.5/tests/Zend/Http/Client/StaticTest.php

https://github.com/bhaumik25/zend-framework
PHP | 346 lines | 181 code | 43 blank | 122 comment | 0 complexity | 2e36b358bd008c8e42cc6d75aaa01efe MD5 | raw file
  1. <?php
  2. require_once 'Zend/Http/Client.php';
  3. require_once 'PHPUnit/Framework/TestCase.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. * URI Tests
  35. */
  36. /**
  37. * Test we can SET and GET a URI as string
  38. *
  39. */
  40. public function testSetGetUriString()
  41. {
  42. $uristr = 'http://www.zend.com:80/';
  43. $this->client->setUri($uristr);
  44. $uri = $this->client->getUri();
  45. $this->assertTrue($uri instanceof Zend_Uri_Http, 'Returned value is not a Uri object as expected');
  46. $this->assertEquals($uri->__toString(), $uristr, 'Returned Uri object does not hold the expected URI');
  47. $uri = $this->client->getUri(true);
  48. $this->assertTrue(is_string($uri), 'Returned value expected to be a string, ' . gettype($uri) . ' returned');
  49. $this->assertEquals($uri, $uristr, 'Returned string is not the expected URI');
  50. }
  51. /**
  52. * Test we can SET and GET a URI as object
  53. *
  54. */
  55. public function testSetGetUriObject()
  56. {
  57. $uriobj = Zend_Uri::factory('http://www.zend.com:80/');
  58. $this->client->setUri($uriobj);
  59. $uri = $this->client->getUri();
  60. $this->assertTrue($uri instanceof Zend_Uri_Http, 'Returned value is not a Uri object as expected');
  61. $this->assertEquals($uri, $uriobj, 'Returned object is not the excepted Uri object');
  62. }
  63. /**
  64. * Test that passing an invalid URI string throws an exception
  65. *
  66. */
  67. public function testInvalidUriStringException()
  68. {
  69. try {
  70. $this->client->setUri('http://__invalid__.com');
  71. $this->fail('Excepted invalid URI string exception was not thrown');
  72. } catch (Zend_Uri_Exception $e) {
  73. // We're good
  74. }
  75. }
  76. /**
  77. * Test that passing an invalid URI object throws an exception
  78. *
  79. */
  80. public function testInvalidUriObjectException()
  81. {
  82. try {
  83. $uri = Zend_Uri::factory('mailto:nobody@example.com');
  84. $this->client->setUri($uri);
  85. $this->fail('Excepted invalid URI object exception was not thrown');
  86. } catch (Zend_Http_Client_Exception $e) {
  87. // We're good
  88. } catch (Zend_Uri_Exception $e) {
  89. // URI is currently unimplemented
  90. $this->markTestIncomplete('Zend_Uri_Mailto is not implemented yet');
  91. }
  92. }
  93. /**
  94. * Header Tests
  95. */
  96. /**
  97. * Make sure an exception is thrown if an invalid header name is used
  98. *
  99. */
  100. public function testInvalidHeaderExcept()
  101. {
  102. try {
  103. $this->client->setHeaders('Ina_lid* Hea%der', 'is not good');
  104. $this->fail('Expected invalid header name exception was not thrown');
  105. } catch (Zend_Http_Client_Exception $e) {
  106. // We're good
  107. }
  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. */
  150. public function testExceptUnsupportedAuthDynamic()
  151. {
  152. try {
  153. $this->client->setAuth('shahar', '1234', 'SuperStrongAlgo');
  154. $this->fail('Trying to use unknown authentication method, setAuth should throw an exception but it didn\'t');
  155. } catch (Zend_Http_Client_Exception $e) {
  156. // We're good!
  157. }
  158. }
  159. /**
  160. * Test encodeAuthHeader (static method) fails when trying to use an
  161. * unsupported authentication scheme
  162. *
  163. */
  164. public function testExceptUnsupportedAuthStatic()
  165. {
  166. try {
  167. Zend_Http_Client::encodeAuthHeader('shahar', '1234', 'SuperStrongAlgo');
  168. $this->fail('Trying to use unknown authentication method, encodeAuthHeader should throw an exception but it didn\'t');
  169. } catch (Zend_Http_Client_Exception $e) {
  170. // We're good!
  171. }
  172. }
  173. /**
  174. * Cookie and Cookie Jar tests
  175. */
  176. /**
  177. * Test we can properly set a new cookie jar
  178. *
  179. */
  180. public function testSetNewCookieJar()
  181. {
  182. $this->client->setCookieJar();
  183. $this->client->setCookie('cookie', 'value');
  184. $this->client->setCookie('chocolate', 'chips');
  185. $jar = $this->client->getCookieJar();
  186. // Check we got the right cookiejar
  187. $this->assertTrue($jar instanceof Zend_Http_CookieJar, '$jar is not an instance of Zend_Http_CookieJar as expected');
  188. $this->assertEquals(count($jar->getAllCookies()), 2, '$jar does not contain 2 cookies as expected');
  189. }
  190. /**
  191. * Test we can properly set an existing cookie jar
  192. *
  193. */
  194. public function testSetReadyCookieJar()
  195. {
  196. $jar = new Zend_Http_CookieJar();
  197. $jar->addCookie('cookie=value', 'http://www.example.com');
  198. $jar->addCookie('chocolate=chips; path=/foo', 'http://www.example.com');
  199. $this->client->setCookieJar($jar);
  200. // Check we got the right cookiejar
  201. $this->assertEquals($jar, $this->client->getCookieJar(), '$jar is not the client\'s cookie jar as expected');
  202. }
  203. /**
  204. * Test we can unset a cookie jar
  205. *
  206. */
  207. public function testUnsetCookieJar()
  208. {
  209. // Set the cookie jar just like in testSetNewCookieJar
  210. $this->client->setCookieJar();
  211. $this->client->setCookie('cookie', 'value');
  212. $this->client->setCookie('chocolate', 'chips');
  213. $jar = $this->client->getCookieJar();
  214. // Try unsetting the cookiejar
  215. $this->client->setCookieJar(null);
  216. $this->assertNull($this->client->getCookieJar(), 'Cookie jar is expected to be null but it is not');
  217. }
  218. /**
  219. * Make sure using an invalid cookie jar object throws an exception
  220. *
  221. */
  222. public function testSetInvalidCookieJar()
  223. {
  224. try {
  225. $this->client->setCookieJar('cookiejar');
  226. $this->fail('Invalid cookiejar exception was not thrown');
  227. } catch (Exception $e) {
  228. // We're good
  229. }
  230. }
  231. /**
  232. * Other Tests
  233. */
  234. /**
  235. * Check we get an exception when trying to send a POST request with an
  236. * invalid content-type header
  237. */
  238. public function testInvalidPostContentType()
  239. {
  240. $this->client->setEncType('x-foo/something-fake');
  241. $this->client->setParameterPost('parameter', 'value');
  242. try {
  243. $this->client->request('POST');
  244. $this->fail('Building the body with an unknown content-type for POST values should have failed, it didn\'t');
  245. } catch (Zend_Http_Client_Exception $e) {
  246. // We are ok!
  247. }
  248. }
  249. /**
  250. * Check we get an exception if there's an error in the socket
  251. *
  252. */
  253. public function testSocketErrorException() {
  254. // Try to connect to an invalid host
  255. $this->client->setUri('http://255.255.255.255');
  256. // Reduce timeout to 3 seconds to avoid waiting
  257. $this->client->setConfig(array('timeout' => 3));
  258. try {
  259. $this->client->request();
  260. $this->fail('Expected connection error exception was not thrown');
  261. } catch (Zend_Http_Client_Adapter_Exception $e) {
  262. // We're good!
  263. }
  264. }
  265. /**
  266. * Check that we can set methods which are not documented in the RFC.
  267. * Also, check that an exception is thrown if non-word characters are
  268. * used in the request method.
  269. *
  270. */
  271. public function testSettingExtendedMethod()
  272. {
  273. $goodMethods = array(
  274. 'OPTIONS',
  275. 'POST',
  276. 'DOSOMETHING',
  277. 'PROPFIND',
  278. 'Some_Characters',
  279. 'X-MS-ENUMATTS'
  280. );
  281. foreach ($goodMethods as $method) {
  282. try {
  283. $this->client->setMethod($method);
  284. } catch (Exception $e) {
  285. $this->fail("An unexpected exception was thrown when setting request method to '{$method}'");
  286. }
  287. }
  288. $badMethods = array(
  289. 'N@5TYM3T#0D',
  290. 'TWO WORDS',
  291. 'GET http://foo.com/?',
  292. "Injected\nnewline"
  293. );
  294. foreach ($badMethods as $method) {
  295. try {
  296. $this->client->setMethod($method);
  297. $this->fail("A Zend_Http_Client_Exception was expected but was not thrown when setting request method to '{$method}'");
  298. } catch (Zend_Http_Client_Exception $e) {
  299. // We're ok!
  300. }
  301. }
  302. }
  303. }