add POST support and fix user specified scheme via commonrequest
This commit is contained in:
@@ -101,7 +101,8 @@ https://cn-hangzhou/?
|
||||
|
||||
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");
|
||||
@@ -121,8 +122,22 @@ https://cn-hangzhou/?
|
||||
// acs accessKeyId:JZD81jGWLp1F3ZIkaLp1yuEZmKc=
|
||||
EXPECT_TRUE(rr.header("Authorization").find("acs accessKeyId:") != string::npos);
|
||||
EXPECT_TRUE(rr.header("unknown-header") == "");
|
||||
|
||||
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");
|
||||
|
||||
HttpRequest rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
|
||||
EXPECT_TRUE(client->serviceName() == "Common");
|
||||
EXPECT_TRUE(rrr.url().scheme() == "http");
|
||||
|
||||
cr.setScheme("");
|
||||
rrr = client->buildRpcHttpRequest("cn-hangzhou", cr, HttpRequest::Method::Post);
|
||||
EXPECT_TRUE(rrr.url().scheme() == "https");
|
||||
|
||||
std::function<void()> func(task);
|
||||
Runnable* rf = new Runnable(func);
|
||||
|
||||
@@ -69,6 +69,7 @@ TEST(CurlHttpClient, basic) {
|
||||
EXPECT_TRUE(out1.result().body() == testBody);
|
||||
|
||||
request.setMethod(HttpRequest::Method::Post);
|
||||
request.setBody("test-body", 9);
|
||||
request.setUrl(url);
|
||||
|
||||
HttpClient::HttpResponseOutcome out2 = client.makeRequest(request);
|
||||
|
||||
@@ -68,6 +68,7 @@ TEST(RoaServiceClient, basic) {
|
||||
RoaServiceRequest req(product, version);
|
||||
|
||||
req.setParameter("a", "b");
|
||||
req.setScheme("");
|
||||
req.setContent("123456789", 9);
|
||||
HttpRequest http_req = client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get);
|
||||
|
||||
@@ -88,4 +89,11 @@ TEST(RoaServiceClient, basic) {
|
||||
const string nounce = "8a013b14-7bac-4652-8b5f-c02c8924e4ae";
|
||||
EXPECT_TRUE(http_req.header("x-acs-signature-nonce").length() == nounce.length());
|
||||
EXPECT_TRUE(http_req.header("Authorization").find("acs accessKeyId:") != string::npos);
|
||||
|
||||
req.setScheme("http");
|
||||
req.setParameter("Accept", "tets-accept");
|
||||
req.setParameter("Content-Type", "test-content-type");
|
||||
http_req = client.buildHttpRequest("cn-shanghai", req, HttpRequest::Method::Get);
|
||||
EXPECT_TRUE(http_req.url().toString() == "http://cn-shanghai/?Accept=tets-accept&Content-Type=test-content-type&a=b");
|
||||
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ TEST(RpcServiceClient, basic) {
|
||||
|
||||
RpcServiceRequest req(product, version, action);
|
||||
|
||||
req.setScheme("");
|
||||
HttpRequest http_req = client.buildHttpRequest(endpoint, req, HttpRequest::Method::Get);
|
||||
EXPECT_TRUE(http_req.header("x-sdk-client") == std::string("CPP/").append(ALIBABACLOUD_VERSION_STR));
|
||||
EXPECT_TRUE(http_req.header("Host") == endpoint);
|
||||
@@ -79,6 +80,11 @@ TEST(RpcServiceClient, basic) {
|
||||
EXPECT_TRUE(http_req.url().query().find("&SignatureVersion=1.0") != string::npos);
|
||||
EXPECT_TRUE(http_req.url().query().find("&Version=1.0") != string::npos);
|
||||
EXPECT_TRUE(http_req.url().query().find("&Timestamp=") != string::npos);
|
||||
EXPECT_TRUE(http_req.url().scheme() == "https");
|
||||
|
||||
req.setScheme("http");
|
||||
http_req = client.buildHttpRequest(endpoint, req, HttpRequest::Method::Get);
|
||||
EXPECT_TRUE(http_req.url().scheme() == "http");
|
||||
}
|
||||
|
||||
TEST(RpcServiceClient, mock) {
|
||||
|
||||
25
test/function_test/nlp/CMakeLists.txt
Normal file
25
test/function_test/nlp/CMakeLists.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
project(demo)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
if(CMAKE_HOST_WIN32)
|
||||
include_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\include")
|
||||
link_directories("C:\\Program Files (x86)\\alibabacloud-sdk\\lib")
|
||||
endif()
|
||||
|
||||
# note ft_build is the dir you build sdk
|
||||
|
||||
include_directories("../../../core/include/")
|
||||
|
||||
link_directories(${CMAKE_SOURCE_DIR}/ft_build/lib)
|
||||
|
||||
add_executable(nlp_ft nlp_wordsegment_ft.cc)
|
||||
target_link_libraries(nlp_ft alibabacloud-sdk-core)
|
||||
|
||||
target_link_libraries(nlp_ft core gtest gmock_main)
|
||||
|
||||
set_target_properties(nlp_ft
|
||||
PROPERTIES
|
||||
OUTPUT_NAME ${TARGET_OUTPUT_NAME_PREFIX}nlp_ft
|
||||
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
add_test(NAME nlp_ft COMMAND nlp_ft)
|
||||
39
test/function_test/nlp/nlp_wordsegment_ft.cc
Normal file
39
test/function_test/nlp/nlp_wordsegment_ft.cc
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include "gtest/gtest.h"
|
||||
#include "../utils.h"
|
||||
#include "alibabacloud/core/AlibabaCloud.h"
|
||||
#include "alibabacloud/core/CommonClient.h"
|
||||
#include "alibabacloud/core/HttpRequest.h"
|
||||
using namespace std;
|
||||
using namespace AlibabaCloud;
|
||||
|
||||
namespace {
|
||||
TEST(nlp, wordsegment) {
|
||||
utUtils utils;
|
||||
string key = utils.get_env("ENV_AccessKeyId");
|
||||
string secret = utils.get_env("ENV_AccessKeySecret");
|
||||
|
||||
AlibabaCloud::InitializeSdk();
|
||||
ClientConfiguration configuration("cn-shanghai");
|
||||
|
||||
CommonClient client(key, secret, configuration);
|
||||
// 创建API请求并设置参数
|
||||
CommonRequest request(CommonRequest::RoaPattern);
|
||||
request.setScheme("http");
|
||||
request.setDomain("nlp.cn-shanghai.aliyuncs.com");
|
||||
request.setResourcePath("/nlp/api/wordsegment/general");
|
||||
request.setHttpMethod(HttpRequest::Post);
|
||||
const char * data = "{\"lang\":\"ZH\",\"text\":\"Iphone is a bad choice 专用数据线\"}";
|
||||
request.setContent(data, strlen(data));
|
||||
request.setHeaderParameter("Content-Type", "application/json;chrset=utf-8");
|
||||
request.setVersion("2018-04-08");
|
||||
|
||||
auto outcome = client.commonResponse(request);
|
||||
EXPECT_TRUE(outcome.error().errorCode().empty());
|
||||
const std::string expected = "{\"data\":[{\"id\":0,\"word\":\"Iphone\"},{\"id\":1,\"word\":\" \"},{\"id\":2,\"word\":\"is\"},{\"id\":3,\"word\":\" \"},{\"id\":4,\"word\":\"a\"},{\"id\":5,\"word\":\" \"},{\"id\":6,\"word\":\"bad\"},{\"id\":7,\"word\":\" \"},{\"id\":8,\"word\":\"choice\"},{\"id\":9,\"word\":\" \"},{\"id\":10,\"word\":\"专用\"},{\"id\":11,\"word\":\"数据线\"}]}";
|
||||
EXPECT_TRUE(outcome.result().payload() == expected);
|
||||
AlibabaCloud::ShutdownSdk();
|
||||
}
|
||||
}
|
||||
@@ -130,9 +130,10 @@ void ServerThread::Run() {
|
||||
Terminate(true);
|
||||
} else if (!strcmp("POST", method)) {
|
||||
if (fopen(path, "r") > 0) {
|
||||
sprintf(buffer, "%s\r\n\r\n", "HTTP/1.1 403 Forbidden");
|
||||
int size = strlen(echo_buffer);
|
||||
sprintf(buffer, "%s%d\r\n\r\n", "HTTP/1.1 200 OK\r\nContent-Length: ", size);
|
||||
send(socket, buffer, strlen(buffer), 0);
|
||||
cout << "402 " << path << endl;
|
||||
send(socket, echo_buffer, size, 0);
|
||||
continue;
|
||||
}
|
||||
FILE *file = fopen(path, "w+");
|
||||
|
||||
Reference in New Issue
Block a user