Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Apr 8, 2023
1 parent 21f9c51 commit d587548
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
3 changes: 1 addition & 2 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
24 changes: 20 additions & 4 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit d587548

Please sign in to comment.