From 06842721d7da53a2235e6e4071760588ec285f90 Mon Sep 17 00:00:00 2001 From: Antony Woods Date: Tue, 14 Oct 2014 09:48:35 +0100 Subject: Wrapped qs_parse as query_string and added tests --- tests/unittest.cpp | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) (limited to 'tests') diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 818032f..87acb52 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -11,6 +11,7 @@ #include "json.h" #include "mustache.h" #include "middleware.h" +#include "query_string.h" using namespace std; using namespace crow; @@ -758,6 +759,122 @@ TEST(bug_quick_repeated_request) server.stop(); } +TEST(simple_url_params) +{ + static char buf[2048]; + + SimpleApp app; + + query_string last_url_params; + + CROW_ROUTE(app, "/params") + ([&last_url_params](const crow::request& req){ + last_url_params = move(req.url_params); + return "OK"; + }); + + ///params?h=1&foo=bar&lol&count[]=1&count[]=4&pew=5.2 + + decltype(app)::server_t server(&app, 45451); + auto _ = async(launch::async, [&]{server.run();}); + asio::io_service is; + std::string sendmsg; + + // check single presence + 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.send(asio::buffer(sendmsg)); + c.receive(asio::buffer(buf, 2048)); + c.close(); + + ASSERT_TRUE(last_url_params.get("missing") == nullptr); + ASSERT_TRUE(last_url_params.get("foobar") != nullptr); + } + // check multiple presence + 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.send(asio::buffer(sendmsg)); + c.receive(asio::buffer(buf, 2048)); + c.close(); + + ASSERT_TRUE(last_url_params.get("missing") == nullptr); + ASSERT_TRUE(last_url_params.get("foo") != nullptr); + ASSERT_TRUE(last_url_params.get("bar") != nullptr); + ASSERT_TRUE(last_url_params.get("baz") != nullptr); + } + // check single value + 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.send(asio::buffer(sendmsg)); + c.receive(asio::buffer(buf, 2048)); + c.close(); + + ASSERT_EQUAL(string(last_url_params.get("hello")), "world"); + } + // check multiple value + 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.send(asio::buffer(sendmsg)); + c.receive(asio::buffer(buf, 2048)); + c.close(); + + ASSERT_EQUAL(string(last_url_params.get("hello")), "world"); + ASSERT_EQUAL(string(last_url_params.get("left")), "right"); + ASSERT_EQUAL(string(last_url_params.get("up")), "down"); + } + // check multiple value, multiple types + 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.send(asio::buffer(sendmsg)); + 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); + ASSERT_EQUAL(boost::lexical_cast(last_url_params.get("boolean")), true); + } + // check single array value + sendmsg = "GET /params?tmnt[]=leonardo\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(sendmsg)); + c.receive(asio::buffer(buf, 2048)); + 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"); + } + // check multiple array value + sendmsg = "GET /params?tmnt[]=leonardo&tmnt[]=donatello&tmnt[]=raphael\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(sendmsg)); + c.receive(asio::buffer(buf, 2048)); + 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"); + ASSERT_EQUAL(string(last_url_params.get_list("tmnt")[2]), "raphael"); + } + server.stop(); +} + int main() { return testmain(); -- cgit v1.2.3-54-g00ecf From 023455ba58cabfc7afc3e99c399c7c25c0a643cb Mon Sep 17 00:00:00 2001 From: ipknHama Date: Fri, 24 Oct 2014 01:20:19 +0900 Subject: add building test for crow_all.h --- examples/CMakeLists.txt | 4 ++++ tests/unittest.cpp | 1 + 2 files changed, 5 insertions(+) (limited to 'tests') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2d449e4..1fcec94 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -9,6 +9,10 @@ if (Tcmalloc_FOUND) target_link_libraries(example ${Tcmalloc_LIBRARIES}) endif(Tcmalloc_FOUND) +add_executable(example_with_all example_with_all.cpp) +#target_link_libraries(example crow) +target_link_libraries(example_with_all ${Boost_LIBRARIES} ) + add_custom_command(OUTPUT example_test.py COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/example_test.py ${CMAKE_CURRENT_BINARY_DIR}/example_test.py diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 1a15742..4dec727 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -863,6 +863,7 @@ TEST(simple_url_params) ASSERT_TRUE(last_url_params.get("missing") == nullptr); ASSERT_TRUE(last_url_params.get("foobar") != nullptr); + ASSERT_TRUE(last_url_params.get_list("missing").empty()); } // check multiple presence sendmsg = "GET /params?foo&bar&baz\r\n\r\n"; -- cgit v1.2.3-54-g00ecf