]>
git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/test/http/message.cpp
2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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)
8 // Test that header file is self-contained.
9 #include <beast/http/message.hpp>
11 #include <beast/http/string_body.hpp>
12 #include <beast/http/fields.hpp>
13 #include <beast/http/string_body.hpp>
14 #include <beast/unit_test/suite.hpp>
15 #include <type_traits>
20 class message_test
: public beast::unit_test::suite
38 // default constructible Body
41 using value_type
= std::string
;
44 // 1-arg constructible Body
50 value_type(Arg1
const&)
55 value_type(Arg1
&& arg
)
57 Arg1
arg_(std::move(arg
));
62 // 2-arg constructible Body
67 value_type(Arg1
const&, Arg2
const&)
75 static_assert(std::is_constructible
<
76 message
<true, default_body
, fields
>>::value
, "");
78 static_assert(std::is_constructible
<
79 message
<true, one_arg_body
, fields
>, Arg1
>::value
, "");
81 static_assert(std::is_constructible
<
82 message
<true, one_arg_body
, fields
>, Arg1
const>::value
, "");
84 static_assert(std::is_constructible
<
85 message
<true, one_arg_body
, fields
>, Arg1
const&>::value
, "");
87 static_assert(std::is_constructible
<
88 message
<true, one_arg_body
, fields
>, Arg1
&&>::value
, "");
90 static_assert(! std::is_constructible
<
91 message
<true, one_arg_body
, fields
>>::value
, "");
93 static_assert(std::is_constructible
<
94 message
<true, one_arg_body
, fields
>,
95 Arg1
, fields::allocator_type
>::value
, "");
97 static_assert(std::is_constructible
<
98 message
<true, one_arg_body
, fields
>, std::piecewise_construct_t
,
99 std::tuple
<Arg1
>>::value
, "");
101 static_assert(std::is_constructible
<
102 message
<true, two_arg_body
, fields
>, std::piecewise_construct_t
,
103 std::tuple
<Arg1
, Arg2
>>::value
, "");
105 static_assert(std::is_constructible
<
106 message
<true, two_arg_body
, fields
>, std::piecewise_construct_t
,
107 std::tuple
<Arg1
, Arg2
>, std::tuple
<fields::allocator_type
>>::value
, "");
111 message
<true, one_arg_body
, fields
>{std::move(arg1
)};
112 BEAST_EXPECT(arg1
.moved
);
117 h
.insert("User-Agent", "test");
118 message
<true, one_arg_body
, fields
> m
{Arg1
{}, h
};
119 BEAST_EXPECT(h
["User-Agent"] == "test");
120 BEAST_EXPECT(m
.fields
["User-Agent"] == "test");
124 h
.insert("User-Agent", "test");
125 message
<true, one_arg_body
, fields
> m
{Arg1
{}, std::move(h
)};
126 BEAST_EXPECT(! h
.exists("User-Agent"));
127 BEAST_EXPECT(m
.fields
["User-Agent"] == "test");
131 message
<true, string_body
, fields
> m1
;
132 message
<true, string_body
, fields
> m2
;
135 m1
.fields
.insert("h", "v");
139 BEAST_EXPECT(m1
.method
== "G");
140 BEAST_EXPECT(m2
.method
.empty());
141 BEAST_EXPECT(m1
.url
.empty());
142 BEAST_EXPECT(m2
.url
== "u");
143 BEAST_EXPECT(m1
.body
== "2");
144 BEAST_EXPECT(m2
.body
== "1");
145 BEAST_EXPECT(! m1
.fields
.exists("h"));
146 BEAST_EXPECT(m2
.fields
.exists("h"));
151 bool moved_to
= false;
152 bool moved_from
= false;
154 MoveHeaders() = default;
156 MoveHeaders(MoveHeaders
&& other
)
159 other
.moved_from
= true;
162 MoveHeaders
& operator=(MoveHeaders
&& other
)
171 using req_type
= request_header
;
172 static_assert(std::is_copy_constructible
<req_type
>::value
, "");
173 static_assert(std::is_move_constructible
<req_type
>::value
, "");
174 static_assert(std::is_copy_assignable
<req_type
>::value
, "");
175 static_assert(std::is_move_assignable
<req_type
>::value
, "");
177 using res_type
= response_header
;
178 static_assert(std::is_copy_constructible
<res_type
>::value
, "");
179 static_assert(std::is_move_constructible
<res_type
>::value
, "");
180 static_assert(std::is_copy_assignable
<res_type
>::value
, "");
181 static_assert(std::is_move_assignable
<res_type
>::value
, "");
186 header
<true, MoveHeaders
> r
{std::move(h
)};
187 BEAST_EXPECT(h
.moved_from
);
188 BEAST_EXPECT(r
.fields
.moved_to
);
189 request
<string_body
, MoveHeaders
> m
{std::move(r
)};
190 BEAST_EXPECT(r
.fields
.moved_from
);
191 BEAST_EXPECT(m
.fields
.moved_to
);
195 void testFreeFunctions()
198 request
<string_body
> m
;
202 m
.fields
.insert("Upgrade", "test");
203 BEAST_EXPECT(! is_upgrade(m
));
205 prepare(m
, connection::upgrade
);
206 BEAST_EXPECT(is_upgrade(m
));
207 BEAST_EXPECT(m
.fields
["Connection"] == "upgrade");
210 BEAST_EXPECT(! is_upgrade(m
));
216 request
<string_body
> m
;
218 BEAST_EXPECT(! is_upgrade(m
));
219 m
.fields
.insert("Transfer-Encoding", "chunked");
225 catch(std::exception
const&)
228 m
.fields
.erase("Transfer-Encoding");
229 m
.fields
.insert("Content-Length", "0");
235 catch(std::exception
const&)
239 m
.fields
.erase("Content-Length");
240 m
.fields
.insert("Connection", "keep-alive");
246 catch(std::exception
const&)
251 m
.fields
.erase("Connection");
252 m
.fields
.insert("Connection", "close");
253 BEAST_EXPECT(! is_keep_alive(m
));
258 message
<false, string_body
, fields
> m1
;
259 message
<false, string_body
, fields
> m2
;
263 m1
.fields
.insert("h", "v");
269 BEAST_EXPECT(m1
.status
== 404);
270 BEAST_EXPECT(m2
.status
== 200);
271 BEAST_EXPECT(m1
.reason
== "OK");
272 BEAST_EXPECT(m2
.reason
.empty());
273 BEAST_EXPECT(m1
.version
== 11);
274 BEAST_EXPECT(m2
.version
== 10);
275 BEAST_EXPECT(m1
.body
== "2");
276 BEAST_EXPECT(m2
.body
== "1");
277 BEAST_EXPECT(! m1
.fields
.exists("h"));
278 BEAST_EXPECT(m2
.fields
.exists("h"));
284 response
<string_body
> r1
;
285 response
<string_body
> r2
{r1
};
286 response
<string_body
> r3
{std::move(r2
)};
301 testSpecialMembers();
305 BEAST_DEFINE_TESTSUITE(message
,http
,beast
);