Browse Source

Added client methods with shared pointer to Response.

yhirose 13 years ago
parent
commit
3c8c835489
4 changed files with 72 additions and 6 deletions
  1. 17 0
      README.md
  2. 5 5
      example/client.cc
  3. 18 1
      httplib.h
  4. 32 0
      test/test.cc

+ 17 - 0
README.md

@@ -24,4 +24,21 @@ Inspired by [Sinatra](http://www.sinatrarb.com/)
         svr.run();
     }
 
+Client Example
+--------------
+
+    #include <httplib.h>
+    #include <iostream>
+    using namespace httplib;
+
+    int main(void)
+    {
+        Client cli("localhost", 1234);
+
+        auto res = cli.get("/hi");
+        if (res && res->status == 200) {
+            std::cout << res->body << std::endl;
+        }
+    }
+
 Copyright (c) 2012 Yuji Hirose. All rights reserved.

+ 5 - 5
example/client.cc

@@ -17,11 +17,11 @@ int main(void)
 
     Client cli("localhost", 8080);
 
-    Response res;
-    if (cli.get(hi, res)) {
-        cout << res.status << endl;
-        cout << res.get_header_value("Content-Type") << endl;
-        cout << res.body << endl;
+    auto res = cli.get(hi);
+    if (res) {
+        cout << res->status << endl;
+        cout << res->get_header_value("Content-Type") << endl;
+        cout << res->body << endl;
     }
 
     return 0;

+ 18 - 1
httplib.h

@@ -126,6 +126,9 @@ public:
     bool post(const char* url, const std::string& body, const char* content_type, Response& res);
     bool send(const Request& req, Response& res);
 
+    std::shared_ptr<Response> get(const char* url);
+    std::shared_ptr<Response> post(const char* url, const std::string& body, const char* content_type);
+
 private:
     bool read_response_line(FILE* fp, Response& res);
 
@@ -634,7 +637,8 @@ inline bool Client::get(const char* url, Response& res)
     return send(req, res);
 }
 
-inline bool Client::post(const char* url, const std::string& body, const char* content_type, Response& res)
+inline bool Client::post(
+    const char* url, const std::string& body, const char* content_type, Response& res)
 {
     Request req;
     req.method = "POST";
@@ -670,6 +674,19 @@ inline bool Client::send(const Request& req, Response& res)
     return true;
 }
 
+inline std::shared_ptr<Response> Client::get(const char* url)
+{
+    auto res = std::make_shared<Response>();
+    return get(url, *res) ? res : nullptr;
+}
+
+inline std::shared_ptr<Response> Client::post(
+    const char* url, const std::string& body, const char* content_type)
+{
+    auto res = std::make_shared<Response>();
+    return post(url, body, content_type, *res) ? res : nullptr;
+}
+
 } // namespace httplib
 
 #endif

+ 32 - 0
test/test.cc

@@ -187,4 +187,36 @@ TEST_F(ServerTest, PostMethod)
     }
 }
 
+TEST_F(ServerTest, GetMethod200Shared)
+{
+    auto res = Client(host_, port_).get("/hi");
+    ASSERT_TRUE(res != nullptr);
+    EXPECT_EQ(200, res->status);
+    EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
+    EXPECT_EQ("Hello World!", res->body);
+}
+
+TEST_F(ServerTest, PostMethodShared)
+{
+    {
+        auto res = Client(host_, port_).get("/person/john3");
+        ASSERT_TRUE(res != nullptr);
+        ASSERT_EQ(404, res->status);
+    }
+    {
+        auto content = "name=john3&note=coder";
+        auto content_type = "application/x-www-form-urlencoded";
+        auto res = Client(host_, port_).post("/person", content, content_type);
+        ASSERT_TRUE(res != nullptr);
+        ASSERT_EQ(200, res->status);
+    }
+    {
+        auto res = Client(host_, port_).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);
+    }
+}
+
 // vim: et ts=4 sw=4 cin cino={1s ff=unix