locationclient_ft.cc 7.5 KB

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