From 1b1210685efc252e5e9053cb75e8e8ade7dc4371 Mon Sep 17 00:00:00 2001 From: Gino Maisto Date: Sat, 19 Mar 2016 16:05:48 +0100 Subject: Bind to interface support: fixed unittest --- tests/unittest.cpp | 105 ++++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 50 deletions(-) (limited to 'tests') diff --git a/tests/unittest.cpp b/tests/unittest.cpp index d2cb231..164cbf0 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -48,7 +48,7 @@ void fail(Args...args) { error_print(args...);failed__ = true; } } \ catch(std::exception&) \ { \ - } + } @@ -57,13 +57,17 @@ void fail(Args...args) { error_print(args...);failed__ = true; } #define DISABLE_TEST(x) struct test##x{void test();}x##_; \ void test##x::test() + +#define LOCALHOST_ADDRESS "127.0.0.1" + + TEST(Rule) { TaggedRule<> r("/http/"); r.name("abc"); // empty handler - fail to validate - try + try { r.validate(); fail("empty handler should fail to validate"); @@ -310,22 +314,22 @@ TEST(http_method) CROW_ROUTE(app, "/") .methods("POST"_method, "GET"_method) - ([](const request& req){ - if (req.method == "GET"_method) - return "2"; - else - return "1"; + ([](const request& req){ + if (req.method == "GET"_method) + return "2"; + else + return "1"; }); CROW_ROUTE(app, "/get_only") .methods("GET"_method) - ([](const request& req){ - return "get"; + ([](const request& req){ + return "get"; }); CROW_ROUTE(app, "/post_only") .methods("POST"_method) - ([](const request& req){ - return "post"; + ([](const request& req){ + return "post"; }); @@ -382,13 +386,13 @@ TEST(server_handling_error_request) static char buf[2048]; SimpleApp app; CROW_ROUTE(app, "/")([]{return "A";}); - Server server(&app, 45451); + Server server(&app, LOCALHOST_ADDRESS, 45451); auto _ = async(launch::async, [&]{server.run();}); std::string sendmsg = "POX"; asio::io_service is; { asio::ip::tcp::socket c(is); - c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451)); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); @@ -413,8 +417,8 @@ TEST(multi_server) CROW_ROUTE(app1, "/").methods("GET"_method, "POST"_method)([]{return "A";}); CROW_ROUTE(app2, "/").methods("GET"_method, "POST"_method)([]{return "B";}); - Server server1(&app1, 45451); - Server server2(&app2, 45452); + Server server1(&app1, LOCALHOST_ADDRESS, 45451); + Server server2(&app2, LOCALHOST_ADDRESS, 45452); auto _ = async(launch::async, [&]{server1.run();}); auto _2 = async(launch::async, [&]{server2.run();}); @@ -423,7 +427,7 @@ TEST(multi_server) asio::io_service is; { asio::ip::tcp::socket c(is); - c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451)); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); @@ -433,7 +437,7 @@ TEST(multi_server) { asio::ip::tcp::socket c(is); - c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45452)); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45452)); for(auto ch:sendmsg) { @@ -452,7 +456,7 @@ TEST(multi_server) TEST(json_read) { { - const char* json_error_tests[] = + const char* json_error_tests[] = { "{} 3", "{{}", "{3}", "3.4.5", "+3", "3-2", "00", "03", "1e3e3", "1e+.3", @@ -681,10 +685,11 @@ struct NullSimpleMiddleware {} }; + TEST(middleware_simple) { App app; - decltype(app)::server_t server(&app, 45451); + decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); CROW_ROUTE(app, "/")([&](const crow::request& req) { app.get_context(req); @@ -714,9 +719,9 @@ std::vector test_middleware_context_vector; struct FirstMW { - struct context - { - std::vector v; + struct context + { + std::vector v; }; void before_handle(request& req, response& res, context& ctx) @@ -799,13 +804,13 @@ TEST(middleware_context) return ""; }); - decltype(app)::server_t server(&app, 45451); + decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 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); - c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451)); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); @@ -828,7 +833,7 @@ TEST(middleware_context) 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.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg2)); @@ -865,13 +870,13 @@ TEST(middleware_cookieparser) return ""; }); - decltype(app)::server_t server(&app, 45451); + decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); auto _ = async(launch::async, [&]{server.run();}); std::string sendmsg = "GET /\r\nCookie: key1=value1; key2=\"val\\\"ue2\"\r\n\r\n"; asio::io_service is; { asio::ip::tcp::socket c(is); - c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451)); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); @@ -895,7 +900,7 @@ TEST(bug_quick_repeated_request) return "hello"; }); - decltype(app)::server_t server(&app, 45451); + decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); auto _ = async(launch::async, [&]{server.run();}); std::string sendmsg = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"; asio::io_service is; @@ -903,11 +908,11 @@ TEST(bug_quick_repeated_request) std::vector> v; for(int i = 0; i < 5; i++) { - v.push_back(async(launch::async, + v.push_back(async(launch::async, [&] { asio::ip::tcp::socket c(is); - c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451)); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); for(int j = 0; j < 5; j ++) { @@ -939,7 +944,7 @@ TEST(simple_url_params) ///params?h=1&foo=bar&lol&count[]=1&count[]=4&pew=5.2 - decltype(app)::server_t server(&app, 45451); + decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); auto _ = async(launch::async, [&]{server.run();}); asio::io_service is; std::string sendmsg; @@ -948,7 +953,7 @@ TEST(simple_url_params) sendmsg = "GET /params\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.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); c.receive(asio::buffer(buf, 2048)); c.close(); @@ -962,7 +967,7 @@ TEST(simple_url_params) sendmsg = "GET /params?foobar\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.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); c.receive(asio::buffer(buf, 2048)); c.close(); @@ -975,7 +980,7 @@ TEST(simple_url_params) sendmsg = "GET /params?foo&bar&baz\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.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); c.receive(asio::buffer(buf, 2048)); c.close(); @@ -989,10 +994,10 @@ TEST(simple_url_params) sendmsg = "GET /params?hello=world\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.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); c.receive(asio::buffer(buf, 2048)); - c.close(); + c.close(); ASSERT_EQUAL(string(last_url_params.get("hello")), "world"); } @@ -1000,7 +1005,7 @@ TEST(simple_url_params) sendmsg = "GET /params?hello=world&left=right&up=down\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.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); c.receive(asio::buffer(buf, 2048)); c.close(); @@ -1013,10 +1018,10 @@ TEST(simple_url_params) sendmsg = "GET /params?int=100&double=123.45&boolean=1\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.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); - c.receive(asio::buffer(buf, 2048)); - c.close(); + c.receive(asio::buffer(buf, 2048)); + c.close(); ASSERT_EQUAL(boost::lexical_cast(last_url_params.get("int")), 100); ASSERT_EQUAL(boost::lexical_cast(last_url_params.get("double")), 123.45); @@ -1027,11 +1032,11 @@ TEST(simple_url_params) { asio::ip::tcp::socket c(is); - c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451)); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); c.receive(asio::buffer(buf, 2048)); - c.close(); - + c.close(); + ASSERT_TRUE(last_url_params.get("tmnt") == nullptr); ASSERT_EQUAL(last_url_params.get_list("tmnt").size(), 1); ASSERT_EQUAL(string(last_url_params.get_list("tmnt")[0]), "leonardo"); @@ -1041,11 +1046,11 @@ TEST(simple_url_params) { asio::ip::tcp::socket c(is); - c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45451)); + c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); c.send(asio::buffer(sendmsg)); c.receive(asio::buffer(buf, 2048)); - c.close(); - + c.close(); + ASSERT_EQUAL(last_url_params.get_list("tmnt").size(), 3); ASSERT_EQUAL(string(last_url_params.get_list("tmnt")[0]), "leonardo"); ASSERT_EQUAL(string(last_url_params.get_list("tmnt")[1]), "donatello"); @@ -1081,7 +1086,7 @@ TEST(route_dynamic) return ""; }); - try + try { app.route_dynamic("/invalid_test//") ([](){ @@ -1089,17 +1094,17 @@ TEST(route_dynamic) }); fail(); } - catch(std::exception&) + catch(std::exception&) { } // app is in an invalid state when route_dynamic throws an exception. - try + try { app.validate(); fail(); } - catch(std::exception&) + catch(std::exception&) { } -- cgit v1.2.3-54-g00ecf