From ab1063c046b363a37ccaf91c7dfb1fecd279be36 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Mon, 8 Sep 2014 07:07:53 +0900 Subject: complete middleware implementation --- tests/unittest.cpp | 146 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 137 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/tests/unittest.cpp b/tests/unittest.cpp index c84e3ed..a69e640 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -479,6 +479,14 @@ int testmain() return failed ? -1 : 0; } +TEST(black_magic) +{ + using namespace black_magic; + static_assert(std::is_same::type>::value, "last_element_type"); + static_assert(std::is_same::rebind::type>::value, "pop_back"); + static_assert(std::is_same::rebind::rebind::type>::value, "pop_back"); +} + struct NullMiddleware { struct context {}; @@ -492,12 +500,25 @@ struct NullMiddleware {} }; +struct NullSimpleMiddleware +{ + struct context {}; + + void before_handle(request& req, response& res, context& ctx) + {} + + void after_handle(request& req, response& res, context& ctx) + {} +}; + TEST(middleware_simple) { - App app; + App app; + decltype(app)::server_t server(&app, 45451); CROW_ROUTE(app, "/")([&](const crow::request& req) { - app.get_middleware_context(req); + app.get_context(req); + app.get_context(req); return ""; }); } @@ -519,21 +540,98 @@ struct IntSettingMiddleware } }; +std::vector test_middleware_context_vector; + +struct FirstMW +{ + struct context + { + std::vector v; + }; + + void before_handle(request& req, response& res, context& ctx) + { + ctx.v.push_back("1 before"); + } + + void after_handle(request& req, response& res, context& ctx) + { + ctx.v.push_back("1 after"); + test_middleware_context_vector = ctx.v; + } +}; + +struct SecondMW +{ + struct context {}; + template + void before_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + { + all_ctx.template get().v.push_back("2 before"); + if (req.url == "/break") + res.end(); + } + + template + void after_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + { + all_ctx.template get().v.push_back("2 after"); + } +}; + +struct ThirdMW +{ + struct context {}; + template + void before_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + { + all_ctx.template get().v.push_back("3 before"); + } + + template + void after_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + { + all_ctx.template get().v.push_back("3 after"); + } +}; + TEST(middleware_context) { + static char buf[2048]; - App app; - Server server(&app, 45451); - auto _ = async(launch::async, [&]{server.run();}); - std::string sendmsg = "GET /\r\n\r\n"; + // SecondMW depends on FirstMW (it uses all_ctx.get) + // so it leads to compile error if we remove FirstMW from definition + // App app; + // or change the order of FirstMW and SecondMW + // App app; + + App app; int x{}; CROW_ROUTE(app, "/")([&](const request& req){ - auto& ctx = app.get_middleware_context(req); - x = ctx.val; + { + auto& ctx = app.get_context(req); + x = ctx.val; + } + { + auto& ctx = app.get_context(req); + ctx.v.push_back("handle"); + } return ""; }); + CROW_ROUTE(app, "/break")([&](const request& req){ + { + auto& ctx = app.get_context(req); + ctx.v.push_back("handle"); + } + + return ""; + }); + + decltype(app)::server_t server(&app, 45451); + auto _ = async(launch::async, [&]{server.run();}); + std::string sendmsg = "GET /\r\n\r\n"; asio::io_service is; { asio::ip::tcp::socket c(is); @@ -543,8 +641,38 @@ TEST(middleware_context) c.send(asio::buffer(sendmsg)); c.receive(asio::buffer(buf, 2048)); + c.close(); + } + { + auto& out = test_middleware_context_vector; + ASSERT_EQUAL(1, x); + ASSERT_EQUAL(7, out.size()); + ASSERT_EQUAL("1 before", out[0]); + ASSERT_EQUAL("2 before", out[1]); + ASSERT_EQUAL("3 before", out[2]); + ASSERT_EQUAL("handle", out[3]); + ASSERT_EQUAL("3 after", out[4]); + ASSERT_EQUAL("2 after", out[5]); + ASSERT_EQUAL("1 after", out[6]); + } + std::string sendmsg2 = "GET /break\r\n\r\n"; + { + asio::ip::tcp::socket c(is); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451)); + + c.send(asio::buffer(sendmsg2)); + + c.receive(asio::buffer(buf, 2048)); + c.close(); + } + { + auto& out = test_middleware_context_vector; + ASSERT_EQUAL(4, out.size()); + ASSERT_EQUAL("1 before", out[0]); + ASSERT_EQUAL("2 before", out[1]); + ASSERT_EQUAL("2 after", out[2]); + ASSERT_EQUAL("1 after", out[3]); } - ASSERT_EQUAL(1, x); server.stop(); } -- cgit v1.2.3-54-g00ecf