websocket_3_decorator.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <boost/beast/_experimental/unit_test/suite.hpp>
  10. #ifdef BOOST_MSVC
  11. #pragma warning(push)
  12. #pragma warning(disable: 4459) // declaration hides global declaration
  13. #endif
  14. #include <boost/beast.hpp>
  15. #include <boost/beast/ssl.hpp>
  16. #include <boost/asio.hpp>
  17. #include <boost/asio/ssl.hpp>
  18. namespace {
  19. #include "websocket_common.ipp"
  20. //[code_websocket_3_1
  21. void set_user_agent(request_type& req)
  22. {
  23. // Set the User-Agent on the request
  24. req.set(http::field::user_agent, "My User Agent");
  25. }
  26. //]
  27. void
  28. snippets()
  29. {
  30. {
  31. //[code_websocket_3_2
  32. stream<tcp_stream> ws(ioc);
  33. // The function `set_user_agent` will be invoked with
  34. // every upgrade request before it is sent by the stream.
  35. ws.set_option(stream_base::decorator(&set_user_agent));
  36. //]
  37. }
  38. stream<tcp_stream> ws(ioc);
  39. {
  40. //[code_websocket_3_3
  41. struct set_server
  42. {
  43. void operator()(response_type& res)
  44. {
  45. // Set the Server field on the response
  46. res.set(http::field::user_agent, "My Server");
  47. }
  48. };
  49. ws.set_option(stream_base::decorator(set_server{}));
  50. //]
  51. }
  52. {
  53. //[code_websocket_3_4
  54. ws.set_option(stream_base::decorator(
  55. [](response_type& res)
  56. {
  57. // Set the Server field on the response
  58. res.set(http::field::user_agent, "My Server");
  59. }));
  60. //]
  61. }
  62. {
  63. //[code_websocket_3_5
  64. struct set_message_fields
  65. {
  66. void operator()(request_type& req)
  67. {
  68. // Set the User-Agent on the request
  69. req.set(http::field::user_agent, "My User Agent");
  70. }
  71. void operator()(response_type& res)
  72. {
  73. // Set the Server field on the response
  74. res.set(http::field::user_agent, "My Server");
  75. }
  76. };
  77. ws.set_option(stream_base::decorator(set_message_fields{}));
  78. //]
  79. }
  80. {
  81. //[code_websocket_3_6
  82. struct set_auth
  83. {
  84. std::unique_ptr<std::string> key;
  85. void operator()(request_type& req)
  86. {
  87. // Set the authorization field
  88. req.set(http::field::authorization, *key);
  89. }
  90. };
  91. // The stream takes ownership of the decorator object
  92. ws.set_option(stream_base::decorator(
  93. set_auth{boost::make_unique<std::string>("Basic QWxhZGRpbjpPcGVuU2VzYW1l")}));
  94. //]
  95. }
  96. }
  97. struct websocket_3_test
  98. : public boost::beast::unit_test::suite
  99. {
  100. void
  101. run() override
  102. {
  103. BEAST_EXPECT(&snippets);
  104. }
  105. };
  106. BEAST_DEFINE_TESTSUITE(beast,doc,websocket_3);
  107. } // (anon)
  108. #ifdef BOOST_MSVC
  109. #pragma warning(pop)
  110. #endif