Files
boost_beast/test/beast/http/span_body.cpp

82 lines
2.2 KiB
C++
Raw Permalink Normal View History

2017-07-08 18:47:33 -07:00
//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-08 18:47:33 -07:00
//
// 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)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2017-07-08 18:47:33 -07:00
// Test that header file is self-contained.
2017-07-20 13:40:34 -07:00
#include <boost/beast/http/span_body.hpp>
2017-07-08 18:47:33 -07:00
2019-03-05 08:25:54 -08:00
#include <boost/beast/core/buffer_traits.hpp>
2017-07-20 13:40:34 -07:00
#include <boost/beast/http/message.hpp>
2018-11-11 14:07:55 -08:00
#include <boost/beast/_experimental/unit_test/suite.hpp>
2017-07-08 18:47:33 -07:00
2017-07-20 13:40:34 -07:00
namespace boost {
2017-07-08 18:47:33 -07:00
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);
2017-07-08 18:47:33 -07:00
req.body() = B::value_type("xyz", 3);
BEAST_EXPECT(req.body().size() == 3);
BEAST_EXPECT(B::size(req.body()) == 3);
2017-07-08 18:47:33 -07:00
B::writer r{req, req.body()};
2017-07-08 18:47:33 -07:00
error_code ec;
r.init(ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
2017-07-08 18:47:33 -07:00
auto const buf = r.get(ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
if(! BEAST_EXPECT(buf != boost::none))
2017-07-08 18:47:33 -07:00
return;
2019-03-05 08:47:02 -08:00
BEAST_EXPECT(buffer_bytes(buf->first) == 3);
2017-07-25 12:35:54 -07:00
BEAST_EXPECT(! buf->second);
2017-07-08 18:47:33 -07:00
}
{
2019-02-05 08:24:26 -08:00
char buf[5] = {};
2017-07-08 18:47:33 -07:00
using B = span_body<char>;
request<B> req;
req.body() = span<char>{buf, sizeof(buf)};
B::reader w{req, req.body()};
2017-07-08 18:47:33 -07:00
error_code ec;
w.init(boost::none, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
w.put(net::const_buffer{
2017-07-08 18:47:33 -07:00
"123", 3}, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(buf[0] == '1');
BEAST_EXPECT(buf[1] == '2');
BEAST_EXPECT(buf[2] == '3');
w.put(net::const_buffer{
2017-07-08 18:47:33 -07:00
"456", 3}, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(ec == error::buffer_overflow, ec.message());
2017-07-08 18:47:33 -07:00
}
}
void
run() override
{
testSpanBody();
}
};
2017-08-01 17:01:57 -07:00
BEAST_DEFINE_TESTSUITE(beast,http,span_body);
2017-07-08 18:47:33 -07:00
} // http
} // beast
2017-07-20 13:40:34 -07:00
} // boost