locationclient_ft.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. req.setMethod(HttpRequest::Post);
  53. LocationClient client(credentials, configuration);
  54. LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
  55. EXPECT_TRUE(out.error().errorCode() == "");
  56. EXPECT_TRUE(out.result().endpoints().size() == 1);
  57. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  58. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  59. EXPECT_TRUE(ep.id == "cn-hangzhou");
  60. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  61. EXPECT_TRUE(ep.type == "openAPI");
  62. EXPECT_TRUE(ep.protocols.size() == 2);
  63. std::vector<std::string>::iterator it;
  64. std::vector<std::string> protocols = ep.protocols;
  65. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  66. EXPECT_TRUE(it != protocols.end());
  67. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  68. EXPECT_TRUE(it != protocols.end());
  69. EXPECT_TRUE(out.result().success());
  70. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  71. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  72. }
  73. TEST(LocationClient, credentials_another)
  74. {
  75. Credentials credentials(key, secret);
  76. ClientConfiguration configuration("cn-hangzhou");
  77. configuration.setConnectTimeout(300000);
  78. configuration.setReadTimeout(300000);
  79. Model::DescribeEndpointsRequest req;
  80. req.setId("cn-hangzhou");
  81. req.setServiceCode("ecs");
  82. req.setType("openAPI");
  83. LocationClient client(std::make_shared<SimpleCredentialsProvider>(credentials), configuration);
  84. LocationClient::DescribeEndpointsOutcome out = client.describeEndpoints(req);
  85. EXPECT_EQ(out.error().errorCode(), "");
  86. EXPECT_EQ(out.result().endpoints().size(), 1);
  87. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  88. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  89. EXPECT_TRUE(ep.id == "cn-hangzhou");
  90. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  91. EXPECT_TRUE(ep.type == "openAPI");
  92. EXPECT_TRUE(ep.protocols.size() == 2);
  93. std::vector<std::string>::iterator it;
  94. std::vector<std::string> protocols = ep.protocols;
  95. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  96. EXPECT_TRUE(it != protocols.end());
  97. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  98. EXPECT_TRUE(it != protocols.end());
  99. EXPECT_TRUE(out.result().success());
  100. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  101. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  102. }
  103. TEST(LocationClient, callable)
  104. {
  105. InitializeSdk();
  106. ClientConfiguration configuration("cn-hangzhou");
  107. configuration.setConnectTimeout(300000);
  108. configuration.setReadTimeout(300000);
  109. Model::DescribeEndpointsRequest req;
  110. req.setId("cn-hangzhou");
  111. req.setServiceCode("ecs");
  112. req.setType("openAPI");
  113. LocationClient client(key, secret, configuration);
  114. LocationClient::DescribeEndpointsOutcomeCallable cb = client.describeEndpointsCallable(req);
  115. LocationClient::DescribeEndpointsOutcome out = cb.get();
  116. EXPECT_EQ("", out.error().errorCode());
  117. EXPECT_EQ(1, out.result().endpoints().size());
  118. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  119. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  120. EXPECT_TRUE(ep.id == "cn-hangzhou");
  121. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  122. EXPECT_TRUE(ep.type == "openAPI");
  123. EXPECT_TRUE(ep.protocols.size() == 2);
  124. std::vector<std::string>::iterator it;
  125. std::vector<std::string> protocols = ep.protocols;
  126. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  127. EXPECT_TRUE(it != protocols.end());
  128. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  129. EXPECT_TRUE(it != protocols.end());
  130. EXPECT_TRUE(out.result().success());
  131. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  132. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  133. ShutdownSdk();
  134. }
  135. void cb(const LocationClient *client,
  136. const Model::DescribeEndpointsRequest &req,
  137. const LocationClient::DescribeEndpointsOutcome &out,
  138. const std::shared_ptr<const AsyncCallerContext> &contex)
  139. {
  140. EXPECT_EQ(out.error().errorCode(), "");
  141. EXPECT_EQ(out.result().endpoints().size(), 1);
  142. Model::DescribeEndpointsResult::Endpoint ep = out.result().endpoints()[0];
  143. EXPECT_TRUE(ep.endpoint == "ecs-cn-hangzhou.aliyuncs.com");
  144. EXPECT_TRUE(ep.id == "cn-hangzhou");
  145. EXPECT_TRUE(ep.namespace_ == "26842" || ep.namespace_.empty());
  146. EXPECT_TRUE(ep.type == "openAPI");
  147. EXPECT_TRUE(ep.protocols.size() == 2);
  148. std::vector<std::string>::iterator it;
  149. std::vector<std::string> protocols = ep.protocols;
  150. it = std::find(protocols.begin(), protocols.end(), string("HTTP"));
  151. EXPECT_TRUE(it != protocols.end());
  152. it = std::find(protocols.begin(), protocols.end(), string("HTTPS"));
  153. EXPECT_TRUE(it != protocols.end());
  154. EXPECT_TRUE(out.result().success());
  155. const string req_id = "205302F3-6E1A-4D6F-82C5-2F165FB94038";
  156. EXPECT_TRUE(out.result().requestId().length() == req_id.length());
  157. }
  158. TEST(LocationClient, async)
  159. {
  160. InitializeSdk();
  161. ClientConfiguration configuration("cn-hangzhou");
  162. Model::DescribeEndpointsRequest req;
  163. req.setId("cn-hangzhou");
  164. req.setServiceCode("ecs");
  165. req.setType("openAPI");
  166. LocationClient client(key, secret, configuration);
  167. LocationClient::DescribeEndpointsOutcome out;
  168. const AsyncCallerContext context;
  169. LocationClient::DescribeEndpointsAsyncHandler handler(cb);
  170. client.describeEndpointsAsync(req, handler, std::make_shared<const AsyncCallerContext>(context));
  171. ShutdownSdk();
  172. }
  173. } // namespace