RpcUtilsTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace AlibabaCloud\Tea\Tests\RpcUtils;
  3. use AlibabaCloud\Tea\Model;
  4. use AlibabaCloud\Tea\Request;
  5. use AlibabaCloud\Tea\RpcUtils\RpcUtils;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * @internal
  9. * @coversNothing
  10. */
  11. class RpcUtilsTest extends TestCase
  12. {
  13. public function testGetEndpoint()
  14. {
  15. $endpoint = 'ecs.cn-hangzhou.aliyun.cs.com';
  16. $useAccelerate = false;
  17. $endpointType = 'public';
  18. $this->assertEquals('ecs.cn-hangzhou.aliyun.cs.com', RpcUtils::getEndpoint($endpoint, $useAccelerate, $endpointType));
  19. $endpointType = 'internal';
  20. $this->assertEquals('ecs-internal.cn-hangzhou.aliyun.cs.com', RpcUtils::getEndpoint($endpoint, $useAccelerate, $endpointType));
  21. $useAccelerate = true;
  22. $endpointType = 'accelerate';
  23. $this->assertEquals('oss-accelerate.aliyuncs.com', RpcUtils::getEndpoint($endpoint, $useAccelerate, $endpointType));
  24. }
  25. public function testGetHost()
  26. {
  27. $serviceCode = 'ecs';
  28. $regionId = 'cn-hangzhou';
  29. $endpoint = 'fake.aliyuncs.com';
  30. $this->assertEquals('ecs.cn-hangzhou.aliyuncs.com', RpcUtils::getHost($serviceCode, $regionId, ''));
  31. $this->assertEquals('fake.aliyuncs.com', RpcUtils::getHost($serviceCode, $regionId, $endpoint));
  32. }
  33. public function testGetSignature()
  34. {
  35. $request = new Request();
  36. $request->query = [
  37. 'query' => 'test',
  38. 'body' => 'test',
  39. ];
  40. $this->assertEquals('XlUyV4sXjOuX5FnjUz9IF9tm5rU=', RpcUtils::getSignature($request, 'secret'));
  41. }
  42. public function testGetStrToSign()
  43. {
  44. $this->assertEquals('GET&%2F&complex%3DFF%253D3iP2yN79-ED6%2529FU%253BR%2528F%252CmpP%252F4f8%252CUY%255B.3.g%252Br%2528806x%257B5%252A%2525%252F%253D%2529O8%25404%2526%255B%255D%2524%255Erp%26empty%3D%26foo%3Dbar%26number%3D0',
  45. RpcUtils::getStrToSign('GET', [
  46. 'foo' => 'bar',
  47. 'empty' => '',
  48. 'null' => null,
  49. 'number' => 0,
  50. 'complex' => 'FF=3iP2yN79-ED6)FU;R(F,mpP/4f8,UY[.3.g+r(806x{5*%/=)O8@4&[]$^rp',
  51. ]));
  52. }
  53. public function testGetSignatureV1()
  54. {
  55. $query = [
  56. 'test' => 'ok',
  57. ];
  58. $this->assertEquals('jHx/oHoHNrbVfhncHEvPdHXZwHU=', RpcUtils::getSignatureV1($query, '', 'accessKeySecret'));
  59. }
  60. public function testHasError()
  61. {
  62. $this->assertTrue(RpcUtils::hasError(null));
  63. $this->assertFalse(RpcUtils::hasError([]));
  64. $dict['Code'] = 'SomeError';
  65. $this->assertTrue(RpcUtils::hasError($dict));
  66. $dict['Code'] = 'Success';
  67. $this->assertFalse(RpcUtils::hasError($dict));
  68. }
  69. public function testGetTimestamp()
  70. {
  71. $date = RpcUtils::getTimestamp();
  72. $this->assertEquals(20, \strlen($date));
  73. }
  74. public function testConvert()
  75. {
  76. $model = new MockModel();
  77. $model->a = 'foo';
  78. $output = new MockModel();
  79. RpcUtils::convert($model, $output);
  80. $this->assertEquals($model->a, $output->a);
  81. }
  82. public function testQuery()
  83. {
  84. $array = [
  85. 'a' => 'a',
  86. 'b1' => [
  87. 'a' => 'a',
  88. ],
  89. 'b2' => [
  90. 'a' => 'a',
  91. ],
  92. 'c'=> ['x', 'y', 'z'],
  93. ];
  94. $this->assertEquals([
  95. 'a' => 'a',
  96. 'b1.a' => 'a',
  97. 'b2.a' => 'a',
  98. 'c.1' => 'x',
  99. 'c.2' => 'y',
  100. 'c.3' => 'z',
  101. ], RpcUtils::query($array));
  102. }
  103. public function testGetOpenPlatFormEndpoint()
  104. {
  105. $endpoint = 'fake.domain.com';
  106. $regionId = '';
  107. // regionId is empty
  108. $this->assertEquals('fake.domain.com', RpcUtils::getOpenPlatFormEndpoint($endpoint, $regionId));
  109. // regionId is invalid
  110. $regionId = 'invalid-regionId';
  111. $this->assertEquals('fake.domain.com', RpcUtils::getOpenPlatFormEndpoint($endpoint, $regionId));
  112. // regionId is valid but have upper character
  113. $regionId = 'cn-Hongkong';
  114. $this->assertEquals('fake.cn-hongkong.domain.com', RpcUtils::getOpenPlatFormEndpoint($endpoint, $regionId));
  115. // valid regionId
  116. $regionId = 'cn-hongkong';
  117. $this->assertEquals('fake.cn-hongkong.domain.com', RpcUtils::getOpenPlatFormEndpoint($endpoint, $regionId));
  118. }
  119. }
  120. class MockModel extends Model
  121. {
  122. public $a = 'A';
  123. public $b = '';
  124. public $c = '';
  125. public function __construct()
  126. {
  127. $this->_name['a'] = 'A';
  128. $this->_required['c'] = true;
  129. }
  130. }