commonclient_ut.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "gtest/gtest.h"
  4. #include "../../core/src/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.result().payload() == "");
  48. CommonClient::CommonResponseOutcome out2 = cc2.commonResponse(cr);
  49. EXPECT_TRUE(out2.error().errorCode() == "NetworkError");
  50. EXPECT_TRUE(out2.result().payload() == "");
  51. CommonClient::CommonResponseOutcome out3 = cc3.commonResponse(cr);
  52. EXPECT_TRUE(out3.error().errorCode() == "NetworkError");
  53. EXPECT_TRUE(out3.result().payload() == "");
  54. cr.setRequestPattern(CommonRequest::RequestPattern::RoaPattern);
  55. CommonClient::CommonResponseOutcome out4 = cc4.commonResponse(cr);
  56. EXPECT_TRUE(out4.error().errorCode() == "NetworkError");
  57. EXPECT_TRUE(out4.result().payload() == "");
  58. /*
  59. https://cn-hangzhou/?
  60. AccessKeyId=accessKeyId
  61. &Format=JSON
  62. &RegionId=cn-hangzhou
  63. &SecurityToken=sessionToken
  64. &Signature=tWGagYMK93km44TOY%2FY60snVYWE%3D
  65. &SignatureMethod=HMAC-SHA1
  66. &SignatureNonce=ef27311a-5151-40a0-974f-2b808a8a683e
  67. &SignatureVersion=1.0
  68. &Timestamp=2019-01-02T08%3A56%3A26Z
  69. &Version=
  70. */
  71. cr.setRequestPattern(CommonRequest::RequestPattern::RpcPattern);
  72. TestCommonClient* client = new TestCommonClient(credentials, cfg);
  73. HttpRequest r = client->buildHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  74. EXPECT_TRUE(r.method() == HttpRequest::Method::Post);
  75. EXPECT_TRUE(r.url().scheme() == "https");
  76. EXPECT_TRUE(r.url().userName() == "");
  77. EXPECT_TRUE(r.url().password() == "");
  78. EXPECT_TRUE(r.url().host() == "cn-hangzhou");
  79. EXPECT_TRUE(r.url().port() == -1);
  80. EXPECT_TRUE(r.url().fragment() == "");
  81. EXPECT_TRUE(r.url().query().find("AccessKeyId=") != string::npos);
  82. EXPECT_TRUE(r.url().query().find("Format=") != string::npos);
  83. EXPECT_TRUE(r.url().query().find("RegionId=") != string::npos);
  84. EXPECT_TRUE(r.url().query().find("SecurityToken=") != string::npos);
  85. EXPECT_TRUE(r.url().query().find("Signature=") != string::npos);
  86. EXPECT_TRUE(r.url().query().find("SignatureMethod=") != string::npos);
  87. EXPECT_TRUE(r.url().query().find("SignatureNonce=") != string::npos);
  88. EXPECT_TRUE(r.url().query().find("SignatureVersion=") != string::npos);
  89. EXPECT_TRUE(r.url().query().find("Timestamp=") != string::npos);
  90. EXPECT_TRUE(r.url().query().find("Version=") != string::npos);
  91. cr.setQueryParameter("query_k1", "query_v1");
  92. cr.setHeaderParameter("header_k1", "header_v1");
  93. cr.setScheme("");
  94. cr.setHeaderParameter("Accept", "");
  95. HttpRequest rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
  96. EXPECT_TRUE(rr.method() == HttpRequest::Method::Get);
  97. EXPECT_TRUE(rr.header("Accept") == "application/json");
  98. EXPECT_TRUE(rr.url().toString() == "https://cn-shanghai/?header_k1=header_v1");
  99. EXPECT_TRUE(rr.header("Host") == "cn-shanghai");
  100. // Wed, 09 Jan 2019 06:32:41 GMT
  101. string date = "Wed, 09 Jan 2019 06:32:41 GMT";
  102. EXPECT_TRUE(rr.header("Date").length() == date.length());
  103. string signature_nonce = "fb62c51d-f735-4ec7-870d-c6ce03368214";
  104. EXPECT_TRUE(rr.header("x-acs-signature-nonce").length() == signature_nonce.length());
  105. EXPECT_TRUE(rr.header("x-acs-signature-method") == "HMAC-SHA1");
  106. EXPECT_TRUE(rr.header("x-acs-signature-version") == "1.0");
  107. EXPECT_TRUE(rr.header("x-acs-version") == "");
  108. EXPECT_TRUE(rr.header("x-sdk-client") == string("CPP/").append(ALIBABACLOUD_VERSION_STR));
  109. EXPECT_TRUE(rr.header("x-acs-region-id") == "cn-hangzhou");
  110. // acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc=
  111. EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos);
  112. EXPECT_TRUE(rr.header("unknown-header") == "");
  113. cr.setScheme("http");
  114. cr.setHeaderParameter("Accept", "test-accept");
  115. cr.setHeaderParameter("Content-Type", "test-content-type");
  116. rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get);
  117. EXPECT_TRUE(rr.url().toString() == "http://cn-shanghai/?Accept=test-accept&Content-Type=test-content-type&header_k1=header_v1");
  118. EXPECT_TRUE(rr.header("Accept") == "test-accept");
  119. EXPECT_TRUE(rr.header("Content-Type") == "test-content-type");
  120. HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  121. EXPECT_TRUE(client->serviceName() == "Common");
  122. EXPECT_TRUE(rrr.url().scheme() == "http");
  123. cr.setScheme("");
  124. rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
  125. EXPECT_TRUE(rrr.url().scheme() == "https");
  126. std::function<void()> func(task);
  127. Runnable* rf = new Runnable(func);
  128. EXPECT_TRUE(nbr == 0);
  129. client->asyncExecute(rf);
  130. usleep(10000);
  131. EXPECT_TRUE(nbr == 1);
  132. ShutdownSdk();
  133. }
  134. void cb(const CommonClient *client,
  135. const CommonRequest &req,
  136. const CommonClient::CommonResponseOutcome& out,
  137. const std::shared_ptr<const AsyncCallerContext>& contex) {
  138. EXPECT_FALSE(out.error().errorCode().empty());
  139. }
  140. TEST(CommonClient, async) {
  141. InitializeSdk();
  142. const ClientConfiguration cfg;
  143. std::string key = "accessKeyId";
  144. std::string secret = "accessSecret";
  145. std::string token = "sessionToken";
  146. const Credentials credentials(key, secret, token);
  147. const CommonClient cc1(credentials, cfg);
  148. CommonRequest cr;
  149. cr.setContent("test-content", 12);
  150. TestCommonClient::CommonResponseAsyncHandler handler(cb);
  151. const AsyncCallerContext context;
  152. cc1.commonResponseAsync(cr, handler, std::make_shared<const AsyncCallerContext>(context));
  153. ShutdownSdk();
  154. }
  155. }