28 #define BOOST_TEST_MODULE transport_integration 29 #include <boost/test/unit_test.hpp> 134 using websocketpp::lib::placeholders::_1;
135 using websocketpp::lib::placeholders::_2;
136 using websocketpp::lib::bind;
138 template <
typename T>
142 websocketpp::lib::error_code ec;
157 s->set_reuse_addr(
true);
172 websocketpp::lib::error_code ec;
174 c.set_reuse_addr(
true);
187 websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(*mutex);
204 websocketpp::lib::error_code ec;
209 websocketpp::lib::thread tthread(websocketpp::lib::bind(
210 &close_after_timeout<client>,
211 websocketpp::lib::ref(c),
221 using boost::asio::ip::tcp;
224 boost::asio::io_service io_service;
225 tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v6(), port));
228 acceptor.accept(socket);
231 boost::system::error_code ec;
232 socket.read_some(boost::asio::buffer(data), ec);
240 }
catch (std::exception & e) {
241 std::cout << e.what() << std::endl;
242 }
catch (boost::system::error_code & ec) {
243 std::cout << ec.message() << std::endl;
248 using boost::asio::ip::tcp;
251 boost::asio::io_service io_service;
252 tcp::resolver resolver(io_service);
253 tcp::resolver::query query(
"localhost", port);
254 tcp::resolver::iterator iterator = resolver.resolve(query);
257 boost::asio::connect(socket, iterator);
260 boost::system::error_code ec;
261 socket.read_some(boost::asio::buffer(data), ec);
269 }
catch (std::exception & e) {
270 std::cout << e.what() << std::endl;
271 }
catch (boost::system::error_code & ec) {
272 std::cout << ec.message() << std::endl;
292 template <
typename T>
295 websocketpp::lib::error_code ec;
296 con->ping(payload,ec);
297 BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
301 BOOST_FAIL(
"expected no pong handler" );
305 BOOST_FAIL(
"expected no pong timeout" );
311 BOOST_CHECK_EQUAL( expected_payload, payload );
315 BOOST_FAIL(
"expected no open handler" );
322 template <
typename T>
323 void check_ec(T * c, websocketpp::lib::error_code ec,
327 BOOST_CHECK_EQUAL( con->get_ec(), ec );
332 template <
typename T>
337 BOOST_CHECK_EQUAL( con->get_ec(), ec );
343 template <
typename T>
348 BOOST_CHECK_EQUAL( payload, expected_payload );
352 template <
typename T>
361 : m_timer(m_io_service,
boost::posix_time::seconds(seconds))
363 m_timer.async_wait(bind(&test_deadline_timer::expired,
this, ::_1));
364 std::size_t (boost::asio::io_service::*run)() = &boost::asio::io_service::run;
365 m_timer_thread = websocketpp::lib::thread(websocketpp::lib::bind(run, &m_io_service));
370 m_timer_thread.join();
374 void expired(
const boost::system::error_code & ec)
379 BOOST_FAIL(
"Test timed out");
382 boost::asio::io_service m_io_service;
383 boost::asio::deadline_timer m_timer;
384 websocketpp::lib::thread m_timer_thread;
400 websocketpp::lib::thread sthread(websocketpp::lib::bind(&
run_server,&s,9005,
false));
418 websocketpp::lib::error_code(),::_1));
424 websocketpp::lib::error_code(),::_1));
426 websocketpp::lib::thread sthread(websocketpp::lib::bind(&
run_server,&s,9005,
false));
445 websocketpp::lib::thread sthread(websocketpp::lib::bind(&
run_dummy_server,9005));
464 websocketpp::lib::thread sthread(websocketpp::lib::bind(&
run_server,&s,9005,
false));
489 websocketpp::lib::thread sthread(websocketpp::lib::bind(&
run_server,&s,9005,
false));
495 run_client(c,
"http://localhost:9005",
false);
523 websocketpp::lib::thread sthread(websocketpp::lib::bind(&
run_server,&s,9005,
false));
528 run_client(c,
"http://localhost:9005",
false);
538 websocketpp::lib::error_code ec;
554 websocketpp::lib::mutex mutex;
556 websocketpp::lib::error_code ec;
562 websocketpp::lib::thread cthread(websocketpp::lib::bind(&
run_client_and_mark,&c,&flag,&mutex));
568 websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(mutex);
569 BOOST_CHECK( !flag );
578 websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(mutex);
601 websocketpp::lib::thread sthread(websocketpp::lib::bind(&
run_server,&s,9005,
false));
606 run_client(c,
"http://localhost:9005",
false);
613 std::string handshake =
"GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
614 char buffer[2] = { char(0x81), char(0x80) };
617 std::stringstream null_output;
618 s.register_ostream(&null_output);
624 BOOST_CHECK_EQUAL( con->read_some(handshake.data(), handshake.length()), handshake.length());
631 con->pause_reading();
632 BOOST_CHECK_EQUAL( con->read_some(buffer, 1), 1);
633 BOOST_CHECK_EQUAL( con->read_some(buffer+1, 1), 0);
636 con->resume_reading();
637 BOOST_CHECK_EQUAL( con->read_some(buffer+1, 1), 1);
645 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_ 651 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_ static level const all
Special aggregate value representing "all levels".
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
static const long timeout_open_handshake
Length of time before an opening handshake is aborted.
base::message_type message_type
base::response_type response_type
Asio based endpoint transport component.
boost::chrono::seconds seconds
void set_pong_timeout_handler(pong_timeout_handler h)
Client config with asio transport and TLS enabled.
test_deadline_timer(int seconds)
void fail_on_pong(websocketpp::connection_hdl, std::string)
type::response_type response_type
void set_open_handler(open_handler h)
type::concurrency_type concurrency_type
type::request_type request_type
span_CONFIG_SIZE_TYPE size_t
void fail_on_pong_timeout(websocketpp::connection_hdl, std::string)
static level const app
Special channel for application specific logs. Not used by the library.
websocketpp::config::asio base
base::request_type request_type
static const long timeout_pong
Length of time to wait for a pong after a ping.
Client endpoint role based on the given config.
Concurrency policy that uses std::mutex / boost::mutex.
base::con_msg_manager_type con_msg_manager_type
base::response_type response_type
websocketpp::transport::asio::endpoint< transport_config > transport_type
static value const normal
Normal closure, meaning that the purpose for which the connection was established has been fulfilled...
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
alog_type & get_alog()
Get reference to access logger.
boost::chrono::duration< Rep, Period > duration
base::elog_type elog_type
base::concurrency_type concurrency_type
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
void run_time_limited_client(client &c, std::string uri, long timeout, bool log)
void set_ping_handler(ping_handler h)
void set_close_handler(close_handler h)
type::request_type request_type
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
websocketpp::transport::asio::endpoint< transport_config > transport_type
Stores, parses, and manipulates HTTP responses.
base::alog_type alog_type
websocketpp::transport::asio::basic_socket::endpoint socket_type
type::concurrency_type concurrency_type
void run_dummy_server(int port)
type::response_type response_type
type::alog_type alog_type
void req_pong(std::string expected_payload, websocketpp::connection_hdl, std::string payload)
void req_pong_timeout(T *c, std::string expected_payload, websocketpp::connection_hdl hdl, std::string payload)
Basic logger that outputs to an ostream.
websocketpp::client< config > client
base::endpoint_msg_manager_type endpoint_msg_manager_type
type::elog_type elog_type
websocketpp::client< websocketpp::config::core_client > iostream_client
void stop_on_close(server *s, websocketpp::connection_hdl hdl)
void start_accept(lib::error_code &ec)
Starts the server's async connection acceptance loop (exception free)
base::endpoint_msg_manager_type endpoint_msg_manager_type
Server endpoint role based on the given config.
Thread safe stub "random" integer generator.
void set_pong_handler(pong_handler h)
void clear_error_channels(log::level channels)
Clear Error logging channels.
void set_access_channels(log::level channels)
Set Access logging channel.
void delay(websocketpp::connection_hdl, long duration)
Represents a buffer for a single WebSocket message.
Stub concurrency policy that implements the interface using no-ops.
void close(T *e, websocketpp::connection_hdl hdl)
base::request_type request_type
Basic ASIO endpoint socket component.
void set_fail_handler(fail_handler h)
base::con_msg_manager_type con_msg_manager_type
void run_client(client &c, std::string uri, bool log=false)
websocketpp::server< websocketpp::config::core > iostream_server
websocketpp::config::asio base
websocketpp::server< config > server
base::concurrency_type concurrency_type
WebSocket close handshake timed out.
connection_ptr get_connection()
Create and initialize a new connection.
type::alog_type alog_type
websocketpp::client< config_tls > client_tls
void clear_access_channels(log::level channels)
Clear Access logging channels.
base::elog_type elog_type
base::message_type message_type
void check_ec(T *c, websocketpp::lib::error_code ec, websocketpp::connection_hdl hdl)
void set_error_channels(log::level channels)
Set Error logging channel.
Stores, parses, and manipulates HTTP requests.
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
bool on_ping(server *s, websocketpp::connection_hdl, std::string)
client::connection_ptr connection_ptr
void run_client_and_mark(client *c, bool *flag, websocketpp::lib::mutex *mutex)
websocketpp::server< config_tls > server_tls
type::elog_type elog_type
Catch-all error for socket component errors that don't fit in other categories.
WebSocket opening handshake timed out.
void close_after_timeout(T &e, websocketpp::connection_hdl hdl, long timeout)
void check_ec_and_stop(T *e, websocketpp::lib::error_code ec, websocketpp::connection_hdl hdl)
void fail_on_open(websocketpp::connection_hdl)
base::alog_type alog_type
Client config with asio transport and TLS disabled.
static const long timeout_close_handshake
Length of time before a closing handshake is aborted.
void run_server(server *s, int port, bool log=false)
A connection message manager that allocates a new message for each request.
void ping_on_open(T *c, std::string payload, websocketpp::connection_hdl hdl)
void run_dummy_client(std::string port)
An endpoint message manager that allocates a new manager for each connection.
static level const all
Special aggregate value representing "all levels".
Stub logger that ignores all input.
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
websocketpp::transport::asio::basic_socket::endpoint socket_type
Server config with asio transport and TLS disabled.
void cancel_on_open(server *s, websocketpp::connection_hdl)
BOOST_AUTO_TEST_CASE(pong_no_timeout)