1 // 2 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) 3 // 4 // Distributed under the Boost Software License, Version 1.0. (See accompanying 5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6 // 7 // Official repository: https://github.com/boostorg/beast 8 // 9 10 #include <boost/config.hpp> 11 12 #ifdef BOOST_MSVC 13 #pragma warning(push) 14 #pragma warning(disable: 4459) // declaration hides global declaration 15 #endif 16 17 #include <boost/beast/_experimental/unit_test/suite.hpp> 18 19 #include <boost/beast.hpp> 20 #include <boost/beast/ssl.hpp> 21 #include <boost/asio.hpp> 22 #include <boost/asio/ssl.hpp> 23 24 namespace { 25 26 #include "websocket_common.ipp" 27 28 //[code_websocket_3_1 29 set_user_agent(request_type & req)30void set_user_agent(request_type& req) 31 { 32 // Set the User-Agent on the request 33 req.set(http::field::user_agent, "My User Agent"); 34 } 35 36 //] 37 38 void snippets()39snippets() 40 { 41 { 42 //[code_websocket_3_2 43 44 stream<tcp_stream> ws(ioc); 45 46 // The function `set_user_agent` will be invoked with 47 // every upgrade request before it is sent by the stream. 48 49 ws.set_option(stream_base::decorator(&set_user_agent)); 50 51 //] 52 } 53 54 stream<tcp_stream> ws(ioc); 55 56 { 57 //[code_websocket_3_3 58 59 struct set_server 60 { 61 void operator()(response_type& res) 62 { 63 // Set the Server field on the response 64 res.set(http::field::user_agent, "My Server"); 65 } 66 }; 67 68 ws.set_option(stream_base::decorator(set_server{})); 69 70 //] 71 } 72 73 { 74 //[code_websocket_3_4 75 76 ws.set_option(stream_base::decorator( 77 [](response_type& res) 78 { 79 // Set the Server field on the response 80 res.set(http::field::user_agent, "My Server"); 81 })); 82 83 //] 84 } 85 86 { 87 //[code_websocket_3_5 88 89 struct set_message_fields 90 { 91 void operator()(request_type& req) 92 { 93 // Set the User-Agent on the request 94 req.set(http::field::user_agent, "My User Agent"); 95 } 96 97 void operator()(response_type& res) 98 { 99 // Set the Server field on the response 100 res.set(http::field::user_agent, "My Server"); 101 } 102 }; 103 104 ws.set_option(stream_base::decorator(set_message_fields{})); 105 106 //] 107 } 108 109 { 110 //[code_websocket_3_6 111 112 struct set_auth 113 { 114 std::unique_ptr<std::string> key; 115 116 void operator()(request_type& req) 117 { 118 // Set the authorization field 119 req.set(http::field::authorization, *key); 120 } 121 }; 122 123 // The stream takes ownership of the decorator object 124 ws.set_option(stream_base::decorator( 125 set_auth{boost::make_unique<std::string>("Basic QWxhZGRpbjpPcGVuU2VzYW1l")})); 126 127 //] 128 } 129 } 130 131 struct websocket_3_test 132 : public boost::beast::unit_test::suite 133 { 134 void run__anon8bb146af0111::websocket_3_test135 run() override 136 { 137 BEAST_EXPECT(&snippets); 138 } 139 }; 140 141 BEAST_DEFINE_TESTSUITE(beast,doc,websocket_3); 142 143 } // (anon) 144 145 #ifdef BOOST_MSVC 146 #pragma warning(pop) 147 #endif 148