ClientTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace UCloud\Tests;
  3. use GuzzleHttp\Exception\RequestException;
  4. use GuzzleHttp\Handler\MockHandler;
  5. use GuzzleHttp\HandlerStack;
  6. use GuzzleHttp\Psr7\Response as HttpResponse;
  7. use GuzzleHttp\Psr7\Request as HttpRequest;
  8. use PHPUnit\Framework\TestCase;
  9. use UCloud\Core\Client;
  10. use UCloud\Core\Exception\UCloudException;
  11. use UCloud\Core\Logger\DisabledLogger;
  12. use UCloud\Core\Middleware\Context;
  13. use UCloud\Core\Middleware\Middleware;
  14. use UCloud\Core\Request\Request;
  15. use UCloud\Core\Response\Response;
  16. use UCloud\Core\Transport\Transport;
  17. /**
  18. * Check that credential package is worked.
  19. */
  20. class ClientTest extends TestCase
  21. {
  22. /**
  23. * Check, that signature algorithm is correct for example
  24. */
  25. public function testClientInvoke()
  26. {
  27. $cases = [
  28. [
  29. "req" => [
  30. "Action" => "GetRegion",
  31. ],
  32. "resp" => [
  33. "RetCode" => 0,
  34. "DataSet" => [["Region" => "cn-bj2"]]
  35. ],
  36. "has_exception" => false,
  37. ],
  38. [
  39. "req" => [
  40. "Action" => "NotFound",
  41. ],
  42. "resp" => [
  43. "RetCode" => 161,
  44. "Message" => "Action [NotFound] not found"
  45. ],
  46. "has_exception" => true,
  47. ],
  48. [
  49. "req" => [
  50. "Action" => "GetRegion",
  51. ],
  52. "resp" => new RequestException('Error Communicating with Server', new HttpRequest('GET', 'test')),
  53. "has_exception" => true,
  54. ],
  55. ];
  56. $ua = "UnitTest";
  57. $client = new Client([
  58. "publicKey" => getenv("UCLOUD_PUBLIC_KEY"),
  59. "privateKey" => getenv("UCLOUD_PRIVATE_KEY"),
  60. "projectId" => getenv("UCLOUD_PROJECT_ID"),
  61. "userAgent" => $ua,
  62. ]);
  63. foreach ($cases as $i => $case) {
  64. // mock with response
  65. $mock = $case["resp"];
  66. if (!($mock instanceof HttpResponse || $mock instanceof RequestException)) {
  67. $mock = new HttpResponse(200, ['Content-Type' => 'application/json'], json_encode($mock));
  68. }
  69. $handlerStack = HandlerStack::create(new MockHandler([$mock]));
  70. $client->setTransport(new Transport(
  71. $client->getConfig()->getBaseUrl(), $ua,
  72. ['handler' => $handlerStack],
  73. ));
  74. // invoke action
  75. try {
  76. $resp = $client->invoke(new Request($case["req"]));
  77. } catch (UCloudException $e) {
  78. $this->assertEquals(True, $case["has_exception"]);
  79. continue;
  80. }
  81. $this->assertEquals(False, $case["has_exception"]);
  82. $this->assertNotEmpty($resp);
  83. }
  84. }
  85. public function testMiddleware()
  86. {
  87. $client = new Client([
  88. "publicKey" => getenv("UCLOUD_PUBLIC_KEY"),
  89. "privateKey" => getenv("UCLOUD_PRIVATE_KEY"),
  90. "projectId" => getenv("UCLOUD_PROJECT_ID"),
  91. "logger" => new DisabledLogger(),
  92. "region" => "cn-bj2",
  93. ]);
  94. $client->setTransport(new Transport(
  95. $client->getConfig()->getBaseUrl(), "UnitTest",
  96. ['handler' => HandlerStack::create(new MockHandler([
  97. new RequestException('Error Communicating with Server', new HttpRequest('GET', 'test')),
  98. ]))],
  99. ));
  100. $m = new DummyMiddleware();
  101. $client->useMiddleware($m);
  102. $req = new Request([]);
  103. $req->setAction("Foo");
  104. $this->assertEquals("Foo", $req->getAction());
  105. try {
  106. $client->invoke($req);
  107. } catch (UCloudException $e) {
  108. // do nothing
  109. }
  110. $this->assertEquals(True, $m->flag);
  111. }
  112. }
  113. class DummyMiddleware extends Middleware
  114. {
  115. /** @var bool */
  116. public bool $flag;
  117. public function handleException(Context $ctx)
  118. {
  119. $this->flag = True;
  120. }
  121. }