mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 14:29:10 -07:00
Fix #330
This commit is contained in:
parent
0d81e20129
commit
6b4df41b30
2 changed files with 52 additions and 0 deletions
23
httplib.h
23
httplib.h
|
@ -631,6 +631,11 @@ public:
|
|||
ContentProvider content_provider,
|
||||
const char *content_type);
|
||||
|
||||
std::shared_ptr<Response> Put(const char *path, const Params ¶ms);
|
||||
|
||||
std::shared_ptr<Response> Put(const char *path, const Headers &headers,
|
||||
const Params ¶ms);
|
||||
|
||||
std::shared_ptr<Response> Patch(const char *path, const std::string &body,
|
||||
const char *content_type);
|
||||
|
||||
|
@ -4087,6 +4092,24 @@ Client::Put(const char *path, const Headers &headers, size_t content_length,
|
|||
content_type);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<Response> Client::Put(const char *path,
|
||||
const Params ¶ms) {
|
||||
return Put(path, Headers(), params);
|
||||
}
|
||||
|
||||
inline std::shared_ptr<Response>
|
||||
Client::Put(const char *path, const Headers &headers, const Params ¶ms) {
|
||||
std::string query;
|
||||
for (auto it = params.begin(); it != params.end(); ++it) {
|
||||
if (it != params.begin()) { query += "&"; }
|
||||
query += it->first;
|
||||
query += "=";
|
||||
query += detail::encode_url(it->second);
|
||||
}
|
||||
|
||||
return Put(path, headers, query, "application/x-www-form-urlencoded");
|
||||
}
|
||||
|
||||
inline std::shared_ptr<Response> Client::Patch(const char *path,
|
||||
const std::string &body,
|
||||
const char *content_type) {
|
||||
|
|
29
test/test.cc
29
test/test.cc
|
@ -694,6 +694,15 @@ protected:
|
|||
res.status = 400;
|
||||
}
|
||||
})
|
||||
.Put("/person",
|
||||
[&](const Request &req, Response &res) {
|
||||
if (req.has_param("name") && req.has_param("note")) {
|
||||
persons_[req.get_param_value("name")] =
|
||||
req.get_param_value("note");
|
||||
} else {
|
||||
res.status = 400;
|
||||
}
|
||||
})
|
||||
.Get("/person/(.*)",
|
||||
[&](const Request &req, Response &res) {
|
||||
string name = req.matches[1];
|
||||
|
@ -1089,6 +1098,26 @@ TEST_F(ServerTest, PostMethod2) {
|
|||
ASSERT_EQ("coder", res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, PutMethod3) {
|
||||
auto res = cli_.Get("/person/john3");
|
||||
ASSERT_TRUE(res != nullptr);
|
||||
ASSERT_EQ(404, res->status);
|
||||
|
||||
Params params;
|
||||
params.emplace("name", "john3");
|
||||
params.emplace("note", "coder");
|
||||
|
||||
res = cli_.Put("/person", params);
|
||||
ASSERT_TRUE(res != nullptr);
|
||||
ASSERT_EQ(200, res->status);
|
||||
|
||||
res = cli_.Get("/person/john3");
|
||||
ASSERT_TRUE(res != nullptr);
|
||||
ASSERT_EQ(200, res->status);
|
||||
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
|
||||
ASSERT_EQ("coder", res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
|
||||
Params params;
|
||||
params.emplace("json", JSON_DATA);
|
||||
|
|
Loading…
Reference in a new issue