From a5fab23f70e6e33c633ba4b646a41d0851169ad1 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Tue, 7 Oct 2014 21:51:24 +0900 Subject: HTTP GET/POST method distinguish --- tests/unittest.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 818032f..dc04a5f 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -249,6 +249,79 @@ TEST(handler_with_response) }); } +TEST(http_method) +{ + SimpleApp app; + + CROW_ROUTE(app, "/") + .methods("POST"_method, "GET"_method) + ([](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"; + }); + CROW_ROUTE(app, "/post_only") + .methods("POST"_method) + ([](const request& req){ + return "post"; + }); + + + // cannot have multiple handlers for the same url + //CROW_ROUTE(app, "/") + //.methods("GET"_method) + //([]{ return "2"; }); + + { + request req; + response res; + + req.url = "/"; + app.handle(req, res); + + ASSERT_EQUAL("2", res.body); + } + { + request req; + response res; + + req.url = "/"; + req.method = "POST"_method; + app.handle(req, res); + + ASSERT_EQUAL("1", res.body); + } + + { + request req; + response res; + + req.url = "/get_only"; + app.handle(req, res); + + ASSERT_EQUAL("get", res.body); + } + + { + request req; + response res; + + req.url = "/get_only"; + req.method = "POST"_method; + app.handle(req, res); + + ASSERT_NOTEQUAL("get", res.body); + } + +} + TEST(server_handling_error_request) { static char buf[2048]; @@ -282,8 +355,8 @@ TEST(multi_server) { static char buf[2048]; SimpleApp app1, app2; - CROW_ROUTE(app1, "/")([]{return "A";}); - CROW_ROUTE(app2, "/")([]{return "B";}); + 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); @@ -297,7 +370,6 @@ TEST(multi_server) 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(sendmsg)); size_t recved = c.receive(asio::buffer(buf, 2048)); -- cgit v1.2.3-54-g00ecf