Завершение контроллера

This commit is contained in:
Антон
2025-08-24 08:25:41 +03:00
parent a5ad3c16b4
commit 738700e8d8
3 changed files with 24 additions and 15 deletions
+9 -5
View File
@@ -9,19 +9,23 @@ namespace uad
template <class Body, class Allocator, class ResponseType> template <class Body, class Allocator, class ResponseType>
class Controller : IController<Body, Allocator, ResponseType> class Controller : IController<Body, Allocator, ResponseType>
{ {
using HTTPMethodsToExecutors = std::unordered_map<boost::beast::http::verb, std::unique_ptr<IExecutor<Body, Allocator, ResponseType>>>; public:
using HTTPMethodsToExecutors = std::unordered_map<boost::beast::http::verb, std::shared_ptr<IExecutor<Body, Allocator, ResponseType>>>;
private:
HTTPMethodsToExecutors executors_; HTTPMethodsToExecutors executors_;
public: public:
Controller() = default; Controller() = default;
explicit Controller(HTTPMethodsToExecutors&& executors) {} explicit Controller(HTTPMethodsToExecutors&& executors): executors_(std::move(executors)) {}
std::optional<std::unique_ptr<IExecutor<Body, Allocator, ResponseType>>> FindExecutor( std::optional<std::shared_ptr<IExecutor<Body, Allocator, ResponseType>>> FindExecutor(
boost::beast::http::verb method boost::beast::http::verb method
) const override ) override
{ {
return std::nullopt; if (!executors_.count(method)) return std::nullopt;
return std::make_optional(executors_.at(method));
} }
}; };
} }
+2 -2
View File
@@ -18,9 +18,9 @@ template <class Body, class Allocator, class ResponseType>
class IController class IController
{ {
public: public:
virtual std::optional<std::unique_ptr<IExecutor<Body, Allocator, ResponseType>>> FindExecutor( virtual std::optional<std::shared_ptr<IExecutor<Body, Allocator, ResponseType>>> FindExecutor(
boost::beast::http::verb method boost::beast::http::verb method
) const = 0; ) = 0;
virtual ~IController() = default; virtual ~IController() = default;
}; };
} }
+13 -8
View File
@@ -15,6 +15,10 @@ using namespace boost;
using namespace std; using namespace std;
using namespace uad; using namespace uad;
using IControllerMock = Controller<beast::http::string_body,
std::allocator<char>,
beast::http::string_body>;
class MockExecutor : public IExecutor<beast::http::string_body, class MockExecutor : public IExecutor<beast::http::string_body,
std::allocator<char>, std::allocator<char>,
beast::http::string_body> beast::http::string_body>
@@ -33,9 +37,7 @@ public:
BOOST_AUTO_TEST_CASE(Should_Be_Initiated_With_No_Executors) BOOST_AUTO_TEST_CASE(Should_Be_Initiated_With_No_Executors)
{ {
Controller<beast::http::string_body, IControllerMock my_controller;
std::allocator<char>,
beast::http::string_body> my_controller;
BOOST_CHECK(!my_controller.FindExecutor(beast::http::verb::get).has_value()); BOOST_CHECK(!my_controller.FindExecutor(beast::http::verb::get).has_value());
BOOST_CHECK(!my_controller.FindExecutor(beast::http::verb::post).has_value()); BOOST_CHECK(!my_controller.FindExecutor(beast::http::verb::post).has_value());
@@ -47,13 +49,16 @@ BOOST_AUTO_TEST_CASE(Should_Be_Initiated_With_No_Executors)
BOOST_AUTO_TEST_CASE(Should_Be_Initiated_With_Unordered_Map_HTTP_Methods_To_Executors) BOOST_AUTO_TEST_CASE(Should_Be_Initiated_With_Unordered_Map_HTTP_Methods_To_Executors)
{ {
std::unordered_map<boost::beast::http::verb, std::unique_ptr<IExecutor<beast::http::string_body, std::allocator<char>, beast::http::string_body>>> executors; IControllerMock::HTTPMethodsToExecutors executors;
executors[beast::http::verb::get] = make_unique<MockExecutor>(); executors[beast::http::verb::get] = make_shared<MockExecutor>();
executors[beast::http::verb::post] = make_shared<MockExecutor>();
executors[beast::http::verb::put] = make_shared<MockExecutor>();
executors[beast::http::verb::delete_] = make_shared<MockExecutor>();
executors[beast::http::verb::head] = make_shared<MockExecutor>();
executors[beast::http::verb::options] = make_shared<MockExecutor>();
Controller<beast::http::string_body, IControllerMock my_controller(std::move(executors));
std::allocator<char>,
beast::http::string_body> my_controller(std::move(executors));
BOOST_CHECK(my_controller.FindExecutor(beast::http::verb::get).has_value()); BOOST_CHECK(my_controller.FindExecutor(beast::http::verb::get).has_value());
BOOST_CHECK(my_controller.FindExecutor(beast::http::verb::post).has_value()); BOOST_CHECK(my_controller.FindExecutor(beast::http::verb::post).has_value());