add unittest for UNIX domain socket with abstract address

This commit is contained in:
Changbin Park 2022-08-01 10:11:55 +09:00
parent 667667d972
commit 08ff48f935
1 changed files with 38 additions and 10 deletions

View File

@ -5070,6 +5070,19 @@ protected:
std::remove(pathname_.c_str());
}
void client_GET(const std::string &addr) {
httplib::Client cli{addr};
cli.set_address_family(AF_UNIX);
ASSERT_TRUE(cli.is_valid());
const auto &result = cli.Get(pattern_);
ASSERT_TRUE(result) << "error: " << result.error();
const auto &resp = result.value();
EXPECT_EQ(resp.status, 200);
EXPECT_EQ(resp.body, content_);
}
const std::string pathname_ {"./httplib-server.sock"};
const std::string pattern_ {"/hi"};
const std::string content_ {"Hello World!"};
@ -5088,17 +5101,32 @@ TEST_F(UnixSocketTest, pathname) {
}
ASSERT_TRUE(svr.is_running());
httplib::Client cli{pathname_};
cli.set_address_family(AF_UNIX);
ASSERT_TRUE(cli.is_valid());
const auto &result = cli.Get(pattern_);
ASSERT_TRUE(result) << "error: " << result.error();
const auto &resp = result.value();
EXPECT_EQ(resp.status, 200);
EXPECT_EQ(resp.body, content_);
client_GET(pathname_);
svr.stop();
t.join();
}
#ifdef __linux__
TEST_F(UnixSocketTest, abstract) {
constexpr char svr_path[] {"\x00httplib-server.sock"};
const std::string abstract_addr {svr_path, sizeof(svr_path) - 1};
httplib::Server svr;
svr.Get(pattern_, [&](const httplib::Request &, httplib::Response &res) {
res.set_content(content_, "text/plain");
});
std::thread t {[&] {
ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(abstract_addr, 80)); }};
while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
ASSERT_TRUE(svr.is_running());
client_GET(abstract_addr);
svr.stop();
t.join();
}
#endif