mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-10 09:43:51 +00:00
Added ServerTest.
This commit is contained in:
parent
0a26cd08f0
commit
ac5c13620c
27
httpsvrkit.h
27
httpsvrkit.h
@ -85,6 +85,8 @@ public:
|
|||||||
void get(const char* pattern, Handler handler);
|
void get(const char* pattern, Handler handler);
|
||||||
void post(const char* pattern, Handler handler);
|
void post(const char* pattern, Handler handler);
|
||||||
|
|
||||||
|
void on_ready(std::function<void ()> callback);
|
||||||
|
|
||||||
bool run();
|
bool run();
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
@ -94,8 +96,10 @@ private:
|
|||||||
const std::string ipaddr_or_hostname_;
|
const std::string ipaddr_or_hostname_;
|
||||||
const int port_;
|
const int port_;
|
||||||
socket_t sock_;
|
socket_t sock_;
|
||||||
std::vector<std::pair<std::regex, Handler>> get_handlers_;
|
|
||||||
|
std::vector<std::pair<std::regex, Handler>> get_handlers_;
|
||||||
std::vector<std::pair<std::string, Handler>> post_handlers_;
|
std::vector<std::pair<std::string, Handler>> post_handlers_;
|
||||||
|
std::function<void ()> on_ready_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Implementation
|
// Implementation
|
||||||
@ -242,12 +246,21 @@ inline void Server::post(const char* pattern, Handler handler)
|
|||||||
post_handlers_.push_back(std::make_pair(pattern, handler));
|
post_handlers_.push_back(std::make_pair(pattern, handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void Server::on_ready(std::function<void ()> callback)
|
||||||
|
{
|
||||||
|
on_ready_ = callback;
|
||||||
|
}
|
||||||
|
|
||||||
inline bool Server::run()
|
inline bool Server::run()
|
||||||
{
|
{
|
||||||
sock_ = create_server_socket(ipaddr_or_hostname_.c_str(), port_);
|
sock_ = create_server_socket(ipaddr_or_hostname_.c_str(), port_);
|
||||||
if (sock_ == -1) {
|
if (sock_ == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (on_ready_) {
|
||||||
|
on_ready_();
|
||||||
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
socket_t fd = accept(sock_, NULL, NULL);
|
socket_t fd = accept(sock_, NULL, NULL);
|
||||||
@ -444,14 +457,14 @@ inline void Server::process_request(FILE* fp_read, FILE* fp_write)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define HTTP_SERVER(host, port) \
|
#define HTTP_SERVER(host, port) \
|
||||||
for (std::shared_ptr<httpsvrkit::Server> svr = std::make_shared<httpsvrkit::Server>(host, port); \
|
for (std::shared_ptr<httpsvrkit::Server> svr_ = std::make_shared<httpsvrkit::Server>(host, port); \
|
||||||
svr; \
|
svr_; \
|
||||||
svr->run(), svr.reset())
|
svr_->run(), svr_.reset())
|
||||||
|
|
||||||
#define GET(url, body) \
|
#define GET(url, body) \
|
||||||
svr->get(url, [&](httpsvrkit::Context& cxt) { \
|
svr_->get(url, [&](httpsvrkit::Context& cxt) { \
|
||||||
const auto& req = cxt.request; \
|
const auto& req_ = cxt.request; \
|
||||||
auto& res = cxt.response; \
|
auto& res_ = cxt.response; \
|
||||||
body \
|
body \
|
||||||
});
|
});
|
||||||
|
|
||||||
|
17
test/test.cc
17
test/test.cc
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <httpsvrkit.h>
|
#include <httpsvrkit.h>
|
||||||
|
#include <future>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace httpsvrkit;
|
using namespace httpsvrkit;
|
||||||
@ -50,4 +51,20 @@ TEST(GetHeaderValueTest, RegularValue)
|
|||||||
ASSERT_STREQ("text/html", val);
|
ASSERT_STREQ("text/html", val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(ServerTest, GetMethod)
|
||||||
|
{
|
||||||
|
Server svr("localhost", 1914);
|
||||||
|
|
||||||
|
svr.get("hi", [&](httpsvrkit::Context& cxt) {
|
||||||
|
cxt.response.set_content("Hello World!");
|
||||||
|
});
|
||||||
|
|
||||||
|
svr.on_ready([&]() {
|
||||||
|
// TODO: HTTP GET request...
|
||||||
|
svr.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
auto f = async([&](){ svr.run(); });
|
||||||
|
}
|
||||||
|
|
||||||
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
||||||
|
Loading…
x
Reference in New Issue
Block a user