OpenApiClientTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. namespace Darabonba\OpenApi\Tests;
  3. use Darabonba\OpenApi\OpenApiClient;
  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 OpenApiClientTest extends TestCase
  13. {
  14. public function testConfig(){
  15. $globalParameters = new GlobalParameters([
  16. "headers" => [
  17. "global-key" => "global-value"
  18. ],
  19. "queries" => [
  20. "global-query" => "global-value"
  21. ]
  22. ]);
  23. $config = new Config([
  24. "endpoint" => "config.endpoint",
  25. "endpointType" => "regional",
  26. "network" => "config.network",
  27. "suffix" => "config.suffix",
  28. "protocol" => "config.protocol",
  29. "method" => "config.method",
  30. "regionId" => "config.regionId",
  31. "userAgent" => "config.userAgent",
  32. "readTimeout" => 3000,
  33. "connectTimeout" => 3000,
  34. "httpProxy" => "config.httpProxy",
  35. "httpsProxy" => "config.httpsProxy",
  36. "noProxy" => "config.noProxy",
  37. "socks5Proxy" => "config.socks5Proxy",
  38. "socks5NetWork" => "config.socks5NetWork",
  39. "maxIdleConns" => 128,
  40. "signatureVersion" => "config.signatureVersion",
  41. "signatureAlgorithm" => "config.signatureAlgorithm",
  42. "globalParameters" => $globalParameters
  43. ]);
  44. $creConfig = new \AlibabaCloud\Credentials\Credential\Config([
  45. "accessKeyId" => "accessKeyId",
  46. "accessKeySecret" => "accessKeySecret",
  47. "securityToken" => "securityToken",
  48. "type" => "sts"
  49. ]);
  50. $credential = new Credential($creConfig);
  51. $config->credential = $credential;
  52. $client = new OpenApiClient($config);
  53. $config->accessKeyId = "ak";
  54. $config->accessKeySecret = "secret";
  55. $config->securityToken = "token";
  56. $config->type = "sts";
  57. $client = new OpenApiClient($config);
  58. }
  59. /**
  60. * @return Config
  61. */
  62. public static function createConfig(){
  63. $globalParameters = new GlobalParameters([
  64. "headers" => [
  65. "global-key" => "global-value"
  66. ],
  67. "queries" => [
  68. "global-query" => "global-value"
  69. ]
  70. ]);
  71. $config = new Config([
  72. "accessKeyId" => "ak",
  73. "accessKeySecret" => "secret",
  74. "securityToken" => "token",
  75. "type" => "sts",
  76. "userAgent" => "config.userAgent",
  77. "readTimeout" => 3000,
  78. "connectTimeout" => 3000,
  79. "maxIdleConns" => 128,
  80. "signatureVersion" => "config.signatureVersion",
  81. "signatureAlgorithm" => "ACS3-HMAC-SHA256",
  82. "globalParameters" => $globalParameters
  83. ]);
  84. return $config;
  85. }
  86. /**
  87. * @return RuntimeOptions
  88. */
  89. public static function createRuntimeOptions(){
  90. $runtime = new RuntimeOptions([
  91. "readTimeout" => 4000,
  92. "connectTimeout" => 4000,
  93. "maxIdleConns" => 100,
  94. "autoretry" => true,
  95. "maxAttempts" => 1,
  96. "backoffPolicy" => "no",
  97. "backoffPeriod" => 1,
  98. "ignoreSSL" => true
  99. ]);
  100. return $runtime;
  101. }
  102. /**
  103. * @return OpenApiRequest
  104. */
  105. public static function createOpenApiRequest(){
  106. $query = [];
  107. $query["key1"] = "value";
  108. $query["key2"] = 1;
  109. $query["key3"] = true;
  110. $body = [];
  111. $body["key1"] = "value";
  112. $body["key2"] = 1;
  113. $body["key3"] = true;
  114. $headers = [
  115. "for-test" => "sdk"
  116. ];
  117. $req = new OpenApiRequest([
  118. "headers" => $headers,
  119. "query" => OpenApiUtilClient::query($query),
  120. "body" => OpenApiUtilClient::parseToMap($body)
  121. ]);
  122. return $req;
  123. }
  124. public function testCallApiForRPCWithV2Sign_AK_Form(){
  125. $config = self::createConfig();
  126. $runtime = self::createRuntimeOptions();
  127. $config->protocol = "HTTP";
  128. $config->signatureAlgorithm = "v2";
  129. $config->endpoint = "test.aliyuncs.com";
  130. $client = new OpenApiClient($config);
  131. $request = self::createOpenApiRequest();
  132. $params = new Params([
  133. "action" => "TestAPI",
  134. "version" => "2022-06-01",
  135. "protocol" => "HTTPS",
  136. "pathname" => "/",
  137. "method" => "POST",
  138. "authType" => "AK",
  139. "style" => "RPC",
  140. "reqBodyType" => "formData",
  141. "bodyType" => "json"
  142. ]);
  143. $client->callApi($params, $request, $runtime);
  144. }
  145. public function testCallApiForRPCWithV2Sign_Anonymous_JSON(){
  146. $config = self::createConfig();
  147. $runtime = self::createRuntimeOptions();
  148. $config->protocol = "HTTP";
  149. $config->signatureAlgorithm = "v2";
  150. $config->endpoint = "test.aliyuncs.com";
  151. $client = new OpenApiClient($config);
  152. $request = self::createOpenApiRequest();
  153. $params = new Params([
  154. "action" => "TestAPI",
  155. "version" => "2022-06-01",
  156. "protocol" => "HTTPS",
  157. "pathname" => "/",
  158. "method" => "POST",
  159. "authType" => "Anonymous",
  160. "style" => "RPC",
  161. "reqBodyType" => "json",
  162. "bodyType" => "json"
  163. ]);
  164. $client->callApi($params, $request, $runtime);
  165. }
  166. public function testCallApiForROAWithV2Sign_HTTPS_AK_Form(){
  167. $config = self::createConfig();
  168. $runtime = self::createRuntimeOptions();
  169. $config->signatureAlgorithm = "v2";
  170. $config->endpoint = "test.aliyuncs.com";
  171. $client = new OpenApiClient($config);
  172. $request = self::createOpenApiRequest();
  173. $params = new Params([
  174. "action" => "TestAPI",
  175. "version" => "2022-06-01",
  176. "protocol" => "HTTPS",
  177. "pathname" => "/test",
  178. "method" => "POST",
  179. "authType" => "AK",
  180. "style" => "ROA",
  181. "reqBodyType" => "formData",
  182. "bodyType" => "json"
  183. ]);
  184. $client->callApi($params, $request, $runtime);
  185. }
  186. public function testCallApiForROAWithV2Sign_Anonymous_JSON(){
  187. $config = self::createConfig();
  188. $runtime = self::createRuntimeOptions();
  189. $config->protocol = "HTTP";
  190. $config->signatureAlgorithm = "v2";
  191. $config->endpoint = "test.aliyuncs.com";
  192. $client = new OpenApiClient($config);
  193. $request = self::createOpenApiRequest();
  194. $params = new Params([
  195. "action" => "TestAPI",
  196. "version" => "2022-06-01",
  197. "protocol" => "HTTPS",
  198. "pathname" => "/test",
  199. "method" => "POST",
  200. "authType" => "Anonymous",
  201. "style" => "ROA",
  202. "reqBodyType" => "json",
  203. "bodyType" => "json"
  204. ]);
  205. $client->callApi($params, $request, $runtime);
  206. }
  207. public function testCallApiForRPCWithV3Sign_AK_Form(){
  208. $config = self::createConfig();
  209. $runtime = self::createRuntimeOptions();
  210. $config->protocol = "HTTP";
  211. $config->endpoint = "test.aliyuncs.com";
  212. $client = new OpenApiClient($config);
  213. $request = self::createOpenApiRequest();
  214. $params = new Params([
  215. "action" => "TestAPI",
  216. "version" => "2022-06-01",
  217. "protocol" => "HTTPS",
  218. "pathname" => "/",
  219. "method" => "POST",
  220. "authType" => "AK",
  221. "style" => "RPC",
  222. "reqBodyType" => "formData",
  223. "bodyType" => "json"
  224. ]);
  225. $client->callApi($params, $request, $runtime);
  226. }
  227. public function testCallApiForRPCWithV3Sign_Anonymous_JSON(){
  228. $config = self::createConfig();
  229. $runtime = self::createRuntimeOptions();
  230. $config->protocol = "HTTP";
  231. $config->endpoint = "test.aliyuncs.com";
  232. $client = new OpenApiClient($config);
  233. $request = self::createOpenApiRequest();
  234. $params = new Params([
  235. "action" => "TestAPI",
  236. "version" => "2022-06-01",
  237. "protocol" => "HTTPS",
  238. "pathname" => "/",
  239. "method" => "POST",
  240. "authType" => "Anonymous",
  241. "style" => "RPC",
  242. "reqBodyType" => "json",
  243. "bodyType" => "json"
  244. ]);
  245. $client->callApi($params, $request, $runtime);
  246. }
  247. public function testCallApiForROAWithV3Sign_AK_Form(){
  248. $config = self::createConfig();
  249. $runtime = self::createRuntimeOptions();
  250. $config->protocol = "HTTP";
  251. $config->endpoint = "test.aliyuncs.com";
  252. $client = new OpenApiClient($config);
  253. $request = self::createOpenApiRequest();
  254. $params = new Params([
  255. "action" => "TestAPI",
  256. "version" => "2022-06-01",
  257. "protocol" => "HTTPS",
  258. "pathname" => "/test",
  259. "method" => "POST",
  260. "authType" => "AK",
  261. "style" => "ROA",
  262. "reqBodyType" => "formData",
  263. "bodyType" => "json"
  264. ]);
  265. $client->callApi($params, $request, $runtime);
  266. }
  267. public function testCallApiForROAWithV3Sign_Anonymous_JSON(){
  268. $config = self::createConfig();
  269. $runtime = self::createRuntimeOptions();
  270. $config->protocol = "HTTP";
  271. $config->endpoint = "test.aliyuncs.com";
  272. $client = new OpenApiClient($config);
  273. $request = self::createOpenApiRequest();
  274. $params = new Params([
  275. "action" => "TestAPI",
  276. "version" => "2022-06-01",
  277. "protocol" => "HTTPS",
  278. "pathname" => "/test",
  279. "method" => "POST",
  280. "authType" => "Anonymous",
  281. "style" => "ROA",
  282. "reqBodyType" => "json",
  283. "bodyType" => "json"
  284. ]);
  285. $client->callApi($params, $request, $runtime);
  286. }
  287. public function testResponseBodyType(){
  288. $config = self::createConfig();
  289. $runtime = self::createRuntimeOptions();
  290. $config->protocol = "HTTP";
  291. $config->endpoint = "test.aliyuncs.com";
  292. $client = new OpenApiClient($config);
  293. $request = self::createOpenApiRequest();
  294. $params = new Params([
  295. "action" => "TestAPI",
  296. "version" => "2022-06-01",
  297. "protocol" => "HTTPS",
  298. "pathname" => "/test",
  299. "method" => "POST",
  300. "authType" => "AK",
  301. "style" => "ROA",
  302. "reqBodyType" => "formData",
  303. "bodyType" => "json"
  304. ]);
  305. $client->callApi($params, $request, $runtime);
  306. $params->bodyType = "array";
  307. $client->callApi($params, $request, $runtime);
  308. $params->bodyType = "string";
  309. $client->callApi($params, $request, $runtime);
  310. $params->bodyType = "byte";
  311. $client->callApi($params, $request, $runtime);
  312. }
  313. }