BearerTokenCredentialTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Feature\Credentials;
  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\CCC\ListPhoneNumbersRequest;
  8. use AlibabaCloud\Client\Tests\Mock\Services\Cdn\DescribeCdnServiceRequest;
  9. use AlibabaCloud\Client\Tests\Mock\Services\Ecs\DescribeAccessPointsRequest;
  10. /**
  11. * Class BearerTokenCredentialTest
  12. *
  13. * @package AlibabaCloud\Client\Tests\Feature\Credentials
  14. */
  15. class BearerTokenCredentialTest extends TestCase
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $clientName = 'BearerTokenCredentialTest';
  21. /**
  22. * @before
  23. * @throws ClientException
  24. */
  25. protected function initialize()
  26. {
  27. parent::setUp();
  28. $regionId = 'cn-hangzhou';
  29. $bearerToken =
  30. 'eyJhbGciOiJSUzI1NiIsImsyaWQiOiJlNE92NnVOUDhsMEY2RmVUMVhvek5wb1NBcVZLblNGRyIsImtpZCI6IkpDOXd4enJocUowZ3RhQ0V0MlFMVWZldkVVSXdsdEZodWk0TzFiaDY3dFUifQ.N3plS0w2cm83YzhtVzJqSkI0U0JIMldzNW45cFBOSWdNellvQ3VpZGV5NzRVOHNsMkJUWTVULzl3RDdkbzhHQkorM3dvclg1SGY1STZXL1FjaVhLVnc5ck5YeVNYanBuK2N6UkN1SnRRc3FRMGJIVTF4cVVjUDVRNUJpK2JsSWxZdlowZ2VWSzYvS2pzcVNjWHJLSlVvWkNnWE0wWGJZZ0NCVm1BUlNXS1plUnNzdnAvUmwwV01tSFFkWmlOMGtKV0o5TllQU3M0QU1aenpHVTdUY1BnYlhIVy9uTmdMY1JVSytROXlrPQ.kvZes7-6IU-xjOzK1goPPjODz1XLt73yCmDLSpRwzlz3d9A_uYvbQK0HHltVKo0K0dI0wJOfpCeOHJlrV0m4RI4bynL9ltl31rscPhQ-G4Ybqw4KXVBZCIzjSqzWcniIWnGWl-TpOy0Y7sAcJmp0Lg2ndu_shGqiTP6DTVBNV8f94mveHmRqouLxr2OKMvCyxTV1zUEJmC-JnZaljfNG-i483qG8Hm60CwAjM91FTGib3eXGzjJa3XOOY7zpZTrvahBYFpyrVhRuvDvRs6tLKVAL_7bYwCIo_tdh9rhRmFtyq0k2iykZQJmAIlDMt-VENP7hJTH62uUQzNLQ28ISTQ';
  31. AlibabaCloud::bearerTokenClient($bearerToken)
  32. ->regionId($regionId)
  33. ->name($this->clientName);
  34. }
  35. /**
  36. * @after
  37. * @throws ClientException
  38. */
  39. protected function finalize()
  40. {
  41. parent::tearDown();
  42. AlibabaCloud::del($this->clientName);
  43. }
  44. /**
  45. * @throws ClientException
  46. */
  47. public function testCCC()
  48. {
  49. try {
  50. (new ListPhoneNumbersRequest())
  51. ->client($this->clientName)
  52. ->withInstanceId(\getenv('CC_INSTANCE_ID'))
  53. ->withOutboundOnly(true)
  54. ->scheme('https')
  55. ->host('ccc.cn-shanghai.aliyuncs.com')
  56. ->options([
  57. 'verify' => false,
  58. ])
  59. ->connectTimeout(25)
  60. ->timeout(30)
  61. ->request();
  62. } catch (ServerException $e) {
  63. $result = $e->getResult();
  64. self::assertEquals(
  65. 'InvalidBearerTokenException: Bearertoken has expired',
  66. $result['Message']
  67. );
  68. }
  69. }
  70. /**
  71. * @throws ClientException
  72. */
  73. public function testEcs()
  74. {
  75. $this->expectException(ServerException::class);
  76. $reg = '/UnsupportedSignatureType: This signature type is not supported./';
  77. if (method_exists($this, 'expectExceptionMessageMatches')) {
  78. $this->expectExceptionMessageMatches($reg);
  79. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  80. $this->expectExceptionMessageRegExp($reg);
  81. }
  82. (new DescribeAccessPointsRequest())
  83. ->client($this->clientName)
  84. ->connectTimeout(25)
  85. ->timeout(30)
  86. ->request();
  87. }
  88. /**
  89. * @throws ClientException
  90. */
  91. public function testCdn()
  92. {
  93. $this->expectException(ServerException::class);
  94. $reg = '/UnsupportedSignatureType: This signature type is not supported./';
  95. if (method_exists($this, 'expectExceptionMessageMatches')) {
  96. $this->expectExceptionMessageMatches($reg);
  97. } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
  98. $this->expectExceptionMessageRegExp($reg);
  99. }
  100. (new DescribeCdnServiceRequest())->client($this->clientName)
  101. ->connectTimeout(25)
  102. ->timeout(30)
  103. ->request();
  104. }
  105. }