mirror of
https://github.com/boostorg/beast.git
synced 2025-07-30 21:07:26 +02:00
Split out and rename test stream classes
This commit is contained in:
@ -1,3 +1,9 @@
|
|||||||
|
1.0.0-b28
|
||||||
|
|
||||||
|
* Split out and rename test stream classes
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
1.0.0-b27
|
1.0.0-b27
|
||||||
|
|
||||||
* Tidy up tests and docs
|
* Tidy up tests and docs
|
||||||
|
@ -5,12 +5,13 @@
|
|||||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
//
|
//
|
||||||
|
|
||||||
#ifndef BEAST_TEST_STRING_STREAM_HPP
|
#ifndef BEAST_TEST_STRING_ISTREAM_HPP
|
||||||
#define BEAST_TEST_STRING_STREAM_HPP
|
#define BEAST_TEST_STRING_ISTREAM_HPP
|
||||||
|
|
||||||
#include <beast/core/async_completion.hpp>
|
#include <beast/core/async_completion.hpp>
|
||||||
#include <beast/core/bind_handler.hpp>
|
#include <beast/core/bind_handler.hpp>
|
||||||
#include <beast/core/error.hpp>
|
#include <beast/core/error.hpp>
|
||||||
|
#include <beast/core/prepare_buffer.hpp>
|
||||||
#include <boost/asio/buffer.hpp>
|
#include <boost/asio/buffer.hpp>
|
||||||
#include <boost/asio/io_service.hpp>
|
#include <boost/asio/io_service.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -24,16 +25,21 @@ namespace test {
|
|||||||
discarded, and when data is read it comes from a string provided
|
discarded, and when data is read it comes from a string provided
|
||||||
at construction.
|
at construction.
|
||||||
*/
|
*/
|
||||||
class string_stream
|
class string_istream
|
||||||
{
|
{
|
||||||
std::string s_;
|
std::string s_;
|
||||||
|
boost::asio::const_buffer cb_;
|
||||||
boost::asio::io_service& ios_;
|
boost::asio::io_service& ios_;
|
||||||
|
std::size_t read_max_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
string_stream(boost::asio::io_service& ios,
|
string_istream(boost::asio::io_service& ios,
|
||||||
std::string s)
|
std::string s, std::size_t read_max =
|
||||||
|
(std::numeric_limits<std::size_t>::max)())
|
||||||
: s_(std::move(s))
|
: s_(std::move(s))
|
||||||
|
, cb_(boost::asio::buffer(s_))
|
||||||
, ios_(ios)
|
, ios_(ios)
|
||||||
|
, read_max_(read_max)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,9 +66,9 @@ public:
|
|||||||
error_code& ec)
|
error_code& ec)
|
||||||
{
|
{
|
||||||
auto const n = boost::asio::buffer_copy(
|
auto const n = boost::asio::buffer_copy(
|
||||||
buffers, boost::asio::buffer(s_));
|
buffers, prepare_buffer(read_max_, cb_));
|
||||||
if(n > 0)
|
if(n > 0)
|
||||||
s_.erase(0, n);
|
cb_ = cb_ + n;
|
||||||
else
|
else
|
||||||
ec = boost::asio::error::eof;
|
ec = boost::asio::error::eof;
|
||||||
return n;
|
return n;
|
118
extras/beast/test/string_ostream.hpp
Normal file
118
extras/beast/test/string_ostream.hpp
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2013-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)
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef BEAST_TEST_STRING_OSTREAM_HPP
|
||||||
|
#define BEAST_TEST_STRING_OSTREAM_HPP
|
||||||
|
|
||||||
|
#include <beast/core/async_completion.hpp>
|
||||||
|
#include <beast/core/bind_handler.hpp>
|
||||||
|
#include <beast/core/error.hpp>
|
||||||
|
#include <boost/asio/buffer.hpp>
|
||||||
|
#include <boost/asio/io_service.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace beast {
|
||||||
|
namespace test {
|
||||||
|
|
||||||
|
class string_ostream
|
||||||
|
{
|
||||||
|
boost::asio::io_service& ios_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::string str;
|
||||||
|
|
||||||
|
explicit
|
||||||
|
string_ostream(boost::asio::io_service& ios)
|
||||||
|
: ios_(ios)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::asio::io_service&
|
||||||
|
get_io_service()
|
||||||
|
{
|
||||||
|
return ios_;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class MutableBufferSequence>
|
||||||
|
std::size_t
|
||||||
|
read_some(MutableBufferSequence const& buffers)
|
||||||
|
{
|
||||||
|
error_code ec;
|
||||||
|
auto const n = read_some(buffers, ec);
|
||||||
|
if(ec)
|
||||||
|
throw system_error{ec};
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class MutableBufferSequence>
|
||||||
|
std::size_t
|
||||||
|
read_some(MutableBufferSequence const& buffers,
|
||||||
|
error_code& ec)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class MutableBufferSequence, class ReadHandler>
|
||||||
|
typename async_completion<ReadHandler,
|
||||||
|
void(error_code, std::size_t)>::result_type
|
||||||
|
async_read_some(MutableBufferSequence const& buffers,
|
||||||
|
ReadHandler&& handler)
|
||||||
|
{
|
||||||
|
async_completion<ReadHandler,
|
||||||
|
void(error_code, std::size_t)> completion{handler};
|
||||||
|
ios_.post(bind_handler(completion.handler,
|
||||||
|
error_code{}, 0));
|
||||||
|
return completion.result.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ConstBufferSequence>
|
||||||
|
std::size_t
|
||||||
|
write_some(ConstBufferSequence const& buffers)
|
||||||
|
{
|
||||||
|
error_code ec;
|
||||||
|
auto const n = write_some(buffers, ec);
|
||||||
|
if(ec)
|
||||||
|
throw system_error{ec};
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ConstBufferSequence>
|
||||||
|
std::size_t
|
||||||
|
write_some(
|
||||||
|
ConstBufferSequence const& buffers, error_code&)
|
||||||
|
{
|
||||||
|
auto const n = buffer_size(buffers);
|
||||||
|
using boost::asio::buffer_size;
|
||||||
|
using boost::asio::buffer_cast;
|
||||||
|
str.reserve(str.size() + n);
|
||||||
|
for(auto const& buffer : buffers)
|
||||||
|
str.append(buffer_cast<char const*>(buffer),
|
||||||
|
buffer_size(buffer));
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ConstBufferSequence, class WriteHandler>
|
||||||
|
typename async_completion<
|
||||||
|
WriteHandler, void(error_code)>::result_type
|
||||||
|
async_write_some(ConstBufferSequence const& buffers,
|
||||||
|
WriteHandler&& handler)
|
||||||
|
{
|
||||||
|
error_code ec;
|
||||||
|
auto const bytes_transferred = write_some(buffers, ec);
|
||||||
|
async_completion<
|
||||||
|
WriteHandler, void(error_code, std::size_t)
|
||||||
|
> completion(handler);
|
||||||
|
get_io_service().post(
|
||||||
|
bind_handler(completion.handler, ec, bytes_transferred));
|
||||||
|
return completion.result.get();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // test
|
||||||
|
} // beast
|
||||||
|
|
||||||
|
#endif
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
#include <beast/core/streambuf.hpp>
|
#include <beast/core/streambuf.hpp>
|
||||||
#include <beast/test/fail_stream.hpp>
|
#include <beast/test/fail_stream.hpp>
|
||||||
#include <beast/test/string_stream.hpp>
|
#include <beast/test/string_istream.hpp>
|
||||||
#include <beast/test/yield_to.hpp>
|
#include <beast/test/yield_to.hpp>
|
||||||
#include <beast/unit_test/suite.hpp>
|
#include <beast/unit_test/suite.hpp>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
@ -54,7 +54,7 @@ public:
|
|||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
test::string_stream> fs(n, ios_, ", world!");
|
test::string_istream> fs(n, ios_, ", world!");
|
||||||
dynabuf_readstream<
|
dynabuf_readstream<
|
||||||
decltype(fs)&, streambuf> srs(fs);
|
decltype(fs)&, streambuf> srs(fs);
|
||||||
srs.buffer().commit(buffer_copy(
|
srs.buffer().commit(buffer_copy(
|
||||||
@ -72,7 +72,7 @@ public:
|
|||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
test::string_stream> fs(n, ios_, ", world!");
|
test::string_istream> fs(n, ios_, ", world!");
|
||||||
dynabuf_readstream<
|
dynabuf_readstream<
|
||||||
decltype(fs)&, streambuf> srs(fs);
|
decltype(fs)&, streambuf> srs(fs);
|
||||||
srs.capacity(3);
|
srs.capacity(3);
|
||||||
@ -91,7 +91,7 @@ public:
|
|||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
test::string_stream> fs(n, ios_, ", world!");
|
test::string_istream> fs(n, ios_, ", world!");
|
||||||
dynabuf_readstream<
|
dynabuf_readstream<
|
||||||
decltype(fs)&, streambuf> srs(fs);
|
decltype(fs)&, streambuf> srs(fs);
|
||||||
srs.buffer().commit(buffer_copy(
|
srs.buffer().commit(buffer_copy(
|
||||||
@ -110,7 +110,7 @@ public:
|
|||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
test::string_stream> fs(n, ios_, ", world!");
|
test::string_istream> fs(n, ios_, ", world!");
|
||||||
dynabuf_readstream<
|
dynabuf_readstream<
|
||||||
decltype(fs)&, streambuf> srs(fs);
|
decltype(fs)&, streambuf> srs(fs);
|
||||||
srs.capacity(3);
|
srs.capacity(3);
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#include <beast/http/header_parser_v1.hpp>
|
#include <beast/http/header_parser_v1.hpp>
|
||||||
#include <beast/http/parse.hpp>
|
#include <beast/http/parse.hpp>
|
||||||
#include <beast/http/string_body.hpp>
|
#include <beast/http/string_body.hpp>
|
||||||
#include <beast/test/string_stream.hpp>
|
#include <beast/test/string_istream.hpp>
|
||||||
#include <beast/test/yield_to.hpp>
|
#include <beast/test/yield_to.hpp>
|
||||||
#include <beast/unit_test/suite.hpp>
|
#include <beast/unit_test/suite.hpp>
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ public:
|
|||||||
|
|
||||||
void testWithBody()
|
void testWithBody()
|
||||||
{
|
{
|
||||||
test::string_stream ss{ios_,
|
test::string_istream ss{ios_,
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
"User-Agent: test\r\n"
|
"User-Agent: test\r\n"
|
||||||
"Content-Length: 1\r\n"
|
"Content-Length: 1\r\n"
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#include <beast/http/fields.hpp>
|
#include <beast/http/fields.hpp>
|
||||||
#include <beast/http/streambuf_body.hpp>
|
#include <beast/http/streambuf_body.hpp>
|
||||||
#include <beast/test/fail_stream.hpp>
|
#include <beast/test/fail_stream.hpp>
|
||||||
#include <beast/test/string_stream.hpp>
|
#include <beast/test/string_istream.hpp>
|
||||||
#include <beast/test/yield_to.hpp>
|
#include <beast/test/yield_to.hpp>
|
||||||
#include <beast/unit_test/suite.hpp>
|
#include <beast/unit_test/suite.hpp>
|
||||||
#include <boost/asio/spawn.hpp>
|
#include <boost/asio/spawn.hpp>
|
||||||
@ -95,7 +95,7 @@ public:
|
|||||||
sb.prepare(len), buffer(s, len)));
|
sb.prepare(len), buffer(s, len)));
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
test::string_stream> fs{fc, ios_, ""};
|
test::string_istream> fs{fc, ios_, ""};
|
||||||
fail_parser<isRequest> p(fc);
|
fail_parser<isRequest> p(fc);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
parse(fs, sb, p, ec);
|
parse(fs, sb, p, ec);
|
||||||
@ -110,7 +110,7 @@ public:
|
|||||||
sb.commit(buffer_copy(
|
sb.commit(buffer_copy(
|
||||||
sb.prepare(pre), buffer(s, pre)));
|
sb.prepare(pre), buffer(s, pre)));
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<test::string_stream> fs{
|
test::fail_stream<test::string_istream> fs{
|
||||||
fc, ios_, std::string{s + pre, len - pre}};
|
fc, ios_, std::string{s + pre, len - pre}};
|
||||||
fail_parser<isRequest> p(fc);
|
fail_parser<isRequest> p(fc);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
@ -126,7 +126,7 @@ public:
|
|||||||
sb.prepare(len), buffer(s, len)));
|
sb.prepare(len), buffer(s, len)));
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
test::string_stream> fs{fc, ios_, ""};
|
test::string_istream> fs{fc, ios_, ""};
|
||||||
fail_parser<isRequest> p(fc);
|
fail_parser<isRequest> p(fc);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
async_parse(fs, sb, p, do_yield[ec]);
|
async_parse(fs, sb, p, do_yield[ec]);
|
||||||
@ -141,7 +141,7 @@ public:
|
|||||||
sb.commit(buffer_copy(
|
sb.commit(buffer_copy(
|
||||||
sb.prepare(pre), buffer(s, pre)));
|
sb.prepare(pre), buffer(s, pre)));
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<test::string_stream> fs{
|
test::fail_stream<test::string_istream> fs{
|
||||||
fc, ios_, std::string{s + pre, len - pre}};
|
fc, ios_, std::string{s + pre, len - pre}};
|
||||||
fail_parser<isRequest> p(fc);
|
fail_parser<isRequest> p(fc);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
@ -156,7 +156,7 @@ public:
|
|||||||
sb.commit(buffer_copy(
|
sb.commit(buffer_copy(
|
||||||
sb.prepare(len), buffer(s, len)));
|
sb.prepare(len), buffer(s, len)));
|
||||||
test::fail_counter fc{n};
|
test::fail_counter fc{n};
|
||||||
test::string_stream ss{ios_, s};
|
test::string_istream ss{ios_, s};
|
||||||
parser_v1<isRequest, fail_body, fields> p{fc};
|
parser_v1<isRequest, fail_body, fields> p{fc};
|
||||||
error_code ec;
|
error_code ec;
|
||||||
parse(ss, sb, p, ec);
|
parse(ss, sb, p, ec);
|
||||||
@ -171,7 +171,7 @@ public:
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
streambuf sb;
|
streambuf sb;
|
||||||
test::string_stream ss(ios_, "GET / X");
|
test::string_istream ss(ios_, "GET / X");
|
||||||
parser_v1<true, streambuf_body, fields> p;
|
parser_v1<true, streambuf_body, fields> p;
|
||||||
parse(ss, sb, p);
|
parse(ss, sb, p);
|
||||||
fail();
|
fail();
|
||||||
@ -251,7 +251,7 @@ public:
|
|||||||
|
|
||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<test::string_stream> fs{n, ios_,
|
test::fail_stream<test::string_istream> fs{n, ios_,
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
"Host: localhost\r\n"
|
"Host: localhost\r\n"
|
||||||
"User-Agent: test\r\n"
|
"User-Agent: test\r\n"
|
||||||
@ -273,7 +273,7 @@ public:
|
|||||||
|
|
||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<test::string_stream> fs(n, ios_,
|
test::fail_stream<test::string_istream> fs(n, ios_,
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
"Host: localhost\r\n"
|
"Host: localhost\r\n"
|
||||||
"User-Agent: test\r\n"
|
"User-Agent: test\r\n"
|
||||||
@ -297,7 +297,7 @@ public:
|
|||||||
|
|
||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<test::string_stream> fs(n, ios_,
|
test::fail_stream<test::string_istream> fs(n, ios_,
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
"Host: localhost\r\n"
|
"Host: localhost\r\n"
|
||||||
"User-Agent: test\r\n"
|
"User-Agent: test\r\n"
|
||||||
@ -319,7 +319,7 @@ public:
|
|||||||
|
|
||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<test::string_stream> fs(n, ios_,
|
test::fail_stream<test::string_istream> fs(n, ios_,
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
"Host: localhost\r\n"
|
"Host: localhost\r\n"
|
||||||
"User-Agent: test\r\n"
|
"User-Agent: test\r\n"
|
||||||
@ -337,7 +337,7 @@ public:
|
|||||||
|
|
||||||
for(n = 0; n < limit; ++n)
|
for(n = 0; n < limit; ++n)
|
||||||
{
|
{
|
||||||
test::fail_stream<test::string_stream> fs(n, ios_,
|
test::fail_stream<test::string_istream> fs(n, ios_,
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
"Host: localhost\r\n"
|
"Host: localhost\r\n"
|
||||||
"User-Agent: test\r\n"
|
"User-Agent: test\r\n"
|
||||||
@ -358,7 +358,7 @@ public:
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
streambuf sb;
|
streambuf sb;
|
||||||
test::string_stream ss(ios_, "");
|
test::string_istream ss(ios_, "");
|
||||||
parser_v1<true, streambuf_body, fields> p;
|
parser_v1<true, streambuf_body, fields> p;
|
||||||
error_code ec;
|
error_code ec;
|
||||||
parse(ss, sb, p, ec);
|
parse(ss, sb, p, ec);
|
||||||
@ -366,7 +366,7 @@ public:
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
streambuf sb;
|
streambuf sb;
|
||||||
test::string_stream ss(ios_, "");
|
test::string_istream ss(ios_, "");
|
||||||
parser_v1<true, streambuf_body, fields> p;
|
parser_v1<true, streambuf_body, fields> p;
|
||||||
error_code ec;
|
error_code ec;
|
||||||
async_parse(ss, sb, p, do_yield[ec]);
|
async_parse(ss, sb, p, do_yield[ec]);
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
#include <beast/http/parser_v1.hpp>
|
#include <beast/http/parser_v1.hpp>
|
||||||
#include <beast/http/read.hpp>
|
#include <beast/http/read.hpp>
|
||||||
#include <beast/http/write.hpp>
|
#include <beast/http/write.hpp>
|
||||||
#include <beast/test/string_stream.hpp>
|
#include <beast/test/string_istream.hpp>
|
||||||
#include <beast/unit_test/suite.hpp>
|
#include <beast/unit_test/suite.hpp>
|
||||||
#include <boost/lexical_cast.hpp>
|
#include <boost/lexical_cast.hpp>
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ public:
|
|||||||
"Content-Length: 3\r\n"
|
"Content-Length: 3\r\n"
|
||||||
"\r\n"
|
"\r\n"
|
||||||
"xyz";
|
"xyz";
|
||||||
test::string_stream ss(ios_, s);
|
test::string_istream ss(ios_, s);
|
||||||
parser_v1<false, streambuf_body, fields> p;
|
parser_v1<false, streambuf_body, fields> p;
|
||||||
streambuf sb;
|
streambuf sb;
|
||||||
parse(ss, sb, p);
|
parse(ss, sb, p);
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
#include <beast/core/streambuf.hpp>
|
#include <beast/core/streambuf.hpp>
|
||||||
#include <beast/core/to_string.hpp>
|
#include <beast/core/to_string.hpp>
|
||||||
#include <beast/test/fail_stream.hpp>
|
#include <beast/test/fail_stream.hpp>
|
||||||
|
#include <beast/test/string_ostream.hpp>
|
||||||
#include <beast/test/yield_to.hpp>
|
#include <beast/test/yield_to.hpp>
|
||||||
#include <beast/unit_test/suite.hpp>
|
#include <beast/unit_test/suite.hpp>
|
||||||
#include <boost/asio/error.hpp>
|
#include <boost/asio/error.hpp>
|
||||||
@ -31,68 +32,6 @@ class write_test
|
|||||||
, public test::enable_yield_to
|
, public test::enable_yield_to
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
class string_write_stream
|
|
||||||
{
|
|
||||||
boost::asio::io_service& ios_;
|
|
||||||
|
|
||||||
public:
|
|
||||||
std::string str;
|
|
||||||
|
|
||||||
explicit
|
|
||||||
string_write_stream(boost::asio::io_service& ios)
|
|
||||||
: ios_(ios)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::asio::io_service&
|
|
||||||
get_io_service()
|
|
||||||
{
|
|
||||||
return ios_;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class ConstBufferSequence>
|
|
||||||
std::size_t
|
|
||||||
write_some(ConstBufferSequence const& buffers)
|
|
||||||
{
|
|
||||||
error_code ec;
|
|
||||||
auto const n = write_some(buffers, ec);
|
|
||||||
if(ec)
|
|
||||||
throw system_error{ec};
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class ConstBufferSequence>
|
|
||||||
std::size_t
|
|
||||||
write_some(
|
|
||||||
ConstBufferSequence const& buffers, error_code&)
|
|
||||||
{
|
|
||||||
auto const n = buffer_size(buffers);
|
|
||||||
using boost::asio::buffer_size;
|
|
||||||
using boost::asio::buffer_cast;
|
|
||||||
str.reserve(str.size() + n);
|
|
||||||
for(auto const& buffer : buffers)
|
|
||||||
str.append(buffer_cast<char const*>(buffer),
|
|
||||||
buffer_size(buffer));
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class ConstBufferSequence, class WriteHandler>
|
|
||||||
typename async_completion<
|
|
||||||
WriteHandler, void(error_code)>::result_type
|
|
||||||
async_write_some(ConstBufferSequence const& buffers,
|
|
||||||
WriteHandler&& handler)
|
|
||||||
{
|
|
||||||
error_code ec;
|
|
||||||
auto const bytes_transferred = write_some(buffers, ec);
|
|
||||||
async_completion<
|
|
||||||
WriteHandler, void(error_code, std::size_t)
|
|
||||||
> completion(handler);
|
|
||||||
get_io_service().post(
|
|
||||||
bind_handler(completion.handler, ec, bytes_transferred));
|
|
||||||
return completion.result.get();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct unsized_body
|
struct unsized_body
|
||||||
{
|
{
|
||||||
using value_type = std::string;
|
using value_type = std::string;
|
||||||
@ -225,7 +164,7 @@ public:
|
|||||||
std::string
|
std::string
|
||||||
str(message<isRequest, Body, Fields> const& m)
|
str(message<isRequest, Body, Fields> const& m)
|
||||||
{
|
{
|
||||||
string_write_stream ss(ios_);
|
test::string_ostream ss(ios_);
|
||||||
write(ss, m);
|
write(ss, m);
|
||||||
return ss.str;
|
return ss.str;
|
||||||
}
|
}
|
||||||
@ -240,7 +179,7 @@ public:
|
|||||||
m.url = "/";
|
m.url = "/";
|
||||||
m.fields.insert("User-Agent", "test");
|
m.fields.insert("User-Agent", "test");
|
||||||
error_code ec;
|
error_code ec;
|
||||||
string_write_stream ss{ios_};
|
test::string_ostream ss{ios_};
|
||||||
async_write(ss, m, do_yield[ec]);
|
async_write(ss, m, do_yield[ec]);
|
||||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||||
BEAST_EXPECT(ss.str ==
|
BEAST_EXPECT(ss.str ==
|
||||||
@ -256,7 +195,7 @@ public:
|
|||||||
m.fields.insert("Server", "test");
|
m.fields.insert("Server", "test");
|
||||||
m.fields.insert("Content-Length", "5");
|
m.fields.insert("Content-Length", "5");
|
||||||
error_code ec;
|
error_code ec;
|
||||||
string_write_stream ss{ios_};
|
test::string_ostream ss{ios_};
|
||||||
async_write(ss, m, do_yield[ec]);
|
async_write(ss, m, do_yield[ec]);
|
||||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||||
BEAST_EXPECT(ss.str ==
|
BEAST_EXPECT(ss.str ==
|
||||||
@ -279,7 +218,7 @@ public:
|
|||||||
m.fields.insert("Content-Length", "5");
|
m.fields.insert("Content-Length", "5");
|
||||||
m.body = "*****";
|
m.body = "*****";
|
||||||
error_code ec;
|
error_code ec;
|
||||||
string_write_stream ss{ios_};
|
test::string_ostream ss{ios_};
|
||||||
async_write(ss, m, do_yield[ec]);
|
async_write(ss, m, do_yield[ec]);
|
||||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||||
BEAST_EXPECT(ss.str ==
|
BEAST_EXPECT(ss.str ==
|
||||||
@ -298,7 +237,7 @@ public:
|
|||||||
m.fields.insert("Transfer-Encoding", "chunked");
|
m.fields.insert("Transfer-Encoding", "chunked");
|
||||||
m.body = "*****";
|
m.body = "*****";
|
||||||
error_code ec;
|
error_code ec;
|
||||||
string_write_stream ss(ios_);
|
test::string_ostream ss(ios_);
|
||||||
async_write(ss, m, do_yield[ec]);
|
async_write(ss, m, do_yield[ec]);
|
||||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||||
BEAST_EXPECT(ss.str ==
|
BEAST_EXPECT(ss.str ==
|
||||||
@ -322,7 +261,7 @@ public:
|
|||||||
{
|
{
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
string_write_stream> fs(fc, ios_);
|
test::string_ostream> fs(fc, ios_);
|
||||||
message<true, fail_body, fields> m(
|
message<true, fail_body, fields> m(
|
||||||
std::piecewise_construct,
|
std::piecewise_construct,
|
||||||
std::forward_as_tuple(fc, ios_));
|
std::forward_as_tuple(fc, ios_));
|
||||||
@ -355,7 +294,7 @@ public:
|
|||||||
{
|
{
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
string_write_stream> fs(fc, ios_);
|
test::string_ostream> fs(fc, ios_);
|
||||||
message<true, fail_body, fields> m(
|
message<true, fail_body, fields> m(
|
||||||
std::piecewise_construct,
|
std::piecewise_construct,
|
||||||
std::forward_as_tuple(fc, ios_));
|
std::forward_as_tuple(fc, ios_));
|
||||||
@ -390,7 +329,7 @@ public:
|
|||||||
{
|
{
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
string_write_stream> fs(fc, ios_);
|
test::string_ostream> fs(fc, ios_);
|
||||||
message<true, fail_body, fields> m(
|
message<true, fail_body, fields> m(
|
||||||
std::piecewise_construct,
|
std::piecewise_construct,
|
||||||
std::forward_as_tuple(fc, ios_));
|
std::forward_as_tuple(fc, ios_));
|
||||||
@ -425,7 +364,7 @@ public:
|
|||||||
{
|
{
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
string_write_stream> fs(fc, ios_);
|
test::string_ostream> fs(fc, ios_);
|
||||||
message<true, fail_body, fields> m(
|
message<true, fail_body, fields> m(
|
||||||
std::piecewise_construct,
|
std::piecewise_construct,
|
||||||
std::forward_as_tuple(fc, ios_));
|
std::forward_as_tuple(fc, ios_));
|
||||||
@ -455,7 +394,7 @@ public:
|
|||||||
{
|
{
|
||||||
test::fail_counter fc(n);
|
test::fail_counter fc(n);
|
||||||
test::fail_stream<
|
test::fail_stream<
|
||||||
string_write_stream> fs(fc, ios_);
|
test::string_ostream> fs(fc, ios_);
|
||||||
message<true, fail_body, fields> m(
|
message<true, fail_body, fields> m(
|
||||||
std::piecewise_construct,
|
std::piecewise_construct,
|
||||||
std::forward_as_tuple(fc, ios_));
|
std::forward_as_tuple(fc, ios_));
|
||||||
@ -547,7 +486,7 @@ public:
|
|||||||
m.fields.insert("User-Agent", "test");
|
m.fields.insert("User-Agent", "test");
|
||||||
m.body = "*";
|
m.body = "*";
|
||||||
prepare(m);
|
prepare(m);
|
||||||
string_write_stream ss(ios_);
|
test::string_ostream ss(ios_);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
write(ss, m, ec);
|
write(ss, m, ec);
|
||||||
BEAST_EXPECT(ec == boost::asio::error::eof);
|
BEAST_EXPECT(ec == boost::asio::error::eof);
|
||||||
@ -584,7 +523,7 @@ public:
|
|||||||
m.fields.insert("User-Agent", "test");
|
m.fields.insert("User-Agent", "test");
|
||||||
m.body = "*";
|
m.body = "*";
|
||||||
prepare(m, connection::close);
|
prepare(m, connection::close);
|
||||||
string_write_stream ss(ios_);
|
test::string_ostream ss(ios_);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
write(ss, m, ec);
|
write(ss, m, ec);
|
||||||
BEAST_EXPECT(ec == boost::asio::error::eof);
|
BEAST_EXPECT(ec == boost::asio::error::eof);
|
||||||
@ -621,7 +560,7 @@ public:
|
|||||||
m.fields.insert("User-Agent", "test");
|
m.fields.insert("User-Agent", "test");
|
||||||
m.body = "*";
|
m.body = "*";
|
||||||
prepare(m);
|
prepare(m);
|
||||||
string_write_stream ss(ios_);
|
test::string_ostream ss(ios_);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
write(ss, m, ec);
|
write(ss, m, ec);
|
||||||
BEAST_EXPECT(ss.str ==
|
BEAST_EXPECT(ss.str ==
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
#include <beast/core/streambuf.hpp>
|
#include <beast/core/streambuf.hpp>
|
||||||
#include <beast/core/to_string.hpp>
|
#include <beast/core/to_string.hpp>
|
||||||
#include <beast/test/fail_stream.hpp>
|
#include <beast/test/fail_stream.hpp>
|
||||||
#include <beast/test/string_stream.hpp>
|
#include <beast/test/string_istream.hpp>
|
||||||
#include <beast/test/yield_to.hpp>
|
#include <beast/test/yield_to.hpp>
|
||||||
#include <beast/unit_test/suite.hpp>
|
#include <beast/unit_test/suite.hpp>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
@ -172,7 +172,7 @@ public:
|
|||||||
req.fields.insert("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==");
|
req.fields.insert("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==");
|
||||||
req.fields.insert("Sec-WebSocket-Version", "13");
|
req.fields.insert("Sec-WebSocket-Version", "13");
|
||||||
stream<test::fail_stream<
|
stream<test::fail_stream<
|
||||||
test::string_stream>> ws(n, ios_, "");
|
test::string_istream>> ws(n, ios_, "");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ws.accept(req);
|
ws.accept(req);
|
||||||
@ -186,7 +186,7 @@ public:
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
// valid
|
// valid
|
||||||
stream<test::string_stream> ws(ios_,
|
stream<test::string_istream> ws(ios_,
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
"Host: localhost:80\r\n"
|
"Host: localhost:80\r\n"
|
||||||
"Upgrade: WebSocket\r\n"
|
"Upgrade: WebSocket\r\n"
|
||||||
@ -207,7 +207,7 @@ public:
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
// invalid
|
// invalid
|
||||||
stream<test::string_stream> ws(ios_,
|
stream<test::string_istream> ws(ios_,
|
||||||
"GET / HTTP/1.0\r\n"
|
"GET / HTTP/1.0\r\n"
|
||||||
"\r\n"
|
"\r\n"
|
||||||
);
|
);
|
||||||
@ -230,7 +230,7 @@ public:
|
|||||||
{
|
{
|
||||||
for(std::size_t i = 0; i < s.size(); ++i)
|
for(std::size_t i = 0; i < s.size(); ++i)
|
||||||
{
|
{
|
||||||
stream<test::string_stream> ws(ios_,
|
stream<test::string_istream> ws(ios_,
|
||||||
s.substr(i, s.size() - i));
|
s.substr(i, s.size() - i));
|
||||||
ws.set_option(keep_alive{true});
|
ws.set_option(keep_alive{true});
|
||||||
try
|
try
|
||||||
@ -339,7 +339,7 @@ public:
|
|||||||
auto const check =
|
auto const check =
|
||||||
[&](std::string const& s)
|
[&](std::string const& s)
|
||||||
{
|
{
|
||||||
stream<test::string_stream> ws(ios_, s);
|
stream<test::string_istream> ws(ios_, s);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ws.handshake("localhost:80", "/");
|
ws.handshake("localhost:80", "/");
|
||||||
|
Reference in New Issue
Block a user