Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test PUT and DEL in test basic http request #414

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions tests/test_cinatra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ TEST_CASE("test basic http request") {
<< "\n";
}

// Setting up GET and POST handlers
server.set_http_handler<GET>(
"/", [&server](request &, response &res) mutable {
res.set_status_and_content(status_type::ok, "hello world");
Expand All @@ -627,6 +628,20 @@ TEST_CASE("test basic http request") {
res.set_status_and_content(status_type::ok, std::move(str));
});

// Setting up PUT handler
server.set_http_handler<PUT>(
"/", [&server](request &req, response &res) mutable {
std::string str(req.body());
str.append(" put successfully");
res.set_status_and_content(status_type::ok, std::move(str));
});

// Setting up DELETE handler
server.set_http_handler<DEL>(
"/", [&server](request &, response &res) mutable {
res.set_status_and_content(status_type::ok, "data deleted");
});

std::promise<void> pr;
std::future<void> f = pr.get_future();
std::thread server_thread([&server, &pr]() {
Expand All @@ -639,13 +654,28 @@ TEST_CASE("test basic http request") {

coro_http_client client{};
std::string uri = "http://127.0.0.1:8090";
resp_data result = async_simple::coro::syncAwait(client.async_get(uri));
size_t size = result.resp_body.size();

// Testing PUT method
resp_data result = async_simple::coro::syncAwait(client.async_request(
uri, http_method::PUT,
req_context<std::string_view>{.content = "data for put"}));
CHECK(result.resp_body == "data for put put successfully");

// Testing DELETE method
result = async_simple::coro::syncAwait(client.async_request(
uri, http_method::DEL, req_context<std::string_view>{}));
CHECK(result.resp_body == "data deleted");

// Testing GET method again after DELETE
result = async_simple::coro::syncAwait(client.async_get(uri));
CHECK(result.resp_body == "hello world");

size_t size = result.resp_body.size();
auto buf = client.release_buf();
CHECK(size == strlen(buf.data()));
CHECK(buf == "hello world");

// Rest of the POST tests
result = async_simple::coro::syncAwait(client.async_post(
uri, "async post hello coro_http_client", req_content_type::string));
CHECK(result.resp_body ==
Expand Down
Loading