From 9dc4e230821339d74fabfb87328396a3a7abe319 Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 17 Apr 2018 00:05:05 -0400 Subject: [PATCH] Unit test for #52 --- httplib.h | 7 +++++++ test/test.cc | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/httplib.h b/httplib.h index aabb0f8..4607342 100644 --- a/httplib.h +++ b/httplib.h @@ -205,6 +205,8 @@ public: bool is_running() const; void stop(); + bool is_handling_requests() const; + protected: bool process_request(Stream& strm, bool last_connection); @@ -1482,6 +1484,11 @@ inline void Server::stop() svr_sock_ = -1; } +inline bool Server::is_handling_requests() const +{ + return running_threads_ > 0; +} + inline bool Server::parse_request_line(const char* s, Request& req) { static std::regex re("(GET|HEAD|POST) ([^?]+)(?:\\?(.+?))? (HTTP/1\\.[01])\r\n"); diff --git a/test/test.cc b/test/test.cc index de2ebf2..2e91ad7 100644 --- a/test/test.cc +++ b/test/test.cc @@ -262,6 +262,10 @@ protected: svr_.get("/hi", [&](const Request& /*req*/, Response& res) { res.set_content("Hello World!", "text/plain"); }) + .get("/slow", [&](const Request& /*req*/, Response& res) { + msleep(3000); + res.set_content("slow", "text/plain"); + }) .get("/remote_addr", [&](const Request& req, Response& res) { auto remote_addr = req.headers.find("REMOTE_ADDR")->second; res.set_content(remote_addr.c_str(), "text/plain"); @@ -358,6 +362,7 @@ protected: virtual void TearDown() { svr_.stop(); t_.join(); + EXPECT_EQ(false, svr_.is_handling_requests()); } map persons_; @@ -664,6 +669,14 @@ TEST_F(ServerTest, GetMethodRemoteAddr) EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1"); } +TEST_F(ServerTest, SlowRequest) +{ + std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); + std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); + std::thread([=]() { auto res = cli_.get("/slow"); }).detach(); + msleep(1000); +} + #ifdef CPPHTTPLIB_ZLIB_SUPPORT TEST_F(ServerTest, Gzip) {