2014-04-14 15:31:51 +00:00
|
|
|
#include <iostream>
|
2014-04-14 18:02:48 +00:00
|
|
|
#include <vector>
|
|
|
|
#include "routing.h"
|
2014-04-14 20:11:37 +00:00
|
|
|
#include "utility.h"
|
2014-04-14 20:35:04 +00:00
|
|
|
#include "flask.h"
|
2014-04-14 15:31:51 +00:00
|
|
|
using namespace std;
|
2014-04-14 18:02:48 +00:00
|
|
|
using namespace flask;
|
|
|
|
|
|
|
|
struct Test { Test(); virtual void test() = 0; };
|
|
|
|
vector<Test*> tests;
|
|
|
|
Test::Test() { tests.push_back(this); }
|
|
|
|
|
|
|
|
bool failed__ = false;
|
2014-04-14 20:11:37 +00:00
|
|
|
void error_print()
|
|
|
|
{
|
|
|
|
cerr << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename A, typename ...Args>
|
|
|
|
void error_print(A a, Args...args)
|
|
|
|
{
|
|
|
|
cerr<<a;
|
|
|
|
error_print(args...);
|
|
|
|
}
|
2014-04-14 18:02:48 +00:00
|
|
|
|
2014-04-14 20:11:37 +00:00
|
|
|
template <typename ...Args>
|
|
|
|
void fail(Args...args) { error_print(args...);failed__ = true; }
|
|
|
|
|
|
|
|
#define ASSERT_EQUAL(a, b) if (a != b) fail("Assert fail: expected ", (a), " actual " , b, ", " #a " == " #b ", at " __FILE__ ":",__LINE__)
|
|
|
|
#define ASSERT_NOTEQUAL(a, b) if (a != b) fail("Assert fail: not expected ", (a), ", " #a " != " #b ", at " __FILE__ ":",__LINE__)
|
2014-04-14 18:02:48 +00:00
|
|
|
#define TEST(x) struct test##x:public Test{void test();}x##_; \
|
|
|
|
void test##x::test()
|
|
|
|
|
|
|
|
TEST(Rule)
|
|
|
|
{
|
|
|
|
Rule r("/http/");
|
|
|
|
r.name("abc");
|
2014-04-14 20:11:37 +00:00
|
|
|
|
|
|
|
// empty handler - fail to validate
|
2014-04-14 18:02:48 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
r.validate();
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
catch(runtime_error& e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
|
2014-04-14 20:11:37 +00:00
|
|
|
// registering handler
|
2014-04-14 18:02:48 +00:00
|
|
|
r([&x]{x = 1;return "";});
|
2014-04-14 20:11:37 +00:00
|
|
|
|
2014-04-14 18:02:48 +00:00
|
|
|
r.validate();
|
2014-04-14 20:11:37 +00:00
|
|
|
|
|
|
|
// executing handler
|
|
|
|
ASSERT_EQUAL(0, x);
|
2014-04-14 18:02:48 +00:00
|
|
|
r.handle(request(), routing_params());
|
2014-04-14 20:11:37 +00:00
|
|
|
ASSERT_EQUAL(1, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ParameterTagging)
|
|
|
|
{
|
2014-04-14 20:35:04 +00:00
|
|
|
static_assert(black_magic::is_valid("<int><int><int>"), "valid url");
|
|
|
|
static_assert(!black_magic::is_valid("<int><int<<int>"), "invalid url");
|
|
|
|
static_assert(!black_magic::is_valid("nt>"), "invalid url");
|
2014-04-14 20:11:37 +00:00
|
|
|
ASSERT_EQUAL(1, black_magic::get_parameter_tag("<int>"));
|
|
|
|
ASSERT_EQUAL(2, black_magic::get_parameter_tag("<uint>"));
|
|
|
|
ASSERT_EQUAL(3, black_magic::get_parameter_tag("<float>"));
|
|
|
|
ASSERT_EQUAL(3, black_magic::get_parameter_tag("<double>"));
|
|
|
|
ASSERT_EQUAL(4, black_magic::get_parameter_tag("<str>"));
|
|
|
|
ASSERT_EQUAL(4, black_magic::get_parameter_tag("<string>"));
|
|
|
|
ASSERT_EQUAL(5, black_magic::get_parameter_tag("<path>"));
|
|
|
|
ASSERT_EQUAL(6*6+6+1, black_magic::get_parameter_tag("<int><int><int>"));
|
|
|
|
ASSERT_EQUAL(6*6+6+2, black_magic::get_parameter_tag("<uint><int><int>"));
|
|
|
|
ASSERT_EQUAL(6*6+6*3+2, black_magic::get_parameter_tag("<uint><double><int>"));
|
|
|
|
|
|
|
|
// url definition parsed in compile time, build into *one number*, and given to template argument
|
2014-04-14 20:35:04 +00:00
|
|
|
static_assert(std::is_same<black_magic::S<uint64_t, double, int64_t>, black_magic::arguments<6*6+6*3+2>::type>::value, "tag to type container");
|
2014-04-14 20:11:37 +00:00
|
|
|
}
|
2014-04-14 18:02:48 +00:00
|
|
|
|
2014-04-14 20:35:04 +00:00
|
|
|
TEST(simple_response_routing_params)
|
2014-04-14 20:11:37 +00:00
|
|
|
{
|
|
|
|
ASSERT_EQUAL(100, response(100).code);
|
|
|
|
ASSERT_EQUAL(200, response("Hello there").code);
|
2014-04-15 13:08:23 +00:00
|
|
|
ASSERT_EQUAL(500, response(500, "Internal Error?").code);
|
2014-04-14 20:35:04 +00:00
|
|
|
|
|
|
|
routing_params rp;
|
|
|
|
rp.int_params.push_back(1);
|
|
|
|
rp.int_params.push_back(5);
|
|
|
|
rp.uint_params.push_back(2);
|
|
|
|
rp.double_params.push_back(3);
|
|
|
|
rp.string_params.push_back("hello");
|
|
|
|
ASSERT_EQUAL(1, rp.get<int64_t>(0));
|
|
|
|
ASSERT_EQUAL(5, rp.get<int64_t>(1));
|
|
|
|
ASSERT_EQUAL(2, rp.get<uint64_t>(0));
|
|
|
|
ASSERT_EQUAL(3, rp.get<double>(0));
|
|
|
|
ASSERT_EQUAL("hello", rp.get<string>(0));
|
2014-04-14 18:02:48 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 13:08:23 +00:00
|
|
|
TEST(multi_server)
|
|
|
|
{
|
|
|
|
static char buf[2048];
|
|
|
|
Flask app1, app2;
|
|
|
|
app1.route("/")([]{return "A";});
|
|
|
|
app2.route("/")([]{return "B";});
|
|
|
|
|
|
|
|
Server<Flask> server1(&app1, 45451);
|
|
|
|
Server<Flask> server2(&app2, 45452);
|
|
|
|
|
|
|
|
auto _ = async(launch::async, [&]{server1.run();});
|
|
|
|
auto _2 = async(launch::async, [&]{server2.run();});
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
std::string sendmsg = "GET /\r\n\r\n";
|
|
|
|
|
|
|
|
c.send(asio::buffer(sendmsg));
|
|
|
|
|
|
|
|
size_t recved = c.receive(asio::buffer(buf, 2048));
|
|
|
|
ASSERT_EQUAL('A', buf[recved-1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
asio::ip::tcp::socket c(is);
|
|
|
|
c.connect(asio::ip::tcp::endpoint(asio::ip::address::from_string("127.0.0.1"), 45452));
|
|
|
|
|
|
|
|
std::string sendmsg = "GET /\r\n\r\n";
|
|
|
|
|
|
|
|
c.send(asio::buffer(sendmsg));
|
|
|
|
|
|
|
|
size_t recved = c.receive(asio::buffer(buf, 2048));
|
|
|
|
ASSERT_EQUAL('B', buf[recved-1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
server1.stop();
|
|
|
|
server2.stop();
|
|
|
|
}
|
|
|
|
|
2014-04-14 18:02:48 +00:00
|
|
|
int testmain()
|
|
|
|
{
|
|
|
|
bool failed = false;
|
|
|
|
for(auto t:tests)
|
|
|
|
{
|
|
|
|
failed__ = false;
|
2014-04-15 13:08:23 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
//cerr << typeid(*t).name() << endl;
|
|
|
|
t->test();
|
|
|
|
}
|
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
|
|
|
fail(e.what());
|
|
|
|
}
|
2014-04-14 18:02:48 +00:00
|
|
|
if (failed__)
|
|
|
|
{
|
|
|
|
cerr << "F";
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cerr << ".";
|
|
|
|
}
|
|
|
|
cerr<<endl;
|
|
|
|
return failed ? -1 : 0;
|
|
|
|
}
|
2014-04-02 16:38:08 +00:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2014-04-14 18:02:48 +00:00
|
|
|
return testmain();
|
2014-04-02 16:38:08 +00:00
|
|
|
}
|