yhirose 5 years ago
parent
commit
ad9fd3bd93
2 changed files with 44 additions and 2 deletions
  1. 14 2
      httplib.h
  2. 30 0
      test/test.cc

+ 14 - 2
httplib.h

@@ -604,6 +604,8 @@ public:
 
   std::shared_ptr<Response> Head(const char *path, const Headers &headers);
 
+  std::shared_ptr<Response> Post(const char *path);
+
   std::shared_ptr<Response> Post(const char *path, const std::string &body,
                                  const char *content_type);
 
@@ -631,6 +633,8 @@ public:
   std::shared_ptr<Response> Post(const char *path, const Headers &headers,
                                  const MultipartFormDataItems &items);
 
+  std::shared_ptr<Response> Put(const char *path);
+
   std::shared_ptr<Response> Put(const char *path, const std::string &body,
                                 const char *content_type);
 
@@ -831,7 +835,7 @@ inline void Post(std::vector<Request> &requests, const char *path,
   req.method = "POST";
   req.path = path;
   req.headers = headers;
-  req.headers.emplace("Content-Type", content_type);
+  if (content_type) { req.headers.emplace("Content-Type", content_type); }
   req.body = body;
   requests.emplace_back(std::move(req));
 }
@@ -4030,7 +4034,7 @@ inline std::shared_ptr<Response> Client::send_with_content_provider(
   req.headers = headers;
   req.path = path;
 
-  req.headers.emplace("Content-Type", content_type);
+  if (content_type) { req.headers.emplace("Content-Type", content_type); }
 
 #ifdef CPPHTTPLIB_ZLIB_SUPPORT
   if (compress_) {
@@ -4222,6 +4226,10 @@ inline std::shared_ptr<Response> Client::Head(const char *path,
   return send(req, *res) ? res : nullptr;
 }
 
+inline std::shared_ptr<Response> Client::Post(const char *path) {
+  return Post(path, std::string(), nullptr);
+}
+
 inline std::shared_ptr<Response> Client::Post(const char *path,
                                               const std::string &body,
                                               const char *content_type) {
@@ -4294,6 +4302,10 @@ Client::Post(const char *path, const Headers &headers,
   return Post(path, headers, body, content_type.c_str());
 }
 
+inline std::shared_ptr<Response> Client::Put(const char *path) {
+  return Put(path, std::string(), nullptr);
+}
+
 inline std::shared_ptr<Response> Client::Put(const char *path,
                                              const std::string &body,
                                              const char *content_type) {

+ 30 - 0
test/test.cc

@@ -964,8 +964,24 @@ protected:
         .Post("/empty",
               [&](const Request &req, Response &res) {
                 EXPECT_EQ(req.body, "");
+                EXPECT_EQ("text/plain", req.get_header_value("Content-Type"));
+                EXPECT_EQ("0", req.get_header_value("Content-Length"));
                 res.set_content("empty", "text/plain");
               })
+        .Post("/empty-no-content-type",
+              [&](const Request &req, Response &res) {
+                EXPECT_EQ(req.body, "");
+                EXPECT_FALSE(req.has_header("Content-Type"));
+                EXPECT_EQ("0", req.get_header_value("Content-Length"));
+                res.set_content("empty-no-content-type", "text/plain");
+              })
+        .Put("/empty-no-content-type",
+              [&](const Request &req, Response &res) {
+                EXPECT_EQ(req.body, "");
+                EXPECT_FALSE(req.has_header("Content-Type"));
+                EXPECT_EQ("0", req.get_header_value("Content-Length"));
+                res.set_content("empty-no-content-type", "text/plain");
+              })
         .Put("/put",
              [&](const Request &req, Response &res) {
                EXPECT_EQ(req.body, "PUT");
@@ -1310,6 +1326,20 @@ TEST_F(ServerTest, PostEmptyContent) {
   ASSERT_EQ("empty", res->body);
 }
 
+TEST_F(ServerTest, PostEmptyContentWithNoContentType) {
+  auto res = cli_.Post("/empty-no-content-type");
+  ASSERT_TRUE(res != nullptr);
+  ASSERT_EQ(200, res->status);
+  ASSERT_EQ("empty-no-content-type", res->body);
+}
+
+TEST_F(ServerTest, PutEmptyContentWithNoContentType) {
+  auto res = cli_.Put("/empty-no-content-type");
+  ASSERT_TRUE(res != nullptr);
+  ASSERT_EQ(200, res->status);
+  ASSERT_EQ("empty-no-content-type", res->body);
+}
+
 TEST_F(ServerTest, GetMethodDir) {
   auto res = cli_.Get("/dir/");
   ASSERT_TRUE(res != nullptr);