LocationServiceTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Regions;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Regions\LocationService;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. use AlibabaCloud\Client\Exception\ServerException;
  8. use AlibabaCloud\Client\Tests\Mock\Services\Rds\DeleteDatabaseRequest;
  9. /**
  10. * Class LocationServiceTest
  11. *
  12. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Unit\Endpoint
  13. */
  14. class LocationServiceTest extends TestCase
  15. {
  16. /**
  17. * @throws ClientException
  18. * @throws ServerException
  19. */
  20. public function testAddHost()
  21. {
  22. // Setup
  23. $product = 'b';
  24. $host = 'c';
  25. $regionId = 'a';
  26. // Test
  27. $request = AlibabaCloud::rpc()->regionId($regionId)->product($product);
  28. LocationService::addHost($product, $host, $regionId);
  29. // Assert
  30. self::assertEquals(LocationService::resolveHost($request), $host);
  31. }
  32. /**
  33. * @throws ClientException
  34. * @throws ServerException
  35. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  36. * @expectedExceptionMessageRegExp /Not found Region ID in location.aliyuncs.com/
  37. */
  38. public function testResolveHostWithServiceUnknownError()
  39. {
  40. AlibabaCloud::mockResponse();
  41. $request = AlibabaCloud::rpc()->product(__METHOD__)
  42. ->regionId('regionId');
  43. $host = LocationService::resolveHost($request);
  44. self::assertEquals('', $host);
  45. }
  46. /**
  47. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  48. * @expectedExceptionMessage Not found Region ID in location.aliyuncs.com
  49. * @throws ClientException
  50. * @throws ServerException
  51. */
  52. public function testResolveHostNotFound()
  53. {
  54. AlibabaCloud::mockResponse();
  55. $request = AlibabaCloud::rpc()->product(__METHOD__)->regionId('regionId');
  56. LocationService::resolveHost($request);
  57. }
  58. /**
  59. * @throws ClientException
  60. * @throws ServerException
  61. */
  62. public function testResolveHostSuccess()
  63. {
  64. $body = [
  65. 'Endpoints' => [
  66. 'Endpoint' => [
  67. 0 => [
  68. 'Endpoint' => 'cdn.aliyun.com',
  69. ],
  70. ],
  71. ],
  72. ];
  73. AlibabaCloud::mockResponse(200, [], $body);
  74. $request = AlibabaCloud::rpc()->product(__METHOD__)->regionId('regionId');
  75. $host = LocationService::resolveHost($request);
  76. self::assertEquals('cdn.aliyun.com', $host);
  77. }
  78. /**
  79. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  80. * @expectedExceptionMessage Product cannot be empty
  81. * @throws ClientException
  82. */
  83. public function testAddHostWithProductEmpty()
  84. {
  85. LocationService::addHost('', 'host', 'regionId');
  86. }
  87. /**
  88. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  89. * @expectedExceptionMessage Product must be a string
  90. * @throws ClientException
  91. */
  92. public function testAddHostWithProductFormat()
  93. {
  94. LocationService::addHost(null, 'host', 'regionId');
  95. }
  96. /**
  97. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  98. * @expectedExceptionMessage Host cannot be empty
  99. * @throws ClientException
  100. */
  101. public function testAddHostWithHostEmpty()
  102. {
  103. LocationService::addHost('product', '', 'regionId');
  104. }
  105. /**
  106. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  107. * @expectedExceptionMessage Host must be a string
  108. * @throws ClientException
  109. */
  110. public function testAddHostWithHostFormat()
  111. {
  112. LocationService::addHost('product', null, 'regionId');
  113. }
  114. /**
  115. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  116. * @expectedExceptionMessage Region ID cannot be empty
  117. * @throws ClientException
  118. */
  119. public function testAddHostWithRegionIdEmpty()
  120. {
  121. LocationService::addHost('product', 'host', '');
  122. }
  123. /**
  124. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  125. * @expectedExceptionMessage Region ID must be a string
  126. * @throws ClientException
  127. */
  128. public function testAddHostWithRegionIdFormat()
  129. {
  130. LocationService::addHost('product', 'host', null);
  131. }
  132. /**
  133. * @expectedException \AlibabaCloud\Client\Exception\ServerException
  134. * @expectedExceptionMessageRegExp /Specified access key is not found/
  135. * @throws ClientException
  136. * @throws ServerException
  137. */
  138. public function testLocationServiceException()
  139. {
  140. // Setup
  141. AlibabaCloud::accessKeyClient('key', 'secret')->asDefaultClient();
  142. AlibabaCloud::mockResponse(
  143. 401,
  144. [],
  145. [
  146. 'message' => 'Specified access key is not found',
  147. ]
  148. );
  149. $request = (new DeleteDatabaseRequest())
  150. ->regionId('cn-hangzhou')
  151. ->connectTimeout(25)
  152. ->timeout(30);
  153. // Test
  154. LocationService::resolveHost($request);
  155. }
  156. /**
  157. * @expectedException \AlibabaCloud\Client\Exception\ClientException
  158. * @expectedExceptionMessageRegExp /cURL error 6: Could not resolve/
  159. * @throws ClientException
  160. * @throws ServerException
  161. */
  162. public function testLocationServiceWithBadServiceDomain()
  163. {
  164. AlibabaCloud::accessKeyClient('key', 'secret')->asDefaultClient();
  165. $request = (new DeleteDatabaseRequest())->regionId('cn-hangzhou');
  166. LocationService::resolveHost($request, 'not.alibaba.com');
  167. }
  168. /**
  169. * @throws ClientException
  170. */
  171. protected function setUp()
  172. {
  173. AlibabaCloud::accessKeyClient('foo', 'bar')->asDefaultClient();
  174. }
  175. protected function tearDown()
  176. {
  177. AlibabaCloud::cancelMock();
  178. }
  179. }