Add const_body, mutable_body to examples

This commit is contained in:
octopus-prime authored and Vinnie Falco committed 2017-07-20 08:15:29 -07:00
1 parent 2d6859831c
commit b5b1295d0c
8 files changed
+347

No files matched your search

+2
View File
@@ -10,6 +10,8 @@ add_executable (common-test
${COMMON_INCLUDES}
detect_ssl.cpp
file_body.cpp
const_body.cpp
mutable_body.cpp
mime_types.cpp
rfc7231.cpp
ssl_stream.cpp
+10
View File
@@ -0,0 +1,10 @@
//
// Copyright (c) 2017 Mike Gresens (mike dot gresens at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Test that header file is self-contained.
#include "../../example/common/const_body.hpp"
+10
View File
@@ -0,0 +1,10 @@
//
// Copyright (c) 2017 Mike Gresens (mike dot gresens at gmail dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Test that header file is self-contained.
#include "../../example/common/mutable_body.hpp"
+78
View File
@@ -7,6 +7,8 @@
#include "example/doc/http_examples.hpp"
#include "example/common/file_body.hpp"
#include "example/common/const_body.hpp"
#include "example/common/mutable_body.hpp"
#include <beast/core/read_size.hpp>
#include <beast/core/detail/clamp.hpp>
@@ -16,10 +18,37 @@
#include <beast/test/yield_to.hpp>
#include <beast/unit_test/suite.hpp>
#include <sstream>
#include <array>
#include <vector>
#include <list>
namespace beast {
namespace http {
struct Thing {
char value;
};
BOOST_STATIC_ASSERT(::detail::is_const_character<char>::value == true);
BOOST_STATIC_ASSERT(::detail::is_const_character<unsigned char>::value == true);
BOOST_STATIC_ASSERT(::detail::is_const_character<char32_t>::value == false);
BOOST_STATIC_ASSERT(::detail::is_const_character<Thing>::value == false);
BOOST_STATIC_ASSERT(::detail::is_const_container<std::string>::value == true);
BOOST_STATIC_ASSERT(::detail::is_const_container<string_view>::value == true);
BOOST_STATIC_ASSERT(::detail::is_const_container<std::vector<char>>::value == true);
BOOST_STATIC_ASSERT(::detail::is_const_container<std::list<char>>::value == false);
BOOST_STATIC_ASSERT(::detail::is_mutable_character<char>::value == true);
BOOST_STATIC_ASSERT(::detail::is_mutable_character<unsigned char>::value == true);
BOOST_STATIC_ASSERT(::detail::is_mutable_character<char32_t>::value == false);
BOOST_STATIC_ASSERT(::detail::is_mutable_character<Thing>::value == false);
BOOST_STATIC_ASSERT(::detail::is_mutable_container<std::string>::value == true);
BOOST_STATIC_ASSERT(::detail::is_mutable_container<string_view>::value == false);
BOOST_STATIC_ASSERT(::detail::is_mutable_container<std::vector<char>>::value == true);
BOOST_STATIC_ASSERT(::detail::is_mutable_container<std::list<char>>::value == false);
class doc_http_samples_test
: public beast::unit_test::suite
, public beast::test::enable_yield_to
@@ -307,6 +336,54 @@ public:
BEAST_EXPECTS(! ec, ec.message());
}
void
doConstAndMutableBody()
{
test::pipe c{ios_};
// people using std::array need to be careful
// because the entire array will be written out
// no matter how big the string is!
std::array<char, 15> const body {{"Hello, world!\n"}};
{
request<const_body<std::array<char, 15>>> req;
req.version = 11;
req.method(verb::put);
req.target("/");
req.body = body;
req.prepare_payload();
write(c.client, req);
}
{
flat_buffer b;
request_parser<empty_body> p0;
read_header(c.server, b, p0);
BEAST_EXPECTS(p0.get().method() == verb::put,
p0.get().method_string());
{
request_parser<mutable_body<std::vector<char>>> p{std::move(p0)};
p.get().body = std::vector<char>(body.begin(), body.end());
read(c.server, b, p);
}
}
{
response<const_body<std::array<char, 15>>> res;
res.version = 11;
res.result(status::ok);
res.insert(field::server, "test");
res.body = body;
res.prepare_payload();
write(c.server, res);
}
{
flat_buffer b;
response<mutable_body<std::vector<char>>> res;
read(c.client, b, res);
BEAST_EXPECTS(res.body == std::vector<char>(body.begin(), body.end()),
std::string(body.begin(), body.end()));
}
}
//--------------------------------------------------------------------------
void
@@ -321,6 +398,7 @@ public:
doHEAD();
doDeferredBody();
doFileBody();
doConstAndMutableBody();
}
};