Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
server.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
Include dependency graph for server.cpp:

Go to the source code of this file.

Macros

#define BOOST_TEST_MODULE   server
 

Typedefs

typedef websocketpp::server< websocketpp::config::coreserver
 

Functions

std::string run_server_test (server &s, std::string input)
 
void echo_func (server *s, websocketpp::connection_hdl hdl, message_ptr msg)
 
bool validate_func_subprotocol (server *s, std::string *out, std::string accept, websocketpp::connection_hdl hdl)
 
void open_func_subprotocol (server *s, std::string *out, websocketpp::connection_hdl hdl)
 
 BOOST_AUTO_TEST_CASE (basic_websocket_request)
 
 BOOST_AUTO_TEST_CASE (invalid_websocket_version)
 
 BOOST_AUTO_TEST_CASE (unimplemented_websocket_version)
 
 BOOST_AUTO_TEST_CASE (list_subprotocol_empty)
 
 BOOST_AUTO_TEST_CASE (list_subprotocol_one)
 
 BOOST_AUTO_TEST_CASE (accept_subprotocol_one)
 
 BOOST_AUTO_TEST_CASE (accept_subprotocol_invalid)
 
 BOOST_AUTO_TEST_CASE (accept_subprotocol_two)
 

Macro Definition Documentation

◆ BOOST_TEST_MODULE

#define BOOST_TEST_MODULE   server

Definition at line 28 of file server.cpp.

Typedef Documentation

◆ server

Definition at line 38 of file server.cpp.

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/8]

BOOST_AUTO_TEST_CASE ( accept_subprotocol_invalid )

Definition at line 203 of file server.cpp.

203 {
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";
205
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";
207
208 std::string validate;
209 std::string open;
210
211 server s;
212 s.set_user_agent("test");
213 s.set_validate_handler(bind(&validate_func_subprotocol,&s,&validate,"foo2",::_1));
214 s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
215
216 std::string o;
217
218 BOOST_CHECK_THROW(o = run_server_test(s,input), websocketpp::exception);
219}
void set_user_agent(std::string const &ua)
Sets the user agent string that this endpoint will use.
Definition endpoint.hpp:196
bool validate(server *, websocketpp::connection_hdl)
bool validate_func_subprotocol(server *s, std::string *out, std::string accept, websocketpp::connection_hdl hdl)
Definition server.cpp:90
std::string run_server_test(server &s, std::string input)
Definition server.cpp:66
void open_func_subprotocol(server *s, std::string *out, websocketpp::connection_hdl hdl)
Definition server.cpp:113
char * s
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [2/8]

BOOST_AUTO_TEST_CASE ( accept_subprotocol_one )

Definition at line 185 of file server.cpp.

185 {
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";
187
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";
189
190 std::string validate;
191 std::string open;
192
193 server s;
194 s.set_user_agent("test");
195 s.set_validate_handler(bind(&validate_func_subprotocol,&s,&validate,"foo",::_1));
196 s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
197
198 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
199 BOOST_CHECK_EQUAL(validate, "foo,");
200 BOOST_CHECK_EQUAL(open, "foo");
201}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [3/8]

BOOST_AUTO_TEST_CASE ( accept_subprotocol_two )

Definition at line 221 of file server.cpp.

221 {
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";
223
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";
225
226 std::string validate;
227 std::string open;
228
229 server s;
230 s.set_user_agent("test");
231 s.set_validate_handler(bind(&validate_func_subprotocol,&s,&validate,"bar",::_1));
232 s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
233
234 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
235 BOOST_CHECK_EQUAL(validate, "foo,bar,");
236 BOOST_CHECK_EQUAL(open, "bar");
237}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [4/8]

BOOST_AUTO_TEST_CASE ( basic_websocket_request )

Definition at line 120 of file server.cpp.

120 {
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";
123
124 server s;
125 s.set_user_agent("test");
126
127 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
128}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [5/8]

BOOST_AUTO_TEST_CASE ( invalid_websocket_version )

Definition at line 130 of file server.cpp.

130 {
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";
133
134 server s;
135 s.set_user_agent("test");
136 //s.set_message_handler(bind(&echo_func,&s,::_1,::_2));
137
138 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
139}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [6/8]

BOOST_AUTO_TEST_CASE ( list_subprotocol_empty )

Definition at line 152 of file server.cpp.

152 {
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";
154
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";
156
157 std::string subprotocol;
158
159 server s;
160 s.set_user_agent("test");
161 s.set_open_handler(bind(&open_func_subprotocol,&s,&subprotocol,::_1));
162
163 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
164 BOOST_CHECK_EQUAL(subprotocol, "");
165}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [7/8]

BOOST_AUTO_TEST_CASE ( list_subprotocol_one )

Definition at line 167 of file server.cpp.

167 {
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";
169
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";
171
172 std::string validate;
173 std::string open;
174
175 server s;
176 s.set_user_agent("test");
177 s.set_validate_handler(bind(&validate_func_subprotocol,&s,&validate,"",::_1));
178 s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
179
180 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
181 BOOST_CHECK_EQUAL(validate, "foo,");
182 BOOST_CHECK_EQUAL(open, "");
183}
Here is the call graph for this function:

◆ BOOST_AUTO_TEST_CASE() [8/8]

BOOST_AUTO_TEST_CASE ( unimplemented_websocket_version )

Definition at line 141 of file server.cpp.

141 {
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";
143
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";
145
146 server s;
147 s.set_user_agent("test");
148
149 BOOST_CHECK_EQUAL(run_server_test(s,input), output);
150}
Here is the call graph for this function:

◆ echo_func()

void echo_func ( server * s,
websocketpp::connection_hdl hdl,
message_ptr msg )

Definition at line 86 of file server.cpp.

86 {
87 s->send(hdl, msg->get_payload(), msg->get_opcode());
88}

◆ open_func_subprotocol()

void open_func_subprotocol ( server * s,
std::string * out,
websocketpp::connection_hdl hdl )

Definition at line 113 of file server.cpp.

113 {
114 server::connection_ptr con = s->get_con_from_hdl(hdl);
115
116 *out = con->get_subprotocol();
117}
Here is the caller graph for this function:

◆ run_server_test()

std::string run_server_test ( server & s,
std::string input )

Definition at line 66 of file server.cpp.

66 {
68 std::stringstream output;
69
70 s.register_ostream(&output);
71 s.clear_access_channels(websocketpp::log::alevel::all);
72 s.clear_error_channels(websocketpp::log::elevel::all);
73
74 con = s.get_connection();
75 con->start();
76
77 std::stringstream channel;
78
79 channel << input;
80 channel >> *con;
81
82 return output.str();
83}
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:80
Here is the caller graph for this function:

◆ validate_func_subprotocol()

bool validate_func_subprotocol ( server * s,
std::string * out,
std::string accept,
websocketpp::connection_hdl hdl )

Definition at line 90 of file server.cpp.

92{
93 server::connection_ptr con = s->get_con_from_hdl(hdl);
94
95 std::stringstream o;
96
97 const std::vector<std::string> & protocols = con->get_requested_subprotocols();
98 std::vector<std::string>::const_iterator it;
99
100 for (it = protocols.begin(); it != protocols.end(); ++it) {
101 o << *it << ",";
102 }
103
104 *out = o.str();
105
106 if (!accept.empty()) {
107 con->select_subprotocol(accept);
108 }
109
110 return true;
111}
@ accept
Accept any value the remote endpoint offers.
Definition enabled.hpp:194
Here is the caller graph for this function: