From: Marcin Siodelski Date: Fri, 15 Dec 2017 17:43:22 +0000 (+0100) Subject: [5451] Use PostHttpRequestJson for outbound messages. X-Git-Tag: trac5457_base~4^2~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=78c5231702bad54c8625366f28a0a80e3f6759e1;p=thirdparty%2Fkea.git [5451] Use PostHttpRequestJson for outbound messages. --- diff --git a/src/lib/http/post_request_json.cc b/src/lib/http/post_request_json.cc index c871dbbca0..d7c401663f 100644 --- a/src/lib/http/post_request_json.cc +++ b/src/lib/http/post_request_json.cc @@ -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 { diff --git a/src/lib/http/post_request_json.h b/src/lib/http/post_request_json.h index a51b484cf1..973a4ac49b 100644 --- a/src/lib/http/post_request_json.h +++ b/src/lib/http/post_request_json.h @@ -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. diff --git a/src/lib/http/request.cc b/src/lib/http/request.cc index 1cc7a2db06..0352e2e856 100644 --- a/src/lib/http/request.cc +++ b/src/lib/http/request.cc @@ -214,7 +214,7 @@ HttpRequest::getBody() const { } std::string -HttpRequest::toText() const { +HttpRequest::toString() const { checkFinalized(); std::ostringstream s; diff --git a/src/lib/http/request.h b/src/lib/http/request.h index 5bbdcdbcbd..3824877880 100644 --- a/src/lib/http/request.h +++ b/src/lib/http/request.h @@ -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. /// diff --git a/src/lib/http/tests/post_request_json_unittests.cc b/src/lib/http/tests/post_request_json_unittests.cc index f335521765..0b6374b85c 100644 --- a/src/lib/http/tests/post_request_json_unittests.cc +++ b/src/lib/http/tests/post_request_json_unittests.cc @@ -12,6 +12,7 @@ #include #include #include +#include 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()); +} + } diff --git a/src/lib/http/tests/request_unittests.cc b/src/lib/http/tests/request_unittests.cc index 9d1e5411df..22b69a189f 100644 --- a/src/lib/http/tests/request_unittests.cc +++ b/src/lib/http/tests/request_unittests.cc @@ -253,7 +253,7 @@ TEST_F(HttpRequestTest, clientRequest) { "Date: " + date_time.rfc1123Format() + "\r\n" "\r\n" "", - 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()); } }