Skip to content

Commit b2bf172

Browse files
committed
Fix #1551
1 parent 1729aa8 commit b2bf172

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

httplib.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,11 @@ class ClientImpl {
14501450
Result Delete(const std::string &path, const Headers &headers,
14511451
const std::string &body, const std::string &content_type,
14521452
Progress progress);
1453+
Result Delete(const std::string &path, const Params &params);
1454+
Result Delete(const std::string &path, const Headers &headers,
1455+
const Params &params);
1456+
Result Delete(const std::string &path, const Headers &headers,
1457+
const Params &params, Progress progress);
14531458

14541459
Result Options(const std::string &path);
14551460
Result Options(const std::string &path, const Headers &headers);
@@ -1894,6 +1899,11 @@ class Client {
18941899
Result Delete(const std::string &path, const Headers &headers,
18951900
const std::string &body, const std::string &content_type,
18961901
Progress progress);
1902+
Result Delete(const std::string &path, const Params &params);
1903+
Result Delete(const std::string &path, const Headers &headers,
1904+
const Params &params);
1905+
Result Delete(const std::string &path, const Headers &headers,
1906+
const Params &params, Progress progress);
18971907

18981908
Result Options(const std::string &path);
18991909
Result Options(const std::string &path, const Headers &headers);
@@ -9172,6 +9182,23 @@ inline Result ClientImpl::Delete(const std::string &path,
91729182
progress);
91739183
}
91749184

9185+
inline Result ClientImpl::Delete(const std::string &path, const Params &params) {
9186+
return Delete(path, Headers(), params);
9187+
}
9188+
9189+
inline Result ClientImpl::Delete(const std::string &path, const Headers &headers,
9190+
const Params &params) {
9191+
auto query = detail::params_to_query_str(params);
9192+
return Delete(path, headers, query, "application/x-www-form-urlencoded");
9193+
}
9194+
9195+
inline Result ClientImpl::Delete(const std::string &path, const Headers &headers,
9196+
const Params &params, Progress progress) {
9197+
auto query = detail::params_to_query_str(params);
9198+
return Delete(path, headers, query, "application/x-www-form-urlencoded",
9199+
progress);
9200+
}
9201+
91759202
inline Result ClientImpl::Options(const std::string &path) {
91769203
return Options(path, Headers());
91779204
}
@@ -10627,6 +10654,17 @@ inline Result Client::Delete(const std::string &path, const Headers &headers,
1062710654
Progress progress) {
1062810655
return cli_->Delete(path, headers, body, content_type, progress);
1062910656
}
10657+
inline Result Client::Delete(const std::string &path, const Params &params) {
10658+
return cli_->Delete(path, params);
10659+
}
10660+
inline Result Client::Delete(const std::string &path, const Headers &headers,
10661+
const Params &params) {
10662+
return cli_->Delete(path, headers, params);
10663+
}
10664+
inline Result Client::Delete(const std::string &path, const Headers &headers,
10665+
const Params &params, Progress progress) {
10666+
return cli_->Delete(path, headers, params, progress);
10667+
}
1063010668
inline Result Client::Options(const std::string &path) {
1063110669
return cli_->Options(path);
1063210670
}

test/test.cc

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,20 @@ class ServerTest : public ::testing::Test {
28502850
res.status = StatusCode::NotFound_404;
28512851
}
28522852
})
2853+
.Delete("/person",
2854+
[&](const Request &req, Response &res) {
2855+
if (req.has_param("name")) {
2856+
string name = req.get_param_value("name");
2857+
if (persons_.find(name) != persons_.end()) {
2858+
persons_.erase(name);
2859+
res.set_content("DELETED", "text/plain");
2860+
} else {
2861+
res.status = StatusCode::NotFound_404;
2862+
}
2863+
} else {
2864+
res.status = StatusCode::BadRequest_400;
2865+
}
2866+
})
28532867
.Post("/x-www-form-urlencoded-json",
28542868
[&](const Request &req, Response &res) {
28552869
auto json = req.get_param_value("json");
@@ -3562,6 +3576,108 @@ TEST_F(ServerTest, PutMethod3) {
35623576
ASSERT_EQ("coder", res->body);
35633577
}
35643578

3579+
TEST_F(ServerTest, DeleteMethod1) {
3580+
auto res = cli_.Get("/person/john4");
3581+
ASSERT_TRUE(res);
3582+
ASSERT_EQ(StatusCode::NotFound_404, res->status);
3583+
3584+
Params params;
3585+
params.emplace("name", "john4");
3586+
params.emplace("note", "coder");
3587+
3588+
res = cli_.Post("/person", params);
3589+
ASSERT_TRUE(res);
3590+
ASSERT_EQ(StatusCode::OK_200, res->status);
3591+
3592+
res = cli_.Get("/person/john4");
3593+
ASSERT_TRUE(res);
3594+
ASSERT_EQ(StatusCode::OK_200, res->status);
3595+
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
3596+
ASSERT_EQ("coder", res->body);
3597+
3598+
Params delete_params;
3599+
delete_params.emplace("name", "john4");
3600+
3601+
res = cli_.Delete("/person", delete_params);
3602+
ASSERT_TRUE(res);
3603+
ASSERT_EQ(StatusCode::OK_200, res->status);
3604+
ASSERT_EQ("DELETED", res->body);
3605+
3606+
res = cli_.Get("/person/john4");
3607+
ASSERT_TRUE(res);
3608+
ASSERT_EQ(StatusCode::NotFound_404, res->status);
3609+
}
3610+
3611+
TEST_F(ServerTest, DeleteMethod2) {
3612+
auto res = cli_.Get("/person/john5");
3613+
ASSERT_TRUE(res);
3614+
ASSERT_EQ(StatusCode::NotFound_404, res->status);
3615+
3616+
Params params;
3617+
params.emplace("name", "john5");
3618+
params.emplace("note", "developer");
3619+
3620+
res = cli_.Post("/person", params);
3621+
ASSERT_TRUE(res);
3622+
ASSERT_EQ(StatusCode::OK_200, res->status);
3623+
3624+
res = cli_.Get("/person/john5");
3625+
ASSERT_TRUE(res);
3626+
ASSERT_EQ(StatusCode::OK_200, res->status);
3627+
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
3628+
ASSERT_EQ("developer", res->body);
3629+
3630+
Params delete_params;
3631+
delete_params.emplace("name", "john5");
3632+
3633+
Headers headers;
3634+
headers.emplace("Custom-Header", "test-value");
3635+
3636+
res = cli_.Delete("/person", headers, delete_params);
3637+
ASSERT_TRUE(res);
3638+
ASSERT_EQ(StatusCode::OK_200, res->status);
3639+
ASSERT_EQ("DELETED", res->body);
3640+
3641+
res = cli_.Get("/person/john5");
3642+
ASSERT_TRUE(res);
3643+
ASSERT_EQ(StatusCode::NotFound_404, res->status);
3644+
}
3645+
3646+
TEST_F(ServerTest, DeleteMethod3) {
3647+
auto res = cli_.Get("/person/john6");
3648+
ASSERT_TRUE(res);
3649+
ASSERT_EQ(StatusCode::NotFound_404, res->status);
3650+
3651+
Params params;
3652+
params.emplace("name", "john6");
3653+
params.emplace("note", "tester");
3654+
3655+
res = cli_.Post("/person", params);
3656+
ASSERT_TRUE(res);
3657+
ASSERT_EQ(StatusCode::OK_200, res->status);
3658+
3659+
res = cli_.Get("/person/john6");
3660+
ASSERT_TRUE(res);
3661+
ASSERT_EQ(StatusCode::OK_200, res->status);
3662+
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
3663+
ASSERT_EQ("tester", res->body);
3664+
3665+
Params delete_params;
3666+
delete_params.emplace("name", "john6");
3667+
3668+
Headers headers;
3669+
headers.emplace("Custom-Header", "test-value");
3670+
3671+
res = cli_.Delete("/person", headers, delete_params, nullptr);
3672+
ASSERT_TRUE(res);
3673+
ASSERT_EQ(StatusCode::OK_200, res->status);
3674+
ASSERT_EQ("DELETED", res->body);
3675+
3676+
res = cli_.Get("/person/john6");
3677+
ASSERT_TRUE(res);
3678+
ASSERT_EQ(StatusCode::NotFound_404, res->status);
3679+
}
3680+
35653681
TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
35663682
Params params;
35673683
params.emplace("json", JSON_DATA);

0 commit comments

Comments
 (0)