Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
client.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the WebSocket++ Project nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27//#define BOOST_TEST_DYN_LINK
28#define BOOST_TEST_MODULE client
29#include <boost/test/unit_test.hpp>
30
31#include <iostream>
32
34
37
39
41 typedef core::concurrency_type concurrency_type;
42
43 typedef core::request_type request_type;
44 typedef core::response_type response_type;
45
46 typedef core::message_type message_type;
47 typedef core::con_msg_manager_type con_msg_manager_type;
48 typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;
49
50 typedef core::alog_type alog_type;
51 typedef core::elog_type elog_type;
52
53 //typedef core::rng_type rng_type;
55
56 typedef core::transport_type transport_type;
57
58 typedef core::endpoint_base endpoint_base;
59
62};
63
66
67BOOST_AUTO_TEST_CASE( invalid_uri ) {
68 client c;
69 websocketpp::lib::error_code ec;
70
71 connection_ptr con = c.get_connection("foo", ec);
72
74}
75
76BOOST_AUTO_TEST_CASE( unsecure_endpoint ) {
77 client c;
78 websocketpp::lib::error_code ec;
79
80 connection_ptr con = c.get_connection("wss://localhost/", ec);
81
83}
84
85BOOST_AUTO_TEST_CASE( get_connection ) {
86 client c;
87 websocketpp::lib::error_code ec;
88
89 connection_ptr con = c.get_connection("ws://localhost/", ec);
90
91 BOOST_CHECK( con );
92 BOOST_CHECK_EQUAL( con->get_host() , "localhost" );
93 BOOST_CHECK_EQUAL( con->get_port() , 80 );
94 BOOST_CHECK_EQUAL( con->get_secure() , false );
95 BOOST_CHECK_EQUAL( con->get_resource() , "/" );
96}
97
98BOOST_AUTO_TEST_CASE( connect_con ) {
99 client c;
100 websocketpp::lib::error_code ec;
101 std::stringstream out;
102 std::string o;
103
104 c.register_ostream(&out);
105
106 connection_ptr con = c.get_connection("ws://localhost/", ec);
107 c.connect(con);
108
109 o = out.str();
111 r.consume(o.data(),o.size());
112
113 BOOST_CHECK( r.ready() );
114 BOOST_CHECK_EQUAL( r.get_method(), "GET");
115 BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1");
116 BOOST_CHECK_EQUAL( r.get_uri(), "/");
117
118 BOOST_CHECK_EQUAL( r.get_header("Host"), "localhost");
119 BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Version"), "13");
120 BOOST_CHECK_EQUAL( r.get_header("Connection"), "Upgrade");
121 BOOST_CHECK_EQUAL( r.get_header("Upgrade"), "websocket");
122
123 // Key is randomly generated & User-Agent will change so just check that
124 // they are not empty.
125 BOOST_CHECK_NE( r.get_header("Sec-WebSocket-Key"), "");
126 BOOST_CHECK_NE( r.get_header("User-Agent"), "" );
127
128 // connection should have written out an opening handshake request and be in
129 // the read response internal state
130
131 // TODO: more tests related to reading the HTTP response
132 std::stringstream channel2;
133 channel2 << "e\r\n\r\n";
134 channel2 >> *con;
135}
136
137BOOST_AUTO_TEST_CASE( select_subprotocol ) {
138 client c;
139 websocketpp::lib::error_code ec;
141
142 connection_ptr con = c.get_connection("ws://localhost/", ec);
143
144 BOOST_CHECK( con );
145
146 con->select_subprotocol("foo",ec);
147 BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::server_only) );
148 BOOST_CHECK_THROW( con->select_subprotocol("foo") , websocketpp::exception );
149}
150
151BOOST_AUTO_TEST_CASE( add_subprotocols_invalid ) {
152 client c;
153 websocketpp::lib::error_code ec;
155
156 connection_ptr con = c.get_connection("ws://localhost/", ec);
157 BOOST_CHECK( con );
158
159 con->add_subprotocol("",ec);
160 BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::invalid_subprotocol) );
161 BOOST_CHECK_THROW( con->add_subprotocol("") , websocketpp::exception );
162
163 con->add_subprotocol("foo,bar",ec);
164 BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::invalid_subprotocol) );
165 BOOST_CHECK_THROW( con->add_subprotocol("foo,bar") , websocketpp::exception );
166}
167
168BOOST_AUTO_TEST_CASE( add_subprotocols ) {
169 client c;
170 websocketpp::lib::error_code ec;
171 std::stringstream out;
172 std::string o;
173
174 c.register_ostream(&out);
175
176 connection_ptr con = c.get_connection("ws://localhost/", ec);
177 BOOST_CHECK( con );
178
179 con->add_subprotocol("foo",ec);
180 BOOST_CHECK( !ec );
181
182 BOOST_CHECK_NO_THROW( con->add_subprotocol("bar") );
183
184 c.connect(con);
185
186 o = out.str();
188 r.consume(o.data(),o.size());
189
190 BOOST_CHECK( r.ready() );
191 BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Protocol"), "foo, bar");
192}
193
194
const mie::Vuint & r
Definition bn.cpp:28
Client endpoint role based on the given config.
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
Stores, parses, and manipulates HTTP requests.
Definition request.hpp:50
Thread safe non-deterministic random integer generator.
BOOST_AUTO_TEST_CASE(invalid_uri)
Definition client.cpp:67
websocketpp::client< stub_config > client
Definition client.cpp:64
client::connection_ptr connection_ptr
@ server_only
Attempted to use a server specific feature on a client endpoint.
Definition error.hpp:108
@ endpoint_not_secure
Attempted to open a secure connection with an insecure endpoint.
Definition error.hpp:57
@ invalid_subprotocol
Invalid subprotocol.
Definition error.hpp:89
@ invalid_uri
An invalid uri was supplied.
Definition error.hpp:65
lib::error_code make_error_code(error::value e)
Definition error.hpp:235
uint32_t level
Type of a channel package.
Definition levels.hpp:37
core::alog_type alog_type
core::endpoint_msg_manager_type endpoint_msg_manager_type
core::elog_type elog_type
core::message_type message_type
static const websocketpp::log::level alog_level
Definition client.cpp:61
core::endpoint_base endpoint_base
core::transport_type transport_type
core::con_msg_manager_type con_msg_manager_type
static const websocketpp::log::level elog_level
Definition client.cpp:60
core::request_type request_type
core::concurrency_type concurrency_type
websocketpp::random::random_device::int_generator< uint32_t, concurrency_type > rng_type
Definition client.cpp:54
core::response_type response_type
Server config with iostream transport.
Definition core.hpp:67
static level const none
Special aggregate value representing "no levels".
Definition levels.hpp:114
static level const none
Special aggregate value representing "no levels".
Definition levels.hpp:61