OpenApiClientTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. "tlsMinVersion" => "TLSv1.2"
  84. ]);
  85. return $config;
  86. }
  87. /**
  88. * @return RuntimeOptions
  89. */
  90. public static function createRuntimeOptions(){
  91. $runtime = new RuntimeOptions([
  92. "readTimeout" => 4000,
  93. "connectTimeout" => 4000,
  94. "maxIdleConns" => 100,
  95. "autoretry" => true,
  96. "maxAttempts" => 1,
  97. "backoffPolicy" => "no",
  98. "backoffPeriod" => 1,
  99. "ignoreSSL" => true
  100. ]);
  101. return $runtime;
  102. }
  103. /**
  104. * @return OpenApiRequest
  105. */
  106. public static function createOpenApiRequest(){
  107. $query = [];
  108. $query["key1"] = "value";
  109. $query["key2"] = 1;
  110. $query["key3"] = true;
  111. $body = [];
  112. $body["key1"] = "value";
  113. $body["key2"] = 1;
  114. $body["key3"] = true;
  115. $headers = [
  116. "for-test" => "sdk"
  117. ];
  118. $req = new OpenApiRequest([
  119. "headers" => $headers,
  120. "query" => OpenApiUtilClient::query($query),
  121. "body" => OpenApiUtilClient::parseToMap($body)
  122. ]);
  123. return $req;
  124. }
  125. public function testCallApiForRPCWithV2Sign_AK_Form(){
  126. $config = self::createConfig();
  127. $runtime = self::createRuntimeOptions();
  128. $config->protocol = "HTTP";
  129. $config->signatureAlgorithm = "v2";
  130. $config->endpoint = "test.aliyuncs.com";
  131. $client = new OpenApiClient($config);
  132. $request = self::createOpenApiRequest();
  133. $params = new Params([
  134. "action" => "TestAPI",
  135. "version" => "2022-06-01",
  136. "protocol" => "HTTPS",
  137. "pathname" => "/",
  138. "method" => "POST",
  139. "authType" => "AK",
  140. "style" => "RPC",
  141. "reqBodyType" => "formData",
  142. "bodyType" => "json"
  143. ]);
  144. $client->callApi($params, $request, $runtime);
  145. }
  146. public function testCallApiForRPCWithV2Sign_Anonymous_JSON(){
  147. $config = self::createConfig();
  148. $runtime = self::createRuntimeOptions();
  149. $config->protocol = "HTTP";
  150. $config->signatureAlgorithm = "v2";
  151. $config->endpoint = "test.aliyuncs.com";
  152. $client = new OpenApiClient($config);
  153. $request = self::createOpenApiRequest();
  154. $params = new Params([
  155. "action" => "TestAPI",
  156. "version" => "2022-06-01",
  157. "protocol" => "HTTPS",
  158. "pathname" => "/",
  159. "method" => "POST",
  160. "authType" => "Anonymous",
  161. "style" => "RPC",
  162. "reqBodyType" => "json",
  163. "bodyType" => "json"
  164. ]);
  165. $client->callApi($params, $request, $runtime);
  166. }
  167. public function testCallApiForROAWithV2Sign_HTTPS_AK_Form(){
  168. $config = self::createConfig();
  169. $runtime = self::createRuntimeOptions();
  170. $config->signatureAlgorithm = "v2";
  171. $config->endpoint = "test.aliyuncs.com";
  172. $client = new OpenApiClient($config);
  173. $request = self::createOpenApiRequest();
  174. $params = new Params([
  175. "action" => "TestAPI",
  176. "version" => "2022-06-01",
  177. "protocol" => "HTTPS",
  178. "pathname" => "/test",
  179. "method" => "POST",
  180. "authType" => "AK",
  181. "style" => "ROA",
  182. "reqBodyType" => "formData",
  183. "bodyType" => "json"
  184. ]);
  185. $client->callApi($params, $request, $runtime);
  186. }
  187. public function testCallApiForROAWithV2Sign_Anonymous_JSON(){
  188. $config = self::createConfig();
  189. $runtime = self::createRuntimeOptions();
  190. $config->protocol = "HTTP";
  191. $config->signatureAlgorithm = "v2";
  192. $config->endpoint = "test.aliyuncs.com";
  193. $client = new OpenApiClient($config);
  194. $request = self::createOpenApiRequest();
  195. $params = new Params([
  196. "action" => "TestAPI",
  197. "version" => "2022-06-01",
  198. "protocol" => "HTTPS",
  199. "pathname" => "/test",
  200. "method" => "POST",
  201. "authType" => "Anonymous",
  202. "style" => "ROA",
  203. "reqBodyType" => "json",
  204. "bodyType" => "json"
  205. ]);
  206. $client->callApi($params, $request, $runtime);
  207. }
  208. public function testCallApiForRPCWithV3Sign_AK_Form(){
  209. $config = self::createConfig();
  210. $runtime = self::createRuntimeOptions();
  211. $config->protocol = "HTTP";
  212. $config->endpoint = "test.aliyuncs.com";
  213. $client = new OpenApiClient($config);
  214. $request = self::createOpenApiRequest();
  215. $params = new Params([
  216. "action" => "TestAPI",
  217. "version" => "2022-06-01",
  218. "protocol" => "HTTPS",
  219. "pathname" => "/",
  220. "method" => "POST",
  221. "authType" => "AK",
  222. "style" => "RPC",
  223. "reqBodyType" => "formData",
  224. "bodyType" => "json"
  225. ]);
  226. $client->callApi($params, $request, $runtime);
  227. }
  228. public function testCallApiForRPCWithV3Sign_Anonymous_JSON(){
  229. $config = self::createConfig();
  230. $runtime = self::createRuntimeOptions();
  231. $config->protocol = "HTTP";
  232. $config->endpoint = "test.aliyuncs.com";
  233. $client = new OpenApiClient($config);
  234. $request = self::createOpenApiRequest();
  235. $params = new Params([
  236. "action" => "TestAPI",
  237. "version" => "2022-06-01",
  238. "protocol" => "HTTPS",
  239. "pathname" => "/",
  240. "method" => "POST",
  241. "authType" => "Anonymous",
  242. "style" => "RPC",
  243. "reqBodyType" => "json",
  244. "bodyType" => "json"
  245. ]);
  246. $client->callApi($params, $request, $runtime);
  247. }
  248. public function testCallApiForROAWithV3Sign_AK_Form(){
  249. $config = self::createConfig();
  250. $runtime = self::createRuntimeOptions();
  251. $config->protocol = "HTTP";
  252. $config->endpoint = "test.aliyuncs.com";
  253. $client = new OpenApiClient($config);
  254. $request = self::createOpenApiRequest();
  255. $params = new Params([
  256. "action" => "TestAPI",
  257. "version" => "2022-06-01",
  258. "protocol" => "HTTPS",
  259. "pathname" => "/test",
  260. "method" => "POST",
  261. "authType" => "AK",
  262. "style" => "ROA",
  263. "reqBodyType" => "formData",
  264. "bodyType" => "json"
  265. ]);
  266. $client->callApi($params, $request, $runtime);
  267. }
  268. public function testCallApiForROAWithV3Sign_Anonymous_JSON(){
  269. $config = self::createConfig();
  270. $runtime = self::createRuntimeOptions();
  271. $config->protocol = "HTTP";
  272. $config->endpoint = "test.aliyuncs.com";
  273. $client = new OpenApiClient($config);
  274. $request = self::createOpenApiRequest();
  275. $params = new Params([
  276. "action" => "TestAPI",
  277. "version" => "2022-06-01",
  278. "protocol" => "HTTPS",
  279. "pathname" => "/test",
  280. "method" => "POST",
  281. "authType" => "Anonymous",
  282. "style" => "ROA",
  283. "reqBodyType" => "json",
  284. "bodyType" => "json"
  285. ]);
  286. $client->callApi($params, $request, $runtime);
  287. }
  288. public function testResponseBodyType(){
  289. $config = self::createConfig();
  290. $runtime = self::createRuntimeOptions();
  291. $config->protocol = "HTTP";
  292. $config->endpoint = "test.aliyuncs.com";
  293. $client = new OpenApiClient($config);
  294. $request = self::createOpenApiRequest();
  295. $params = new Params([
  296. "action" => "TestAPI",
  297. "version" => "2022-06-01",
  298. "protocol" => "HTTPS",
  299. "pathname" => "/test",
  300. "method" => "POST",
  301. "authType" => "AK",
  302. "style" => "ROA",
  303. "reqBodyType" => "formData",
  304. "bodyType" => "json"
  305. ]);
  306. $client->callApi($params, $request, $runtime);
  307. $params->bodyType = "array";
  308. $client->callApi($params, $request, $runtime);
  309. $params->bodyType = "string";
  310. $client->callApi($params, $request, $runtime);
  311. $params->bodyType = "byte";
  312. $client->callApi($params, $request, $runtime);
  313. }
  314. }