Refactored, added Tests

This commit is contained in:
Benedikt-Alexander Mokroß 2019-09-27 17:48:39 +02:00
parent 95c0b8c462
commit 9c44be5cd1
8 changed files with 163 additions and 53 deletions

View File

@ -530,7 +530,7 @@ public: \
// CORS MACRO // ------------------------------------------------------
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_0(ENDPOINTNAME, ...) \
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_1(ENDPOINTNAME, ...) \
ResponseInterceptor Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME = [this](std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> &resp) { \
resp->putHeaderIfNotExists("Access-Control-Allow-Origin", "*"); \
resp->putHeaderIfNotExists("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); \
@ -550,11 +550,11 @@ ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_E
return Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME(resp); \
}
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_1(ENDPOINTNAME, ORIGIN) \
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> Z__ENDPOINT_CORS_##ENDPOINTNAME(std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> &resp) { \
response->putHeaderIfNotExists("Access-Control-Allow-Origin", ORIGIN); \
response->putHeaderIfNotExists("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); \
response->putHeaderIfNotExists("Access-Control-Allow-Headers", "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range");\
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_2(ENDPOINTNAME, ORIGIN) \
ResponseInterceptor Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME = [this](std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> &resp) { \
resp->putHeaderIfNotExists("Access-Control-Allow-Origin", ORIGIN); \
resp->putHeaderIfNotExists("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); \
resp->putHeaderIfNotExists("Access-Control-Allow-Headers", "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range");\
resp->putHeaderIfNotExists("Access-Control-Max-Age", "1728000"); \
resp->putHeaderIfNotExists("Content-Type", "text/plain; charset=utf-8"); \
resp->putHeaderIfNotExists("Content-Length", "0"); \
@ -570,11 +570,11 @@ ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_E
return Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME(resp); \
}
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_2(ENDPOINTNAME, ORIGIN, METHODS) \
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> Z__ENDPOINT_CORS_##ENDPOINTNAME(std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> &resp) { \
response->putHeaderIfNotExists("Access-Control-Allow-Origin", ORIGIN); \
response->putHeaderIfNotExists("Access-Control-Allow-Methods", METHODS); \
response->putHeaderIfNotExists("Access-Control-Allow-Headers", "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range");\
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_3(ENDPOINTNAME, ORIGIN, METHODS) \
ResponseInterceptor Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME = [this](std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> &resp) { \
resp->putHeaderIfNotExists("Access-Control-Allow-Origin", ORIGIN); \
resp->putHeaderIfNotExists("Access-Control-Allow-Methods", METHODS); \
resp->putHeaderIfNotExists("Access-Control-Allow-Headers", "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range");\
resp->putHeaderIfNotExists("Access-Control-Max-Age", "1728000"); \
resp->putHeaderIfNotExists("Content-Type", "text/plain; charset=utf-8"); \
resp->putHeaderIfNotExists("Content-Length", "0"); \
@ -590,16 +590,16 @@ ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_E
return Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME(resp); \
}
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_3(ENDPOINTNAME, ORIGIN, METHODS, HEADERS) \
std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> Z__ENDPOINT_CORS_##ENDPOINTNAME(std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> &resp) { \
response->putHeaderIfNotExists("Access-Control-Allow-Origin", ORIGIN); \
response->putHeaderIfNotExists("Access-Control-Allow-Methods", METHODS); \
response->putHeaderIfNotExists("Access-Control-Allow-Headers", HEADERS);\
#define OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_4(ENDPOINTNAME, ORIGIN, METHODS, HEADERS) \
ResponseInterceptor Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME = [this](std::shared_ptr<oatpp::web::protocol::http::outgoing::Response> &resp) { \
resp->putHeaderIfNotExists("Access-Control-Allow-Origin", ORIGIN); \
resp->putHeaderIfNotExists("Access-Control-Allow-Methods", METHODS); \
resp->putHeaderIfNotExists("Access-Control-Allow-Headers", HEADERS);\
resp->putHeaderIfNotExists("Access-Control-Max-Age", "1728000"); \
resp->putHeaderIfNotExists("Content-Type", "text/plain; charset=utf-8"); \
resp->putHeaderIfNotExists("Content-Length", "0"); \
return resp; \
}; \
}; \
const std::shared_ptr<ResponseInterceptor> Z__RESPONSE_INTERCEPTOR_##ENDPOINTNAME = addInterceptorForEndpoint( \
m_interceptors, \
std::make_shared<ResponseInterceptor>(Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME), \
@ -610,5 +610,5 @@ ENDPOINT("OPTIONS", Z__ENDPOINT_##ENDPOINTNAME->info()->path, ZZ__CORS_OPTIONS_E
return Z__RESPONSE_INTERCEPTOR_FUNC_##ENDPOINTNAME(resp); \
}
#define ADDCORS(ENDPOINTNAME, ...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_BINARY_SELECTOR(OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_, (__VA_ARGS__)) (ENDPOINTNAME, __VA_ARGS__))
#define ADDCORS(...) \
OATPP_MACRO_EXPAND(OATPP_MACRO_MACRO_SELECTOR(OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_, (__VA_ARGS__)) (__VA_ARGS__))

