aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAntony Woods <acron1@gmail.com>2014-10-14 09:48:35 +0100
committerAntony Woods <acron1@gmail.com>2014-10-14 09:48:35 +0100
commit06842721d7da53a2235e6e4071760588ec285f90 (patch)
treec72f69e9fe4154be039ba89e474395e21b6d5fdf /tests
parenta8f6c5a92fea5439b4a0372ccc3b367e3c3bed3b (diff)
downloadcrow-06842721d7da53a2235e6e4071760588ec285f90.tar.gz
crow-06842721d7da53a2235e6e4071760588ec285f90.zip
Wrapped qs_parse as query_string and added tests
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest.cpp117
1 files changed, 117 insertions, 0 deletions
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<int>(last_url_params.get("int")), 100);
+ ASSERT_EQUAL(boost::lexical_cast<double>(last_url_params.get("double")), 123.45);
+ ASSERT_EQUAL(boost::lexical_cast<bool>(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();