]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/beast/test/beast/http/serializer.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / beast / test / beast / http / serializer.cpp
1 //
2 // Copyright (c) 2016-2017 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/http/string_body.hpp>
14 #include <boost/beast/unit_test/suite.hpp>
15
16 namespace boost {
17 namespace beast {
18 namespace http {
19
20 class serializer_test : public beast::unit_test::suite
21 {
22 public:
23 struct deprecated_body
24 {
25 using value_type = std::string;
26
27 class writer
28 {
29 public:
30 using const_buffers_type =
31 boost::asio::const_buffer;
32
33 value_type const& body_;
34
35 template<bool isRequest, class Fields>
36 explicit
37 writer(message<isRequest, deprecated_body, Fields> const& m):
38 body_{m.body()}
39 {
40 }
41
42 void init(error_code& ec)
43 {
44 ec.assign(0, ec.category());
45 }
46
47 boost::optional<std::pair<const_buffers_type, bool>>
48 get(error_code& ec)
49 {
50 ec.assign(0, ec.category());
51 return {{const_buffers_type{
52 body_.data(), body_.size()}, false}};
53 }
54 };
55 };
56
57 struct const_body
58 {
59 struct value_type{};
60
61 struct writer
62 {
63 using const_buffers_type =
64 boost::asio::const_buffer;
65
66 template<bool isRequest, class Fields>
67 writer(header<isRequest, Fields> const&, value_type const&);
68
69 void
70 init(error_code& ec);
71
72 boost::optional<std::pair<const_buffers_type, bool>>
73 get(error_code&);
74 };
75 };
76
77 struct mutable_body
78 {
79 struct value_type{};
80
81 struct writer
82 {
83 using const_buffers_type =
84 boost::asio::const_buffer;
85
86 template<bool isRequest, class Fields>
87 writer(header<isRequest, Fields>&, value_type&);
88
89 void
90 init(error_code& ec);
91
92 boost::optional<std::pair<const_buffers_type, bool>>
93 get(error_code&);
94 };
95 };
96
97 BOOST_STATIC_ASSERT(std::is_const< serializer<
98 true, const_body>::value_type>::value);
99
100 BOOST_STATIC_ASSERT(! std::is_const<serializer<
101 true, mutable_body>::value_type>::value);
102
103 BOOST_STATIC_ASSERT(std::is_constructible<
104 serializer<true, const_body>,
105 message <true, const_body>&>::value);
106
107 BOOST_STATIC_ASSERT(std::is_constructible<
108 serializer<true, const_body>,
109 message <true, const_body> const&>::value);
110
111 BOOST_STATIC_ASSERT(std::is_constructible<
112 serializer<true, mutable_body>,
113 message <true, mutable_body>&>::value);
114
115 BOOST_STATIC_ASSERT(! std::is_constructible<
116 serializer<true, mutable_body>,
117 message <true, mutable_body> const&>::value);
118
119 struct lambda
120 {
121 std::size_t size;
122
123 template<class ConstBufferSequence>
124 void
125 operator()(error_code&,
126 ConstBufferSequence const& buffers)
127 {
128 size = boost::asio::buffer_size(buffers);
129 }
130 };
131
132 void
133 testWriteLimit()
134 {
135 auto const limit = 30;
136 lambda visit;
137 error_code ec;
138 response<string_body> res;
139 res.body().append(1000, '*');
140 serializer<false, string_body> sr{res};
141 sr.limit(limit);
142 for(;;)
143 {
144 sr.next(ec, visit);
145 BEAST_EXPECT(visit.size <= limit);
146 sr.consume(visit.size);
147 if(sr.is_done())
148 break;
149 }
150 }
151
152 void testBodyWriterCtor()
153 {
154 response<deprecated_body> res;
155 request<deprecated_body> req;
156 serializer<false, deprecated_body> sr1{res};
157 serializer<true, deprecated_body> sr2{req};
158 boost::ignore_unused(sr1, sr2);
159 }
160
161 void
162 run() override
163 {
164 testWriteLimit();
165 testBodyWriterCtor();
166 }
167 };
168
169 BEAST_DEFINE_TESTSUITE(beast,http,serializer);
170
171 } // http
172 } // beast
173 } // boost