aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAntony Woods <acron1@gmail.com>2014-10-24 09:40:09 +0100
committerAntony Woods <acron1@gmail.com>2014-10-24 09:40:09 +0100
commit693aac108de4dc62811b9a2737d895ada474cfb2 (patch)
tree44cc722afea8b6ef26095cd27aa64b97ec48d74e /tests
parent27bf11d35c8ce44fd9fef656e2975712846b9bb2 (diff)
parent4b3b8070e75ce0fc181e5c012c47da2a1e7a918e (diff)
downloadcrow-693aac108de4dc62811b9a2737d895ada474cfb2.tar.gz
crow-693aac108de4dc62811b9a2737d895ada474cfb2.zip
Post-pull commit
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/tests/unittest.cpp b/tests/unittest.cpp
index dc04a5f..4dec727 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;
@@ -830,6 +831,123 @@ 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);
+ ASSERT_TRUE(last_url_params.get_list("missing").empty());
+ }
+ // 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();