mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 01:08:27 +00:00
Changed to use c++11 features.
This commit is contained in:
parent
e7e8f5e70e
commit
a1ef364247
3 changed files with 24 additions and 89 deletions
97
test/test.cc
97
test/test.cc
|
@ -1,7 +1,7 @@
|
|||
|
||||
#include <gtest/gtest.h>
|
||||
#include <httplib.h>
|
||||
//#include <future>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
@ -17,70 +17,6 @@ using namespace httplib;
|
|||
const char* HOST = "localhost";
|
||||
const int PORT = 1234;
|
||||
|
||||
class thread
|
||||
{
|
||||
public:
|
||||
thread(std::function<void ()> fn);
|
||||
~thread();
|
||||
|
||||
void join();
|
||||
|
||||
private:
|
||||
thread();
|
||||
|
||||
#ifdef _WIN32
|
||||
HANDLE thread_;
|
||||
static unsigned int __stdcall TreadFunc(void* arg);
|
||||
#else
|
||||
pthread_t thread_;
|
||||
static void* TreadFunc(void* arg);
|
||||
#endif
|
||||
|
||||
static std::map<void*, std::function<void ()>> tasks_;
|
||||
};
|
||||
|
||||
std::map<void*, std::function<void ()>> thread::tasks_;
|
||||
|
||||
inline thread::thread(std::function<void ()> fn)
|
||||
: thread_(NULL)
|
||||
{
|
||||
tasks_[this] = fn;
|
||||
#ifdef _WIN32
|
||||
thread_ = (HANDLE)_beginthreadex(NULL, 0, TreadFunc, this, 0, NULL);
|
||||
#else
|
||||
pthread_create(&thread_, NULL, TreadFunc, this);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline thread::~thread()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
CloseHandle(thread_);
|
||||
#endif
|
||||
}
|
||||
|
||||
inline void thread::join()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
::WaitForSingleObject(thread_, INFINITE);
|
||||
#else
|
||||
pthread_join(thread_, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
unsigned int __stdcall thread::TreadFunc(void* arg)
|
||||
#else
|
||||
void* thread::TreadFunc(void* arg)
|
||||
#endif
|
||||
{
|
||||
thread* pThis = static_cast<thread*>(arg);
|
||||
tasks_[pThis]();
|
||||
tasks_.erase(pThis);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
TEST(StartupTest, WSAStartup)
|
||||
{
|
||||
|
@ -137,7 +73,7 @@ TEST(GetHeaderValueTest, DefaultValue)
|
|||
{
|
||||
//MultiMap map = {{"Dummy","Dummy"}};
|
||||
MultiMap map;
|
||||
map.insert(std::make_pair("Dummy", "Dummy"));
|
||||
map.insert(make_pair("Dummy", "Dummy"));
|
||||
auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
|
||||
ASSERT_STREQ("text/plain", val);
|
||||
}
|
||||
|
@ -146,7 +82,7 @@ TEST(GetHeaderValueTest, DefaultValueInt)
|
|||
{
|
||||
//MultiMap map = {{"Dummy","Dummy"}};
|
||||
MultiMap map;
|
||||
map.insert(std::make_pair("Dummy", "Dummy"));
|
||||
map.insert(make_pair("Dummy", "Dummy"));
|
||||
auto val = detail::get_header_value_int(map, "Content-Length", 100);
|
||||
EXPECT_EQ(100, val);
|
||||
}
|
||||
|
@ -155,8 +91,8 @@ TEST(GetHeaderValueTest, RegularValue)
|
|||
{
|
||||
//MultiMap map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
|
||||
MultiMap map;
|
||||
map.insert(std::make_pair("Content-Type","text/html"));
|
||||
map.insert(std::make_pair("Dummy", "Dummy"));
|
||||
map.insert(make_pair("Content-Type","text/html"));
|
||||
map.insert(make_pair("Dummy", "Dummy"));
|
||||
auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
|
||||
ASSERT_STREQ("text/html", val);
|
||||
}
|
||||
|
@ -165,8 +101,8 @@ TEST(GetHeaderValueTest, RegularValueInt)
|
|||
{
|
||||
//MultiMap map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
|
||||
MultiMap map;
|
||||
map.insert(std::make_pair("Content-Length", "100"));
|
||||
map.insert(std::make_pair("Dummy", "Dummy"));
|
||||
map.insert(make_pair("Content-Length", "100"));
|
||||
map.insert(make_pair("Dummy", "Dummy"));
|
||||
auto val = detail::get_header_value_int(map, "Content-Length", 0);
|
||||
EXPECT_EQ(100, val);
|
||||
}
|
||||
|
@ -194,7 +130,7 @@ protected:
|
|||
});
|
||||
|
||||
svr_.get("/person/(.*)", [&](const Request& req, Response& res) {
|
||||
std::string name = req.matches[1];
|
||||
string name = req.matches[1];
|
||||
if (persons_.find(name) != persons_.end()) {
|
||||
auto note = persons_[name];
|
||||
res.set_content(note, "text/plain");
|
||||
|
@ -209,8 +145,7 @@ protected:
|
|||
|
||||
persons_["john"] = "programmer";
|
||||
|
||||
//f_ = async([&](){ svr_.listen(HOST, PORT); });
|
||||
t_ = std::make_shared<thread>([&](){
|
||||
f_ = async([&](){
|
||||
up_ = true;
|
||||
svr_.listen(HOST, PORT);
|
||||
});
|
||||
|
@ -224,16 +159,14 @@ protected:
|
|||
//svr_.stop(); // NOTE: This causes dead lock on Windows.
|
||||
cli_.get("/stop");
|
||||
|
||||
//f_.get();
|
||||
t_->join();
|
||||
f_.get();
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> persons_;
|
||||
Server svr_;
|
||||
Client cli_;
|
||||
//std::future<void> f_;
|
||||
std::shared_ptr<thread> t_;
|
||||
bool up_;
|
||||
map<string, string> persons_;
|
||||
Server svr_;
|
||||
Client cli_;
|
||||
future<void> f_;
|
||||
bool up_;
|
||||
};
|
||||
|
||||
TEST_F(ServerTest, GetMethod200)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue