RequestTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Feature\Request;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Exception\ServerException;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. use AlibabaCloud\Client\Tests\Mock\Services\CS\DescribeClusterServicesRequest;
  8. /**
  9. * Class RequestTest
  10. *
  11. * @package AlibabaCloud\Client\Tests\Feature\Request
  12. */
  13. class RequestTest extends TestCase
  14. {
  15. /**
  16. * @before
  17. * @throws ClientException
  18. */
  19. protected function initialize()
  20. {
  21. parent::setUp();
  22. AlibabaCloud::accessKeyClient(
  23. \getenv('ACCESS_KEY_ID'),
  24. \getenv('ACCESS_KEY_SECRET')
  25. )->asDefaultClient()->regionId(\AlibabaCloud\Client\env('REGION_ID', 'cn-hangzhou'));
  26. }
  27. /**
  28. * @throws ClientException
  29. */
  30. public function testConstruct()
  31. {
  32. // Setup
  33. $request = new DescribeClusterServicesRequest();
  34. $clusterId = \time();
  35. $request->withClusterId($clusterId);
  36. // Test
  37. try {
  38. $request->connectTimeout(25)
  39. ->timeout(30)
  40. ->request();
  41. } catch (ServerException $e) {
  42. // Assert
  43. self::assertEquals('ErrorClusterNotFound', $e->getErrorCode());
  44. }
  45. }
  46. /**
  47. * @throws ClientException
  48. */
  49. public function testWithBearerTokenCredential()
  50. {
  51. // Setup
  52. $regionId = 'cn-hangzhou';
  53. $bearerToken = 'BEARER_TOKEN';
  54. AlibabaCloud::bearerTokenClient($bearerToken)
  55. ->name('BEARER_TOKEN')
  56. ->regionId($regionId);
  57. // Test
  58. try {
  59. (new DescribeClusterServicesRequest())
  60. ->client('BEARER_TOKEN')
  61. ->withClusterId(\time())
  62. ->connectTimeout(25)
  63. ->timeout(50)
  64. ->request();
  65. } catch (ServerException $e) {
  66. // Assert
  67. static::assertEquals('UnsupportedSignatureType', $e->getErrorCode());
  68. }
  69. }
  70. /**
  71. * @throws ClientException
  72. */
  73. public function testInvalidUrl()
  74. {
  75. // Setup
  76. $regionId = 'cn-hangzhou';
  77. $accessKeyId = \getenv('ACCESS_KEY_ID');
  78. $accessKeySecret = \getenv('ACCESS_KEY_SECRET');
  79. AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
  80. ->regionId($regionId)
  81. ->name(__METHOD__);
  82. // Test
  83. try {
  84. (new DescribeClusterServicesRequest())
  85. ->connectTimeout(25)
  86. ->timeout(30)
  87. ->client(__METHOD__)
  88. ->withClusterId(\time())
  89. ->request();
  90. } catch (ServerException $e) {
  91. // Assert
  92. self::assertEquals('ErrorClusterNotFound', $e->getErrorCode());
  93. }
  94. }
  95. /**
  96. * @throws ClientException
  97. */
  98. public function testROA()
  99. {
  100. // Setup
  101. $request = new DescribeClusterServicesRequest();
  102. $request->withClusterId(\time());
  103. // Test
  104. try {
  105. $request->connectTimeout(25)
  106. ->timeout(30)
  107. ->request();
  108. // Assert
  109. } catch (ServerException $exception) {
  110. // Assert
  111. static::assertEquals('ErrorClusterNotFound', $exception->getErrorCode());
  112. }
  113. }
  114. /**
  115. * @throws ClientException
  116. */
  117. public function testAccept()
  118. {
  119. // Setup
  120. $roa = AlibabaCloud::roa();
  121. $rpc = AlibabaCloud::rpc();
  122. // Test
  123. $roa->accept('accept');
  124. $rpc->accept('accept');
  125. // Assert
  126. self::assertEquals('accept', $roa->options['headers']['Accept']);
  127. self::assertEquals('accept', $rpc->options['headers']['Accept']);
  128. }
  129. }