View File

@ -173,8 +173,8 @@
// CORS MACRO // ------------------------------------------------------
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_0
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_1
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_2
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_3
#undef OATPP_MACRO_API_CONTROLLER_ADDCORS_MACRO_4
#undef ADDCORS

View File

@ -121,7 +121,12 @@ const char* const Header::HOST = "Host";
const char* const Header::USER_AGENT = "User-Agent";
const char* const Header::SERVER = "Server";
const char* const Header::UPGRADE = "Upgrade";
const char* const Header::CORS_ORIGIN = "Access-Control-Allow-Origin";
const char* const Header::CORS_METHODS = "Access-Control-Allow-Methods";
const char* const Header::CORS_HEADERS = "Access-Control-Allow-Headers";
const char* const Range::UNIT_BYTES = "bytes";
const char* const ContentRange::UNIT_BYTES = "bytes";

View File

@ -497,6 +497,9 @@ public:
static const char* const USER_AGENT; // "User-Agent"
static const char* const SERVER; // "Server"
static const char* const UPGRADE; // "Upgrade"
static const char* const CORS_ORIGIN; // Access-Control-Allow-Origin
static const char* const CORS_METHODS; // Access-Control-Allow-Methods
static const char* const CORS_HEADERS; // Access-Control-Allow-Headers
};
class Range {

View File

@ -56,37 +56,37 @@ void runTests() {
OATPP_LOGD("aaa", "coroutine size=%d", sizeof(oatpp::async::AbstractCoroutine));
OATPP_LOGD("aaa", "action size=%d", sizeof(oatpp::async::Action));
// OATPP_RUN_TEST(oatpp::test::base::RegRuleTest);
// OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest);
//
// OATPP_RUN_TEST(oatpp::test::memory::MemoryPoolTest);
// OATPP_RUN_TEST(oatpp::test::memory::PerfTest);
//
// OATPP_RUN_TEST(oatpp::test::collection::LinkedListTest);
//
// OATPP_RUN_TEST(oatpp::test::core::data::share::MemoryLabelTest);
// OATPP_RUN_TEST(oatpp::test::core::data::stream::ChunkedBufferTest);
// OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::TypeTest);
//
// OATPP_RUN_TEST(oatpp::test::async::LockTest);
//
// OATPP_RUN_TEST(oatpp::test::parser::CaretTest);
// OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DeserializerTest);
// OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DTOMapperPerfTest);
// OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DTOMapperTest);
//
// OATPP_RUN_TEST(oatpp::test::encoding::Base64Test);
// OATPP_RUN_TEST(oatpp::test::encoding::UnicodeTest);
//
// OATPP_RUN_TEST(oatpp::test::network::UrlTest);
// OATPP_RUN_TEST(oatpp::test::network::virtual_::PipeTest);
// OATPP_RUN_TEST(oatpp::test::network::virtual_::InterfaceTest);
//
// OATPP_RUN_TEST(oatpp::test::web::mime::multipart::StatefulParserTest);
//
// OATPP_RUN_TEST(oatpp::test::web::server::api::ApiControllerTest);
//
// OATPP_RUN_TEST(oatpp::test::web::server::handler::AuthorizationHandlerTest);
OATPP_RUN_TEST(oatpp::test::base::RegRuleTest);
OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest);
OATPP_RUN_TEST(oatpp::test::memory::MemoryPoolTest);
OATPP_RUN_TEST(oatpp::test::memory::PerfTest);
OATPP_RUN_TEST(oatpp::test::collection::LinkedListTest);
OATPP_RUN_TEST(oatpp::test::core::data::share::MemoryLabelTest);
OATPP_RUN_TEST(oatpp::test::core::data::stream::ChunkedBufferTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::TypeTest);
OATPP_RUN_TEST(oatpp::test::async::LockTest);
OATPP_RUN_TEST(oatpp::test::parser::CaretTest);
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DeserializerTest);
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DTOMapperPerfTest);
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DTOMapperTest);
OATPP_RUN_TEST(oatpp::test::encoding::Base64Test);
OATPP_RUN_TEST(oatpp::test::encoding::UnicodeTest);
OATPP_RUN_TEST(oatpp::test::network::UrlTest);
OATPP_RUN_TEST(oatpp::test::network::virtual_::PipeTest);
OATPP_RUN_TEST(oatpp::test::network::virtual_::InterfaceTest);
OATPP_RUN_TEST(oatpp::test::web::mime::multipart::StatefulParserTest);
OATPP_RUN_TEST(oatpp::test::web::server::api::ApiControllerTest);
OATPP_RUN_TEST(oatpp::test::web::server::handler::AuthorizationHandlerTest);
{

View File

@ -168,6 +168,84 @@ void FullTest::onRun() {
OATPP_ASSERT(value == "Hello World!!!");
}
{ // test simple GET with CORS
auto response = client->getCors(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Ping");
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "*");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, POST, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range");
}
{ // test simple OPTIONS with CORS
auto response = client->optionsCors(connection);
OATPP_ASSERT(response->getStatusCode() == 204);
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "*");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, POST, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range");
}
{ // test simple GET with CORS
auto response = client->getCorsOrigin(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Pong");
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "127.0.0.1");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, POST, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range");
}
{ // test simple GET with CORS
auto response = client->getCorsOriginMethods(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Ping");
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "127.0.0.1");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range");
}
{ // test simple GET with CORS
auto response = client->getCorsOriginMethodsHeader(connection);
OATPP_ASSERT(response->getStatusCode() == 200);
auto value = response->readBodyToString();
OATPP_ASSERT(value == "Pong");
auto header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_ORIGIN);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "127.0.0.1");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_METHODS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "GET, OPTIONS");
header = response->getHeaders().find(oatpp::web::protocol::http::Header::CORS_HEADERS);
OATPP_ASSERT(header != response->getHeaders().end());
OATPP_ASSERT(header->second.toString() == "X-PWNT");
}
{ // test GET with path parameter
auto response = client->getWithParams("my_test_param", connection);
OATPP_ASSERT(response->getStatusCode() == 200);

View File

@ -42,6 +42,11 @@ public:
API_CLIENT_INIT(Client)
API_CALL("GET", "/", getRoot)
API_CALL("GET", "/cors", getCors)
API_CALL("OPTIONS", "/cors", optionsCors)
API_CALL("GET", "/cors-origin", getCorsOrigin)
API_CALL("GET", "/cors-origin-methods", getCorsOriginMethods)
API_CALL("GET", "/cors-origin-methods-headers", getCorsOriginMethodsHeader)
API_CALL("GET", "params/{param}", getWithParams, PATH(String, param))
API_CALL("GET", "queries", getWithQueries, QUERY(String, name), QUERY(Int32, age))
API_CALL("GET", "queries/map", getWithQueriesMap, QUERY(String, key1), QUERY(Int32, key2), QUERY(Float32, key3))

View File

@ -61,12 +61,31 @@ public:
#include OATPP_CODEGEN_BEGIN(ApiController)
ADDCORS(root);
ENDPOINT("GET", "/", root) {
OATPP_LOGV(TAG, "GET '/'");
return createResponse(Status::CODE_200, "Hello World!!!");
}
ADDCORS(cors);
ENDPOINT("GET", "/cors", cors) {
return createResponse(Status::CODE_200, "Ping");
}
ADDCORS(corsOrigin, "127.0.0.1");
ENDPOINT("GET", "/cors-origin", corsOrigin) {
return createResponse(Status::CODE_200, "Pong");
}
ADDCORS(corsOriginMethods, "127.0.0.1", "GET, OPTIONS");
ENDPOINT("GET", "/cors-origin-methods", corsOriginMethods) {
return createResponse(Status::CODE_200, "Ping");
}
ADDCORS(corsOriginMethodsHeaders, "127.0.0.1", "GET, OPTIONS", "X-PWNT");
ENDPOINT("GET", "/cors-origin-methods-headers", corsOriginMethodsHeaders) {
return createResponse(Status::CODE_200, "Pong");
}
ENDPOINT("GET", "params/{param}", getWithParams,
PATH(String, param)) {
//OATPP_LOGV(TAG, "GET params/%s", param->c_str());