Files
boost_beast/test/beast/websocket/read1.cpp

154 lines
4.6 KiB
C++
Raw Normal View History

2019-02-13 08:00:07 -08:00
//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-08-24 06:21:30 -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.
#include <boost/beast/websocket/stream.hpp>
2019-02-13 08:00:07 -08:00
#include <boost/beast/_experimental/test/stream.hpp>
#include <boost/beast/_experimental/test/tcp.hpp>
#include <boost/beast/core/flat_buffer.hpp>
2018-11-20 20:51:38 -08:00
2017-08-24 06:21:30 -07:00
namespace boost {
namespace beast {
namespace websocket {
2019-02-13 08:00:07 -08:00
class read1_test : public unit_test::suite
2017-08-24 06:21:30 -07:00
{
public:
2017-08-24 07:41:27 -07:00
void
2019-02-13 08:00:07 -08:00
testTimeout()
2017-08-24 07:41:27 -07:00
{
2019-02-13 08:00:07 -08:00
using tcp = net::ip::tcp;
2017-08-24 07:41:27 -07:00
2019-02-13 08:00:07 -08:00
net::io_context ioc;
2017-08-24 07:41:27 -07:00
2019-02-13 08:00:07 -08:00
// success
2017-08-24 07:41:27 -07:00
{
2019-02-13 08:00:07 -08:00
stream<tcp::socket> ws1(ioc);
stream<tcp::socket> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler());
ws2.async_accept(test::success_handler());
test::run(ioc);
2017-08-24 06:21:30 -07:00
2019-02-13 08:00:07 -08:00
flat_buffer b;
ws1.async_write(net::const_buffer("Hello, world!", 13),
test::success_handler());
ws2.async_read(b, test::success_handler());
test::run(ioc);
2017-08-24 06:21:30 -07:00
}
2017-08-24 07:41:27 -07:00
{
2019-02-13 08:00:07 -08:00
stream<test::stream> ws1(ioc);
stream<test::stream> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler());
ws2.async_accept(test::success_handler());
test::run(ioc);
2017-08-24 06:21:30 -07:00
flat_buffer b;
2019-02-13 08:00:07 -08:00
ws1.async_write(net::const_buffer("Hello, world!", 13),
test::success_handler());
ws2.async_read(b, test::success_handler());
test::run(ioc);
}
2019-02-13 08:00:07 -08:00
// success, timeout enabled
2017-08-24 07:41:27 -07:00
{
2019-02-13 08:00:07 -08:00
stream<tcp::socket> ws1(ioc);
stream<tcp::socket> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler());
ws2.async_accept(test::success_handler());
test::run(ioc);
2017-08-24 07:41:27 -07:00
2019-02-13 08:00:07 -08:00
flat_buffer b;
ws1.set_option(stream_base::timeout{
stream_base::none(),
std::chrono::milliseconds(50),
false});
ws1.async_read(b, test::success_handler());
ws2.async_write(net::const_buffer("Hello, world!", 13),
test::success_handler());
test::run(ioc);
}
2017-08-24 07:41:27 -07:00
{
2019-02-13 08:00:07 -08:00
stream<test::stream> ws1(ioc);
stream<test::stream> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler());
ws2.async_accept(test::success_handler());
test::run(ioc);
2017-08-24 07:41:27 -07:00
2019-02-13 08:00:07 -08:00
flat_buffer b;
ws1.set_option(stream_base::timeout{
stream_base::none(),
std::chrono::milliseconds(50),
false});
ws1.async_read(b, test::success_handler());
ws2.async_write(net::const_buffer("Hello, world!", 13),
test::success_handler());
test::run(ioc);
}
2017-08-24 06:21:30 -07:00
2019-02-13 08:00:07 -08:00
// timeout
2017-08-24 06:21:30 -07:00
2017-08-24 07:41:27 -07:00
{
2019-02-13 08:00:07 -08:00
stream<tcp::socket> ws1(ioc);
stream<tcp::socket> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler());
ws2.async_accept(test::success_handler());
test::run(ioc);
2017-08-24 07:41:27 -07:00
2019-02-13 08:00:07 -08:00
flat_buffer b;
ws1.set_option(stream_base::timeout{
stream_base::none(),
std::chrono::milliseconds(50),
false});
ws1.async_read(b, test::fail_handler(
beast::error::timeout));
test::run(ioc);
}
2017-08-24 07:41:27 -07:00
2017-08-24 06:21:30 -07:00
{
2019-02-13 08:00:07 -08:00
stream<test::stream> ws1(ioc);
stream<test::stream> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler());
ws2.async_accept(test::success_handler());
test::run(ioc);
2017-08-24 06:21:30 -07:00
2019-02-13 08:00:07 -08:00
flat_buffer b;
ws1.set_option(stream_base::timeout{
stream_base::none(),
std::chrono::milliseconds(50),
false});
ws1.async_read(b, test::fail_handler(
beast::error::timeout));
test::run(ioc);
2017-08-24 06:21:30 -07:00
}
}
2017-08-24 06:21:30 -07:00
void
run() override
{
2019-02-13 08:00:07 -08:00
testTimeout();
2017-08-24 06:21:30 -07:00
}
};
BEAST_DEFINE_TESTSUITE(beast,websocket,read1);
2017-08-24 06:21:30 -07:00
} // websocket
} // beast
} // boost