28#define BOOST_TEST_MODULE server
29#include <boost/test/unit_test.hpp>
41using websocketpp::lib::placeholders::_1;
42using websocketpp::lib::placeholders::_2;
43using websocketpp::lib::bind;
68 std::stringstream output;
70 s.register_ostream(&output);
74 con =
s.get_connection();
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");
void set_user_agent(std::string const &ua)
Sets the user agent string that this endpoint will use.
lib::shared_ptr< message > ptr
Server endpoint role based on the given config.
connection_type::ptr connection_ptr
websocketpp::config::asio_tls_client::message_type::ptr message_ptr
bool validate(server *, websocketpp::connection_hdl)
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
bool validate_func_subprotocol(server *s, std::string *out, std::string accept, websocketpp::connection_hdl hdl)
BOOST_AUTO_TEST_CASE(basic_websocket_request)
websocketpp::server< websocketpp::config::core > server
void echo_func(server *s, websocketpp::connection_hdl hdl, message_ptr msg)
std::string run_server_test(server &s, std::string input)
void open_func_subprotocol(server *s, std::string *out, websocketpp::connection_hdl hdl)
static level const all
Special aggregate value representing "all levels".
static level const all
Special aggregate value representing "all levels".