diff --git a/test/core/commonclient_ut.cc b/test/core/commonclient_ut.cc index 8f4cea996..652c9effc 100644 --- a/test/core/commonclient_ut.cc +++ b/test/core/commonclient_ut.cc @@ -25,6 +25,7 @@ namespace AlibabaCloud { using CommonClient::buildRpcHttpRequest; using CommonClient::makeRequest; using CommonClient::asyncExecute; + using CommonClient::commonResponseAsync; }; } @@ -33,117 +34,157 @@ static void task() { nbr |= 0x01; } +namespace { -TEST(CommonClient, basic) { + TEST(CommonClient, basic) { + InitializeSdk(); + const ClientConfiguration cfg; - InitializeSdk(); - const ClientConfiguration cfg; + std::string key = "accessKeyId"; + std::string secret = "accessSecret"; + std::string token = "sessionToken"; - std::string key = "accessKeyId"; - std::string secret = "accessSecret"; - std::string token = "sessionToken"; + const Credentials credentials(key, secret, token); - const Credentials credentials(key, secret, token); + const CommonClient cc1(credentials, cfg); + CommonClient cc2(std::make_shared(credentials), cfg); + CommonClient cc3(key, secret, cfg); + CommonClient cc4(key, secret, cfg); - const CommonClient cc1(credentials, cfg); - CommonClient cc2(std::make_shared(credentials), cfg); - CommonClient cc3(key, secret, cfg); + CommonRequest cr; + cr.setContent("test-content", 12); - CommonRequest cr; - cr.setContent("test-content", 12); + CommonClient::CommonResponseOutcome out1 = cc1.commonResponse(cr); + EXPECT_TRUE(out1.error().errorCode() == "NetworkError"); + EXPECT_TRUE(out1.result().payload() == ""); - CommonClient::CommonResponseOutcome out1 = cc1.commonResponse(cr); - EXPECT_TRUE(out1.error().errorCode() == "NetworkError"); - EXPECT_TRUE(out1.result().payload() == ""); + CommonClient::CommonResponseOutcome out2 = cc2.commonResponse(cr); + EXPECT_TRUE(out2.error().errorCode() == "NetworkError"); + EXPECT_TRUE(out2.result().payload() == ""); - CommonClient::CommonResponseOutcome out2 = cc2.commonResponse(cr); - EXPECT_TRUE(out2.error().errorCode() == "NetworkError"); - EXPECT_TRUE(out2.result().payload() == ""); + CommonClient::CommonResponseOutcome out3 = cc3.commonResponse(cr); + EXPECT_TRUE(out3.error().errorCode() == "NetworkError"); + EXPECT_TRUE(out3.result().payload() == ""); - CommonClient::CommonResponseOutcome out3 = cc3.commonResponse(cr); - EXPECT_TRUE(out3.error().errorCode() == "NetworkError"); - EXPECT_TRUE(out3.result().payload() == ""); + cr.setRequestPattern(CommonRequest::RequestPattern::RoaPattern); + CommonClient::CommonResponseOutcome out4 = cc4.commonResponse(cr); + EXPECT_TRUE(out4.error().errorCode() == "NetworkError"); + EXPECT_TRUE(out4.result().payload() == ""); -/* -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= -*/ + /* + 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= + */ + cr.setRequestPattern(CommonRequest::RequestPattern::RpcPattern); + TestCommonClient* client = new TestCommonClient(credentials, cfg); + HttpRequest r = client->buildHttpRequest("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); - TestCommonClient* client = new TestCommonClient(credentials, cfg); - HttpRequest r = client->buildHttpRequest("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() == ""); + cr.setQueryParameter("query_k1", "query_v1"); + cr.setHeaderParameter("header_k1", "header_v1"); + cr.setScheme(""); + cr.setHeaderParameter("Accept", ""); + HttpRequest rr = client->buildRoaHttpRequest("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(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); + 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") == ""); - cr.setQueryParameter("query_k1", "query_v1"); - cr.setHeaderParameter("header_k1", "header_v1"); - cr.setScheme(""); - cr.setHeaderParameter("Accept", ""); - HttpRequest rr = client->buildRoaHttpRequest("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()); + cr.setScheme("http"); + cr.setHeaderParameter("Accept", "test-accept"); + cr.setHeaderParameter("Content-Type", "test-content-type"); + rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get); + EXPECT_TRUE(rr.url().toString() == "http://cn-shanghai/?Accept=test-accept&Content-Type=test-content-type&header_k1=header_v1"); + EXPECT_TRUE(rr.header("Accept") == "test-accept"); + EXPECT_TRUE(rr.header("Content-Type") == "test-content-type"); - 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->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post); + EXPECT_TRUE(client->serviceName() == "Common"); + EXPECT_TRUE(rrr.url().scheme() == "http"); - cr.setScheme("http"); - cr.setHeaderParameter("Accept", "test-accept"); - cr.setHeaderParameter("Content-Type", "test-content-type"); - rr = client->buildRoaHttpRequest("cn-shanghai", cr, HttpRequest::Method::Get); - EXPECT_TRUE(rr.url().toString() == "http://cn-shanghai/?Accept=test-accept&Content-Type=test-content-type&header_k1=header_v1"); - EXPECT_TRUE(rr.header("Accept") == "test-accept"); - EXPECT_TRUE(rr.header("Content-Type") == "test-content-type"); + cr.setScheme(""); + rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post); + EXPECT_TRUE(rrr.url().scheme() == "https"); - HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post); - EXPECT_TRUE(client->serviceName() == "Common"); - EXPECT_TRUE(rrr.url().scheme() == "http"); + std::function func(task); + Runnable* rf = new Runnable(func); + EXPECT_TRUE(nbr == 0); + client->asyncExecute(rf); + usleep(10000); + EXPECT_TRUE(nbr == 1); + ShutdownSdk(); + } - cr.setScheme(""); - rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post); - EXPECT_TRUE(rrr.url().scheme() == "https"); - std::function func(task); - Runnable* rf = new Runnable(func); - EXPECT_TRUE(nbr == 0); - client->asyncExecute(rf); - usleep(10000); - EXPECT_TRUE(nbr == 1); - ShutdownSdk(); -} + + void cb(const CommonClient *client, + const CommonRequest &req, + const CommonClient::CommonResponseOutcome& out, + const std::shared_ptr& contex) { + EXPECT_FALSE(out.error().errorCode().empty()); + } + + TEST(CommonClient, async) { + InitializeSdk(); + 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); + + CommonRequest cr; + cr.setContent("test-content", 12); + + TestCommonClient::CommonResponseAsyncHandler handler(cb); + + + const AsyncCallerContext context; + cc1.commonResponseAsync(cr, handler, std::make_shared(context)); + + ShutdownSdk(); + } + +} \ No newline at end of file diff --git a/test/core/error_ut.cc b/test/core/error_ut.cc index 3e7dbb293..40aa87e60 100644 --- a/test/core/error_ut.cc +++ b/test/core/error_ut.cc @@ -30,4 +30,8 @@ TEST(Error, basic) { EXPECT_TRUE(error.errorCode() == code1); EXPECT_TRUE(error.errorMessage() == message1); + + const string detail = "any error detail"; + error.setDetail(detail); + EXPECT_TRUE(error.detail() == detail); } diff --git a/test/core/locationclient_ut.cc b/test/core/locationclient_ut.cc index a89dbfc3e..337a79248 100644 --- a/test/core/locationclient_ut.cc +++ b/test/core/locationclient_ut.cc @@ -132,9 +132,6 @@ namespace { EXPECT_TRUE(it != protocols.end()); } - - - TEST(LocationClient, callable) { InitializeSdk(); ClientConfiguration configuration("cn-hangzhou"); @@ -171,4 +168,32 @@ namespace { EXPECT_TRUE(it != protocols.end()); ShutdownSdk(); } + + +void cb(const LocationClient *client, + const Model::DescribeEndpointsRequest &req, + const LocationClient::DescribeEndpointsOutcome& out, + const std::shared_ptr& contex) { + EXPECT_FALSE(out.error().errorCode().empty()); + } + + TEST(LocationClient, async) { + InitializeSdk(); + + ClientConfiguration configuration("cn-hangzhou"); + Model::DescribeEndpointsRequest req; + req.setId("cn-hangzhou"); + req.setServiceCode("ecs"); + req.setType("openAPI"); + + LocationClient client("key", "secret", configuration); + LocationClient::DescribeEndpointsOutcome out; + + const AsyncCallerContext context; + + LocationClient::DescribeEndpointsAsyncHandler handler(cb); + client.describeEndpointsAsync(req, handler, std::make_shared(context)); + ShutdownSdk(); + } + } diff --git a/test/core/servicerequest_ut.cc b/test/core/servicerequest_ut.cc index fd9ac72a6..40b5fdc64 100644 --- a/test/core/servicerequest_ut.cc +++ b/test/core/servicerequest_ut.cc @@ -32,7 +32,7 @@ namespace { using ServiceRequest::setVersion; }; - TEST(ServiceRequestTest, basic) { + TEST(ServiceRequest, basic) { TestServiceRequest sr1("ECS", "1.0"); TestServiceRequest sr2(sr1); diff --git a/test/core/stsclient_ut.cc b/test/core/stsclient_ut.cc index 378bb1788..a3dcaffda 100644 --- a/test/core/stsclient_ut.cc +++ b/test/core/stsclient_ut.cc @@ -15,6 +15,7 @@ using namespace AlibabaCloud; using namespace AlibabaCloud::Sts; namespace { + class mockStsClient : public StsClient { public: mockStsClient(const string& key, const string& secret, const ClientConfiguration& cfg): @@ -102,6 +103,25 @@ namespace { ShutdownSdk(); } + void cb(const StsClient *client, + const Model::GetCallerIdentityRequest &req, + const StsClient::GetCallerIdentityOutcome& out, + const std::shared_ptr& contex) { + EXPECT_FALSE(out.error().errorCode().empty()); + } + + TEST(StsClient, getCallerIdentity_async) { + InitializeSdk(); + ClientConfiguration configuration("cn-hangzhou"); + Model::GetCallerIdentityRequest req; + StsClient client("key", "secret", configuration); + const AsyncCallerContext context; + + StsClient::GetCallerIdentityAsyncHandler handler(cb); + client.getCallerIdentityAsync(req, handler, std::make_shared(context)); + ShutdownSdk(); + } + TEST(StsClient, assumeRole) { ClientConfiguration configuration; RpcServiceClient::JsonOutcome xout("{\"RequestId\":\"any-request-id\",\"success\":true,\"AssumedRoleUser\":{\"AssumedRoleId\":\"123456789\",\"Arn\":\"any-arn\"},\"Credentials\":{\"AccessKeyId\":\"any-key\",\"AccessKeySecret\":\"any-secret\",\"Expiration\":\"2019-01-15T16:31:10Z\",\"SecurityToken\":\"any-token\"}}"); @@ -151,4 +171,24 @@ namespace { EXPECT_TRUE(out.error().errorCode() == "any-error-code"); } + void assumerole_cb(const StsClient *client, + const Model::AssumeRoleRequest &req, + const StsClient::AssumeRoleOutcome& out, + const std::shared_ptr& contex) { + EXPECT_FALSE(out.error().errorCode().empty()); + } + + TEST(StsClient, assumeRole_async) { + InitializeSdk(); + ClientConfiguration configuration("cn-hangzhou"); + Model::AssumeRoleRequest req; + req.setRoleArn("acs:ram::1940345800212929:root"); + req.setRoleSessionName("test_session"); + req.setDurationSeconds(1000); + StsClient client("key", "secret", configuration); + const AsyncCallerContext context; + StsClient::AssumeRoleAsyncHandler handler(assumerole_cb); + client.assumeRoleAsync(req, handler, std::make_shared(context)); + ShutdownSdk(); + } }