1
0
mirror of synced 2025-07-04 07:22:28 +03:00
This commit is contained in:
yhirose
2025-06-24 19:39:57 -04:00
parent 1729aa8c1f
commit b2bf172393
2 changed files with 154 additions and 0 deletions

View File

@ -1450,6 +1450,11 @@ public:
Result Delete(const std::string &path, const Headers &headers,
const std::string &body, const std::string &content_type,
Progress progress);
Result Delete(const std::string &path, const Params &params);
Result Delete(const std::string &path, const Headers &headers,
const Params &params);
Result Delete(const std::string &path, const Headers &headers,
const Params &params, Progress progress);
Result Options(const std::string &path);
Result Options(const std::string &path, const Headers &headers);
@ -1894,6 +1899,11 @@ public:
Result Delete(const std::string &path, const Headers &headers,
const std::string &body, const std::string &content_type,
Progress progress);
Result Delete(const std::string &path, const Params &params);
Result Delete(const std::string &path, const Headers &headers,
const Params &params);
Result Delete(const std::string &path, const Headers &headers,
const Params &params, Progress progress);
Result Options(const std::string &path);
Result Options(const std::string &path, const Headers &headers);
@ -9172,6 +9182,23 @@ inline Result ClientImpl::Delete(const std::string &path,
progress);
}
inline Result ClientImpl::Delete(const std::string &path, const Params &params) {
return Delete(path, Headers(), params);
}
inline Result ClientImpl::Delete(const std::string &path, const Headers &headers,
const Params &params) {
auto query = detail::params_to_query_str(params);
return Delete(path, headers, query, "application/x-www-form-urlencoded");
}
inline Result ClientImpl::Delete(const std::string &path, const Headers &headers,
const Params &params, Progress progress) {
auto query = detail::params_to_query_str(params);
return Delete(path, headers, query, "application/x-www-form-urlencoded",
progress);
}
inline Result ClientImpl::Options(const std::string &path) {
return Options(path, Headers());
}
@ -10627,6 +10654,17 @@ inline Result Client::Delete(const std::string &path, const Headers &headers,
Progress progress) {
return cli_->Delete(path, headers, body, content_type, progress);
}
inline Result Client::Delete(const std::string &path, const Params &params) {
return cli_->Delete(path, params);
}
inline Result Client::Delete(const std::string &path, const Headers &headers,
const Params &params) {
return cli_->Delete(path, headers, params);
}
inline Result Client::Delete(const std::string &path, const Headers &headers,
const Params &params, Progress progress) {
return cli_->Delete(path, headers, params, progress);
}
inline Result Client::Options(const std::string &path) {
return cli_->Options(path);
}

View File

@ -2850,6 +2850,20 @@ protected:
res.status = StatusCode::NotFound_404;
}
})
.Delete("/person",
[&](const Request &req, Response &res) {
if (req.has_param("name")) {
string name = req.get_param_value("name");
if (persons_.find(name) != persons_.end()) {
persons_.erase(name);
res.set_content("DELETED", "text/plain");
} else {
res.status = StatusCode::NotFound_404;
}
} else {
res.status = StatusCode::BadRequest_400;
}
})
.Post("/x-www-form-urlencoded-json",
[&](const Request &req, Response &res) {
auto json = req.get_param_value("json");
@ -3562,6 +3576,108 @@ TEST_F(ServerTest, PutMethod3) {
ASSERT_EQ("coder", res->body);
}
TEST_F(ServerTest, DeleteMethod1) {
auto res = cli_.Get("/person/john4");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::NotFound_404, res->status);
Params params;
params.emplace("name", "john4");
params.emplace("note", "coder");
res = cli_.Post("/person", params);
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
res = cli_.Get("/person/john4");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
ASSERT_EQ("coder", res->body);
Params delete_params;
delete_params.emplace("name", "john4");
res = cli_.Delete("/person", delete_params);
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
ASSERT_EQ("DELETED", res->body);
res = cli_.Get("/person/john4");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::NotFound_404, res->status);
}
TEST_F(ServerTest, DeleteMethod2) {
auto res = cli_.Get("/person/john5");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::NotFound_404, res->status);
Params params;
params.emplace("name", "john5");
params.emplace("note", "developer");
res = cli_.Post("/person", params);
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
res = cli_.Get("/person/john5");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
ASSERT_EQ("developer", res->body);
Params delete_params;
delete_params.emplace("name", "john5");
Headers headers;
headers.emplace("Custom-Header", "test-value");
res = cli_.Delete("/person", headers, delete_params);
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
ASSERT_EQ("DELETED", res->body);
res = cli_.Get("/person/john5");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::NotFound_404, res->status);
}
TEST_F(ServerTest, DeleteMethod3) {
auto res = cli_.Get("/person/john6");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::NotFound_404, res->status);
Params params;
params.emplace("name", "john6");
params.emplace("note", "tester");
res = cli_.Post("/person", params);
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
res = cli_.Get("/person/john6");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
ASSERT_EQ("tester", res->body);
Params delete_params;
delete_params.emplace("name", "john6");
Headers headers;
headers.emplace("Custom-Header", "test-value");
res = cli_.Delete("/person", headers, delete_params, nullptr);
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::OK_200, res->status);
ASSERT_EQ("DELETED", res->body);
res = cli_.Get("/person/john6");
ASSERT_TRUE(res);
ASSERT_EQ(StatusCode::NotFound_404, res->status);
}
TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
Params params;
params.emplace("json", JSON_DATA);