aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common.h2
-rw-r--r--http_connection.h5
-rw-r--r--http_response.h6
-rw-r--r--routing.h1
-rw-r--r--unittest.cpp52
-rw-r--r--utility.h3
6 files changed, 57 insertions, 12 deletions
diff --git a/common.h b/common.h
index db55e8e..ff63e9f 100644
--- a/common.h
+++ b/common.h
@@ -6,8 +6,6 @@ namespace flask
{
enum class ParamType
{
- INVALID,
-
INT,
UINT,
DOUBLE,
diff --git a/http_connection.h b/http_connection.h
index 2f245ba..f248907 100644
--- a/http_connection.h
+++ b/http_connection.h
@@ -36,7 +36,10 @@ namespace flask
std::vector<boost::asio::const_buffer> buffers;
- buffers.push_back(boost::asio::buffer(statusCodes[res.status]));
+ buffers.push_back(boost::asio::buffer(statusCodes[res.code]));
+
+ if (res.code > 400 && res.body.empty())
+ res.body = statusCodes[res.code].substr(9);
bool has_content_length = false;
for(auto& kv : res.headers)
diff --git a/http_response.h b/http_response.h
index 7b862c4..9cbfd02 100644
--- a/http_response.h
+++ b/http_response.h
@@ -29,11 +29,11 @@ namespace flask
struct response
{
std::string body;
- int status{200};
+ int code{200};
std::unordered_map<std::string, std::string> headers;
response() {}
- explicit response(int status) : status(status) {}
+ explicit response(int code) : code(code) {}
response(std::string body) : body(std::move(body)) {}
- response(int status, std::string body) : body(std::move(body)), status(status) {}
+ response(int code, std::string body) : body(std::move(body)), code(code) {}
};
}
diff --git a/routing.h b/routing.h
index 539c3aa..4515af3 100644
--- a/routing.h
+++ b/routing.h
@@ -309,6 +309,7 @@ public:
{ ParamType::DOUBLE, "<float>" },
{ ParamType::DOUBLE, "<double>" },
{ ParamType::STRING, "<str>" },
+ { ParamType::STRING, "<string>" },
{ ParamType::PATH, "<path>" },
};
diff --git a/unittest.cpp b/unittest.cpp
index 5bdd911..5ebf15e 100644
--- a/unittest.cpp
+++ b/unittest.cpp
@@ -1,6 +1,7 @@
#include <iostream>
#include <vector>
#include "routing.h"
+#include "utility.h"
using namespace std;
using namespace flask;
@@ -9,8 +10,23 @@ vector<Test*> tests;
Test::Test() { tests.push_back(this); }
bool failed__ = false;
-void fail() { failed__ = true; }
+void error_print()
+{
+ cerr << endl;
+}
+
+template <typename A, typename ...Args>
+void error_print(A a, Args...args)
+{
+ cerr<<a;
+ error_print(args...);
+}
+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__)
#define TEST(x) struct test##x:public Test{void test();}x##_; \
void test##x::test()
@@ -18,6 +34,8 @@ TEST(Rule)
{
Rule r("/http/");
r.name("abc");
+
+ // empty handler - fail to validate
try
{
r.validate();
@@ -29,14 +47,38 @@ TEST(Rule)
int x = 0;
+ // registering handler
r([&x]{x = 1;return "";});
+
r.validate();
- if (x!=0)
- fail();
+
+ // executing handler
+ ASSERT_EQUAL(0, x);
r.handle(request(), routing_params());
- if (x == 0)
- fail();
+ ASSERT_EQUAL(1, x);
+}
+
+TEST(ParameterTagging)
+{
+ 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
+ static_assert(is_same<black_magic::S<uint64_t, double, int64_t>, black_magic::arguments<6*6+6*3+2>::type>::value, "tag to type container");
+}
+TEST(response)
+{
+ ASSERT_EQUAL(100, response(100).code);
+ ASSERT_EQUAL(200, response("Hello there").code);
}
int testmain()
diff --git a/utility.h b/utility.h
index 5da8b7e..f824d61 100644
--- a/utility.h
+++ b/utility.h
@@ -94,7 +94,8 @@ namespace flask
constexpr bool is_str(const_str s, unsigned i)
{
- return is_equ_n(s, i, "<str>", 0, 5);
+ return is_equ_n(s, i, "<str>", 0, 5) ||
+ is_equ_n(s, i, "<string>", 0, 8);
}
constexpr bool is_path(const_str s, unsigned i)