NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
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 
36 #include <websocketpp/client.hpp>
37 
39 
40 struct stub_config : public websocketpp::config::core {
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;
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 
68  client c;
69  websocketpp::lib::error_code ec;
70 
71  connection_ptr con = c.get_connection("foo", ec);
72 
74 }
75 
76 BOOST_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 
85 BOOST_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 
98 BOOST_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 
137 BOOST_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 
151 BOOST_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 
168 BOOST_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 
connection_type::ptr connection_ptr
Type of a shared pointer to the connections this server will create.
static level const none
Special aggregate value representing "no levels".
Definition: levels.hpp:61
lib::error_code make_error_code(error::value e)
Definition: error.hpp:235
core::con_msg_manager_type con_msg_manager_type
Definition: client.cpp:47
core::message_type message_type
Definition: client.cpp:46
core::endpoint_base endpoint_base
Definition: client.cpp:58
core::transport_type transport_type
Definition: client.cpp:56
Client endpoint role based on the given config.
stub_config::con_msg_manager_type con_msg_manager_type
Definition: hybi13.cpp:89
websocketpp::random::random_device::int_generator< uint32_t, concurrency_type > rng_type
Definition: client.cpp:54
std::string const & get_uri() const
Return the requested URI.
Definition: request.hpp:104
connection_ptr connect(connection_ptr con)
Begin the connection process for the given connection.
BOOST_AUTO_TEST_CASE(invalid_uri)
Definition: client.cpp:67
core::elog_type elog_type
Definition: client.cpp:51
std::string const & get_header(std::string const &key) const
Get the value of an HTTP header.
Definition: parser.hpp:45
static const websocketpp::log::level elog_level
Definition: client.cpp:60
core::endpoint_msg_manager_type endpoint_msg_manager_type
Definition: client.cpp:48
static level const none
Special aggregate value representing "no levels".
Definition: levels.hpp:114
client::connection_ptr connection_ptr
Definition: client.cpp:65
std::string const & get_version() const
Get the HTTP version string.
Definition: parser.hpp:410
websocketpp::client< stub_config > client
Definition: client.cpp:64
core::alog_type alog_type
Definition: client.cpp:50
core::concurrency_type concurrency_type
Definition: client.cpp:41
core::request_type request_type
Definition: client.cpp:43
Thread safe non-deterministic random integer generator.
Stores, parses, and manipulates HTTP requests.
Definition: request.hpp:50
static const websocketpp::log::level alog_level
Definition: client.cpp:61
size_t consume(char const *buf, size_t len)
Process bytes in the input buffer.
Definition: request.hpp:41
client::connection_ptr connection_ptr
core::response_type response_type
Definition: client.cpp:44
uint32_t level
Type of a channel package.
Definition: levels.hpp:37
Attempted to use a server specific feature on a client endpoint.
Definition: error.hpp:108
Attempted to open a secure connection with an insecure endpoint.
Definition: error.hpp:57
connection_ptr get_connection(uri_ptr location, lib::error_code &ec)
Get a new connection.
bool ready() const
Returns whether or not the request is ready for reading.
Definition: request.hpp:82
An invalid uri was supplied.
Definition: error.hpp:65
Server config with iostream transport.
Definition: core.hpp:67
std::string const & get_method() const
Return the request method.
Definition: request.hpp:96