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 {
/// @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.
}
std::string
-HttpRequest::toText() const {
+HttpRequest::toString() const {
checkFinalized();
std::ostringstream s;
/// 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.
///
#include <http/tests/request_test.h>
#include <gtest/gtest.h>
#include <map>
+#include <sstream>
using namespace isc::data;
using namespace isc::http;
/// @brief Default value of the JSON body.
std::string json_body_;
-
};
// This test verifies that PostHttpRequestJson class only accepts
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());
+}
+
}
"Date: " + date_time.rfc1123Format() + "\r\n"
"\r\n"
"<html></html>",
- request_.toText());
+ request_.toString());
}
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());
}
}