OpenApiUtilClientTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. $this->assertEquals(
  137. 'ok,test,2,3',
  138. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  139. $data,
  140. 'instance',
  141. 'simple'
  142. )
  143. );
  144. $this->assertEquals(
  145. 'ok test 2 3',
  146. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  147. $data,
  148. 'instance',
  149. 'spaceDelimited'
  150. )
  151. );
  152. $this->assertEquals(
  153. 'ok|test|2|3',
  154. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  155. $data,
  156. 'instance',
  157. 'pipeDelimited'
  158. )
  159. );
  160. $this->assertEquals(
  161. '',
  162. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  163. $data,
  164. 'instance',
  165. 'piDelimited'
  166. )
  167. );
  168. $this->assertEquals(
  169. '',
  170. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  171. null,
  172. 'instance',
  173. 'pipeDelimited'
  174. )
  175. );
  176. }
  177. public function testParseToArray()
  178. {
  179. $test = $this->parseData();
  180. $data = $test['data'];
  181. $expected = $test['expected'];
  182. foreach ($data as $index => $item) {
  183. $this->assertEquals($expected[$index], OpenApiUtilClient::parseToArray($item));
  184. }
  185. }
  186. public function testParseToMap()
  187. {
  188. $test = $this->parseData();
  189. $data = $test['data'];
  190. $expected = $test['expected'];
  191. foreach ($data as $index => $item) {
  192. $this->assertEquals($expected[$index], OpenApiUtilClient::parseToMap($item));
  193. }
  194. }
  195. public function testGetEndpoint()
  196. {
  197. $endpoint = 'ecs.cn-hangzhou.aliyun.cs.com';
  198. $useAccelerate = false;
  199. $endpointType = 'public';
  200. $this->assertEquals('ecs.cn-hangzhou.aliyun.cs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  201. $endpointType = 'internal';
  202. $this->assertEquals('ecs-internal.cn-hangzhou.aliyun.cs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  203. $useAccelerate = true;
  204. $endpointType = 'accelerate';
  205. $this->assertEquals('oss-accelerate.aliyuncs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  206. }
  207. public function testHexEncode()
  208. {
  209. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HMAC-SHA256');
  210. $this->assertEquals(
  211. Utils::toBytes(hex2bin('9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')),
  212. $data
  213. );
  214. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-RSA-SHA256');
  215. $this->assertEquals(
  216. Utils::toBytes(hex2bin('9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')),
  217. $data
  218. );
  219. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HMAC-SM3');
  220. $this->assertEquals(
  221. Utils::toBytes(hex2bin('55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23')),
  222. $data
  223. );
  224. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HM-SHA256');
  225. $this->assertEquals('', Utils::toString($data));
  226. }
  227. public function testGetEncodePath()
  228. {
  229. $this->assertEquals(
  230. '/path/%20test',
  231. OpenApiUtilClient::getEncodePath('/path/ test')
  232. );
  233. }
  234. public function testGetEncodeParam()
  235. {
  236. $this->assertEquals(
  237. 'a%2Fb%2Fc%2F%20test',
  238. OpenApiUtilClient::getEncodeParam('a/b/c/ test')
  239. );
  240. }
  241. public function testGetAuthorization()
  242. {
  243. $request = new Request();
  244. $request->method = '';
  245. $request->pathname = '';
  246. $request->query = [
  247. 'test' => 'ok',
  248. 'empty' => '',
  249. ];
  250. $request->headers = [
  251. 'x-acs-test' => 'http',
  252. 'x-acs-TEST' => 'https',
  253. ];
  254. $res = OpenApiUtilClient::getAuthorization($request, 'ACS3-HMAC-SHA256', '55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23', 'acesskey', 'secret');
  255. $this->assertEquals('ACS3-HMAC-SHA256 Credential=acesskey,SignedHeaders=x-acs-test,Signature=0a0f89a45f1ec3537a2d1a1046c71b95513a8f1f02526056968da19b99a5b914', $res);
  256. $request->query = null;
  257. $res = OpenApiUtilClient::getAuthorization($request, 'ACS3-HMAC-SHA256', '55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23', 'acesskey', 'secret');
  258. $this->assertEquals('ACS3-HMAC-SHA256 Credential=acesskey,SignedHeaders=x-acs-test,Signature=af6d32deb090ae85a21d7183055cf18dca17751da96979cdf964f8f0853e9dd2', $res);
  259. }
  260. public function testSign()
  261. {
  262. $this->assertEquals(
  263. 'b9ff646822f41ef647c1416fa2b8408923828abc0464af6706e18db3e8553da8',
  264. OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SM3'))
  265. );
  266. $this->assertEquals(
  267. '1d93c62698a1c26427265668e79fac099aa26c1df873669599a2fb2f272e64c9',
  268. OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SHA256'))
  269. );
  270. }
  271. private function parseData()
  272. {
  273. return [
  274. 'data' => [
  275. 'NotArray',
  276. new ParseModel([
  277. 'str' => 'A',
  278. 'model' => new ParseModel(['str' => 'sub model']),
  279. 'array' => [1, 2, 3],
  280. ]),
  281. [ // model item in array
  282. new ParseModel([
  283. 'str' => 'A',
  284. ]),
  285. ],
  286. [ // model item in map
  287. 'model' => new ParseModel([
  288. 'str' => 'A',
  289. ]),
  290. ],
  291. ],
  292. 'expected' => [
  293. ['NotArray'],
  294. [
  295. 'str' => 'A',
  296. 'model' => [
  297. 'str' => 'sub model',
  298. 'model' => null,
  299. 'array' => null,
  300. ],
  301. 'array' => [1, 2, 3],
  302. ],
  303. [
  304. [
  305. 'str' => 'A',
  306. 'model' => null,
  307. 'array' => null,
  308. ],
  309. ],
  310. [
  311. 'model' => [
  312. 'str' => 'A',
  313. 'model' => null,
  314. 'array' => null,
  315. ],
  316. ],
  317. ],
  318. ];
  319. }
  320. }
  321. class MockModel extends Model
  322. {
  323. public $a = 'A';
  324. public $b = '';
  325. public $c = '';
  326. public function __construct()
  327. {
  328. $this->_name['a'] = 'A';
  329. $this->_required['c'] = true;
  330. parent::__construct([]);
  331. }
  332. }
  333. class ParseModel extends Model
  334. {
  335. public $str;
  336. public $model;
  337. public $array;
  338. }