mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 14:29:10 -07:00
Fix #1545
This commit is contained in:
parent
21f9c51556
commit
d587548250
2 changed files with 21 additions and 6 deletions
|
@ -4941,10 +4941,9 @@ inline void Response::set_content(const std::string &s,
|
|||
inline void Response::set_content_provider(
|
||||
size_t in_length, const std::string &content_type, ContentProvider provider,
|
||||
ContentProviderResourceReleaser resource_releaser) {
|
||||
assert(in_length > 0);
|
||||
set_header("Content-Type", content_type);
|
||||
content_length_ = in_length;
|
||||
content_provider_ = std::move(provider);
|
||||
if (in_length > 0) { content_provider_ = std::move(provider); }
|
||||
content_provider_resource_releaser_ = resource_releaser;
|
||||
is_chunked_content_provider_ = false;
|
||||
}
|
||||
|
|
24
test/test.cc
24
test/test.cc
|
@ -4253,6 +4253,16 @@ TEST(ClientProblemDetectionTest, ContentProvider) {
|
|||
[](bool success) { ASSERT_FALSE(success); });
|
||||
});
|
||||
|
||||
svr.Get("/empty", [&](const Request & /*req*/, Response &res) {
|
||||
res.set_content_provider(
|
||||
0, "text/plain",
|
||||
[&](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) -> bool {
|
||||
EXPECT_TRUE(false);
|
||||
return true;
|
||||
},
|
||||
[](bool success) { ASSERT_FALSE(success); });
|
||||
});
|
||||
|
||||
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
|
@ -4269,11 +4279,17 @@ TEST(ClientProblemDetectionTest, ContentProvider) {
|
|||
|
||||
Client cli("localhost", PORT);
|
||||
|
||||
auto res = cli.Get("/hi", [&](const char * /*data*/, size_t /*data_length*/) {
|
||||
return false;
|
||||
});
|
||||
{
|
||||
auto res = cli.Get("/hi", [&](const char * /*data*/,
|
||||
size_t /*data_length*/) { return false; });
|
||||
ASSERT_FALSE(res);
|
||||
}
|
||||
|
||||
ASSERT_FALSE(res);
|
||||
{
|
||||
auto res = cli.Get("/empty", [&](const char * /*data*/,
|
||||
size_t /*data_length*/) { return false; });
|
||||
ASSERT_TRUE(res);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) {
|
||||
|
|
Loading…
Reference in a new issue