Files
boost_beast/test/beast/http/span_body.cpp
T
Damian Jarek b76ab4450a New BodyReader and BodyWriter constructors (API Change):
fix #884

* BodyReader and BodyWriter constructors now require the header and
  body elements to be passed as distinct header and value_type objects.

This enables the composition of body types, for example:

    http::response<compressed_body<http::string_body>> res;

* The previous single-argument constructors are deprecated and will be
  removed in a subsequent version.

Actions Required:

* Change user-defined instances of BodyReader or BodyWriter constructor
  signatures to the two-argument form.

OR

* Define the macro BOOST_BEAST_ALLOW_DEPRECATED in the project (which
  will accept both the new and the deprecated signatures).
2017-12-15 09:27:39 -08:00

81 lines
2.2 KiB
C++

//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco 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)
//
// Official repository: https://github.com/boostorg/beast
//
// Test that header file is self-contained.
#include <boost/beast/http/span_body.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/beast/unit_test/suite.hpp>
namespace boost {
namespace beast {
namespace http {
struct span_body_test
: public beast::unit_test::suite
{
void
testSpanBody()
{
{
using B = span_body<char const>;
request<B> req;
BEAST_EXPECT(req.body().size() == 0);
BEAST_EXPECT(B::size(req.body()) == 0);
req.body() = B::value_type("xyz", 3);
BEAST_EXPECT(req.body().size() == 3);
BEAST_EXPECT(B::size(req.body()) == 3);
B::writer r{req, req.body()};
error_code ec;
r.init(ec);
BEAST_EXPECTS(! ec, ec.message());
auto const buf = r.get(ec);
BEAST_EXPECTS(! ec, ec.message());
if(! BEAST_EXPECT(buf != boost::none))
return;
BEAST_EXPECT(boost::asio::buffer_size(buf->first) == 3);
BEAST_EXPECT(! buf->second);
}
{
char buf[5];
using B = span_body<char>;
request<B> req;
req.body() = span<char>{buf, sizeof(buf)};
B::reader w{req, req.body()};
error_code ec;
w.init(boost::none, ec);
BEAST_EXPECTS(! ec, ec.message());
w.put(boost::asio::const_buffer{
"123", 3}, ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(buf[0] == '1');
BEAST_EXPECT(buf[1] == '2');
BEAST_EXPECT(buf[2] == '3');
w.put(boost::asio::const_buffer{
"456", 3}, ec);
BEAST_EXPECTS(ec == error::buffer_overflow, ec.message());
}
}
void
run() override
{
testSpanBody();
}
};
BEAST_DEFINE_TESTSUITE(beast,http,span_body);
} // http
} // beast
} // boost