| 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 | // Test that header file is self-contained. |
| 11 | #include <boost/beast/http/serializer.hpp> |
| 12 | |
| 13 | #include <boost/beast/core/buffer_traits.hpp> |
| 14 | #include <boost/beast/http/string_body.hpp> |
| 15 | #include <boost/beast/_experimental/unit_test/suite.hpp> |
| 16 | |
| 17 | namespace boost { |
| 18 | namespace beast { |
| 19 | namespace http { |
| 20 | |
| 21 | class serializer_test : public beast::unit_test::suite |
| 22 | { |
| 23 | public: |
| 24 | struct const_body |
| 25 | { |
| 26 | struct value_type{}; |
| 27 | |
| 28 | struct writer |
| 29 | { |
| 30 | using const_buffers_type = |
| 31 | net::const_buffer; |
| 32 | |
| 33 | template<bool isRequest, class Fields> |
| 34 | writer(header<isRequest, Fields> const&, value_type const&); |
| 35 | |
| 36 | void |
| 37 | init(error_code& ec); |
| 38 | |
| 39 | boost::optional<std::pair<const_buffers_type, bool>> |
| 40 | get(error_code&); |
| 41 | }; |
| 42 | }; |
| 43 | |
| 44 | struct mutable_body |
| 45 | { |
| 46 | struct value_type{}; |
| 47 | |
| 48 | struct writer |
| 49 | { |
| 50 | using const_buffers_type = |
| 51 | net::const_buffer; |
| 52 | |
| 53 | template<bool isRequest, class Fields> |
| 54 | writer(header<isRequest, Fields>&, value_type&); |
| 55 | |
| 56 | void |
| 57 | init(error_code& ec); |
| 58 | |
| 59 | boost::optional<std::pair<const_buffers_type, bool>> |
| 60 | get(error_code&); |
| 61 | }; |
| 62 | }; |
| 63 | |
| 64 | BOOST_STATIC_ASSERT(std::is_const< serializer< |
| 65 | true, const_body>::value_type>::value); |
| 66 | |
| 67 | BOOST_STATIC_ASSERT(! std::is_const<serializer< |
| 68 | true, mutable_body>::value_type>::value); |
| 69 | |
| 70 | BOOST_STATIC_ASSERT(std::is_constructible< |
| 71 | serializer<true, const_body>, |
| 72 | message <true, const_body>&>::value); |
| 73 | |
| 74 | BOOST_STATIC_ASSERT(std::is_constructible< |
| 75 | serializer<true, const_body>, |
| 76 | message <true, const_body> const&>::value); |
| 77 | |
| 78 | BOOST_STATIC_ASSERT(std::is_constructible< |
| 79 | serializer<true, mutable_body>, |
| 80 | message <true, mutable_body>&>::value); |
| 81 | |
| 82 | BOOST_STATIC_ASSERT(! std::is_constructible< |
| 83 | serializer<true, mutable_body>, |
| 84 | message <true, mutable_body> const&>::value); |
| 85 | |
| 86 | struct lambda |
| 87 | { |
| 88 | std::size_t size; |
| 89 | |
| 90 | template<class ConstBufferSequence> |
| 91 | void |
| 92 | operator()(error_code&, |
| 93 | ConstBufferSequence const& buffers) |
| 94 | { |
| 95 | size = buffer_bytes(buffers); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | void |
| 100 | testWriteLimit() |
| 101 | { |
| 102 | auto const limit = 30; |
| 103 | lambda visit; |
| 104 | error_code ec; |
| 105 | response<string_body> res; |
| 106 | res.body().append(n: 1000, c: '*'); |
| 107 | serializer<false, string_body> sr{res}; |
| 108 | sr.limit(limit); |
| 109 | for(;;) |
| 110 | { |
| 111 | sr.next(ec, visit); |
| 112 | BEAST_EXPECT(visit.size <= limit); |
| 113 | sr.consume(n: visit.size); |
| 114 | if(sr.is_done()) |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | run() override |
| 121 | { |
| 122 | testWriteLimit(); |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | BEAST_DEFINE_TESTSUITE(beast,http,serializer); |
| 127 | |
| 128 | } // http |
| 129 | } // beast |
| 130 | } // boost |
| 131 | |