AcsTraitTest.php 14 KB

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