RpcRequestTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Request;
  3. use ReflectionMethod;
  4. use ReflectionObject;
  5. use RuntimeException;
  6. use ReflectionException;
  7. use PHPUnit\Framework\TestCase;
  8. use AlibabaCloud\Client\Support\Sign;
  9. use AlibabaCloud\Client\AlibabaCloud;
  10. use AlibabaCloud\Client\Request\RpcRequest;
  11. use AlibabaCloud\Client\Credentials\StsCredential;
  12. use AlibabaCloud\Client\Exception\ClientException;
  13. use AlibabaCloud\Client\Credentials\CredentialsInterface;
  14. use AlibabaCloud\Client\Credentials\BearerTokenCredential;
  15. /**
  16. * Class RpcRequestTest
  17. *
  18. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Request
  19. *
  20. * @coversDefaultClass \AlibabaCloud\Client\Request\RpcRequest
  21. */
  22. class RpcRequestTest extends TestCase
  23. {
  24. /**
  25. * @return array
  26. */
  27. public static function boolToString()
  28. {
  29. return [
  30. ['true', 'true'],
  31. ['false', 'false'],
  32. [true, 'true'],
  33. [false, 'false'],
  34. ['string', 'string'],
  35. [1, 1],
  36. [null, null],
  37. ];
  38. }
  39. /**
  40. * @param $value
  41. * @param $expected
  42. *
  43. * @throws ReflectionException
  44. * @throws ClientException
  45. * @dataProvider boolToString
  46. */
  47. public function testBoolToString($value, $expected)
  48. {
  49. // Setup
  50. $request = new RpcRequest();
  51. // Test
  52. $method = new ReflectionMethod(
  53. RpcRequest::class,
  54. 'boolToString'
  55. );
  56. $method->setAccessible(true);
  57. $actual = $method->invokeArgs($request, [$value]);
  58. // Assert
  59. self::assertEquals($expected, $actual);
  60. }
  61. /**
  62. * @param CredentialsInterface $credential
  63. *
  64. * @throws ReflectionException
  65. * @throws ClientException
  66. * @dataProvider resolveQuery
  67. */
  68. public function testResolveCommonParameters($credential)
  69. {
  70. // Setup
  71. AlibabaCloud::bearerTokenClient('token')->name('token');
  72. $request = new RpcRequest();
  73. $request->client('token');
  74. $request->regionId('cn-hangzhou');
  75. $request->options(
  76. [
  77. 'query' => [
  78. 'A' => 'A',
  79. ],
  80. ]
  81. );
  82. // Test
  83. $method = new ReflectionMethod(
  84. RpcRequest::class,
  85. 'resolveCommonParameters'
  86. );
  87. $method->setAccessible(true);
  88. $method->invokeArgs($request, [$credential]);
  89. // Assert
  90. self::assertInternalType('array', $request->options['query']);
  91. self::assertEquals('A', $request->options['query']['A']);
  92. }
  93. /**
  94. * @return array
  95. * @throws ClientException
  96. */
  97. public function resolveQuery()
  98. {
  99. return [
  100. [
  101. new StsCredential('foo', 'bar', 'token'),
  102. ],
  103. [
  104. new BearerTokenCredential('token'),
  105. ],
  106. ];
  107. }
  108. /**
  109. * @param CredentialsInterface $credential
  110. *
  111. * @throws ReflectionException
  112. * @throws ClientException
  113. * @dataProvider resolveQuery
  114. */
  115. public function testResolveParameters($credential)
  116. {
  117. // Setup
  118. AlibabaCloud::bearerTokenClient('token')->name('token');
  119. $request = new RpcRequest();
  120. $request->client('token');
  121. $request->regionId('cn-hangzhou');
  122. $request->method('post');
  123. $request->options(
  124. [
  125. 'query' => [
  126. 'A' => 'A',
  127. ],
  128. ]
  129. );
  130. // Test
  131. $method = new ReflectionMethod(
  132. RpcRequest::class,
  133. 'resolveParameter'
  134. );
  135. $method->setAccessible(true);
  136. $method->invokeArgs($request, [$credential]);
  137. // Assert
  138. self::assertFalse(isset($request->options['query']));
  139. self::assertEquals('http://localhost', (string)$request->uri);
  140. }
  141. /**
  142. * @param $value
  143. * @param $expected
  144. *
  145. * @throws ReflectionException
  146. * @throws ClientException
  147. * @dataProvider percentEncode
  148. */
  149. public function testPercentEncode($value, $expected)
  150. {
  151. // Setup
  152. $request = new RpcRequest();
  153. // Test
  154. $method = new ReflectionMethod(
  155. Sign::class,
  156. 'percentEncode'
  157. );
  158. $method->setAccessible(true);
  159. $actual = $method->invokeArgs($request, [$value]);
  160. // Assert
  161. self::assertEquals($expected, $actual);
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function percentEncode()
  167. {
  168. return [
  169. ['2018-12-26T05:36:28Z', '2018-12-26T05%3A36%3A28Z'],
  170. [
  171. 'AccessKeyId=LTAIfoSsg3EaQoip&Action=ListAccessKeys&Format=JSON&RegionId=cn-hangzhou&SignatureMethod=HMAC-SHA1&SignatureNonce=4b22e3fd9f3746ea487b298800b44572&SignatureVersion=1.0&Timestamp=2018-12-26T05%3A36%3A26Z&UserName=1545802586&Version=2015-05-01',
  172. 'AccessKeyId%3DLTAIfoSsg3EaQoip%26Action%3DListAccessKeys%26Format%3DJSON%26RegionId%3Dcn-hangzhou%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D4b22e3fd9f3746ea487b298800b44572%26SignatureVersion%3D1.0%26Timestamp%3D2018-12-26T05%253A36%253A26Z%26UserName%3D1545802586%26Version%3D2015-05-01',
  173. ],
  174. [
  175. 'AccessKeyId=LTAIfoSsg3EaQoip&Action=AssumeRole&DurationSeconds=3600&Format=JSON&RegionId=cn-hangzhou&RoleArn=acs%3Aram%3A%3A1483445875618637%3Arole%2Ftest&RoleSessionName=session_name&SignatureMethod=HMAC-SHA1&SignatureNonce=5e87f2b001977f40d261a3b59758048f&SignatureVersion=1.0&Timestamp=2018-12-26T05%3A36%3A23Z&Version=2015-04-01',
  176. 'AccessKeyId%3DLTAIfoSsg3EaQoip%26Action%3DAssumeRole%26DurationSeconds%3D3600%26Format%3DJSON%26RegionId%3Dcn-hangzhou%26RoleArn%3Dacs%253Aram%253A%253A1483445875618637%253Arole%252Ftest%26RoleSessionName%3Dsession_name%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D5e87f2b001977f40d261a3b59758048f%26SignatureVersion%3D1.0%26Timestamp%3D2018-12-26T05%253A36%253A23Z%26Version%3D2015-04-01',
  177. ],
  178. ];
  179. }
  180. /**
  181. * @param $parameters
  182. * @param $expected
  183. *
  184. * @throws ReflectionException
  185. * @throws ClientException
  186. * @dataProvider signature
  187. */
  188. public function testSignature($parameters, $expected)
  189. {
  190. // Setup
  191. $request = new RpcRequest();
  192. $request->options['query'] = $parameters;
  193. // Test
  194. $method = new ReflectionMethod(
  195. RpcRequest::class,
  196. 'signature'
  197. );
  198. $method->setAccessible(true);
  199. $actual = $method->invokeArgs($request, []);
  200. // Assert
  201. self::assertEquals($expected, $actual);
  202. }
  203. /**
  204. * @return array
  205. */
  206. public function signature()
  207. {
  208. return [
  209. [
  210. [1, 2],
  211. 'liZ2rLCylB5dy2kwxbuTa/BY5Uw=',
  212. ],
  213. [
  214. [3, 4],
  215. 'gAQmuhD1C77I3WEgo4j1k+bFfss=',
  216. ],
  217. ];
  218. }
  219. /**
  220. * @throws ClientException
  221. */
  222. public function testCall()
  223. {
  224. $request = new RpcRequest();
  225. $request->withPrefix('with');
  226. self::assertEquals('with', $request->getPrefix());
  227. self::assertEquals(['Prefix' => 'with',], $request->options['query']);
  228. $request->withprefix('with');
  229. self::assertEquals('with', $request->getprefix());
  230. self::assertEquals(['Prefix' => 'with', 'prefix' => 'with',], $request->options['query']);
  231. }
  232. /**
  233. * @expectedException RuntimeException
  234. * @expectedExceptionMessage Call to undefined method AlibabaCloud\Client\Request\RpcRequest::nowithvalue()
  235. * @throws ClientException
  236. */
  237. public function testCallException()
  238. {
  239. $request = new RpcRequest();
  240. $request->nowithvalue('value');
  241. }
  242. /**
  243. * @expectedException RuntimeException
  244. * @expectedExceptionMessage Please use withParameter instead of setParameter
  245. */
  246. public function testExceptionWithSet()
  247. {
  248. $request = AlibabaCloud::rpc();
  249. $request->setParameter();
  250. }
  251. /**
  252. * @covers \AlibabaCloud\Client\Request\RpcRequest::resolveSecurityToken
  253. * @throws ReflectionException
  254. * @throws ClientException
  255. */
  256. public function testResolveSecurityToken()
  257. {
  258. // Setup
  259. AlibabaCloud::stsClient('foo', 'bar', 'token')->name('resolveSecurityToken');
  260. $request = AlibabaCloud::rpc()->client('resolveSecurityToken');
  261. $object = new ReflectionObject($request);
  262. // Test
  263. $method = $object->getMethod('resolveSecurityToken');
  264. $method->setAccessible(true);
  265. $method->invoke($request);
  266. // Assert
  267. self::assertEquals('token', $request->options['query']['SecurityToken']);
  268. }
  269. /**
  270. * @covers \AlibabaCloud\Client\Request\RoaRequest::resolveSecurityToken
  271. * @throws ReflectionException
  272. * @throws ClientException
  273. */
  274. public function testNoSecurityToken()
  275. {
  276. // Setup
  277. $request = AlibabaCloud::rpc();
  278. $object = new ReflectionObject($request);
  279. AlibabaCloud::stsClient('foo', 'bar')->asDefaultClient();
  280. // Test
  281. $method = $object->getMethod('resolveSecurityToken');
  282. $method->setAccessible(true);
  283. $method->invoke($request);
  284. // Assert
  285. self::assertFalse(isset($request->options['query']['SecurityToken']));
  286. }
  287. }