add more unit test

This commit is contained in:
zhangzifa
2019-01-10 23:53:03 +08:00
committed by Jackson Tian
parent 790577e792
commit 66910b1961
5 changed files with 269 additions and 26 deletions

View File

@@ -26,7 +26,8 @@ namespace AlibabaCloud
public:
RoaServiceRequest(const std::string &product, const std::string &version);
virtual ~RoaServiceRequest();
using ServiceRequest::setParameter;
using ServiceRequest::setContent;
};
}
#endif // !ALIBABACLOUD_CORE_ROASERVICEREQUEST_H_
#endif // !ALIBABACLOUD_CORE_ROASERVICEREQUEST_H_

View File

@@ -1,24 +0,0 @@
#!/bin/bash
action=$1
if [ $action == "init" ]; then
echo 'init coverage'
lcov -d sdk_build -z
lcov -d sdk_build -b . --no-external --initial -c -o initCoverage.info
fi
echo 'run test'
cd sdk_build
ctest
cd ..
echo 'create info after test'
lcov -d sdk_build -b . --no-external -c -o testCoverage.info
echo 'generate html'
genhtml -o coverageReport --prefix=`pwd` initCoverage.info testCoverage.info
echo 'check report ' `pwd`/coverageReport/index.html

View File

@@ -39,6 +39,7 @@ add_executable(core_ut
alibabacloud_ut.cc
asynccallercontext_ut.cc
clientconfiguration_ut.cc
commonclient_ut.cc
commonrequest_ut.cc
commonresponse_ut.cc
credentials_ut.cc
@@ -51,6 +52,7 @@ add_executable(core_ut
httpmessage_ut.cc
hmacsha1signer_ut.cc
networkproxy_ut.cc
roaserviceclient_ut.cc
roaservicerequest_ut.cc
rpcservicerequest_ut.cc
runnable_ut.cc

View File

@@ -0,0 +1,115 @@
#include <iostream>
#include <stdio.h>
#include "gtest/gtest.h"
#include "../../core/src/Utils.h"
#include "alibabacloud/core/Config.h"
#include "alibabacloud/core/CommonClient.h"
#include "alibabacloud/core/CommonResponse.h"
#include "alibabacloud/core/CommonRequest.h"
#include "alibabacloud/core/SimpleCredentialsProvider.h"
using namespace std;
using namespace AlibabaCloud;
namespace AlibabaCloud {
class TestCommonClient : public CommonClient {
public:
TestCommonClient(const Credentials &credentials, const ClientConfiguration &configuration):
CommonClient(credentials, configuration)
{}
HttpRequest buildHttp(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) {
return CommonClient::buildHttpRequest(endpoint, msg, method);
}
HttpRequest buildRoa(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) {
return CommonClient::buildRoaHttpRequest(endpoint, msg, method);
}
HttpRequest buildRpc(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) {
return CommonClient::buildRpcHttpRequest(endpoint, msg, method);
}
JsonOutcome buildReq(const std::string & endpoint, const CommonRequest &msg, HttpRequest::Method method) {
return CommonClient::makeRequest(endpoint, msg, method);
}
};
}
TEST(CommonClient, basic) {
const ClientConfiguration cfg;
std::string key = "accessKeyId";
std::string secret = "accessSecret";
std::string token = "sessionToken";
const Credentials credentials(key, secret, token);
const CommonClient cc1(credentials, cfg);
CommonClient cc2(std::make_shared<SimpleCredentialsProvider>(credentials), cfg);
CommonClient cc3(key, secret, cfg);
CommonRequest cr;
cr.setContent("test-content", 12);
CommonClient::CommonResponseOutcome out = cc1.commonResponse(cr);
/*
https://cn-hangzhou/?
AccessKeyId=accessKeyId
&Format=JSON
&RegionId=cn-hangzhou
&SecurityToken=sessionToken
&Signature=tWGagYMK93km44TOY%2FY60snVYWE%3D
&SignatureMethod=HMAC-SHA1
&SignatureNonce=ef27311a-5151-40a0-974f-2b808a8a683e
&SignatureVersion=1.0
&Timestamp=2019-01-02T08%3A56%3A26Z
&Version=
*/
TestCommonClient* client = new TestCommonClient(credentials, cfg);
HttpRequest r = client->buildHttp("cn-hangzhou", cr, HttpRequest::Method::Post);
EXPECT_TRUE(r.method() == HttpRequest::Method::Post);
EXPECT_TRUE(r.url().scheme() == "https");
EXPECT_TRUE(r.url().userName() == "");
EXPECT_TRUE(r.url().password() == "");
EXPECT_TRUE(r.url().host() == "cn-hangzhou");
EXPECT_TRUE(r.url().port() == -1);
EXPECT_TRUE(r.url().fragment() == "");
EXPECT_TRUE(r.url().query().find("AccessKeyId=") != string::npos);
EXPECT_TRUE(r.url().query().find("Format=") != string::npos);
EXPECT_TRUE(r.url().query().find("RegionId=") != string::npos);
EXPECT_TRUE(r.url().query().find("SecurityToken=") != string::npos);
EXPECT_TRUE(r.url().query().find("Signature=") != string::npos);
EXPECT_TRUE(r.url().query().find("SignatureMethod=") != string::npos);
EXPECT_TRUE(r.url().query().find("SignatureNonce=") != string::npos);
EXPECT_TRUE(r.url().query().find("SignatureVersion=") != string::npos);
EXPECT_TRUE(r.url().query().find("Timestamp=") != string::npos);
EXPECT_TRUE(r.url().query().find("Version=") != string::npos);
cr.setQueryParameter("query_k1", "query_v1");
cr.setHeaderParameter("header_k1", "header_v1");
HttpRequest rr = client->buildRoa("cn-shanghai", cr, HttpRequest::Method::Get);
EXPECT_TRUE(rr.method() == HttpRequest::Method::Get);
EXPECT_TRUE(rr.header("Accept") == "application/json");
EXPECT_TRUE(rr.url().toString() == "https://cn-shanghai/?header_k1=header_v1");
EXPECT_TRUE(rr.header("Host") == "cn-shanghai");
// Wed, 09 Jan 2019 06:32:41 GMT
string date = "Wed, 09 Jan 2019 06:32:41 GMT";
EXPECT_TRUE(rr.header("Date").length() == date.length());
string signature_nonce = "fb62c51d-f735-4ec7-870d-c6ce03368214";
EXPECT_TRUE(rr.header("x-acs-signature-nonce").length() == signature_nonce.length());
EXPECT_TRUE(rr.header("x-acs-signature-method") == "HMAC-SHA1");
EXPECT_TRUE(rr.header("x-acs-signature-version") == "1.0");
EXPECT_TRUE(rr.header("x-acs-version") == "");
EXPECT_TRUE(rr.header("x-sdk-client") == string("CPP/").append(ALIBABACLOUD_VERSION_STR));
EXPECT_TRUE(rr.header("x-acs-region-id") == "cn-hangzhou");
// acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc=
EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos);
EXPECT_TRUE(rr.header("unknown-header") == "");
HttpRequest rrr = client->buildRpc("cn-hangzhou", cr, HttpRequest::Method::Post);
cout << "" <<endl;
}

View File

@@ -0,0 +1,149 @@
#include <iostream>
#include <stdio.h>
#include "gtest/gtest.h"
#include "alibabacloud/core/SimpleCredentialsProvider.h"
#include "alibabacloud/core/RoaServiceClient.h"
#include "alibabacloud/core/HttpMessage.h"
using namespace std;
using namespace AlibabaCloud;
namespace AlibabaCloud {
class TestRoaClient : public RoaServiceClient {
public:
TestRoaClient(const std::string & servicename, const std::shared_ptr<CredentialsProvider> &credentialsProvider,
const ClientConfiguration &configuration,
const std::shared_ptr<Signer> &signer = std::make_shared<HmacSha1Signer>()):
RoaServiceClient(servicename, credentialsProvider, configuration, signer)
{}
JsonOutcome makeRequest(const std::string &endpoint, const RoaServiceRequest &msg, HttpRequest::Method method = HttpRequest::Method::Get)const {
return RoaServiceClient::makeRequest(endpoint, msg, method);
}
HttpRequest buildHttpRequest(const std::string & endpoint, const ServiceRequest &msg, HttpRequest::Method method)const {
return RoaServiceClient::buildHttpRequest(endpoint, msg, method);
}
};
class TestRoaServiceRequest: public ServiceRequest {
public:
TestRoaServiceRequest(const std::string &product, const std::string &version):
ServiceRequest(product, version){}
void addParameter(const ParameterNameType &name, const ParameterValueType &value) {
addParameter(name, value);
}
ParameterValueType parameter(const ParameterNameType &name)const {
return parameter(name);
}
void removeParameter(const ParameterNameType &name) {
removeParameter(name);
}
void setContent(const char *data, size_t size) {
setContent(data, size);
}
void setParameter(const ParameterNameType &name, const ParameterValueType &value) {
setParameter(name, value);
}
void setParameters(const ParameterCollection &params) {
setParameters(params);
}
void setResourcePath(const std::string &path) {
setResourcePath(path);
}
void setProduct(const std::string &product) {
setProduct(product);
}
void setVersion(const std::string &version) {
setVersion(version);
}
};
}
TEST(RoaServiceClient, b0) {
const ClientConfiguration config;
std::string key = "accessKeyId";
std::string secret = "accessSecret";
std::string token = "sessionToken";
const Credentials credentials(key, secret, token);
// const CommonClient cc1(credentials, cfg);
// CommonClient cc2(std::make_shared<SimpleCredentialsProvider>(credentials), cfg);
TestRoaClient client("ecs", std::make_shared<SimpleCredentialsProvider>(credentials), config);
const string product = "ECS";
const string version = "1.0";
RoaServiceRequest roa_req(product, version);
// roa_req.setParameter("key1", "value1");
// roa_req.setQueryParameter("query_k1", "query_v1");
// string content = "test-content";
// roa_req.setContent(content.c_str(), content.length());
client.makeRequest("cn-shanghai", roa_req, HttpRequest::Method::Get);
RoaServiceRequest req(product, version);
req.setParameter("a", "b");
req.setContent("123456789", 9);
client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get);
}
TEST(RoaServiceClient, basic) {
const string service = "ECS";
const std::string regionId = "cn-shanghai";
const std::string hostname = "hostname";
const std::string user = "user";
const std::string password = "password";
uint16_t port = 12345;
const NetworkProxy proxy(NetworkProxy::Http, hostname, port, user, password);
HmacSha1Signer sig;
const std::string access_key = "accessKeyId";
const std::string access_secret = "accessKeySecret";
const std::string session_token = "sessionToken";
Credentials credentials(access_key, access_secret, session_token);
ClientConfiguration config(regionId, proxy);
RoaServiceClient roa(service,
std::make_shared<SimpleCredentialsProvider>(credentials),
config);
TestRoaClient client(service, std::make_shared<SimpleCredentialsProvider>(credentials), config);
HttpMessage::HeaderCollection headers;
// client.canonicalizedHeaders(headers);
// REQUIRE(roa.actionName() == action);
}
// EcsClient::EcsClient(const Credentials &credentials, const ClientConfiguration &configuration) :
// RpcServiceClient(SERVICE_NAME, std::make_shared<SimpleCredentialsProvider>(credentials), configuration)
// {
// auto locationClient = std::make_shared<LocationClient>(credentials, configuration);
// endpointProvider_ = std::make_shared<EndpointProvider>(locationClient, configuration.regionId(), SERVICE_NAME, "ecs");
// }