OauthTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_Oauth
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 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 'Zend/Oauth.php';
  23. class Test_Http_Client_19485876 extends Zend_Http_Client {}
  24. /**
  25. * @category Zend
  26. * @package Zend_Oauth
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Oauth
  31. */
  32. class Zend_OauthTest extends PHPUnit_Framework_TestCase
  33. {
  34. public function teardown()
  35. {
  36. Zend_Oauth::clearHttpClient();
  37. }
  38. public function testCanSetCustomHttpClient()
  39. {
  40. Zend_Oauth::setHttpClient(new Test_Http_Client_19485876());
  41. $this->assertTrue(Zend_Oauth::getHttpClient() instanceof Test_Http_Client_19485876);
  42. }
  43. public function testGetHttpClientResetsParameters()
  44. {
  45. $client = new Test_Http_Client_19485876();
  46. $client->setParameterGet(array('key'=>'value'));
  47. Zend_Oauth::setHttpClient($client);
  48. $resetClient = Zend_Oauth::getHttpClient();
  49. $resetClient->setUri('http://www.example.com');
  50. $this->assertEquals('http://www.example.com:80', $resetClient->getUri(true));
  51. }
  52. public function testGetHttpClientResetsAuthorizationHeader()
  53. {
  54. $client = new Test_Http_Client_19485876();
  55. $client->setHeaders('Authorization', 'realm="http://www.example.com",oauth_version="1.0"');
  56. Zend_Oauth::setHttpClient($client);
  57. $resetClient = Zend_Oauth::getHttpClient();
  58. $this->assertEquals(null, $resetClient->getHeader('Authorization'));
  59. }
  60. /**
  61. * @group ZF-10182
  62. * @dataProvider providerOauthClientOauthOptions
  63. */
  64. public function testOauthClientOauthOptionsInConstructor($oauthOptions)
  65. {
  66. require_once 'Zend/Oauth/Client.php';
  67. $client = new Zend_Oauth_Client($oauthOptions);
  68. $this->assertEquals('GET', $client->getRequestMethod());
  69. $this->assertEquals('http://www.example.com', $client->getSiteUrl());
  70. }
  71. /**
  72. * @group ZF-10182
  73. * @dataProvider providerOauthClientConfigHttpClient
  74. */
  75. public function testOauthClientConfigHttpClientInConstructor($configHttpClient, $expected)
  76. {
  77. require_once 'Zend/Oauth/Client.php';
  78. $client = new Zend_Oauth_Client(null, null, $configHttpClient);
  79. $config = $client->getAdapter()->getConfig();
  80. $this->assertEquals($expected['rfc'], $config['rfc3986_strict']);
  81. $this->assertEquals($expected['useragent'], $config['useragent']);
  82. $this->assertEquals($expected['timeout'], $config['timeout']);
  83. }
  84. public function providerOauthClientOauthOptions()
  85. {
  86. $options = array(
  87. 'requestMethod' => 'GET',
  88. 'siteUrl' => 'http://www.example.com'
  89. );
  90. require_once 'Zend/Config.php';
  91. return array(
  92. array($options),
  93. array(new Zend_Config($options))
  94. );
  95. }
  96. public function providerOauthClientConfigHttpClient()
  97. {
  98. return array(
  99. array(
  100. array('adapter' => 'Zend_Http_Client_Adapter_Test'),
  101. array('rfc' => true,
  102. 'timeout' => 10,
  103. 'useragent' => 'Zend_Http_Client'
  104. )
  105. ),
  106. array(
  107. new Zend_Config(array('adapter' => 'Zend_Http_Client_Adapter_Test')),
  108. array('rfc' => true,
  109. 'timeout' => 10,
  110. 'useragent' => 'Zend_Http_Client'
  111. )
  112. ),
  113. array(
  114. new Zend_Config(array(
  115. 'adapter' => 'Zend_Http_Client_Adapter_Test',
  116. 'rfc3986_strict' => false,
  117. 'timeout' => 100,
  118. 'useragent' => 'Zend_Http_ClientCustom'
  119. )),
  120. array('rfc' => false,
  121. 'timeout' => 100,
  122. 'useragent' => 'Zend_Http_ClientCustom'
  123. )
  124. ),
  125. array(
  126. null,
  127. array('rfc' => true,
  128. 'timeout' => 10,
  129. 'useragent' => 'Zend_Http_Client'
  130. )
  131. ),
  132. );
  133. }
  134. /**
  135. * @group ZF-10851
  136. */
  137. public function testOauthClientAcceptsRealmConfigurationOption()
  138. {
  139. $options = array(
  140. 'realm' => 'http://www.example.com'
  141. );
  142. require_once 'Zend/Oauth/Client.php';
  143. $client = new Zend_Oauth_Client($options);
  144. $this->assertEquals('http://www.example.com', $client->getRealm());
  145. }
  146. /**
  147. * @group ZF-10851
  148. */
  149. public function testOauthClientPreparationWithRealmConfigurationOption()
  150. {
  151. require_once "Zend/Oauth/Token/Access.php";
  152. $options = array(
  153. 'requestMethod' => 'GET',
  154. 'siteUrl' => 'http://www.example.com',
  155. 'realm' => 'someRealm'
  156. );
  157. $token = new Zend_Oauth_Token_Access();
  158. require_once 'Zend/Oauth/Client.php';
  159. $client = new Zend_Oauth_Client($options);
  160. $this->assertEquals(NULL,$client->getHeader('Authorization'));
  161. $client->setToken($token);
  162. $client->setUri('http://oauth.example.com');
  163. $client->prepareOauth();
  164. $this->assertNotContains('realm=""',$client->getHeader('Authorization'));
  165. $this->assertContains('realm="someRealm"',$client->getHeader('Authorization'));
  166. }
  167. /**
  168. * @group ZF-11663
  169. */
  170. public function testOauthClientAcceptsGetParametersThroughSetter()
  171. {
  172. require_once "Zend/Oauth/Token/Access.php";
  173. $token = new Zend_Oauth_Token_Access();
  174. $options = array(
  175. 'requestMethod' => 'GET',
  176. 'requestScheme' => Zend_Oauth::REQUEST_SCHEME_QUERYSTRING,
  177. 'realm' => 'someRealm'
  178. );
  179. require_once 'Zend/Oauth/Client.php';
  180. $client = new Zend_Oauth_Client($options);
  181. $client->setToken($token);
  182. $client->setUri('http://www.example.com/?test=FooBar');
  183. $queryString = $client->getUri()->getQuery();
  184. // Check that query string was set properly
  185. $this->assertSame('test=FooBar', $queryString);
  186. // Change the GET parameters
  187. $client->setParameterGet('test', 'FooBaz');
  188. $client->setParameterGet('second', 'TestTest');
  189. // Prepare the OAuth request
  190. $client->prepareOauth();
  191. $queryString = $client->getUri()->getQuery();
  192. // Ensure that parameter 'test' is unchanged, as URI parameters
  193. // should take precedence over ones set with setParameterGet
  194. $this->assertContains('test=FooBar', $queryString);
  195. // Ensure that new parameter was added
  196. $this->assertContains('second=TestTest', $queryString);
  197. }
  198. }