commonclient_ut.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "gtest/gtest.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include "alibabacloud/core/Utils.h"
  5. #include "alibabacloud/core/Config.h"
  6. #include "alibabacloud/core/AlibabaCloud.h"
  7. #include "alibabacloud/core/CommonClient.h"
  8. #include "alibabacloud/core/CommonResponse.h"
  9. #include "alibabacloud/core/CommonRequest.h"
  10. #include "alibabacloud/core/SimpleCredentialsProvider.h"
  11. using namespace std;
  12. using namespace AlibabaCloud;
  13. namespace AlibabaCloud {
  14. class TestCommonClient : public CommonClient {
  15. public:
  16. TestCommonClient(const Credentials &credentials, const ClientConfiguration &configuration):
  17. CommonClient(credentials, configuration)
  18. {}
  19. using CommonClient::buildHttpRequest;
  20. using CommonClient::buildRoaHttpRequest;
  21. using CommonClient::buildRpcHttpRequest;
  22. using CommonClient::makeRequest;
  23. using CommonClient::asyncExecute;
  24. using CommonClient::commonResponseAsync;
  25. };
  26. }
  27. static int nbr = 0;
  28. static void task() {
  29. nbr |= 0x01;
  30. }
  31. namespace {
  32. TEST(CommonClient, basic) {
  33. InitializeSdk();
  34. const ClientConfiguration cfg;
  35. std::string key = "accessKeyId";
  36. std::string secret = "accessSecret";
  37. std::string token = "sessionToken";
  38. const Credentials credentials(key, secret, token);
  39. const CommonClient cc1(credentials, cfg);
  40. CommonClient cc2(std::make_shared<SimpleCredentialsProvider>(credentials), cfg);
  41. CommonClient cc3(key, secret, cfg);
  42. CommonClient cc4(key, secret, cfg);
  43. CommonRequest cr;
  44. cr.setContent("test-content", 12);
  45. CommonClient::CommonResponseOutcome out1 = cc1.commonResponse(cr);
  46. EXPECT_TRUE(out1.error().errorCode() == "NetworkError");
  47. EXPECT_TRUE(out1.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
  48. EXPECT_TRUE(out1.result().payload() == "");
  49. CommonClient::CommonResponseOutcome out2 = cc2.commonResponse(cr);
  50. EXPECT_TRUE(out2.error().errorCode() == "NetworkError");
  51. EXPECT_TRUE(out2.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
  52. EXPECT_TRUE(out2.result().payload() == "");
  53. CommonClient::CommonResponseOutcome out3 = cc3.commonResponse(cr);
  54. EXPECT_TRUE(out3.error().errorCode() == "NetworkError");
  55. EXPECT_TRUE(out3.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
  56. EXPECT_TRUE(out3.result().payload() == "");
  57. cr.setRequestPattern(CommonRequest::RequestPattern::RoaPattern);
  58. CommonClient::CommonResponseOutcome out4 = cc4.commonResponse(cr);
  59. EXPECT_TRUE(out4.error().errorCode() == "NetworkError");
  60. EXPECT_TRUE(out4.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
  61. EXPECT_TRUE(out4.result().payload() == "");
  62. /*
  63. https://cn-hangzhou/?
  64. AccessKeyId=accessKeyId
  65. &Format=JSON
  66. &RegionId=cn-hangzhou
  67. &SecurityToken=sessionToken
  68. &Signature=tWGagYMK93km44TOY%2FY60snVYWE%3D
  69. &SignatureMethod=HMAC-SHA1
  70. &SignatureNonce=ef27311a-5151-40a0-974f-2b808a8a683e
  71. &SignatureVersion=1.0
  72. &Timestamp=2019-01-02T08%3A56%3A26Z
  73. &Version=
  74. */
  75. cr.setRequestPattern(CommonRequest::RequestPattern::RpcPattern);
  76. TestCommonClient* client = new TestCommonClient(credentials, cfg);
  77. HttpRequest r = client->buildHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  78. EXPECT_TRUE(r.method() == HttpRequest::Method::Post);
  79. EXPECT_TRUE(r.url().scheme() == "https");
  80. EXPECT_TRUE(r.url().userName() == "");
  81. EXPECT_TRUE(r.url().password() == "");
  82. EXPECT_TRUE(r.url().host() == "cn-hangzhou");
  83. EXPECT_TRUE(r.url().port() == -1);
  84. EXPECT_TRUE(r.url().fragment() == "");
  85. EXPECT_TRUE(r.url().query().find("AccessKeyId=") != string::npos);
  86. EXPECT_TRUE(r.url().query().find("Format=") != string::npos);
  87. EXPECT_TRUE(r.url().query().find("RegionId=") != string::npos);
  88. EXPECT_TRUE(r.url().query().find("SecurityToken=") != string::npos);
  89. EXPECT_TRUE(r.url().query().find("Signature=") != string::npos);
  90. EXPECT_TRUE(r.url().query().find("SignatureMethod=") != string::npos);
  91. EXPECT_TRUE(r.url().query().find("SignatureNonce=") != string::npos);
  92. EXPECT_TRUE(r.url().query().find("SignatureVersion=") != string::npos);
  93. EXPECT_TRUE(r.url().query().find("Timestamp=") != string::npos);
  94. EXPECT_TRUE(r.url().query().find("Version=") != string::npos);
  95. cr.setQueryParameter("query_k1", "query_v1");
  96. cr.setHeaderParameter("header_k1", "header_v1");
  97. cr.setScheme("");
  98. cr.setHeaderParameter("Accept", "");
  99. HttpRequest rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
  100. EXPECT_TRUE(rr.method() == HttpRequest::Method::Get);
  101. EXPECT_TRUE(rr.header("Accept") == "application/json");
  102. EXPECT_TRUE(rr.url().toString() == "https://cn-shanghai/?query_k1=query_v1");
  103. EXPECT_TRUE(rr.header("Host") == "cn-shanghai");
  104. // Wed, 09 Jan 2019 06:32:41 GMT
  105. string date = "Wed, 09 Jan 2019 06:32:41 GMT";
  106. EXPECT_TRUE(rr.header("Date").length() == date.length());
  107. string signature_nonce = "fb62c51d-f735-4ec7-870d-c6ce03368214";
  108. EXPECT_TRUE(rr.header("x-acs-signature-nonce").length() == signature_nonce.length());
  109. EXPECT_TRUE(rr.header("x-acs-signature-method") == "HMAC-SHA1");
  110. EXPECT_TRUE(rr.header("x-acs-signature-version") == "1.0");
  111. EXPECT_TRUE(rr.header("x-acs-version") == "");
  112. EXPECT_TRUE(rr.header("x-sdk-client") == string("CPP/").append(ALIBABACLOUD_VERSION_STR));
  113. EXPECT_TRUE(rr.header("x-acs-region-id") == "cn-hangzhou");
  114. // acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc=
  115. EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos);
  116. EXPECT_TRUE(rr.header("unknown-header") == "");
  117. cr.setScheme("http");
  118. cr.setHeaderParameter("Accept", "test-accept");
  119. cr.setHeaderParameter("Content-Type", "test-content-type");
  120. cr.setQueryParameter("query_key", "query_value");
  121. rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
  122. EXPECT_TRUE(rr.url().toString() == "http://cn-shanghai/?query_k1=query_v1&query_key=query_value");
  123. EXPECT_TRUE(rr.header("Accept") == "test-accept");
  124. EXPECT_TRUE(rr.header("Content-Type") == "test-content-type");
  125. HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  126. EXPECT_TRUE(client->serviceName() == "Common");
  127. EXPECT_TRUE(rrr.url().scheme() == "http");
  128. cr.setScheme("");
  129. rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  130. EXPECT_TRUE(rrr.url().scheme() == "https");
  131. std::function<void()> func(task);
  132. Runnable* rf = new Runnable(func);
  133. EXPECT_TRUE(nbr == 0);
  134. client->asyncExecute(rf);
  135. usleep(10000);
  136. EXPECT_EQ(nbr, 1);
  137. ShutdownSdk();
  138. }
  139. TEST(CommonClient, bodyParam) {
  140. const ClientConfiguration cfg;
  141. std::string key = "accessKeyId";
  142. std::string secret = "accessSecret";
  143. std::string token = "sessionToken";
  144. const Credentials credentials(key, secret, token);
  145. CommonRequest cr;
  146. cr.setBodyParameter("TestBodyParamKey", "TestBodyParamValue");
  147. TestCommonClient* client = new TestCommonClient(credentials, cfg);
  148. HttpRequest r = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  149. const char *s = "TestBodyParamKey=TestBodyParamValue";
  150. EXPECT_EQ(*r.body(), *s);
  151. }
  152. TEST(CommonClient, abnormal) {
  153. InitializeSdk();
  154. ClientConfiguration configuration("cn-hangzhou");
  155. CommonClient client("key", "secret", configuration);
  156. CommonRequest req;
  157. req.setScheme("http");
  158. req.setDomain("aliyun.com");
  159. auto out = client.commonResponse(req);
  160. EXPECT_TRUE(out.error().errorCode() == "InvalidResponse");
  161. EXPECT_TRUE(out.result().payload().empty());
  162. /*
  163. 1: =====================<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  164. 1: <html>
  165. 1: <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  166. 1: <title>301 Moved Permanently</title></head>
  167. 1: <body bgcolor="white">
  168. 1: <h1>301 Moved Permanently</h1>
  169. 1: <p>The requested resource has been assigned a new permanent URI.</p>
  170. 1: <hr/>Powered by Tengine/Aserver</body>
  171. 1: </html>
  172. */
  173. EXPECT_FALSE(out.error().errorMessage().empty());
  174. ShutdownSdk();
  175. }
  176. void cb(const CommonClient *client,
  177. const CommonRequest &req,
  178. const CommonClient::CommonResponseOutcome& out,
  179. const std::shared_ptr<const AsyncCallerContext>& contex) {
  180. EXPECT_FALSE(out.error().errorCode().empty());
  181. }
  182. TEST(CommonClient, async) {
  183. InitializeSdk();
  184. const ClientConfiguration cfg;
  185. std::string key = "accessKeyId";
  186. std::string secret = "accessSecret";
  187. std::string token = "sessionToken";
  188. const Credentials credentials(key, secret, token);
  189. const CommonClient cc1(credentials, cfg);
  190. CommonRequest cr;
  191. cr.setContent("test-content", 12);
  192. TestCommonClient::CommonResponseAsyncHandler handler(cb);
  193. const AsyncCallerContext context;
  194. cc1.commonResponseAsync(cr, handler, std::make_shared<const AsyncCallerContext>(context));
  195. ShutdownSdk();
  196. }
  197. }