mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 01:08:27 +00:00
Add optional user defined header writer (#1683)
* Add optional user defined header writer * Fix errors and add test
This commit is contained in:
parent
c029597a5a
commit
a609330e4c
2 changed files with 74 additions and 2 deletions
40
test/test.cc
40
test/test.cc
|
@ -1592,6 +1592,46 @@ TEST(URLFragmentTest, WithFragment) {
|
|||
}
|
||||
}
|
||||
|
||||
TEST(HeaderWriter, SetHeaderWriter) {
|
||||
Server svr;
|
||||
|
||||
svr.set_header_writer([](Stream &strm, Headers &hdrs) {
|
||||
hdrs.emplace("CustomServerHeader", "CustomServerValue");
|
||||
return detail::write_headers(strm, hdrs);
|
||||
});
|
||||
svr.Get("/hi", [](const Request &req, Response &res) {
|
||||
auto it = req.headers.find("CustomClientHeader");
|
||||
EXPECT_TRUE(it != req.headers.end());
|
||||
EXPECT_EQ(it->second, "CustomClientValue");
|
||||
res.set_content("Hello World!\n", "text/plain");
|
||||
});
|
||||
|
||||
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
thread.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
});
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
{
|
||||
Client cli(HOST, PORT);
|
||||
cli.set_header_writer([](Stream &strm, Headers &hdrs) {
|
||||
hdrs.emplace("CustomClientHeader", "CustomClientValue");
|
||||
return detail::write_headers(strm, hdrs);
|
||||
});
|
||||
|
||||
auto res = cli.Get("/hi");
|
||||
EXPECT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
|
||||
auto it = res->headers.find("CustomServerHeader");
|
||||
EXPECT_TRUE(it != res->headers.end());
|
||||
EXPECT_EQ(it->second, "CustomServerValue");
|
||||
}
|
||||
}
|
||||
|
||||
class ServerTest : public ::testing::Test {
|
||||
protected:
|
||||
ServerTest()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue