rpcserviceclient_ut.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "gtest/gtest.h"
  4. #include "gmock/gmock.h"
  5. #include "alibabacloud/core/SimpleCredentialsProvider.h"
  6. #include "alibabacloud/core/RpcServiceClient.h"
  7. #include "alibabacloud/core/HttpMessage.h"
  8. #include "alibabacloud/core/HttpClient.h"
  9. #include "alibabacloud/core/HttpRequest.h"
  10. using namespace std;
  11. using namespace AlibabaCloud;
  12. using ::testing::_;
  13. using ::testing::DefaultValue;
  14. namespace AlibabaCloud {
  15. class mockRpcServiceClient : public RpcServiceClient {
  16. public:
  17. mockRpcServiceClient(const std::string & servicename, const std::shared_ptr<CredentialsProvider> &credentialsProvider,
  18. const ClientConfiguration &configuration,
  19. const std::shared_ptr<Signer> &signer = std::make_shared<HmacSha1Signer>()):
  20. RpcServiceClient(servicename, credentialsProvider, configuration, signer)
  21. {}
  22. MOCK_CONST_METHOD3(AttemptRequest, HttpClient::HttpResponseOutcome (const std::string & endpoint, const ServiceRequest &request, HttpRequest::Method method));
  23. using RpcServiceClient::makeRequest;
  24. using RpcServiceClient::buildHttpRequest;
  25. };
  26. class mockServiceRequest: public ServiceRequest {
  27. public:
  28. mockServiceRequest(const std::string &product, const std::string &version):
  29. ServiceRequest(product, version){}
  30. using ServiceRequest::addParameter;
  31. using ServiceRequest::parameter;
  32. using ServiceRequest::removeParameter;
  33. using ServiceRequest::setContent;
  34. using ServiceRequest::setParameter;
  35. using ServiceRequest::setParameters;
  36. using ServiceRequest::setResourcePath;
  37. using ServiceRequest::setProduct;
  38. using ServiceRequest::setVersion;
  39. };
  40. }
  41. TEST(RpcServiceClient, basic) {
  42. const ClientConfiguration config;
  43. std::string key = "fake-key";
  44. std::string secret = "fake-secret";
  45. std::string token = "fake-token";
  46. const Credentials credentials(key, secret, token);
  47. mockRpcServiceClient client("ecs", std::make_shared<SimpleCredentialsProvider>(credentials), config);
  48. const string product = "ECS";
  49. const string version = "1.0";
  50. const string action = "fake-action";
  51. const string endpoint = "test-cn-shanghai";
  52. RpcServiceRequest req(product, version, action);
  53. HttpRequest http_req = client.buildHttpRequest(endpoint, req, HttpRequest::Method::Get);
  54. EXPECT_TRUE(http_req.header("x-sdk-client") == std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
  55. EXPECT_TRUE(http_req.header("Host") == endpoint);
  56. EXPECT_TRUE(http_req.method() == HttpRequest::Method::Get);
  57. EXPECT_TRUE(http_req.url().query().find("AccessKeySecret") == string::npos);
  58. EXPECT_TRUE(http_req.url().query().find("AccessKeyId=fake-key") != string::npos);
  59. EXPECT_TRUE(http_req.url().query().find("&SecurityToken=fake-token") != string::npos);
  60. EXPECT_TRUE(http_req.url().query().find("&Action=fake-action") != string::npos);
  61. EXPECT_TRUE(http_req.url().query().find("&Format=JSON") != string::npos);
  62. EXPECT_TRUE(http_req.url().query().find("&RegionId=cn-hangzhou") != string::npos);
  63. EXPECT_TRUE(http_req.url().query().find("&Signature=") != string::npos);
  64. EXPECT_TRUE(http_req.url().query().find("&SignatureMethod=HMAC-SHA1") != string::npos);
  65. EXPECT_TRUE(http_req.url().query().find("&SignatureNonce=") != string::npos);
  66. EXPECT_TRUE(http_req.url().query().find("&SignatureVersion=1.0") != string::npos);
  67. EXPECT_TRUE(http_req.url().query().find("&Version=1.0") != string::npos);
  68. EXPECT_TRUE(http_req.url().query().find("&Timestamp=") != string::npos);
  69. }
  70. TEST(RpcServiceClient, mock) {
  71. const string product = "ECS";
  72. const string version = "1.0";
  73. const string action = "fake-action";
  74. const string endpoint = "test-cn-shanghai";
  75. const string servicename = "fake-service-name";
  76. const ClientConfiguration configuration;
  77. std::string key = "fake-key";
  78. std::string secret = "fake-secret";
  79. std::string token = "fake-token";
  80. const Credentials credentials(key, secret, token);
  81. RpcServiceRequest rpc_req(product, version, action);
  82. HttpClient::HttpResponseOutcome xout(Error("any-error-code", "any-error-message"));
  83. DefaultValue<HttpClient::HttpResponseOutcome>::Set(xout);
  84. mockRpcServiceClient client(servicename, std::make_shared<SimpleCredentialsProvider>(credentials), configuration);
  85. EXPECT_CALL(client, AttemptRequest(_, _, _));
  86. RpcServiceClient::JsonOutcome out = client.makeRequest(endpoint, rpc_req, HttpRequest::Method::Get);
  87. EXPECT_TRUE(out.error().errorCode() == "any-error-code");
  88. }
  89. TEST(RpcServiceClient, mock_1) {
  90. const string product = "ECS";
  91. const string version = "1.0";
  92. const string action = "fake-action";
  93. const string endpoint = "test-cn-shanghai";
  94. const string servicename = "fake-service-name";
  95. const ClientConfiguration configuration;
  96. std::string key = "fake-key";
  97. std::string secret = "fake-secret";
  98. std::string token = "fake-token";
  99. const Credentials credentials(key, secret, token);
  100. RpcServiceRequest rpc_req(product, version, action);
  101. Url url;
  102. url.setHost("example.com");
  103. HttpRequest req(url, HttpRequest::Method::Post);
  104. HttpResponse res(req);
  105. res.setBody("any-body", 8);
  106. HttpClient::HttpResponseOutcome xout(res);
  107. DefaultValue<HttpClient::HttpResponseOutcome>::Set(xout);
  108. mockRpcServiceClient client(servicename, std::make_shared<SimpleCredentialsProvider>(credentials), configuration);
  109. EXPECT_CALL(client, AttemptRequest(_, _, _));
  110. RpcServiceClient::JsonOutcome out = client.makeRequest(endpoint, rpc_req, HttpRequest::Method::Get);
  111. EXPECT_TRUE(out.error().errorCode().empty());
  112. EXPECT_TRUE(out.result() == "any-body");
  113. }