RpcRequestTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request;
  3. use PHPUnit\Framework\TestCase;
  4. use AlibabaCloud\Client\AlibabaCloud;
  5. use AlibabaCloud\Client\Request\RpcRequest;
  6. use AlibabaCloud\Client\Exception\ClientException;
  7. use AlibabaCloud\Client\Exception\ServerException;
  8. use AlibabaCloud\Client\Tests\Mock\Services\Ecs\DescribeRegionsRequest;
  9. /**
  10. * Class RpcRequestTest
  11. *
  12. * @package AlibabaCloud\Client\Tests\LowerthanVersion7_2\Feature\Request
  13. * @coversDefaultClass \AlibabaCloud\Client\Request\RpcRequest
  14. */
  15. class RpcRequestTest extends TestCase
  16. {
  17. /**
  18. * @throws ServerException
  19. * @throws ClientException
  20. */
  21. public function testWithCredential()
  22. {
  23. // Setup
  24. $nameClient = 'name';
  25. $regionId = \AlibabaCloud\Client\env('REGION_ID', 'cn-hangzhou');
  26. $accessKeyId = \getenv('ACCESS_KEY_ID');
  27. $accessKeySecret = \getenv('ACCESS_KEY_SECRET');
  28. // Test
  29. AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
  30. ->regionId($regionId)
  31. ->name($nameClient);
  32. // Assert
  33. $result = (new DescribeRegionsRequest())->client($nameClient)
  34. ->connectTimeout(25)
  35. ->timeout(30)
  36. ->request();
  37. static::assertNotNull($result->RequestId);
  38. static::assertNotNull($result->Regions->Region[0]->LocalName);
  39. static::assertNotNull($result->Regions->Region[0]->RegionId);
  40. }
  41. /**
  42. * @covers \AlibabaCloud\Client\Request\Request::setQueryParameters
  43. * @throws ClientException
  44. */
  45. public function testWithBearerTokenCredential()
  46. {
  47. // Setup
  48. $regionId = \AlibabaCloud\Client\env('REGION_ID', 'cn-hangzhou');
  49. $bearerToken = 'BEARER_TOKEN';
  50. // Test
  51. AlibabaCloud::bearerTokenClient($bearerToken)
  52. ->regionId($regionId)
  53. ->name($bearerToken);
  54. // Assert
  55. try {
  56. $request = new DescribeRegionsRequest();
  57. $request->options(
  58. [
  59. 'query' => [
  60. 'test_true' => 1,
  61. 'test_false' => 1,
  62. ],
  63. ]
  64. );
  65. static::assertEquals(1, $request->options['query']['test_true']);
  66. static::assertEquals(1, $request->options['query']['test_false']);
  67. $result = $request->connectTimeout(25)
  68. ->timeout(30)
  69. ->request();
  70. self::assertArrayHasKey('Regions', $result);
  71. } catch (ServerException $e) {
  72. static::assertEquals('UnsupportedSignatureType', $e->getErrorCode());
  73. }
  74. }
  75. /**
  76. * @expectedException \AlibabaCloud\Client\Exception\ServerException
  77. * @expectedExceptionMessageRegExp /Forbidden.RAM: User not authorized to operate on the specified resource, or this API doesn't support RAM/
  78. * @throws ClientException
  79. * @throws ServerException
  80. */
  81. public function testRpc()
  82. {
  83. AlibabaCloud::accessKeyClient(
  84. \getenv('ACCESS_KEY_ID'),
  85. \getenv('ACCESS_KEY_SECRET')
  86. )->asDefaultClient()->regionId('cn-hangzhou');
  87. $result = (new RpcRequest())->method('POST')
  88. ->product('Cdn')
  89. ->version('2014-11-11')
  90. ->action('DescribeCdnService')
  91. ->connectTimeout(25)
  92. ->timeout(30)
  93. ->request();
  94. // self::assertNotEmpty('PayByTraffic', $result['ChangingChargeType']);
  95. }
  96. }