28 #define BOOST_TEST_MODULE server 29 #include <boost/test/unit_test.hpp> 41 using websocketpp::lib::placeholders::_1;
42 using websocketpp::lib::placeholders::_2;
43 using websocketpp::lib::bind;
68 std::stringstream output;
70 s.register_ostream(&output);
77 std::stringstream channel;
87 s->
send(hdl, msg->get_payload(), msg->get_opcode());
97 const std::vector<std::string> & protocols = con->get_requested_subprotocols();
98 std::vector<std::string>::const_iterator it;
100 for (it = protocols.begin(); it != protocols.end(); ++it) {
106 if (!accept.empty()) {
107 con->select_subprotocol(accept);
116 *out = con->get_subprotocol();
121 std::string input =
"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\nOrigin: http://www.example.com\r\n\r\n";
122 std::string output =
"HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
131 std::string input =
"GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: a\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
132 std::string output =
"HTTP/1.1 400 Bad Request\r\nServer: test\r\n\r\n";
142 std::string input =
"GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 14\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
144 std::string output =
"HTTP/1.1 400 Bad Request\r\nSec-WebSocket-Version: 0,7,8,13\r\nServer: test\r\n\r\n";
153 std::string input =
"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\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
155 std::string output =
"HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
157 std::string subprotocol;
164 BOOST_CHECK_EQUAL(subprotocol,
"");
168 std::string input =
"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\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
170 std::string output =
"HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
181 BOOST_CHECK_EQUAL(validate,
"foo,");
182 BOOST_CHECK_EQUAL(open,
"");
186 std::string input =
"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\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
188 std::string output =
"HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nSec-WebSocket-Protocol: foo\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
199 BOOST_CHECK_EQUAL(validate,
"foo,");
200 BOOST_CHECK_EQUAL(open,
"foo");
204 std::string input =
"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\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
206 std::string output =
"HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nSec-WebSocket-Protocol: foo\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
222 std::string input =
"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\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo, bar\r\n\r\n";
224 std::string output =
"HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nSec-WebSocket-Protocol: bar\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
235 BOOST_CHECK_EQUAL(validate,
"foo,bar,");
236 BOOST_CHECK_EQUAL(open,
"bar");
static level const all
Special aggregate value representing "all levels".
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
void set_open_handler(open_handler h)
Accept any value the remote endpoint offers.
void open_func_subprotocol(server *s, std::string *out, websocketpp::connection_hdl hdl)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
connection_ptr get_con_from_hdl(connection_hdl hdl, lib::error_code &ec)
Retrieves a connection_ptr from a connection_hdl (exception free)
websocketpp::server< websocketpp::config::core > server
lib::shared_ptr< message > ptr
bool validate_func_subprotocol(server *s, std::string *out, std::string accept, websocketpp::connection_hdl hdl)
std::string run_server_test(server &s, std::string input)
Server endpoint role based on the given config.
void clear_error_channels(log::level channels)
Clear Error logging channels.
websocketpp::config::core::message_type::ptr message_ptr
bool validate(server *, websocketpp::connection_hdl)
void set_validate_handler(validate_handler h)
connection_ptr get_connection()
Create and initialize a new connection.
void clear_access_channels(log::level channels)
Clear Access logging channels.
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
BOOST_AUTO_TEST_CASE(basic_websocket_request)
void set_user_agent(std::string const &ua)
Sets the user agent string that this endpoint will use.
static level const all
Special aggregate value representing "all levels".
void echo_func(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
void send(connection_hdl hdl, std::string const &payload, frame::opcode::value op, lib::error_code &ec)
Create a message and add it to the outgoing send queue (exception free)