RpcRequestTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\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\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. if (method_exists($this, 'assertIsArray')) {
  91. self::assertIsArray($request->options['query']);
  92. } elseif (method_exists($this, 'assertInternalType')) {
  93. self::assertInternalType('array', $request->options['query']);
  94. }
  95. self::assertEquals('A', $request->options['query']['A']);
  96. }
  97. /**
  98. * @return array
  99. * @throws ClientException
  100. */
  101. public function resolveQuery()
  102. {
  103. return [
  104. [
  105. new StsCredential('foo', 'bar', 'token'),
  106. ],
  107. [
  108. new BearerTokenCredential('token'),
  109. ],
  110. ];
  111. }
  112. /**
  113. * @param CredentialsInterface $credential
  114. *
  115. * @throws ReflectionException
  116. * @throws ClientException
  117. * @dataProvider resolveQuery
  118. */
  119. public function testResolveParameters($credential)
  120. {
  121. // Setup
  122. AlibabaCloud::bearerTokenClient('token')->name('token');
  123. $request = new RpcRequest();
  124. $request->client('token');
  125. $request->regionId('cn-hangzhou');
  126. $request->method('post');
  127. $request->options(
  128. [
  129. 'query' => [
  130. 'A' => 'A',
  131. ],
  132. ]
  133. );
  134. // Test
  135. $method = new ReflectionMethod(
  136. RpcRequest::class,
  137. 'resolveParameter'
  138. );
  139. $method->setAccessible(true);
  140. $method->invokeArgs($request, [$credential]);
  141. // Assert
  142. self::assertFalse(isset($request->options['query']));
  143. self::assertEquals('http://localhost', (string) $request->uri);
  144. }
  145. /**
  146. * @param $value
  147. * @param $expected
  148. *
  149. * @throws ReflectionException
  150. * @throws ClientException
  151. * @dataProvider percentEncode
  152. */
  153. public function testPercentEncode($value, $expected)
  154. {
  155. // Setup
  156. $request = new RpcRequest();
  157. // Test
  158. $method = new ReflectionMethod(
  159. Sign::class,
  160. 'percentEncode'
  161. );
  162. $method->setAccessible(true);
  163. $actual = $method->invokeArgs($request, [$value]);
  164. // Assert
  165. self::assertEquals($expected, $actual);
  166. }
  167. /**
  168. * @return array
  169. */
  170. public function percentEncode()
  171. {
  172. return [
  173. ['2018-12-26T05:36:28Z', '2018-12-26T05%3A36%3A28Z'],
  174. [
  175. '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',
  176. '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',
  177. ],
  178. [
  179. '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',
  180. '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',
  181. ],
  182. ];
  183. }
  184. /**
  185. * @param $parameters
  186. * @param $expected
  187. *
  188. * @throws ReflectionException
  189. * @throws ClientException
  190. * @dataProvider signature
  191. */
  192. public function testSignature($parameters, $expected)
  193. {
  194. // Setup
  195. $request = new RpcRequest();
  196. $request->options['query'] = $parameters;
  197. // Test
  198. $method = new ReflectionMethod(
  199. RpcRequest::class,
  200. 'signature'
  201. );
  202. $method->setAccessible(true);
  203. $actual = $method->invokeArgs($request, []);
  204. // Assert
  205. self::assertEquals($expected, $actual);
  206. }
  207. /**
  208. * @return array
  209. */
  210. public function signature()
  211. {
  212. return [
  213. [
  214. [1, 2],
  215. 'liZ2rLCylB5dy2kwxbuTa/BY5Uw=',
  216. ],
  217. [
  218. [3, 4],
  219. 'gAQmuhD1C77I3WEgo4j1k+bFfss=',
  220. ],
  221. ];
  222. }
  223. /**
  224. * @throws ClientException
  225. */
  226. public function testCall()
  227. {
  228. $request = new RpcRequest();
  229. $request->withPrefix('with');
  230. self::assertEquals('with', $request->getPrefix());
  231. self::assertEquals(['Prefix' => 'with',], $request->options['query']);
  232. $request->withprefix('with');
  233. self::assertEquals('with', $request->getprefix());
  234. self::assertEquals(['Prefix' => 'with', 'prefix' => 'with',], $request->options['query']);
  235. }
  236. /**
  237. * @throws ClientException
  238. */
  239. public function testCallException()
  240. {
  241. $this->expectException(RuntimeException::class);
  242. $this->expectExceptionMessage("Call to undefined method AlibabaCloud\Client\Request\RpcRequest::nowithvalue()");
  243. $request = new RpcRequest();
  244. $request->nowithvalue('value');
  245. }
  246. public function testExceptionWithSet()
  247. {
  248. $this->expectException(RuntimeException::class);
  249. $this->expectExceptionMessage("Please use withParameter instead of setParameter");
  250. $request = AlibabaCloud::rpc();
  251. $request->setParameter();
  252. }
  253. /**
  254. * @covers \AlibabaCloud\Client\Request\RpcRequest::resolveSecurityToken
  255. * @throws ReflectionException
  256. * @throws ClientException
  257. */
  258. public function testResolveSecurityToken()
  259. {
  260. // Setup
  261. AlibabaCloud::stsClient('foo', 'bar', 'token')->name('resolveSecurityToken');
  262. $request = AlibabaCloud::rpc()->client('resolveSecurityToken');
  263. $object = new ReflectionObject($request);
  264. // Test
  265. $method = $object->getMethod('resolveSecurityToken');
  266. $method->setAccessible(true);
  267. $method->invoke($request);
  268. // Assert
  269. self::assertEquals('token', $request->options['query']['SecurityToken']);
  270. }
  271. /**
  272. * @covers \AlibabaCloud\Client\Request\RoaRequest::resolveSecurityToken
  273. * @throws ReflectionException
  274. * @throws ClientException
  275. */
  276. public function testNoSecurityToken()
  277. {
  278. // Setup
  279. $request = AlibabaCloud::rpc();
  280. $object = new ReflectionObject($request);
  281. AlibabaCloud::stsClient('foo', 'bar')->asDefaultClient();
  282. // Test
  283. $method = $object->getMethod('resolveSecurityToken');
  284. $method->setAccessible(true);
  285. $method->invoke($request);
  286. // Assert
  287. self::assertFalse(isset($request->options['query']['SecurityToken']));
  288. }
  289. }