Commit 78c52317 authored by Marcin Siodelski's avatar Marcin Siodelski

[5451] Use PostHttpRequestJson for outbound messages.

parent 166ef01e
......@@ -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 {
......
......@@ -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.
......
......@@ -214,7 +214,7 @@ HttpRequest::getBody() const {
}
std::string
HttpRequest::toText() const {
HttpRequest::toString() const {
checkFinalized();
std::ostringstream s;
......
......@@ -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.
///
......
......@@ -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());
}
}
......@@ -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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment