locationclient_ft.cc 7.4 KB

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