provide more information when error occurs
This commit is contained in:
@@ -56,19 +56,24 @@ namespace {
|
||||
|
||||
CommonClient::CommonResponseOutcome out1 = cc1.commonResponse(cr);
|
||||
EXPECT_TRUE(out1.error().errorCode() == "NetworkError");
|
||||
EXPECT_TRUE(out1.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
|
||||
|
||||
EXPECT_TRUE(out1.result().payload() == "");
|
||||
|
||||
CommonClient::CommonResponseOutcome out2 = cc2.commonResponse(cr);
|
||||
EXPECT_TRUE(out2.error().errorCode() == "NetworkError");
|
||||
EXPECT_TRUE(out2.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
|
||||
EXPECT_TRUE(out2.result().payload() == "");
|
||||
|
||||
CommonClient::CommonResponseOutcome out3 = cc3.commonResponse(cr);
|
||||
EXPECT_TRUE(out3.error().errorCode() == "NetworkError");
|
||||
EXPECT_TRUE(out3.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
|
||||
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.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
|
||||
EXPECT_TRUE(out4.result().payload() == "");
|
||||
|
||||
/*
|
||||
@@ -154,7 +159,33 @@ namespace {
|
||||
ShutdownSdk();
|
||||
}
|
||||
|
||||
TEST(CommonClient, abnormal) {
|
||||
InitializeSdk();
|
||||
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
CommonClient client("key", "secret", configuration);
|
||||
|
||||
CommonRequest req;
|
||||
req.setScheme("http");
|
||||
req.setDomain("aliyun.com");
|
||||
|
||||
auto out = client.commonResponse(req);
|
||||
EXPECT_TRUE(out.error().errorCode() == "InvalidResponse");
|
||||
EXPECT_TRUE(out.result().payload().empty());
|
||||
/*
|
||||
1: =====================<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
|
||||
1: <html>
|
||||
1: <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
1: <title>301 Moved Permanently</title></head>
|
||||
1: <body bgcolor="white">
|
||||
1: <h1>301 Moved Permanently</h1>
|
||||
1: <p>The requested resource has been assigned a new permanent URI.</p>
|
||||
1: <hr/>Powered by Tengine/Aserver</body>
|
||||
1: </html>
|
||||
*/
|
||||
EXPECT_FALSE(out.error().errorMessage().empty());
|
||||
ShutdownSdk();
|
||||
}
|
||||
|
||||
void cb(const CommonClient *client,
|
||||
const CommonRequest &req,
|
||||
|
||||
@@ -34,6 +34,7 @@ TEST(CoreClient, basic) {
|
||||
HttpResponse res;
|
||||
Error e1 = client.buildCoreError(res);
|
||||
EXPECT_TRUE(e1.errorCode() == ("InvalidResponse"));
|
||||
EXPECT_TRUE(e1.errorMessage() == ("body is empty"));
|
||||
string body = "{\"Code\":\"any-error-code\",\"Message\":\"any-error-message\",\"HostId\":\"any-host-id\",\"RequestId\":\"any-request-id\"}";
|
||||
res.setBody(body.c_str(), body.size());
|
||||
Error e2 = client.buildCoreError(res);
|
||||
|
||||
@@ -83,7 +83,7 @@ TEST(CurlHttpClient, netWorkError) {
|
||||
HttpRequest request;
|
||||
HttpClient::HttpResponseOutcome out = client.makeRequest(request);
|
||||
EXPECT_TRUE(out.error().errorCode() == "NetworkError");
|
||||
EXPECT_TRUE(out.error().errorMessage() == "Failed to connect to host or proxy.");
|
||||
EXPECT_TRUE(out.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
|
||||
}
|
||||
|
||||
TEST(CurlHttpClient, netWorkErrorWithHttpProxy) {
|
||||
@@ -99,7 +99,7 @@ TEST(CurlHttpClient, netWorkErrorWithHttpProxy) {
|
||||
HttpRequest request;
|
||||
HttpClient::HttpResponseOutcome out = client.makeRequest(request);
|
||||
EXPECT_TRUE(out.error().errorCode() == "NetworkError");
|
||||
EXPECT_TRUE(out.error().errorMessage() == "Failed to connect to host or proxy.");
|
||||
EXPECT_TRUE(out.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
|
||||
}
|
||||
|
||||
TEST(CurlHttpClient, netWorkErrorWithSocks5Proxy) {
|
||||
@@ -115,7 +115,7 @@ TEST(CurlHttpClient, netWorkErrorWithSocks5Proxy) {
|
||||
HttpRequest request;
|
||||
HttpClient::HttpResponseOutcome out = client.makeRequest(request);
|
||||
EXPECT_TRUE(out.error().errorCode() == "NetworkError");
|
||||
EXPECT_TRUE(out.error().errorMessage() == "Failed to connect to host or proxy.");
|
||||
EXPECT_TRUE(out.error().errorMessage().find("Failed to connect to host or proxy: GET ") == 0);
|
||||
}
|
||||
|
||||
TEST(CurlHttpClient, mock) {
|
||||
|
||||
@@ -5,21 +5,46 @@
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
namespace {
|
||||
|
||||
TEST(HmacSha1Signer, assign) {
|
||||
|
||||
HmacSha1Signer sig;
|
||||
string sign = sig.generate("GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeRegions"
|
||||
"%26Format%3DXML%26RegionId%3Dregion1%26SignatureMethod%3DHMAC-SHA1"
|
||||
"%26SignatureNonce%3DNwDAxvLU6tFE0DVb%26SignatureVersion%3D1.0"
|
||||
"%26TimeStamp%3D2012-12-26T10%253A33%253A56Z%26Version%3D2013-01-10",
|
||||
"testsecret&");
|
||||
EXPECT_TRUE("axE3FUHgDyfm9/+Iep0HpZXrRwE=" == sign);
|
||||
EXPECT_TRUE(sig.type() == HmacSha1Signer::HmacSha1);
|
||||
EXPECT_TRUE(sig.name() == "HMAC-SHA1");
|
||||
EXPECT_TRUE(sig.version() == "1.0");
|
||||
}
|
||||
HmacSha1Signer sig;
|
||||
string sign = sig.generate("GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeRegions"
|
||||
"%26Format%3DXML%26RegionId%3Dregion1%26SignatureMethod%3DHMAC-SHA1"
|
||||
"%26SignatureNonce%3DNwDAxvLU6tFE0DVb%26SignatureVersion%3D1.0"
|
||||
"%26TimeStamp%3D2012-12-26T10%253A33%253A56Z%26Version%3D2013-01-10",
|
||||
"testsecret&");
|
||||
EXPECT_TRUE("axE3FUHgDyfm9/+Iep0HpZXrRwE=" == sign);
|
||||
EXPECT_TRUE(sig.type() == HmacSha1Signer::HmacSha1);
|
||||
EXPECT_TRUE(sig.name() == "HMAC-SHA1");
|
||||
EXPECT_TRUE(sig.version() == "1.0");
|
||||
}
|
||||
|
||||
TEST(HmacSha1Signer, edge_from_empty_string) {
|
||||
HmacSha1Signer sign;
|
||||
EXPECT_TRUE(sign.generate("", "") == "");
|
||||
}
|
||||
TEST(HmacSha1Signer, edge_from_empty_string) {
|
||||
HmacSha1Signer sign;
|
||||
EXPECT_TRUE(sign.generate("", "") == "");
|
||||
}
|
||||
|
||||
TEST(HmacSha1Signer, unicode) {
|
||||
HmacSha1Signer sig;
|
||||
string sign = sig.generate("GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeRegions"
|
||||
"%26TestUnicode%3DDescribeRegions啊啊啊"
|
||||
"%26Format%3DXML%26RegionId%3Dregion1%26SignatureMethod%3DHMAC-SHA1"
|
||||
"%26SignatureNonce%3DNwDAxvLU6tFE0DVb%26SignatureVersion%3D1.0"
|
||||
"%26TimeStamp%3D2012-12-26T10%253A33%253A56Z%26Version%3D2013-01-10",
|
||||
"testsecret&");
|
||||
EXPECT_TRUE("VJ1a5YBLy+shtBK/l97MCMq+mzs=" == sign);
|
||||
}
|
||||
|
||||
TEST(HmacSha1Signer, specialQuery) {
|
||||
HmacSha1Signer sig;
|
||||
string sign = sig.generate("GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeRegions"
|
||||
"%26TestUnicode%3DDescri&beRegions"
|
||||
"%26Format%3DXML%26RegionId%3Dregion1%26SignatureMethod%3DHMAC-SHA1"
|
||||
"%26SignatureNonce%3DNwDAxvLU6tFE0DVb%26SignatureVersion%3D1.0"
|
||||
"%26TimeStamp%3D2012-12-26T10%253A33%253A56Z%26Version%3D2013-01-10",
|
||||
"testsecret&");
|
||||
EXPECT_TRUE("3PuleV7r01lLr4wwvWNCOuLKV5A=" == sign);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ using namespace AlibabaCloud;
|
||||
|
||||
namespace {
|
||||
TEST(Signer, basic) {
|
||||
|
||||
HmacSha1Signer sig;
|
||||
string sign = sig.generate("GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeRegions"
|
||||
"%26Format%3DXML%26RegionId%3Dregion1%26SignatureMethod%3DHMAC-SHA1"
|
||||
@@ -15,6 +14,17 @@ namespace {
|
||||
"%26TimeStamp%3D2012-12-26T10%253A33%253A56Z%26Version%3D2013-01-10",
|
||||
"testsecret&");
|
||||
EXPECT_TRUE("axE3FUHgDyfm9/+Iep0HpZXrRwE=" == sign);
|
||||
}
|
||||
|
||||
TEST(Signer, unicode) {
|
||||
HmacSha1Signer sig;
|
||||
string sign = sig.generate("GET&%2F&AccessKeyId%3Dtestid%26Action%3DDescribeRegions"
|
||||
"%26TestUnicode%3DDescribeRegions啊啊啊"
|
||||
"%26Format%3DXML%26RegionId%3Dregion1%26SignatureMethod%3DHMAC-SHA1"
|
||||
"%26SignatureNonce%3DNwDAxvLU6tFE0DVb%26SignatureVersion%3D1.0"
|
||||
"%26TimeStamp%3D2012-12-26T10%253A33%253A56Z%26Version%3D2013-01-10",
|
||||
"testsecret&");
|
||||
cout << "aaaaaaaaaaa======================================= " << sign << endl;
|
||||
EXPECT_TRUE("axE3FUHgDyfm9/+Iep0HpZXrRwE=" == sign);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/ecs/EcsClient.h"
|
||||
#include "alibabacloud/core/CommonClient.h"
|
||||
#include "alibabacloud/core/CommonRequest.h"
|
||||
#include "alibabacloud/core/sts/StsClient.h"
|
||||
#include "alibabacloud/core/StsAssumeRoleCredentialsProvider.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
@@ -25,4 +30,64 @@ namespace {
|
||||
EXPECT_TRUE(outcome.result().getTotalCount() == 16);
|
||||
ShutdownSdk();
|
||||
}
|
||||
|
||||
TEST(ecs, describeInstancesViaCommonRequest) {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
InitializeSdk();
|
||||
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
CommonClient client(key, secret, configuration);
|
||||
CommonRequest request(CommonRequest::RpcPattern);
|
||||
|
||||
request.setScheme("https");
|
||||
request.setDomain("ecs.aliyuncs.com");
|
||||
request.setHttpMethod(HttpRequest::Get);
|
||||
request.setHeaderParameter("Content-Type", "application/json;chrset=utf-8");
|
||||
request.setVersion("2014-05-26");
|
||||
request.setQueryParameter("action", "DescribeInstances");
|
||||
|
||||
auto outcome = client.commonResponse(request);
|
||||
|
||||
EXPECT_TRUE(outcome.error().errorCode().empty());
|
||||
EXPECT_TRUE(outcome.result().payload().find("\"TotalCount\":16") != string::npos);
|
||||
ShutdownSdk();
|
||||
}
|
||||
|
||||
TEST(ecs, describeInstancesViaCommonRequestAndStsAssumeRole) {
|
||||
|
||||
utUtils utils;
|
||||
const string sub_user_key = utils.get_env("ENV_AccessKeyId_AssumeRole");
|
||||
const string sub_user_secret = utils.get_env("ENV_AccessKeySecret_AssumeRole");
|
||||
const Credentials sub_user_credentials(sub_user_key, sub_user_secret);
|
||||
|
||||
const string arn = "acs:ram::1940345800212929:role/testassumerole";
|
||||
const string session = "roleSessionName";
|
||||
const string policy = "";
|
||||
int duration = 1800;
|
||||
|
||||
InitializeSdk();
|
||||
ClientConfiguration configuration("cn-hangzhou");
|
||||
StsAssumeRoleCredentialsProvider provider(sub_user_credentials, configuration, arn, session, policy, duration);
|
||||
Credentials assumed_role_credentials = provider.getCredentials();
|
||||
CommonClient client(assumed_role_credentials, configuration);
|
||||
CommonRequest request(CommonRequest::RpcPattern);
|
||||
|
||||
request.setScheme("https");
|
||||
request.setDomain("ecs.aliyuncs.com");
|
||||
request.setHttpMethod(HttpRequest::Get);
|
||||
request.setHeaderParameter("Content-Type", "application/json;chrset=utf-8");
|
||||
request.setVersion("2014-05-26");
|
||||
request.setQueryParameter("action", "DescribeInstances");
|
||||
|
||||
auto outcome = client.commonResponse(request);
|
||||
|
||||
EXPECT_TRUE(outcome.error().errorCode().empty());
|
||||
EXPECT_TRUE(outcome.result().payload().find("\"TotalCount\":") != string::npos);
|
||||
EXPECT_TRUE(outcome.result().payload().find("\"PageNumber\":") != string::npos);
|
||||
EXPECT_TRUE(outcome.result().payload().find("\"PageSize\":") != string::npos);
|
||||
EXPECT_TRUE(outcome.result().payload().find("\"Instances\":") != string::npos);
|
||||
ShutdownSdk();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace {
|
||||
ClientConfiguration configuration("cn-shanghai");
|
||||
|
||||
CommonClient client(key, secret, configuration);
|
||||
// 创建API请求并设置参数
|
||||
// create request and assign parameters
|
||||
CommonRequest request(CommonRequest::RoaPattern);
|
||||
request.setScheme("http");
|
||||
request.setDomain("nlp.cn-shanghai.aliyuncs.com");
|
||||
@@ -46,7 +46,7 @@ namespace {
|
||||
ClientConfiguration configuration("cn-shanghai");
|
||||
|
||||
CommonClient client(key, secret, configuration);
|
||||
// 创建API请求并设置参数
|
||||
// create request and assign parameters
|
||||
CommonRequest request(CommonRequest::RoaPattern);
|
||||
request.setScheme("http");
|
||||
request.setDomain("nlp.cn-shanghai.aliyuncs.com");
|
||||
|
||||
Reference in New Issue
Block a user