AcsTraitTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Request;
  3. use AlibabaCloud\Client\AlibabaCloud;
  4. use AlibabaCloud\Client\Exception\ClientException;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\Client\Request\RpcRequest;
  7. use PHPUnit\Framework\TestCase;
  8. use InvalidArgumentException;
  9. /**
  10. * Class AcsTraitTest
  11. *
  12. * @package AlibabaCloud\Client\Tests\Unit\Request
  13. *
  14. * @coversDefaultClass \AlibabaCloud\Client\Request\Request
  15. */
  16. class AcsTraitTest extends TestCase
  17. {
  18. /**
  19. * @after
  20. */
  21. protected function finalize()
  22. {
  23. parent::tearDown();
  24. AlibabaCloud::cancelMock();
  25. }
  26. /**
  27. * @throws ClientException
  28. */
  29. public function testAction()
  30. {
  31. // Setup
  32. $action = 'action';
  33. $request = new RpcRequest();
  34. // Test
  35. $request->action($action);
  36. // Assert
  37. self::assertEquals($action, $request->action);
  38. }
  39. /**
  40. * @throws ClientException
  41. */
  42. public function testActionWithEmpty()
  43. {
  44. $this->expectException(ClientException::class);
  45. $this->expectExceptionMessage("Action cannot be empty");
  46. // Setup
  47. $request = new RpcRequest();
  48. // Test
  49. $request->action('');
  50. }
  51. /**
  52. * @throws ClientException
  53. */
  54. public function testActionWithFormat()
  55. {
  56. $this->expectException(ClientException::class);
  57. $this->expectExceptionMessage("Action must be a string");
  58. // Setup
  59. $request = new RpcRequest();
  60. // Test
  61. $request->action(null);
  62. }
  63. /**
  64. * @throws ClientException
  65. */
  66. public function testVersion()
  67. {
  68. // Setup
  69. $version = 'version';
  70. $request = new RpcRequest();
  71. // Test
  72. $request->version($version);
  73. // Assert
  74. self::assertEquals($version, $request->version);
  75. }
  76. /**
  77. * @throws ClientException
  78. */
  79. public function testVersionWithEmpty()
  80. {
  81. $this->expectException(ClientException::class);
  82. $this->expectExceptionMessage("Version cannot be empty");
  83. // Setup
  84. $request = new RpcRequest();
  85. // Test
  86. $request->version('');
  87. }
  88. /**
  89. * @throws ClientException
  90. */
  91. public function testVersionWithFormat()
  92. {
  93. $this->expectException(ClientException::class);
  94. $this->expectExceptionMessage("Version must be a string");
  95. // Setup
  96. $request = new RpcRequest();
  97. // Test
  98. $request->version(null);
  99. }
  100. /**
  101. * @throws ClientException
  102. */
  103. public function testProduct()
  104. {
  105. // Setup
  106. $product = 'product';
  107. $request = new RpcRequest();
  108. // Test
  109. $request->product($product);
  110. // Assert
  111. self::assertEquals($product, $request->product);
  112. }
  113. public function testNetwork()
  114. {
  115. // Setup
  116. $network = 'vpc';
  117. $request = new RpcRequest();
  118. // Test
  119. $request->network($network);
  120. // Assert
  121. self::assertEquals($network, $request->network);
  122. }
  123. /**
  124. * @throws ClientException
  125. */
  126. public function testProductWithEmpty()
  127. {
  128. $this->expectException(ClientException::class);
  129. $this->expectExceptionMessage("Product cannot be empty");
  130. // Setup
  131. $request = new RpcRequest();
  132. // Test
  133. $request->product('');
  134. }
  135. /**
  136. * @throws ClientException
  137. */
  138. public function testProductWithFormat()
  139. {
  140. $this->expectException(ClientException::class);
  141. $this->expectExceptionMessage("Product must be a string");
  142. // Setup
  143. $request = new RpcRequest();
  144. // Test
  145. $request->product(null);
  146. }
  147. /**
  148. * @throws ClientException
  149. */
  150. public function testLocationEndpointType()
  151. {
  152. // Setup
  153. $endpointType = 'endpointType';
  154. $request = new RpcRequest();
  155. // Test
  156. $request->endpointType($endpointType);
  157. // Assert
  158. self::assertEquals(
  159. $endpointType,
  160. $request->endpointType
  161. );
  162. }
  163. /**
  164. * @throws ClientException
  165. */
  166. public function testLocationEndpointTypeEmpty()
  167. {
  168. $this->expectException(ClientException::class);
  169. $this->expectExceptionMessage("Endpoint Type cannot be empty");
  170. // Setup
  171. $request = new RpcRequest();
  172. // Test
  173. $request->endpointType('');
  174. }
  175. /**
  176. * @throws ClientException
  177. */
  178. public function testLocationEndpointTypeFormat()
  179. {
  180. $this->expectException(ClientException::class);
  181. $this->expectExceptionMessage("Endpoint Type must be a string");
  182. // Setup
  183. $request = new RpcRequest();
  184. // Test
  185. $request->endpointType(null);
  186. }
  187. /**
  188. * @throws ClientException
  189. */
  190. public function testLocationServiceCode()
  191. {
  192. // Setup
  193. $serviceCode = 'serviceCode';
  194. $request = new RpcRequest();
  195. // Test
  196. $request->serviceCode($serviceCode);
  197. // Assert
  198. self::assertEquals(
  199. $serviceCode,
  200. $request->serviceCode
  201. );
  202. }
  203. /**
  204. * @throws ClientException
  205. */
  206. public function testServiceCodeEmpty()
  207. {
  208. $this->expectException(ClientException::class);
  209. $this->expectExceptionMessage("Service Code cannot be empty");
  210. // Setup
  211. $request = new RpcRequest();
  212. // Test
  213. $request->serviceCode('');
  214. }
  215. /**
  216. * @throws ClientException
  217. */
  218. public function testServiceCodeFormat()
  219. {
  220. $this->expectException(ClientException::class);
  221. $this->expectExceptionMessage("Service Code must be a string");
  222. // Setup
  223. $request = new RpcRequest();
  224. // Test
  225. $request->serviceCode(null);
  226. }
  227. /**
  228. * @throws ClientException
  229. */
  230. public function testRealRegionIdOnRequest()
  231. {
  232. // Setup
  233. $regionId = 'regionId';
  234. $request = new RpcRequest();
  235. // Test
  236. $request->regionId($regionId);
  237. // Assert
  238. self::assertEquals(
  239. strtolower($regionId),
  240. $request->realRegionId()
  241. );
  242. }
  243. /**
  244. * @throws ClientException
  245. */
  246. public function testRegionIdEmpty()
  247. {
  248. $this->expectException(ClientException::class);
  249. $this->expectExceptionMessage("Region ID cannot be empty");
  250. // Setup
  251. $regionId = '';
  252. $request = new RpcRequest();
  253. // Test
  254. $request->regionId($regionId);
  255. }
  256. /**
  257. * @throws ClientException
  258. */
  259. public function testRegionIdFormat()
  260. {
  261. $this->expectException(ClientException::class);
  262. $this->expectExceptionMessage("Region ID must be a string");
  263. // Setup
  264. $regionId = null;
  265. $request = new RpcRequest();
  266. // Test
  267. $request->regionId($regionId);
  268. }
  269. /**
  270. * @throws ClientException
  271. */
  272. public function testTimeoutEmpty()
  273. {
  274. $this->expectException(ClientException::class);
  275. $this->expectExceptionMessage("Timeout cannot be empty");
  276. // Setup
  277. $request = new RpcRequest();
  278. // Test
  279. $request->timeout('');
  280. }
  281. /**
  282. * @throws ClientException
  283. */
  284. public function testConnectTimeout()
  285. {
  286. $this->expectException(ClientException::class);
  287. $this->expectExceptionMessage("Connect Timeout cannot be empty");
  288. // Setup
  289. $request = new RpcRequest();
  290. // Test
  291. $request->connectTimeout('');
  292. }
  293. /**
  294. * @throws ClientException
  295. */
  296. public function testRealRegionIdOnClient()
  297. {
  298. // Setup
  299. $regionId = 'regionId';
  300. AlibabaCloud::accessKeyClient('foo', 'bar')
  301. ->regionId($regionId)
  302. ->name('regionId');
  303. $request = new RpcRequest();
  304. // Test
  305. $request->client('regionId');
  306. // Assert
  307. self::assertEquals(
  308. strtolower($regionId),
  309. $request->realRegionId()
  310. );
  311. }
  312. /**
  313. * @throws ClientException
  314. */
  315. public function testRealRegionIdOnDefault()
  316. {
  317. // Setup
  318. $regionId = 'regionId';
  319. AlibabaCloud::accessKeyClient('foo', 'bar')
  320. ->name('regionId');
  321. AlibabaCloud::setDefaultRegionId($regionId);
  322. // Test
  323. $request = new RpcRequest();
  324. $request->client('regionId');
  325. // Assert
  326. self::assertEquals(
  327. strtolower($regionId),
  328. $request->realRegionId()
  329. );
  330. }
  331. /**
  332. * @throws ClientException
  333. * @throws ServerException
  334. */
  335. public function testEndpointMap()
  336. {
  337. // Setup
  338. $request = AlibabaCloud::rpc();
  339. $region = 'cn-hangzhou';
  340. $request->regionId($region);
  341. $request->endpointMap[$region] = 'b.com';
  342. // Test
  343. $request->resolveHost();
  344. self::assertEquals('http://b.com', (string) $request->uri);
  345. // Setup
  346. $request = AlibabaCloud::rpc();
  347. $region = 'cn-shanghai';
  348. $request->regionId($region);
  349. $request->product('ecs');
  350. // Test
  351. $request->resolveHost();
  352. self::assertEquals('http://ecs-cn-hangzhou.aliyuncs.com', (string) $request->uri);
  353. }
  354. /**
  355. * @throws ClientException
  356. * @throws ServerException
  357. */
  358. public function testEndpointRegional()
  359. {
  360. // Setup
  361. $request = AlibabaCloud::rpc();
  362. $region = 'cn-hangzhou';
  363. $request->regionId($region);
  364. $request->product('ecs');
  365. $request->endpointRegional = 'regional';
  366. // Test
  367. $request->resolveHost();
  368. self::assertEquals('http://ecs.cn-hangzhou.aliyuncs.com', (string) $request->uri);
  369. }
  370. /**
  371. * @throws ClientException
  372. * @throws ServerException
  373. */
  374. public function testEndpointCentral()
  375. {
  376. // Setup
  377. $request = AlibabaCloud::rpc();
  378. $region = 'cn-hangzhou';
  379. $request->regionId($region);
  380. $request->product('ecs');
  381. $request->endpointRegional = 'central';
  382. // Test
  383. $request->resolveHost();
  384. self::assertEquals('http://ecs.aliyuncs.com', (string) $request->uri);
  385. }
  386. /**
  387. * @throws ClientException
  388. * @throws ServerException
  389. */
  390. public function testEndpointRegionalRnvalid()
  391. {
  392. $this->expectException(InvalidArgumentException::class);
  393. $this->expectExceptionMessage("endpointRegional is invalid.");
  394. // Setup
  395. $request = AlibabaCloud::rpc();
  396. $region = 'cn-hangzhou';
  397. $request->regionId($region);
  398. $request->product('ecs');
  399. $request->endpointRegional = 'invalid';
  400. // Test
  401. $request->resolveHost();
  402. }
  403. /**
  404. * @throws ClientException
  405. */
  406. public function testRealRegionIdException()
  407. {
  408. $this->expectException(ClientException::class);
  409. $this->expectExceptionMessage("Missing required 'RegionId' for Request");
  410. // Setup
  411. AlibabaCloud::flush();
  412. AlibabaCloud::accessKeyClient('foo', 'bar')
  413. ->name('regionId');
  414. // Test
  415. $request = new RpcRequest();
  416. $request->client('regionId');
  417. $request->realRegionId();
  418. }
  419. /**
  420. * @throws ClientException
  421. */
  422. public function testSetDefaultRegionIdNull()
  423. {
  424. $this->expectException(ClientException::class);
  425. $this->expectExceptionMessage("Region ID must be a string");
  426. // Test
  427. AlibabaCloud::accessKeyClient('foo', 'bar')
  428. ->name('regionId');
  429. AlibabaCloud::setDefaultRegionId(null);
  430. }
  431. /**
  432. * @throws ClientException
  433. */
  434. public function testSetDefaultRegionIdEmpty()
  435. {
  436. $this->expectException(ClientException::class);
  437. $this->expectExceptionMessage("Region ID cannot be empty");
  438. // Test
  439. AlibabaCloud::accessKeyClient('foo', 'bar')
  440. ->name('regionId');
  441. AlibabaCloud::setDefaultRegionId('');
  442. }
  443. /**
  444. * @throws ClientException
  445. * @throws ServerException
  446. */
  447. public function testFindDomainInConfig()
  448. {
  449. // Setup
  450. $request = new RpcRequest();
  451. $request->product('ecs');
  452. $request->regionId('eu-central-1');
  453. // Test
  454. $request->resolveHost();
  455. // Assert
  456. self::assertEquals(
  457. 'ecs.eu-central-1.aliyuncs.com',
  458. $request->uri->getHost()
  459. );
  460. }
  461. /**
  462. * @throws ClientException
  463. * @throws ServerException
  464. */
  465. public function testFindDomainOnLocationService()
  466. {
  467. // Setup
  468. $body = [
  469. 'Endpoints' => [
  470. 'Endpoint' => [
  471. 0 => [
  472. 'Endpoint' => 'ecs-cn-hangzhou.aliyuncs.com',
  473. ],
  474. ],
  475. ],
  476. ];
  477. AlibabaCloud::mockResponse(200, [], $body);
  478. AlibabaCloud::accessKeyClient('foo', 'bar')->asDefaultClient()->regionId('cn-hangzhou');
  479. // Test
  480. $request = new RpcRequest();
  481. $request->product('ecs2');
  482. $request->serviceCode('ecs');
  483. // Assert
  484. $request->resolveHost();
  485. self::assertEquals('ecs-cn-hangzhou.aliyuncs.com', $request->uri->getHost());
  486. }
  487. /**
  488. * @throws ClientException
  489. * @throws ServerException
  490. */
  491. public function testFindDomainOnLocationServiceWithEmpty()
  492. {
  493. $this->expectException(ClientException::class);
  494. $this->expectExceptionMessage("No host found for no in the cn-hangzhou, you can specify host by host() method. Like \$request->host('xxx.xxx.aliyuncs.com')");
  495. // Setup
  496. $body = [
  497. 'Endpoints' => [
  498. 'Endpoint' => [
  499. 0 => [
  500. 'Endpoint' => '',
  501. ],
  502. ],
  503. ],
  504. ];
  505. AlibabaCloud::mockResponse(200, [], $body);
  506. AlibabaCloud::setDefaultRegionId('cn-hangzhou');
  507. AlibabaCloud::accessKeyClient('ak', 'bar')->asDefaultClient();
  508. // Test
  509. $request = new RpcRequest();
  510. $request->product('no');
  511. $request->serviceCode('no');
  512. // Assert
  513. $request->resolveHost();
  514. self::assertEquals('ecs-cn-hangzhou.aliyuncs.com', $request->uri->getHost());
  515. }
  516. }