2018-06-08 12:14:29 -07:00
|
|
|
//
|
2019-02-21 07:00:31 -08:00
|
|
|
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2018-06-08 12:14:29 -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)
|
|
|
|
|
//
|
|
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Test that header file is self-contained.
|
2018-11-11 14:07:55 -08:00
|
|
|
#include <boost/beast/_experimental/http/icy_stream.hpp>
|
2018-06-08 12:14:29 -07:00
|
|
|
|
2018-12-14 11:13:26 -08:00
|
|
|
#include <boost/beast/core/buffers_adaptor.hpp>
|
2018-06-08 12:14:29 -07:00
|
|
|
#include <boost/beast/core/buffers_to_string.hpp>
|
|
|
|
|
#include <boost/beast/core/read_size.hpp>
|
2018-11-11 14:07:55 -08:00
|
|
|
#include <boost/beast/_experimental/test/stream.hpp>
|
|
|
|
|
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
2018-06-08 12:14:29 -07:00
|
|
|
#include <array>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
|
namespace beast {
|
|
|
|
|
namespace http {
|
|
|
|
|
|
|
|
|
|
class icy_stream_test
|
|
|
|
|
: public unit_test::suite
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
void
|
|
|
|
|
doMatrix(string_view in, string_view out)
|
|
|
|
|
{
|
2018-11-30 14:58:38 -08:00
|
|
|
using net::mutable_buffer;
|
|
|
|
|
net::io_context ioc;
|
2018-06-08 12:14:29 -07:00
|
|
|
auto len = out.size() + 8;
|
|
|
|
|
std::unique_ptr<char[]> p(new char[len]);
|
|
|
|
|
for(std::size_t i = 1; i < len; ++i)
|
|
|
|
|
{
|
2018-07-03 10:16:35 -07:00
|
|
|
std::array<mutable_buffer, 2> mbs{{
|
2018-06-08 12:14:29 -07:00
|
|
|
mutable_buffer(p.get(), i),
|
2018-07-03 10:16:35 -07:00
|
|
|
mutable_buffer(p.get() + i, len - i)}};
|
2018-06-08 12:14:29 -07:00
|
|
|
for(std::size_t j = 1; j < in.size(); ++j)
|
|
|
|
|
{
|
|
|
|
|
for(std::size_t k = 1; k < len; ++k)
|
|
|
|
|
{
|
|
|
|
|
// sync
|
|
|
|
|
{
|
2018-12-14 11:13:26 -08:00
|
|
|
buffers_adaptor<decltype(mbs)> ba(mbs);
|
2018-06-08 12:14:29 -07:00
|
|
|
std::memset(p.get(), 0, len);
|
|
|
|
|
|
|
|
|
|
icy_stream<test::stream> is{ioc};
|
|
|
|
|
is.next_layer().read_size(j);
|
|
|
|
|
is.next_layer().append(in);
|
|
|
|
|
connect(is.next_layer()).close();
|
|
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
ba.commit(is.read_some(
|
|
|
|
|
ba.prepare(read_size(ba, k)), ec));
|
|
|
|
|
if(ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(! BEAST_EXPECTS(
|
2018-11-30 14:58:38 -08:00
|
|
|
ec == net::error::eof, ec.message()))
|
2018-06-08 12:14:29 -07:00
|
|
|
continue;
|
|
|
|
|
auto const s = buffers_to_string(ba.data());
|
|
|
|
|
BEAST_EXPECTS(s == out, s);
|
|
|
|
|
}
|
|
|
|
|
// async
|
|
|
|
|
{
|
2018-12-14 11:13:26 -08:00
|
|
|
buffers_adaptor<decltype(mbs)> ba(mbs);
|
2018-06-08 12:14:29 -07:00
|
|
|
std::memset(p.get(), 0, len);
|
|
|
|
|
|
|
|
|
|
icy_stream<test::stream> is{ioc};
|
|
|
|
|
is.next_layer().read_size(j);
|
|
|
|
|
is.next_layer().append(in);
|
|
|
|
|
connect(is.next_layer()).close();
|
|
|
|
|
|
|
|
|
|
error_code ec;
|
|
|
|
|
for(;;)
|
|
|
|
|
{
|
|
|
|
|
is.async_read_some(
|
|
|
|
|
ba.prepare(read_size(ba, k)),
|
|
|
|
|
[&ec, &ba](error_code ec_, std::size_t n)
|
|
|
|
|
{
|
|
|
|
|
ec = ec_;
|
|
|
|
|
ba.commit(n);
|
|
|
|
|
});
|
|
|
|
|
ioc.run();
|
2018-11-22 20:49:30 -08:00
|
|
|
ioc.restart();
|
2018-06-08 12:14:29 -07:00
|
|
|
if(ec)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if(! BEAST_EXPECTS(
|
2018-11-30 14:58:38 -08:00
|
|
|
ec == net::error::eof, ec.message()))
|
2018-06-08 12:14:29 -07:00
|
|
|
continue;
|
|
|
|
|
auto const s = buffers_to_string(ba.data());
|
|
|
|
|
if(! BEAST_EXPECTS(s == out, s))
|
|
|
|
|
{
|
|
|
|
|
s.size();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
testStream()
|
|
|
|
|
{
|
|
|
|
|
doMatrix("HTTP/1.1 200 OK\r\n", "HTTP/1.1 200 OK\r\n");
|
|
|
|
|
doMatrix("ICY 200 OK\r\n", "HTTP/1.1 200 OK\r\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
run() override
|
|
|
|
|
{
|
|
|
|
|
testStream();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BEAST_DEFINE_TESTSUITE(beast,http,icy_stream);
|
|
|
|
|
|
|
|
|
|
} // http
|
|
|
|
|
} // beast
|
|
|
|
|
} // boost
|