mirror of
https://github.com/oatpp/oatpp.git
synced 2024-11-27 08:30:07 +08:00
better web/FullTest, web/FullAsyncTest
This commit is contained in:
parent
062411e072
commit
141a4fcd25
@ -65,16 +65,37 @@ bool FullAsyncTest::onRun() {
|
||||
|
||||
auto server = oatpp::network::server::Server::createShared(serverConnectionProvider, connectionHandler);
|
||||
|
||||
std::thread clientThread([client, server, connectionHandler]{
|
||||
std::thread clientThread([client, server, connectionHandler, objectMapper]{
|
||||
|
||||
for(v_int32 i = 0; i < 10; i ++) {
|
||||
|
||||
try {
|
||||
{ /* test simple GET */
|
||||
auto response = client->getRoot();
|
||||
auto text = response->readBodyToString();
|
||||
OATPP_LOGD("client", "body='%s'", text->c_str());
|
||||
} catch(std::runtime_error e) {
|
||||
OATPP_LOGD("client", "error='%s'", e.what());
|
||||
auto value = response->readBodyToString();
|
||||
OATPP_ASSERT(value == "Hello World Async!!!");
|
||||
}
|
||||
|
||||
{ /* test GET with path parameter */
|
||||
auto response = client->getWithParams("my_test_param-Async");
|
||||
auto dto = response->readBodyToDto<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_param-Async");
|
||||
}
|
||||
|
||||
{ /* test GET with header parameter */
|
||||
auto response = client->getWithHeaders("my_test_header-Async");
|
||||
//auto str = response->readBodyToString();
|
||||
//OATPP_LOGE("AAA", "code=%d, str='%s'", response->statusCode, str->c_str());
|
||||
auto dto = response->readBodyToDto<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_header-Async");
|
||||
}
|
||||
|
||||
{ /* test POST with body */
|
||||
auto response = client->postBody("my_test_body-Async");
|
||||
auto dto = response->readBodyToDto<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_body-Async");
|
||||
}
|
||||
|
||||
}
|
||||
@ -82,10 +103,9 @@ bool FullAsyncTest::onRun() {
|
||||
try {
|
||||
connectionHandler->stop();
|
||||
server->stop();
|
||||
client->getRoot();
|
||||
OATPP_LOGD("client", "stoped");
|
||||
client->getRoot(); // wake blocking server accept
|
||||
} catch(std::runtime_error e) {
|
||||
OATPP_LOGD("client", "stoped with e");
|
||||
// DO NOTHING
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -49,7 +49,7 @@ bool FullTest::onRun() {
|
||||
auto serverConnectionProvider = oatpp::network::virtual_::server::ConnectionProvider::createShared(interface);
|
||||
auto clientConnectionProvider = oatpp::network::virtual_::client::ConnectionProvider::createShared(interface);
|
||||
|
||||
serverConnectionProvider->setSocketMaxAvailableToReadWrtie(-1, -1);
|
||||
serverConnectionProvider->setSocketMaxAvailableToReadWrtie(1, 1);
|
||||
clientConnectionProvider->setSocketMaxAvailableToReadWrtie(1, 1);
|
||||
|
||||
auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
|
||||
@ -66,26 +66,44 @@ bool FullTest::onRun() {
|
||||
|
||||
auto server = oatpp::network::server::Server::createShared(serverConnectionProvider, connectionHandler);
|
||||
|
||||
std::thread clientThread([client, server]{
|
||||
std::thread clientThread([client, server, objectMapper]{
|
||||
|
||||
for(v_int32 i = 0; i < 10; i ++) {
|
||||
|
||||
try {
|
||||
{ /* test simple GET */
|
||||
auto response = client->getRoot();
|
||||
auto text = response->readBodyToString();
|
||||
OATPP_LOGD("client", "body='%s'", text->c_str());
|
||||
} catch(std::runtime_error e) {
|
||||
OATPP_LOGD("client", "error='%s'", e.what());
|
||||
auto value = response->readBodyToString();
|
||||
OATPP_ASSERT(value == "Hello World!!!");
|
||||
}
|
||||
|
||||
{ /* test GET with path parameter */
|
||||
auto response = client->getWithParams("my_test_param");
|
||||
auto dto = response->readBodyToDto<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_param");
|
||||
}
|
||||
|
||||
{ /* test GET with header parameter */
|
||||
auto response = client->getWithHeaders("my_test_header");
|
||||
auto dto = response->readBodyToDto<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_header");
|
||||
}
|
||||
|
||||
{ /* test POST with body */
|
||||
auto response = client->postBody("my_test_body");
|
||||
auto dto = response->readBodyToDto<app::TestDto>(objectMapper);
|
||||
OATPP_ASSERT(dto);
|
||||
OATPP_ASSERT(dto->testValue == "my_test_body");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
server->stop();
|
||||
client->getRoot();
|
||||
OATPP_LOGD("client", "stoped");
|
||||
client->getRoot(); // wake blocking server accept
|
||||
} catch(std::runtime_error e) {
|
||||
OATPP_LOGD("client", "stoped with e");
|
||||
// DO NOTHING
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -36,6 +36,9 @@ class Client : public oatpp::web::client::ApiClient {
|
||||
API_CLIENT_INIT(Client)
|
||||
|
||||
API_CALL("GET", "/", getRoot)
|
||||
API_CALL("GET", "params/{param}", getWithParams, PATH(String, param))
|
||||
API_CALL("GET", "headers", getWithHeaders, HEADER(String, param, "X-TEST-HEADER"))
|
||||
API_CALL("POST", "body", postBody, BODY_STRING(String, body))
|
||||
|
||||
#include OATPP_CODEGEN_END(ApiClient)
|
||||
};
|
||||
|
@ -25,6 +25,7 @@
|
||||
#ifndef oatpp_test_web_app_Controller_hpp
|
||||
#define oatpp_test_web_app_Controller_hpp
|
||||
|
||||
#include "./DTOs.hpp"
|
||||
#include "oatpp/web/server/api/ApiController.hpp"
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
@ -33,6 +34,8 @@
|
||||
namespace oatpp { namespace test { namespace web { namespace app {
|
||||
|
||||
class Controller : public oatpp::web::server::api::ApiController {
|
||||
private:
|
||||
static constexpr const char* TAG = "test::web::app::Controller";
|
||||
public:
|
||||
Controller(const std::shared_ptr<ObjectMapper>& objectMapper)
|
||||
: oatpp::web::server::api::ApiController(objectMapper)
|
||||
@ -46,9 +49,34 @@ public:
|
||||
#include OATPP_CODEGEN_BEGIN(ApiController)
|
||||
|
||||
ENDPOINT("GET", "/", root) {
|
||||
OATPP_LOGD(TAG, "GET '/'");
|
||||
return createResponse(Status::CODE_200, "Hello World!!!");
|
||||
}
|
||||
|
||||
ENDPOINT("GET", "params/{param}", getWithParams,
|
||||
PATH(String, param)) {
|
||||
OATPP_LOGD(TAG, "GET params/%s", param->c_str());
|
||||
auto dto = TestDto::createShared();
|
||||
dto->testValue = param;
|
||||
return createDtoResponse(Status::CODE_200, dto);
|
||||
}
|
||||
|
||||
ENDPOINT("GET", "headers", getWithHeaders,
|
||||
HEADER(String, param, "X-TEST-HEADER")) {
|
||||
OATPP_LOGD(TAG, "GET headers {X-TEST-HEADER: %s}", param->c_str());
|
||||
auto dto = TestDto::createShared();
|
||||
dto->testValue = param;
|
||||
return createDtoResponse(Status::CODE_200, dto);
|
||||
}
|
||||
|
||||
ENDPOINT("POST", "body", postBody,
|
||||
BODY_STRING(String, body)) {
|
||||
OATPP_LOGD(TAG, "POST body %s", body->c_str());
|
||||
auto dto = TestDto::createShared();
|
||||
dto->testValue = body;
|
||||
return createDtoResponse(Status::CODE_200, dto);
|
||||
}
|
||||
|
||||
#include OATPP_CODEGEN_END(ApiController)
|
||||
|
||||
};
|
||||
|
@ -25,6 +25,8 @@
|
||||
#ifndef oatpp_test_web_app_ControllerAsync_hpp
|
||||
#define oatpp_test_web_app_ControllerAsync_hpp
|
||||
|
||||
#include "./DTOs.hpp"
|
||||
|
||||
#include "oatpp/web/server/api/ApiController.hpp"
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
@ -33,6 +35,8 @@
|
||||
namespace oatpp { namespace test { namespace web { namespace app {
|
||||
|
||||
class ControllerAsync : public oatpp::web::server::api::ApiController {
|
||||
private:
|
||||
static constexpr const char* TAG = "test::web::app::ControllerAsync";
|
||||
public:
|
||||
ControllerAsync(const std::shared_ptr<ObjectMapper>& objectMapper)
|
||||
: oatpp::web::server::api::ApiController(objectMapper)
|
||||
@ -50,7 +54,54 @@ public:
|
||||
ENDPOINT_ASYNC_INIT(Root)
|
||||
|
||||
Action act() {
|
||||
return _return(controller->createResponse(Status::CODE_200, "Hello World Async!!! " + request->getHeader("host")));
|
||||
OATPP_LOGD(TAG, "GET '/'");
|
||||
return _return(controller->createResponse(Status::CODE_200, "Hello World Async!!!"));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
ENDPOINT_ASYNC("GET", "params/{param}", GetWithParams) {
|
||||
|
||||
ENDPOINT_ASYNC_INIT(GetWithParams)
|
||||
|
||||
Action act() {
|
||||
auto param = request->getPathVariable("param");
|
||||
OATPP_LOGD(TAG, "GET params/%s", param->c_str());
|
||||
auto dto = TestDto::createShared();
|
||||
dto->testValue = param;
|
||||
return _return(controller->createDtoResponse(Status::CODE_200, dto));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
ENDPOINT_ASYNC("GET", "headers", GetWithHeaders) {
|
||||
|
||||
ENDPOINT_ASYNC_INIT(GetWithHeaders)
|
||||
|
||||
Action act() {
|
||||
auto param = request->getHeader("X-TEST-HEADER");
|
||||
OATPP_LOGD(TAG, "GET headers {X-TEST-HEADER: %s}", param->c_str());
|
||||
auto dto = TestDto::createShared();
|
||||
dto->testValue = param;
|
||||
return _return(controller->createDtoResponse(Status::CODE_200, dto));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
ENDPOINT_ASYNC("POST", "body", PostBody) {
|
||||
|
||||
ENDPOINT_ASYNC_INIT(PostBody)
|
||||
|
||||
Action act() {
|
||||
OATPP_LOGD(TAG, "POST body. Reading body...");
|
||||
return request->readBodyToStringAsync(this, &PostBody::onBodyRead);
|
||||
}
|
||||
|
||||
Action onBodyRead(const String& body) {
|
||||
OATPP_LOGD(TAG, "POST body %s", body->c_str());
|
||||
auto dto = TestDto::createShared();
|
||||
dto->testValue = body;
|
||||
return _return(controller->createDtoResponse(Status::CODE_200, dto));
|
||||
}
|
||||
|
||||
};
|
||||
|
47
test/web/app/DTOs.hpp
Normal file
47
test/web/app/DTOs.hpp
Normal file
@ -0,0 +1,47 @@
|
||||
/***************************************************************************
|
||||
*
|
||||
* Project _____ __ ____ _ _
|
||||
* ( _ ) /__\ (_ _)_| |_ _| |_
|
||||
* )(_)( /(__)\ )( (_ _)(_ _)
|
||||
* (_____)(__)(__)(__) |_| |_|
|
||||
*
|
||||
*
|
||||
* Copyright 2018-present, Leonid Stryzhevskyi, <lganzzzo@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef oatpp_test_web_app_DTOs_hpp
|
||||
#define oatpp_test_web_app_DTOs_hpp
|
||||
|
||||
#include "oatpp/core/data/mapping/type/Object.hpp"
|
||||
#include "oatpp/core/macro/codegen.hpp"
|
||||
|
||||
namespace oatpp { namespace test { namespace web { namespace app {
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(DTO)
|
||||
|
||||
class TestDto : public oatpp::data::mapping::type::Object {
|
||||
|
||||
DTO_INIT(TestDto, Object)
|
||||
|
||||
DTO_FIELD(String, testValue);
|
||||
|
||||
};
|
||||
|
||||
#include OATPP_CODEGEN_END(DTO)
|
||||
|
||||
}}}}
|
||||
|
||||
#endif /* oatpp_test_web_app_DTOs_hpp */
|
Loading…
Reference in New Issue
Block a user