]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5451] Use PostHttpRequestJson for outbound messages.
authorMarcin Siodelski <marcin@isc.org>
Fri, 15 Dec 2017 17:43:22 +0000 (18:43 +0100)
committerMarcin Siodelski <marcin@isc.org>
Fri, 15 Dec 2017 17:43:22 +0000 (18:43 +0100)
src/lib/http/post_request_json.cc
src/lib/http/post_request_json.h
src/lib/http/request.cc
src/lib/http/request.h
src/lib/http/tests/post_request_json_unittests.cc
src/lib/http/tests/request_unittests.cc

index c871dbbca09126f0b6af519db86d03dabc7abad3..d7c401663f81250e0f2389cd73b02c2bf69a045e 100644 (file)
@@ -39,6 +39,17 @@ PostHttpRequestJson::getBodyAsJson() const {
     return (json_);
 }
 
+void
+PostHttpRequestJson::setBodyAsJson(const data::ConstElementPtr& body) {
+    if (body) {
+        context()->body_ = body->str();
+        json_ = body;
+
+    } else {
+        context()->body_.clear();
+    }
+}
+
 ConstElementPtr
 PostHttpRequestJson::getJsonElement(const std::string& element_name) const {
     try {
index a51b484cf1a4a0afa9c9b9acc7fb4943bf1edb96..973a4ac49baad8f1c5d0b32ddc9d0bbcb2419de4 100644 (file)
@@ -57,6 +57,16 @@ public:
     /// @throw HttpRequestJsonError if an error occurred.
     data::ConstElementPtr getBodyAsJson() const;
 
+    /// @brief Sets JSON body for an outbound message.
+    ///
+    /// Note that this method copies the pointer to the body, rather than
+    /// the entire data structure. Thus, the original object should not be
+    /// modified after this method is called. If the specified pointer is
+    /// null, the empty body is set.
+    ///
+    /// @param body JSON structure to be used as a body.
+    void setBodyAsJson(const data::ConstElementPtr& body);
+
     /// @brief Retrieves a single JSON element.
     ///
     /// The element must be at top level of the JSON structure.
index 1cc7a2db06033087d42c0701dfcc299903b53c8f..0352e2e8567ebed3202aa67474890ec649a2f74b 100644 (file)
@@ -214,7 +214,7 @@ HttpRequest::getBody() const {
 }
 
 std::string
-HttpRequest::toText() const {
+HttpRequest::toString() const {
     checkFinalized();
 
     std::ostringstream s;
index 5bbdcdbcbd2d3d46190202670c6e34eaaf989501..3824877880cd6f4ef3c5f0cea946c37ddfe2cb2a 100644 (file)
@@ -232,7 +232,7 @@ public:
     /// This method is called to generate the outbound HTTP message to be sent
     /// to a server. Make sure to call @c HttpRequest::finalize prior to
     /// calling this method.
-    virtual std::string toText() const;
+    virtual std::string toString() const;
 
     /// @brief Checks if the request has been successfully finalized.
     ///
index f33552176581c2684c6aa0ec4bc8203ef2bb2967..0b6374b85c6823a9b783c4ac97242d291544330e 100644 (file)
@@ -12,6 +12,7 @@
 #include <http/tests/request_test.h>
 #include <gtest/gtest.h>
 #include <map>
+#include <sstream>
 
 using namespace isc::data;
 using namespace isc::http;
@@ -43,7 +44,6 @@ public:
 
     /// @brief Default value of the JSON body.
     std::string json_body_;
-
 };
 
 // This test verifies that PostHttpRequestJson class only accepts
@@ -170,4 +170,25 @@ TEST_F(PostHttpRequestJsonTest, getJsonElement) {
     EXPECT_FALSE(request_.getJsonElement("bar"));
 }
 
+// This test verifies that it is possible to create client side request
+// containing JSON body.
+TEST_F(PostHttpRequestJsonTest, clientRequest) {
+    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
+    addHeaderToContext("Content-Type", "application/json");
+
+    ElementPtr json = Element::fromJSON(json_body_);
+    request_.setBodyAsJson(json);
+    // Commit and validate the data.
+    ASSERT_NO_THROW(request_.finalize());
+
+    std::ostringstream expected_response;
+    expected_response << "POST /isc/org HTTP/1.0\r\n"
+        "Content-Length: " << json->str().size() << "\r\n"
+        "Content-Type: application/json\r\n"
+        "\r\n"
+        << json->str();
+
+    EXPECT_EQ(expected_response.str(), request_.toString());
+}
+
 }
index 9d1e5411df89e4a31a99fe69073d406a7c8b5492..22b69a189f1c77d11a04816fdb5330016920b835 100644 (file)
@@ -253,7 +253,7 @@ TEST_F(HttpRequestTest, clientRequest) {
               "Date: " + date_time.rfc1123Format() + "\r\n"
               "\r\n"
               "<html></html>",
-              request_.toText());
+              request_.toString());
 }
 
 TEST_F(HttpRequestTest, clientRequestNoBody) {
@@ -268,7 +268,7 @@ TEST_F(HttpRequestTest, clientRequestNoBody) {
     EXPECT_EQ("GET /isc/org HTTP/1.1\r\n"
               "Content-Type: text/html\r\n"
               "\r\n",
-              request_.toText());
+              request_.toString());
 }
 
 }