OpenApiUtilClientTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace AlibabaCloud\OpenApiUtil\Tests;
  3. use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
  4. use AlibabaCloud\Tea\Model;
  5. use AlibabaCloud\Tea\Request;
  6. use AlibabaCloud\Tea\Utils\Utils;
  7. use PHPUnit\Framework\TestCase;
  8. /**
  9. * @internal
  10. * @coversNothing
  11. */
  12. class OpenApiUtilClientTest extends TestCase
  13. {
  14. public function testConvert()
  15. {
  16. $model = new MockModel();
  17. $model->a = 'foo';
  18. $output = new MockModel();
  19. OpenApiUtilClient::convert($model, $output);
  20. $this->assertEquals($model->a, $output->a);
  21. }
  22. public function testGetStringToSign()
  23. {
  24. $request = new Request();
  25. $request->method = 'GET';
  26. $request->pathname = '/';
  27. $request->headers['accept'] = 'application/json';
  28. $this->assertEquals("GET\napplication/json\n\n\n\n/", OpenApiUtilClient::getStringToSign($request));
  29. $request->headers = [
  30. 'accept' => 'application/json',
  31. 'content-md5' => 'md5',
  32. 'content-type' => 'application/json',
  33. 'date' => 'date',
  34. ];
  35. $this->assertEquals("GET\napplication/json\nmd5\napplication/json\ndate\n/", OpenApiUtilClient::getStringToSign($request));
  36. $request->headers = [
  37. 'accept' => 'application/json',
  38. 'content-md5' => 'md5',
  39. 'content-type' => 'application/json',
  40. 'date' => 'date',
  41. 'x-acs-custom-key' => 'any value',
  42. ];
  43. $this->assertEquals("GET\napplication/json\nmd5\napplication/json\ndate\nx-acs-custom-key:any value\n/", OpenApiUtilClient::getStringToSign($request));
  44. $request->query = [
  45. 'key' => 'val ue with space',
  46. ];
  47. $this->assertEquals("GET\napplication/json\nmd5\napplication/json\ndate\nx-acs-custom-key:any value\n/?key=val ue with space", OpenApiUtilClient::getStringToSign($request));
  48. }
  49. public function testGetROASignature()
  50. {
  51. $this->assertEquals('OmuTAr79tpI6CRoAdmzKRq5lHs0=', OpenApiUtilClient::getROASignature('stringtosign', 'secret'));
  52. }
  53. public function testToForm()
  54. {
  55. $this->assertEquals('client=test&strs.1=str1&strs.2=str2&tag.key=value', OpenApiUtilClient::toForm([
  56. 'client' => 'test',
  57. 'tag' => [
  58. 'key' => 'value',
  59. ],
  60. 'strs' => ['str1', 'str2'],
  61. ]));
  62. }
  63. public function testGetTimestamp()
  64. {
  65. $date = OpenApiUtilClient::getTimestamp();
  66. $this->assertEquals(20, \strlen($date));
  67. }
  68. public function testQuery()
  69. {
  70. $array = [
  71. 'a' => 'a',
  72. 'b1' => [
  73. 'a' => 'a',
  74. ],
  75. 'b2' => [
  76. 'a' => 'a',
  77. ],
  78. 'c' => ['x', 'y', 'z'],
  79. ];
  80. $this->assertEquals([
  81. 'a' => 'a',
  82. 'b1.a' => 'a',
  83. 'b2.a' => 'a',
  84. 'c.1' => 'x',
  85. 'c.2' => 'y',
  86. 'c.3' => 'z',
  87. ], OpenApiUtilClient::query($array));
  88. }
  89. public function testGetRPCSignature()
  90. {
  91. $request = new Request();
  92. $request->pathname = '';
  93. $request->query = [
  94. 'query' => 'test',
  95. 'body' => 'test',
  96. ];
  97. $this->assertEquals('XlUyV4sXjOuX5FnjUz9IF9tm5rU=', OpenApiUtilClient::getRPCSignature($request->query, $request->method, 'secret'));
  98. }
  99. public function testArrayToStringWithSpecifiedStyle()
  100. {
  101. $data = ['ok', 'test', 2, 3];
  102. $this->assertEquals(
  103. 'instance.1=ok&instance.2=test&instance.3=2&instance.4=3',
  104. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  105. $data,
  106. 'instance',
  107. 'repeatList'
  108. )
  109. );
  110. $this->assertEquals(
  111. '["ok","test",2,3]',
  112. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  113. $data,
  114. 'instance',
  115. 'json'
  116. )
  117. );
  118. $this->assertEquals(
  119. 'ok,test,2,3',
  120. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  121. $data,
  122. 'instance',
  123. 'simple'
  124. )
  125. );
  126. $this->assertEquals(
  127. 'ok test 2 3',
  128. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  129. $data,
  130. 'instance',
  131. 'spaceDelimited'
  132. )
  133. );
  134. $this->assertEquals(
  135. 'ok|test|2|3',
  136. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  137. $data,
  138. 'instance',
  139. 'pipeDelimited'
  140. )
  141. );
  142. $this->assertEquals(
  143. '',
  144. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  145. $data,
  146. 'instance',
  147. 'piDelimited'
  148. )
  149. );
  150. $this->assertEquals(
  151. '',
  152. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  153. null,
  154. 'instance',
  155. 'pipeDelimited'
  156. )
  157. );
  158. }
  159. public function testParseToArray()
  160. {
  161. $test = $this->parseData();
  162. $data = $test['data'];
  163. $expected = $test['expected'];
  164. foreach ($data as $index => $item) {
  165. $this->assertEquals($expected[$index], OpenApiUtilClient::parseToArray($item));
  166. }
  167. }
  168. public function testParseToMap()
  169. {
  170. $test = $this->parseData();
  171. $data = $test['data'];
  172. $expected = $test['expected'];
  173. foreach ($data as $index => $item) {
  174. $this->assertEquals($expected[$index], OpenApiUtilClient::parseToMap($item));
  175. }
  176. }
  177. public function testGetEndpoint()
  178. {
  179. $endpoint = 'ecs.cn-hangzhou.aliyun.cs.com';
  180. $useAccelerate = false;
  181. $endpointType = 'public';
  182. $this->assertEquals('ecs.cn-hangzhou.aliyun.cs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  183. $endpointType = 'internal';
  184. $this->assertEquals('ecs-internal.cn-hangzhou.aliyun.cs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  185. $useAccelerate = true;
  186. $endpointType = 'accelerate';
  187. $this->assertEquals('oss-accelerate.aliyuncs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  188. }
  189. public function testHexEncode()
  190. {
  191. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HMAC-SHA256');
  192. $this->assertEquals(
  193. Utils::toBytes(hex2bin('9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')),
  194. $data
  195. );
  196. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-RSA-SHA256');
  197. $this->assertEquals(
  198. Utils::toBytes(hex2bin('9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')),
  199. $data
  200. );
  201. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HMAC-SM3');
  202. $this->assertEquals(
  203. Utils::toBytes(hex2bin('55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23')),
  204. $data
  205. );
  206. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HM-SHA256');
  207. $this->assertEquals('', Utils::toString($data));
  208. }
  209. public function testGetEncodePath()
  210. {
  211. $this->assertEquals(
  212. '/path/%20test',
  213. OpenApiUtilClient::getEncodePath('/path/ test')
  214. );
  215. }
  216. public function testGetAuthorization()
  217. {
  218. $request = new Request();
  219. $request->method = '';
  220. $request->pathname = '';
  221. $request->query = [
  222. 'test' => 'ok',
  223. 'empty' => '',
  224. ];
  225. $request->headers = [
  226. 'x-acs-test' => 'http',
  227. 'x-acs-TEST' => 'https',
  228. ];
  229. $res = OpenApiUtilClient::getAuthorization($request, 'ACS3-HMAC-SHA256', '55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23', 'acesskey', 'secret');
  230. $this->assertEquals('ACS3-HMAC-SHA256 Credential=acesskey,SignedHeaders=x-acs-test,Signature=0a0f89a45f1ec3537a2d1a1046c71b95513a8f1f02526056968da19b99a5b914', $res);
  231. }
  232. public function testSign()
  233. {
  234. $this->assertEquals(
  235. 'b9ff646822f41ef647c1416fa2b8408923828abc0464af6706e18db3e8553da8',
  236. OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SM3'))
  237. );
  238. $this->assertEquals('1d93c62698a1c26427265668e79fac099aa26c1df873669599a2fb2f272e64c9',
  239. OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SHA256'))
  240. );
  241. }
  242. private function parseData()
  243. {
  244. return [
  245. 'data' => [
  246. 'NotArray',
  247. new ParseModel([
  248. 'str' => 'A',
  249. 'model' => new ParseModel(['str' => 'sub model']),
  250. 'array' => [1, 2, 3],
  251. ]),
  252. [ // model item in array
  253. new ParseModel([
  254. 'str' => 'A',
  255. ]),
  256. ],
  257. [ // model item in map
  258. 'model' => new ParseModel([
  259. 'str' => 'A',
  260. ]),
  261. ],
  262. ],
  263. 'expected' => [
  264. ['NotArray'],
  265. [
  266. 'str' => 'A',
  267. 'model' => [
  268. 'str' => 'sub model',
  269. 'model' => null,
  270. 'array' => null,
  271. ],
  272. 'array' => [1, 2, 3],
  273. ],
  274. [
  275. [
  276. 'str' => 'A',
  277. 'model' => null,
  278. 'array' => null,
  279. ],
  280. ],
  281. [
  282. 'model' => [
  283. 'str' => 'A',
  284. 'model' => null,
  285. 'array' => null,
  286. ],
  287. ],
  288. ],
  289. ];
  290. }
  291. }
  292. class MockModel extends Model
  293. {
  294. public $a = 'A';
  295. public $b = '';
  296. public $c = '';
  297. public function __construct()
  298. {
  299. $this->_name['a'] = 'A';
  300. $this->_required['c'] = true;
  301. parent::__construct([]);
  302. }
  303. }
  304. class ParseModel extends Model
  305. {
  306. public $str;
  307. public $model;
  308. public $array;
  309. }