OpenApiClientTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace Darabonba\OpenApi\Tests;
  3. use Darabonba\OpenApi\OpenApiClient;
  4. use Darabonba\OpenApi\Utils;
  5. use Darabonba\OpenApi\Models\Config;
  6. use Darabonba\OpenApi\Models\Params;
  7. use Darabonba\OpenApi\Models\GlobalParameters;
  8. use AlibabaCloud\Dara\Models\RuntimeOptions;
  9. use AlibabaCloud\Dara\Models\ExtendsParameters;
  10. use Darabonba\OpenApi\Models\OpenApiRequest;
  11. use AlibabaCloud\Credentials\Credential;
  12. use PHPUnit\Framework\TestCase;
  13. /**
  14. * @internal
  15. * @coversNothing
  16. */
  17. class OpenApiClientTest extends TestCase
  18. {
  19. /**
  20. * @var resource
  21. */
  22. private $pid = 0;
  23. /**
  24. * @before
  25. */
  26. protected function initialize()
  27. {
  28. // $server = dirname(__DIR__). \DIRECTORY_SEPARATOR . 'tests' . \DIRECTORY_SEPARATOR . 'Mock' . \DIRECTORY_SEPARATOR . 'MockServer.php';
  29. // $command = "php $server > /dev/null 2>&1 & echo $!";
  30. // // $command = "php -S localhost:8000 $server";
  31. // $output = shell_exec($command);
  32. // $this->pid = (int)trim($output);
  33. // sleep(2);
  34. }
  35. /**
  36. * @after
  37. */
  38. protected function cleanup()
  39. {
  40. // shell_exec('kill '.$this->pid);
  41. }
  42. public function testConfig(){
  43. $globalParameters = new GlobalParameters([
  44. "headers" => [
  45. "global-key" => "global-value"
  46. ],
  47. "queries" => [
  48. "global-query" => "global-value"
  49. ]
  50. ]);
  51. $config = new Config([
  52. "endpoint" => "config.endpoint",
  53. "endpointType" => "regional",
  54. "network" => "config.network",
  55. "suffix" => "config.suffix",
  56. "protocol" => "config.protocol",
  57. "method" => "config.method",
  58. "regionId" => "config.regionId",
  59. "userAgent" => "config.userAgent",
  60. "readTimeout" => 3000,
  61. "connectTimeout" => 3000,
  62. "httpProxy" => "config.httpProxy",
  63. "httpsProxy" => "config.httpsProxy",
  64. "noProxy" => "config.noProxy",
  65. "socks5Proxy" => "config.socks5Proxy",
  66. "socks5NetWork" => "config.socks5NetWork",
  67. "maxIdleConns" => 128,
  68. "signatureVersion" => "config.signatureVersion",
  69. "signatureAlgorithm" => "config.signatureAlgorithm",
  70. "globalParameters" => $globalParameters
  71. ]);
  72. $creConfig = new \AlibabaCloud\Credentials\Credential\Config([
  73. "accessKeyId" => "accessKeyId",
  74. "accessKeySecret" => "accessKeySecret",
  75. "securityToken" => "securityToken",
  76. "type" => "sts"
  77. ]);
  78. $credential = new Credential($creConfig);
  79. $config->credential = $credential;
  80. $client = new OpenApiClient($config);
  81. $config->accessKeyId = "ak";
  82. $config->accessKeySecret = "secret";
  83. $config->securityToken = "token";
  84. $config->type = "sts";
  85. $client = new OpenApiClient($config);
  86. }
  87. /**
  88. * @return Config
  89. */
  90. public static function createConfig(){
  91. $globalParameters = new GlobalParameters([
  92. "headers" => [
  93. "global-key" => "global-value"
  94. ],
  95. "queries" => [
  96. "global-query" => "global-value"
  97. ]
  98. ]);
  99. $config = new Config([
  100. "accessKeyId" => "ak",
  101. "accessKeySecret" => "secret",
  102. "securityToken" => "token",
  103. "type" => "sts",
  104. "userAgent" => "config.userAgent",
  105. "readTimeout" => 3000,
  106. "connectTimeout" => 3000,
  107. "maxIdleConns" => 128,
  108. "signatureVersion" => "config.signatureVersion",
  109. "signatureAlgorithm" => "ACS3-HMAC-SHA256",
  110. "globalParameters" => $globalParameters
  111. ]);
  112. return $config;
  113. }
  114. /**
  115. * @return RuntimeOptions
  116. */
  117. public static function createRuntimeOptions(){
  118. $runtime = new RuntimeOptions([
  119. "readTimeout" => 4000,
  120. "connectTimeout" => 4000,
  121. "maxIdleConns" => 100,
  122. "autoretry" => true,
  123. "maxAttempts" => 1,
  124. "backoffPolicy" => "no",
  125. "backoffPeriod" => 1,
  126. "ignoreSSL" => true,
  127. "extendsParameters" => new ExtendsParameters([
  128. "headers" => [
  129. "extends-key" => "extends-value"
  130. ],
  131. ])
  132. ]);
  133. return $runtime;
  134. }
  135. /**
  136. * @return OpenApiRequest
  137. */
  138. public static function createOpenApiRequest(){
  139. $query = [];
  140. $query["key1"] = "value";
  141. $query["key2"] = 1;
  142. $query["key3"] = true;
  143. $body = [];
  144. $body["key1"] = "value";
  145. $body["key2"] = 1;
  146. $body["key3"] = true;
  147. $headers = [
  148. "for-test" => "sdk"
  149. ];
  150. $req = new OpenApiRequest([
  151. "headers" => $headers,
  152. "query" => Utils::query($query),
  153. "body" => Utils::parseToMap($body)
  154. ]);
  155. return $req;
  156. }
  157. public function testCallApiForRPCWithV2Sign_AK_Form(){
  158. $config = self::createConfig();
  159. $runtime = self::createRuntimeOptions();
  160. $config->protocol = "HTTP";
  161. $config->signatureAlgorithm = "v2";
  162. $config->endpoint = "test.aliyuncs.com";
  163. $client = new OpenApiClient($config);
  164. $request = self::createOpenApiRequest();
  165. $params = new Params([
  166. "action" => "TestAPI",
  167. "version" => "2022-06-01",
  168. "protocol" => "HTTPS",
  169. "pathname" => "/",
  170. "method" => "POST",
  171. "authType" => "AK",
  172. "style" => "RPC",
  173. "reqBodyType" => "formData",
  174. "bodyType" => "json"
  175. ]);
  176. $client->callApi($params, $request, $runtime);
  177. }
  178. public function testCallApiForRPCWithV2Sign_Anonymous_JSON(){
  179. $config = self::createConfig();
  180. $runtime = self::createRuntimeOptions();
  181. $config->protocol = "HTTP";
  182. $config->signatureAlgorithm = "v2";
  183. $config->endpoint = "test.aliyuncs.com";
  184. $client = new OpenApiClient($config);
  185. $request = self::createOpenApiRequest();
  186. $params = new Params([
  187. "action" => "TestAPI",
  188. "version" => "2022-06-01",
  189. "protocol" => "HTTPS",
  190. "pathname" => "/",
  191. "method" => "POST",
  192. "authType" => "Anonymous",
  193. "style" => "RPC",
  194. "reqBodyType" => "json",
  195. "bodyType" => "json"
  196. ]);
  197. $client->callApi($params, $request, $runtime);
  198. }
  199. public function testCallApiForROAWithV2Sign_HTTPS_AK_Form(){
  200. $config = self::createConfig();
  201. $runtime = self::createRuntimeOptions();
  202. $config->signatureAlgorithm = "v2";
  203. $config->endpoint = "test.aliyuncs.com";
  204. $client = new OpenApiClient($config);
  205. $request = self::createOpenApiRequest();
  206. $params = new Params([
  207. "action" => "TestAPI",
  208. "version" => "2022-06-01",
  209. "protocol" => "HTTPS",
  210. "pathname" => "/test",
  211. "method" => "POST",
  212. "authType" => "AK",
  213. "style" => "ROA",
  214. "reqBodyType" => "formData",
  215. "bodyType" => "json"
  216. ]);
  217. $client->callApi($params, $request, $runtime);
  218. }
  219. public function testCallApiForROAWithV2Sign_Anonymous_JSON(){
  220. $config = self::createConfig();
  221. $runtime = self::createRuntimeOptions();
  222. $config->protocol = "HTTP";
  223. $config->signatureAlgorithm = "v2";
  224. $config->endpoint = "test.aliyuncs.com";
  225. $client = new OpenApiClient($config);
  226. $request = self::createOpenApiRequest();
  227. $params = new Params([
  228. "action" => "TestAPI",
  229. "version" => "2022-06-01",
  230. "protocol" => "HTTPS",
  231. "pathname" => "/test",
  232. "method" => "POST",
  233. "authType" => "Anonymous",
  234. "style" => "ROA",
  235. "reqBodyType" => "json",
  236. "bodyType" => "json"
  237. ]);
  238. $client->callApi($params, $request, $runtime);
  239. }
  240. public function testCallApiForRPCWithV3Sign_AK_Form(){
  241. $config = self::createConfig();
  242. $runtime = self::createRuntimeOptions();
  243. $config->protocol = "HTTP";
  244. $config->endpoint = "test.aliyuncs.com";
  245. $client = new OpenApiClient($config);
  246. $request = self::createOpenApiRequest();
  247. $params = new Params([
  248. "action" => "TestAPI",
  249. "version" => "2022-06-01",
  250. "protocol" => "HTTPS",
  251. "pathname" => "/",
  252. "method" => "POST",
  253. "authType" => "AK",
  254. "style" => "RPC",
  255. "reqBodyType" => "formData",
  256. "bodyType" => "json"
  257. ]);
  258. $client->callApi($params, $request, $runtime);
  259. }
  260. public function testCallApiForRPCWithV3Sign_Anonymous_JSON(){
  261. $config = self::createConfig();
  262. $runtime = self::createRuntimeOptions();
  263. $config->protocol = "HTTP";
  264. $config->endpoint = "test.aliyuncs.com";
  265. $client = new OpenApiClient($config);
  266. $request = self::createOpenApiRequest();
  267. $params = new Params([
  268. "action" => "TestAPI",
  269. "version" => "2022-06-01",
  270. "protocol" => "HTTPS",
  271. "pathname" => "/",
  272. "method" => "POST",
  273. "authType" => "Anonymous",
  274. "style" => "RPC",
  275. "reqBodyType" => "json",
  276. "bodyType" => "json"
  277. ]);
  278. $client->callApi($params, $request, $runtime);
  279. }
  280. public function testCallApiForROAWithV3Sign_AK_Form(){
  281. $config = self::createConfig();
  282. $runtime = self::createRuntimeOptions();
  283. $config->protocol = "HTTP";
  284. $config->endpoint = "test.aliyuncs.com";
  285. $client = new OpenApiClient($config);
  286. $request = self::createOpenApiRequest();
  287. $params = new Params([
  288. "action" => "TestAPI",
  289. "version" => "2022-06-01",
  290. "protocol" => "HTTPS",
  291. "pathname" => "/test",
  292. "method" => "POST",
  293. "authType" => "AK",
  294. "style" => "ROA",
  295. "reqBodyType" => "formData",
  296. "bodyType" => "json"
  297. ]);
  298. $client->callApi($params, $request, $runtime);
  299. }
  300. public function testCallApiForROAWithV3Sign_Anonymous_JSON(){
  301. $config = self::createConfig();
  302. $runtime = self::createRuntimeOptions();
  303. $config->protocol = "HTTP";
  304. $config->endpoint = "test.aliyuncs.com";
  305. $client = new OpenApiClient($config);
  306. $request = self::createOpenApiRequest();
  307. $params = new Params([
  308. "action" => "TestAPI",
  309. "version" => "2022-06-01",
  310. "protocol" => "HTTPS",
  311. "pathname" => "/test",
  312. "method" => "POST",
  313. "authType" => "Anonymous",
  314. "style" => "ROA",
  315. "reqBodyType" => "json",
  316. "bodyType" => "json"
  317. ]);
  318. $client->callApi($params, $request, $runtime);
  319. }
  320. public function testResponseBodyType(){
  321. $config = self::createConfig();
  322. $runtime = self::createRuntimeOptions();
  323. $config->protocol = "HTTP";
  324. $config->endpoint = "test.aliyuncs.com";
  325. $client = new OpenApiClient($config);
  326. $request = self::createOpenApiRequest();
  327. $params = new Params([
  328. "action" => "TestAPI",
  329. "version" => "2022-06-01",
  330. "protocol" => "HTTPS",
  331. "pathname" => "/test",
  332. "method" => "POST",
  333. "authType" => "AK",
  334. "style" => "ROA",
  335. "reqBodyType" => "formData",
  336. "bodyType" => "json"
  337. ]);
  338. $client->callApi($params, $request, $runtime);
  339. $params->bodyType = "array";
  340. $client->callApi($params, $request, $runtime);
  341. $params->bodyType = "string";
  342. $client->callApi($params, $request, $runtime);
  343. $params->bodyType = "byte";
  344. $client->callApi($params, $request, $runtime);
  345. }
  346. public function testCallSSEApiWithSignV3()
  347. {
  348. $config = self::createConfig();
  349. $runtime = self::createRuntimeOptions();
  350. $config->protocol = "HTTP";
  351. $config->endpoint = "127.0.0.1:8000";
  352. $client = new OpenApiClient($config);
  353. $request = self::createOpenApiRequest();
  354. $params = new Params([
  355. "action" => "TestAPI",
  356. "version" => "2022-06-01",
  357. "protocol" => "HTTPS",
  358. "pathname" => "/sse",
  359. "method" => "POST",
  360. "authType" => "AK",
  361. "style" => "ROA",
  362. "reqBodyType" => "json",
  363. "bodyType" => "sse"
  364. ]);
  365. $response = $client->callSSEApi($params, $request, $runtime);
  366. // Add more assertions as needed
  367. $events = [];
  368. // SSE events are typically separated by double newline
  369. foreach ($response as $event) {
  370. $this->assertEquals(200, $event->statusCode);
  371. $headers = $event->headers;
  372. $this->assertEquals('text/event-stream;charset=UTF-8', $headers['Content-Type'][0]);
  373. $this->assertEquals('sdk', $headers['for-test'][0]);
  374. $userAgentArray = explode(' ', $headers['user-agent'][0]);
  375. $this->assertEquals('config.userAgent', end($userAgentArray));
  376. $this->assertEquals('global-value', $headers['global-key'][0]);
  377. $this->assertEquals('extends-value', $headers['extends-key'][0]);
  378. $this->assertNotEmpty($headers['x-acs-signature-nonce'][0]);
  379. $this->assertNotEmpty($headers['x-acs-date'][0]);
  380. $this->assertEquals('application/json', $headers['accept'][0]);
  381. $event = $event->event->toArray();
  382. // var_dump($event);
  383. $events[] = json_decode($event['data'], true);
  384. }
  385. $expectedEvents = [
  386. ['count' => 0],
  387. ['count' => 1],
  388. ['count' => 2],
  389. ['count' => 3],
  390. ['count' => 4],
  391. ];
  392. $this->assertCount(5, $events);
  393. $this->assertEquals($expectedEvents, $events);
  394. }
  395. }