diff --git a/httplib.h b/httplib.h index 63718f5..ae815c2 100644 --- a/httplib.h +++ b/httplib.h @@ -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 ¶ms); + Result Delete(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Delete(const std::string &path, const Headers &headers, + const Params ¶ms, 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 ¶ms); + Result Delete(const std::string &path, const Headers &headers, + const Params ¶ms); + Result Delete(const std::string &path, const Headers &headers, + const Params ¶ms, 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 ¶ms) { + return Delete(path, Headers(), params); +} + +inline Result ClientImpl::Delete(const std::string &path, const Headers &headers, + const Params ¶ms) { + 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 ¶ms, 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 ¶ms) { + return cli_->Delete(path, params); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const Params ¶ms) { + return cli_->Delete(path, headers, params); +} +inline Result Client::Delete(const std::string &path, const Headers &headers, + const Params ¶ms, Progress progress) { + return cli_->Delete(path, headers, params, progress); +} inline Result Client::Options(const std::string &path) { return cli_->Options(path); } diff --git a/test/test.cc b/test/test.cc index 347b90e..08b786d 100644 --- a/test/test.cc +++ b/test/test.cc @@ -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);