OpenApiUtilClientTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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('bool=true&client=test&strs.1=str1&strs.2=str2&strs.3=false&tag.key=value', OpenApiUtilClient::toForm([
  56. 'client' => 'test',
  57. 'tag' => [
  58. 'key' => 'value',
  59. ],
  60. 'strs' => ['str1', 'str2', false],
  61. 'bool' => true,
  62. 'null' => null,
  63. ]));
  64. }
  65. public function testGetTimestamp()
  66. {
  67. $date = OpenApiUtilClient::getTimestamp();
  68. $this->assertEquals(20, \strlen($date));
  69. }
  70. public function testQuery()
  71. {
  72. $model = new MockModel();
  73. $model->a = 'foo';
  74. $model->c = 'boo';
  75. $model->r = true;
  76. $array = [
  77. 'a' => 'a',
  78. 'b1' => [
  79. 'a' => 'a',
  80. ],
  81. 'b2' => [
  82. 'a' => 'a',
  83. ],
  84. 'c' => ['x', 'y', 'z'],
  85. 'd' => [
  86. $model
  87. ],
  88. 'e' => true,
  89. 'f' => null,
  90. ];
  91. $this->assertEquals([
  92. 'a' => 'a',
  93. 'b1.a' => 'a',
  94. 'b2.a' => 'a',
  95. 'c.1' => 'x',
  96. 'c.2' => 'y',
  97. 'c.3' => 'z',
  98. 'd.1.A' => 'foo',
  99. 'd.1.b' => '',
  100. 'd.1.c' => 'boo',
  101. 'd.1.c' => 'boo',
  102. 'd.1.r' => 'true',
  103. 'e' => 'true',
  104. 'f' => null
  105. ], OpenApiUtilClient::query($array));
  106. }
  107. public function testGetRPCSignature()
  108. {
  109. $request = new Request();
  110. $request->pathname = '';
  111. $request->query = [
  112. 'query' => 'test',
  113. 'body' => 'test',
  114. ];
  115. $this->assertEquals('XlUyV4sXjOuX5FnjUz9IF9tm5rU=', OpenApiUtilClient::getRPCSignature($request->query, $request->method, 'secret'));
  116. }
  117. public function testArrayToStringWithSpecifiedStyle()
  118. {
  119. $data = ['ok', 'test', 2, 3];
  120. $this->assertEquals(
  121. 'instance.1=ok&instance.2=test&instance.3=2&instance.4=3',
  122. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  123. $data,
  124. 'instance',
  125. 'repeatList'
  126. )
  127. );
  128. $this->assertEquals(
  129. '["ok","test",2,3]',
  130. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  131. $data,
  132. 'instance',
  133. 'json'
  134. )
  135. );
  136. $test = new ParseModel([
  137. 'str' => 'A',
  138. 'model' => new ParseModel(['str' => 'sub model']),
  139. 'array' => [1, 2, 3],
  140. ]);
  141. $this->assertEquals(
  142. '{"str":"A","model":{"str":"sub model","model":null,"array":null},"array":[1,2,3]}',
  143. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  144. $test,
  145. 'instance',
  146. 'json'
  147. )
  148. );
  149. // model item in array
  150. $test = [
  151. new ParseModel([
  152. 'str' => 'A',
  153. ]),
  154. ];
  155. $this->assertEquals(
  156. '[{"str":"A","model":null,"array":null}]',
  157. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  158. $test,
  159. 'instance',
  160. 'json'
  161. )
  162. );
  163. // model item in map
  164. $test = [
  165. 'model' => new ParseModel([
  166. 'str' => 'A',
  167. ]),
  168. ];
  169. $this->assertEquals(
  170. '{"model":{"str":"A","model":null,"array":null}}',
  171. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  172. $test,
  173. 'instance',
  174. 'json'
  175. )
  176. );
  177. $this->assertEquals(
  178. 'ok,test,2,3',
  179. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  180. $data,
  181. 'instance',
  182. 'simple'
  183. )
  184. );
  185. $this->assertEquals(
  186. 'ok test 2 3',
  187. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  188. $data,
  189. 'instance',
  190. 'spaceDelimited'
  191. )
  192. );
  193. $this->assertEquals(
  194. 'ok|test|2|3',
  195. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  196. $data,
  197. 'instance',
  198. 'pipeDelimited'
  199. )
  200. );
  201. $this->assertEquals(
  202. '',
  203. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  204. $data,
  205. 'instance',
  206. 'piDelimited'
  207. )
  208. );
  209. $this->assertEquals(
  210. '',
  211. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  212. null,
  213. 'instance',
  214. 'pipeDelimited'
  215. )
  216. );
  217. }
  218. public function testParseToArray()
  219. {
  220. $test = $this->parseData();
  221. $data = $test['data'];
  222. $expected = $test['expected'];
  223. foreach ($data as $index => $item) {
  224. $this->assertEquals($expected[$index], OpenApiUtilClient::parseToArray($item));
  225. }
  226. }
  227. public function testParseToMap()
  228. {
  229. $test = $this->parseData();
  230. $data = $test['data'];
  231. $expected = $test['expected'];
  232. foreach ($data as $index => $item) {
  233. $this->assertEquals($expected[$index], OpenApiUtilClient::parseToMap($item));
  234. }
  235. }
  236. public function testGetEndpoint()
  237. {
  238. $endpoint = 'ecs.cn-hangzhou.aliyun.cs.com';
  239. $useAccelerate = false;
  240. $endpointType = 'public';
  241. $this->assertEquals('ecs.cn-hangzhou.aliyun.cs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  242. $endpointType = 'internal';
  243. $this->assertEquals('ecs-internal.cn-hangzhou.aliyun.cs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  244. $useAccelerate = true;
  245. $endpointType = 'accelerate';
  246. $this->assertEquals('oss-accelerate.aliyuncs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  247. }
  248. public function testHexEncode()
  249. {
  250. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HMAC-SHA256');
  251. $this->assertEquals(
  252. Utils::toBytes(hex2bin('9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')),
  253. $data
  254. );
  255. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-RSA-SHA256');
  256. $this->assertEquals(
  257. Utils::toBytes(hex2bin('9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')),
  258. $data
  259. );
  260. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HMAC-SM3');
  261. $this->assertEquals(
  262. Utils::toBytes(hex2bin('55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23')),
  263. $data
  264. );
  265. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HM-SHA256');
  266. $this->assertEquals('', Utils::toString($data));
  267. }
  268. public function testGetEncodePath()
  269. {
  270. $this->assertEquals(
  271. '/path/%20test',
  272. OpenApiUtilClient::getEncodePath('/path/ test')
  273. );
  274. }
  275. public function testGetEncodeParam()
  276. {
  277. $this->assertEquals(
  278. 'a%2Fb%2Fc%2F%20test',
  279. OpenApiUtilClient::getEncodeParam('a/b/c/ test')
  280. );
  281. }
  282. public function testGetAuthorization()
  283. {
  284. $request = new Request();
  285. $request->method = '';
  286. $request->pathname = '';
  287. $request->query = [
  288. 'test' => 'ok',
  289. 'empty' => '',
  290. ];
  291. $request->headers = [
  292. 'x-acs-test' => 'http',
  293. 'x-acs-TEST' => 'https',
  294. ];
  295. $res = OpenApiUtilClient::getAuthorization($request, 'ACS3-HMAC-SHA256', '55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23', 'acesskey', 'secret');
  296. $this->assertEquals('ACS3-HMAC-SHA256 Credential=acesskey,SignedHeaders=x-acs-test,Signature=0a0f89a45f1ec3537a2d1a1046c71b95513a8f1f02526056968da19b99a5b914', $res);
  297. $request->query = null;
  298. $res = OpenApiUtilClient::getAuthorization($request, 'ACS3-HMAC-SHA256', '55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23', 'acesskey', 'secret');
  299. $this->assertEquals('ACS3-HMAC-SHA256 Credential=acesskey,SignedHeaders=x-acs-test,Signature=af6d32deb090ae85a21d7183055cf18dca17751da96979cdf964f8f0853e9dd2', $res);
  300. }
  301. public function testSign()
  302. {
  303. $this->assertEquals(
  304. 'b9ff646822f41ef647c1416fa2b8408923828abc0464af6706e18db3e8553da8',
  305. OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SM3'))
  306. );
  307. $this->assertEquals(
  308. '1d93c62698a1c26427265668e79fac099aa26c1df873669599a2fb2f272e64c9',
  309. OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SHA256'))
  310. );
  311. }
  312. private function parseData()
  313. {
  314. return [
  315. 'data' => [
  316. 'NotArray',
  317. new ParseModel([
  318. 'str' => 'A',
  319. 'model' => new ParseModel(['str' => 'sub model']),
  320. 'array' => [1, 2, 3],
  321. ]),
  322. [ // model item in array
  323. new ParseModel([
  324. 'str' => 'A',
  325. ]),
  326. ],
  327. [ // model item in map
  328. 'model' => new ParseModel([
  329. 'str' => 'A',
  330. ]),
  331. ],
  332. ],
  333. 'expected' => [
  334. ['NotArray'],
  335. [
  336. 'str' => 'A',
  337. 'model' => [
  338. 'str' => 'sub model',
  339. 'model' => null,
  340. 'array' => null,
  341. ],
  342. 'array' => [1, 2, 3],
  343. ],
  344. [
  345. [
  346. 'str' => 'A',
  347. 'model' => null,
  348. 'array' => null,
  349. ],
  350. ],
  351. [
  352. 'model' => [
  353. 'str' => 'A',
  354. 'model' => null,
  355. 'array' => null,
  356. ],
  357. ],
  358. ],
  359. 'expectedJsonStr' => [
  360. '["NotArray"]',
  361. 'NotArray',
  362. 'NotArray',
  363. 'NotArray',
  364. ],
  365. ];
  366. }
  367. }
  368. class MockModel extends Model
  369. {
  370. public $a = 'A';
  371. public $b = '';
  372. public $c = '';
  373. public function __construct()
  374. {
  375. $this->_name['a'] = 'A';
  376. $this->_required['c'] = true;
  377. parent::__construct([]);
  378. }
  379. }
  380. class ParseModel extends Model
  381. {
  382. public $str;
  383. public $model;
  384. public $array;
  385. }