OpenApiUtilClientTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. $model = new MockModel();
  71. $model->a = 'foo';
  72. $model->c = 'boo';
  73. $array = [
  74. 'a' => 'a',
  75. 'b1' => [
  76. 'a' => 'a',
  77. ],
  78. 'b2' => [
  79. 'a' => 'a',
  80. ],
  81. 'c' => ['x', 'y', 'z'],
  82. 'd' => [
  83. $model
  84. ]
  85. ];
  86. $this->assertEquals([
  87. 'a' => 'a',
  88. 'b1.a' => 'a',
  89. 'b2.a' => 'a',
  90. 'c.1' => 'x',
  91. 'c.2' => 'y',
  92. 'c.3' => 'z',
  93. 'd.1.A' => 'foo',
  94. 'd.1.b' => '',
  95. 'd.1.c' => 'boo',
  96. ], OpenApiUtilClient::query($array));
  97. }
  98. public function testGetRPCSignature()
  99. {
  100. $request = new Request();
  101. $request->pathname = '';
  102. $request->query = [
  103. 'query' => 'test',
  104. 'body' => 'test',
  105. ];
  106. $this->assertEquals('XlUyV4sXjOuX5FnjUz9IF9tm5rU=', OpenApiUtilClient::getRPCSignature($request->query, $request->method, 'secret'));
  107. }
  108. public function testArrayToStringWithSpecifiedStyle()
  109. {
  110. $data = ['ok', 'test', 2, 3];
  111. $this->assertEquals(
  112. 'instance.1=ok&instance.2=test&instance.3=2&instance.4=3',
  113. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  114. $data,
  115. 'instance',
  116. 'repeatList'
  117. )
  118. );
  119. $this->assertEquals(
  120. '["ok","test",2,3]',
  121. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  122. $data,
  123. 'instance',
  124. 'json'
  125. )
  126. );
  127. $this->assertEquals(
  128. 'ok,test,2,3',
  129. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  130. $data,
  131. 'instance',
  132. 'simple'
  133. )
  134. );
  135. $this->assertEquals(
  136. 'ok test 2 3',
  137. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  138. $data,
  139. 'instance',
  140. 'spaceDelimited'
  141. )
  142. );
  143. $this->assertEquals(
  144. 'ok|test|2|3',
  145. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  146. $data,
  147. 'instance',
  148. 'pipeDelimited'
  149. )
  150. );
  151. $this->assertEquals(
  152. '',
  153. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  154. $data,
  155. 'instance',
  156. 'piDelimited'
  157. )
  158. );
  159. $this->assertEquals(
  160. '',
  161. OpenApiUtilClient::arrayToStringWithSpecifiedStyle(
  162. null,
  163. 'instance',
  164. 'pipeDelimited'
  165. )
  166. );
  167. }
  168. public function testParseToArray()
  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::parseToArray($item));
  175. }
  176. }
  177. public function testParseToMap()
  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::parseToMap($item));
  184. }
  185. }
  186. public function testGetEndpoint()
  187. {
  188. $endpoint = 'ecs.cn-hangzhou.aliyun.cs.com';
  189. $useAccelerate = false;
  190. $endpointType = 'public';
  191. $this->assertEquals('ecs.cn-hangzhou.aliyun.cs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  192. $endpointType = 'internal';
  193. $this->assertEquals('ecs-internal.cn-hangzhou.aliyun.cs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  194. $useAccelerate = true;
  195. $endpointType = 'accelerate';
  196. $this->assertEquals('oss-accelerate.aliyuncs.com', OpenApiUtilClient::getEndpoint($endpoint, $useAccelerate, $endpointType));
  197. }
  198. public function testHexEncode()
  199. {
  200. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HMAC-SHA256');
  201. $this->assertEquals(
  202. Utils::toBytes(hex2bin('9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')),
  203. $data
  204. );
  205. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-RSA-SHA256');
  206. $this->assertEquals(
  207. Utils::toBytes(hex2bin('9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')),
  208. $data
  209. );
  210. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HMAC-SM3');
  211. $this->assertEquals(
  212. Utils::toBytes(hex2bin('55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23')),
  213. $data
  214. );
  215. $data = OpenApiUtilClient::hash(Utils::toBytes('test'), 'ACS3-HM-SHA256');
  216. $this->assertEquals('', Utils::toString($data));
  217. }
  218. public function testGetEncodePath()
  219. {
  220. $this->assertEquals(
  221. '/path/%20test',
  222. OpenApiUtilClient::getEncodePath('/path/ test')
  223. );
  224. }
  225. public function testGetEncodeParam()
  226. {
  227. $this->assertEquals(
  228. 'a%2Fb%2Fc%2F%20test',
  229. OpenApiUtilClient::getEncodeParam('a/b/c/ test')
  230. );
  231. }
  232. public function testGetAuthorization()
  233. {
  234. $request = new Request();
  235. $request->method = '';
  236. $request->pathname = '';
  237. $request->query = [
  238. 'test' => 'ok',
  239. 'empty' => '',
  240. ];
  241. $request->headers = [
  242. 'x-acs-test' => 'http',
  243. 'x-acs-TEST' => 'https',
  244. ];
  245. $res = OpenApiUtilClient::getAuthorization($request, 'ACS3-HMAC-SHA256', '55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23', 'acesskey', 'secret');
  246. $this->assertEquals('ACS3-HMAC-SHA256 Credential=acesskey,SignedHeaders=x-acs-test,Signature=0a0f89a45f1ec3537a2d1a1046c71b95513a8f1f02526056968da19b99a5b914', $res);
  247. $request->query = null;
  248. $res = OpenApiUtilClient::getAuthorization($request, 'ACS3-HMAC-SHA256', '55e12e91650d2fec56ec74e1d3e4ddbfce2ef3a65890c2a19ecf88a307e76a23', 'acesskey', 'secret');
  249. $this->assertEquals('ACS3-HMAC-SHA256 Credential=acesskey,SignedHeaders=x-acs-test,Signature=af6d32deb090ae85a21d7183055cf18dca17751da96979cdf964f8f0853e9dd2', $res);
  250. }
  251. public function testSign()
  252. {
  253. $this->assertEquals(
  254. 'b9ff646822f41ef647c1416fa2b8408923828abc0464af6706e18db3e8553da8',
  255. OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SM3'))
  256. );
  257. $this->assertEquals('1d93c62698a1c26427265668e79fac099aa26c1df873669599a2fb2f272e64c9',
  258. OpenApiUtilClient::hexEncode(OpenApiUtilClient::sign('secret', 'source', 'ACS3-HMAC-SHA256'))
  259. );
  260. }
  261. private function parseData()
  262. {
  263. return [
  264. 'data' => [
  265. 'NotArray',
  266. new ParseModel([
  267. 'str' => 'A',
  268. 'model' => new ParseModel(['str' => 'sub model']),
  269. 'array' => [1, 2, 3],
  270. ]),
  271. [ // model item in array
  272. new ParseModel([
  273. 'str' => 'A',
  274. ]),
  275. ],
  276. [ // model item in map
  277. 'model' => new ParseModel([
  278. 'str' => 'A',
  279. ]),
  280. ],
  281. ],
  282. 'expected' => [
  283. ['NotArray'],
  284. [
  285. 'str' => 'A',
  286. 'model' => [
  287. 'str' => 'sub model',
  288. 'model' => null,
  289. 'array' => null,
  290. ],
  291. 'array' => [1, 2, 3],
  292. ],
  293. [
  294. [
  295. 'str' => 'A',
  296. 'model' => null,
  297. 'array' => null,
  298. ],
  299. ],
  300. [
  301. 'model' => [
  302. 'str' => 'A',
  303. 'model' => null,
  304. 'array' => null,
  305. ],
  306. ],
  307. ],
  308. ];
  309. }
  310. }
  311. class MockModel extends Model
  312. {
  313. public $a = 'A';
  314. public $b = '';
  315. public $c = '';
  316. public function __construct()
  317. {
  318. $this->_name['a'] = 'A';
  319. $this->_required['c'] = true;
  320. parent::__construct([]);
  321. }
  322. }
  323. class ParseModel extends Model
  324. {
  325. public $str;
  326. public $model;
  327. public $array;
  328. }