commonclient_ut.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "gtest/gtest.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/?header_k1=header_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. rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
  121. EXPECT_TRUE(rr.url().toString() == "http://cn-shanghai/?Accept=test-accept&Content-Type=test-content-type&header_k1=header_v1");
  122. EXPECT_TRUE(rr.header("Accept") == "test-accept");
  123. EXPECT_TRUE(rr.header("Content-Type") == "test-content-type");
  124. HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  125. EXPECT_TRUE(client->serviceName() == "Common");
  126. EXPECT_TRUE(rrr.url().scheme() == "http");
  127. cr.setScheme("");
  128. rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  129. EXPECT_TRUE(rrr.url().scheme() == "https");
  130. std::function<void()> func(task);
  131. Runnable* rf = new Runnable(func);
  132. EXPECT_TRUE(nbr == 0);
  133. client->asyncExecute(rf);
  134. usleep(10000);
  135. EXPECT_EQ(nbr, 1);
  136. ShutdownSdk();
  137. }
  138. TEST(CommonClient, bodyParam) {
  139. const ClientConfiguration cfg;
  140. std::string key = "accessKeyId";
  141. std::string secret = "accessSecret";
  142. std::string token = "sessionToken";
  143. const Credentials credentials(key, secret, token);
  144. CommonRequest cr;
  145. cr.setBodyParameter("TestBodyParamKey", "TestBodyParamValue");
  146. TestCommonClient* client = new TestCommonClient(credentials, cfg);
  147. HttpRequest r = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  148. const char *s = "TestBodyParamKey=TestBodyParamValue";
  149. EXPECT_EQ(*r.body(), *s);
  150. }
  151. TEST(CommonClient, abnormal) {
  152. InitializeSdk();
  153. ClientConfiguration configuration("cn-hangzhou");
  154. CommonClient client("key", "secret", configuration);
  155. CommonRequest req;
  156. req.setScheme("http");
  157. req.setDomain("aliyun.com");
  158. auto out = client.commonResponse(req);
  159. EXPECT_TRUE(out.error().errorCode() == "InvalidResponse");
  160. EXPECT_TRUE(out.result().payload().empty());
  161. /*
  162. 1: =====================<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
  163. 1: <html>
  164. 1: <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  165. 1: <title>301 Moved Permanently</title></head>
  166. 1: <body bgcolor="white">
  167. 1: <h1>301 Moved Permanently</h1>
  168. 1: <p>The requested resource has been assigned a new permanent URI.</p>
  169. 1: <hr/>Powered by Tengine/Aserver</body>
  170. 1: </html>
  171. */
  172. EXPECT_FALSE(out.error().errorMessage().empty());
  173. ShutdownSdk();
  174. }
  175. void cb(const CommonClient *client,
  176. const CommonRequest &req,
  177. const CommonClient::CommonResponseOutcome& out,
  178. const std::shared_ptr<const AsyncCallerContext>& contex) {
  179. EXPECT_FALSE(out.error().errorCode().empty());
  180. }
  181. TEST(CommonClient, async) {
  182. InitializeSdk();
  183. const ClientConfiguration cfg;
  184. std::string key = "accessKeyId";
  185. std::string secret = "accessSecret";
  186. std::string token = "sessionToken";
  187. const Credentials credentials(key, secret, token);
  188. const CommonClient cc1(credentials, cfg);
  189. CommonRequest cr;
  190. cr.setContent("test-content", 12);
  191. TestCommonClient::CommonResponseAsyncHandler handler(cb);
  192. const AsyncCallerContext context;
  193. cc1.commonResponseAsync(cr, handler, std::make_shared<const AsyncCallerContext>(context));
  194. ShutdownSdk();
  195. }
  196. }