locationclient_ft.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <algorithm>
  4. #include "../utils.h"
  5. #include "gtest/gtest.h"
  6. #include "alibabacloud/core/AlibabaCloud.h"
  7. #include "alibabacloud/core/SimpleCredentialsProvider.h"
  8. #include "alibabacloud/core/location/LocationClient.h"
  9. using namespace std;
  10. using namespace AlibabaCloud;
  11. using namespace AlibabaCloud::Location;
  12. namespace
  13. {
  14. utUtils utils;
  15. const string key = utils.get_env("ENV_AccessKeyId");
  16. const string secret = utils.get_env("ENV_AccessKeySecret");
  17. TEST(LocationClient, basic)
  18. {
  19. ClientConfiguration configuration("cn-hangzhou");
  20. Model::DescribeEndpointsRequest req;
  21. req.setId("cn-hangzhou");
  22. req.setServiceCode("ecs");
  23. req.setType("openAPI");
  24. LocationClient client(key, secret, configuration);
  25. LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
  26. EXPECT_TRUE(out.error().errorCode() == "");
  27. EXPECT_TRUE(out.result().endpoints().size() == 1);
  28. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  29. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  30. EXPECT_TRUE(ep.id == "cn-hangzhou");
  31. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  32. EXPECT_TRUE(ep.type == "openAPI");
  33. EXPECT_TRUE(ep.protocols.size() == 2);
  34. std::vector<std::string>::iterator it;
  35. std::vector<std::string> protocols = ep.protocols;
  36. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  37. EXPECT_TRUE(it != protocols.end());
  38. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  39. EXPECT_TRUE(it != protocols.end());
  40. EXPECT_TRUE(out.result().success());
  41. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  42. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  43. }
  44. TEST(LocationClient, credentials)
  45. {
  46. Credentials credentials(key, secret);
  47. ClientConfiguration configuration("cn-hangzhou");
  48. Model::DescribeEndpointsRequest req;
  49. req.setId("cn-hangzhou");
  50. req.setServiceCode("ecs");
  51. req.setType("openAPI");
  52. LocationClient client(credentials, configuration);
  53. LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
  54. EXPECT_TRUE(out.error().errorCode() == "");
  55. EXPECT_TRUE(out.result().endpoints().size() == 1);
  56. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  57. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  58. EXPECT_TRUE(ep.id == "cn-hangzhou");
  59. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  60. EXPECT_TRUE(ep.type == "openAPI");
  61. EXPECT_TRUE(ep.protocols.size() == 2);
  62. std::vector<std::string>::iterator it;
  63. std::vector<std::string> protocols = ep.protocols;
  64. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  65. EXPECT_TRUE(it != protocols.end());
  66. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  67. EXPECT_TRUE(it != protocols.end());
  68. EXPECT_TRUE(out.result().success());
  69. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  70. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  71. }
  72. TEST(LocationClient, credentials_another)
  73. {
  74. Credentials credentials(key, secret);
  75. ClientConfiguration configuration("cn-hangzhou");
  76. Model::DescribeEndpointsRequest req;
  77. req.setId("cn-hangzhou");
  78. req.setServiceCode("ecs");
  79. req.setType("openAPI");
  80. LocationClient client(std::make_shared<SimpleCredentialsProvider>(credentials), configuration);
  81. LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
  82. EXPECT_TRUE(out.error().errorCode() == "");
  83. EXPECT_TRUE(out.result().endpoints().size() == 1);
  84. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  85. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  86. EXPECT_TRUE(ep.id == "cn-hangzhou");
  87. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  88. EXPECT_TRUE(ep.type == "openAPI");
  89. EXPECT_TRUE(ep.protocols.size() == 2);
  90. std::vector<std::string>::iterator it;
  91. std::vector<std::string> protocols = ep.protocols;
  92. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  93. EXPECT_TRUE(it != protocols.end());
  94. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  95. EXPECT_TRUE(it != protocols.end());
  96. EXPECT_TRUE(out.result().success());
  97. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  98. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  99. }
  100. TEST(LocationClient, callable)
  101. {
  102. InitializeSdk();
  103. ClientConfiguration configuration("cn-hangzhou");
  104. Model::DescribeEndpointsRequest req;
  105. req.setId("cn-hangzhou");
  106. req.setServiceCode("ecs");
  107. req.setType("openAPI");
  108. LocationClient client(key, secret, configuration);
  109. LocationClient::DescribeEndpointsOutcomeCallable cb = client.describeEndpointsCallable(req);
  110. LocationClient::DescribeEndpointsOutcome out = cb.get();
  111. EXPECT_TRUE(out.error().errorCode() == "");
  112. EXPECT_TRUE(out.result().endpoints().size() == 1);
  113. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  114. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  115. EXPECT_TRUE(ep.id == "cn-hangzhou");
  116. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  117. EXPECT_TRUE(ep.type == "openAPI");
  118. EXPECT_TRUE(ep.protocols.size() == 2);
  119. std::vector<std::string>::iterator it;
  120. std::vector<std::string> protocols = ep.protocols;
  121. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  122. EXPECT_TRUE(it != protocols.end());
  123. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  124. EXPECT_TRUE(it != protocols.end());
  125. EXPECT_TRUE(out.result().success());
  126. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  127. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  128. ShutdownSdk();
  129. }
  130. void cb(const LocationClient *client,
  131. const Model::DescribeEndpointsRequest &req,
  132. const LocationClient::DescribeEndpointsOutcome &out,
  133. const std::shared_ptr<const AsyncCallerContext> &contex)
  134. {
  135. EXPECT_TRUE(out.error().errorCode() == "");
  136. EXPECT_TRUE(out.result().endpoints().size() == 1);
  137. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  138. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  139. EXPECT_TRUE(ep.id == "cn-hangzhou");
  140. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  141. EXPECT_TRUE(ep.type == "openAPI");
  142. EXPECT_TRUE(ep.protocols.size() == 2);
  143. std::vector<std::string>::iterator it;
  144. std::vector<std::string> protocols = ep.protocols;
  145. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  146. EXPECT_TRUE(it != protocols.end());
  147. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  148. EXPECT_TRUE(it != protocols.end());
  149. EXPECT_TRUE(out.result().success());
  150. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  151. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  152. }
  153. TEST(LocationClient, async)
  154. {
  155. InitializeSdk();
  156. ClientConfiguration configuration("cn-hangzhou");
  157. Model::DescribeEndpointsRequest req;
  158. req.setId("cn-hangzhou");
  159. req.setServiceCode("ecs");
  160. req.setType("openAPI");
  161. LocationClient client(key, secret, configuration);
  162. LocationClient::DescribeEndpointsOutcome out;
  163. const AsyncCallerContext context;
  164. LocationClient::DescribeEndpointsAsyncHandler handler(cb);
  165. client.describeEndpointsAsync(req, handler, std::make_shared<const AsyncCallerContext>(context));
  166. ShutdownSdk();
  167. }
  168. } // namespace