forked from boostorg/beast
Refactor header and message constructors:
fix #581 * request and response headers now have convenience constructors so important fields like method, target, result, and version may be set upon construction. Actions Required: * Evaluate each message constructor call site and adjust the constructor argument list as needed.
This commit is contained in:
+129
-64
@@ -8,6 +8,7 @@
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/message.hpp>
|
||||
|
||||
#include <beast/http/empty_body.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/http/fields.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
@@ -70,62 +71,78 @@ public:
|
||||
};
|
||||
};
|
||||
|
||||
// 0-arg
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<default_body>>::value);
|
||||
|
||||
// 1-arg
|
||||
BOOST_STATIC_ASSERT(! std::is_constructible<request<one_arg_body>
|
||||
>::value);
|
||||
|
||||
//BOOST_STATIC_ASSERT(! std::is_constructible<request<one_arg_body>,
|
||||
// verb, string_view, unsigned>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1&&>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1 const>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1 const&>::value);
|
||||
|
||||
// 1-arg + fields
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1, fields::allocator_type>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1>>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<two_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1, Arg2>>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<two_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1, Arg2>, std::tuple<fields::allocator_type>>::value);
|
||||
|
||||
// special members
|
||||
BOOST_STATIC_ASSERT(std::is_copy_constructible<header<true>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_constructible<header<true>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_copy_assignable<header<true>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_assignable<header<true>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_copy_constructible<header<false>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_constructible<header<false>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_copy_assignable<header<false>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_assignable<header<false>>::value);
|
||||
|
||||
void
|
||||
testMessage()
|
||||
{
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<default_body>>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<one_arg_body>, Arg1>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<one_arg_body>, Arg1 const>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<one_arg_body>, Arg1 const&>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<one_arg_body>, Arg1&&>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(! std::is_constructible<
|
||||
request<one_arg_body>>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<one_arg_body>,
|
||||
Arg1, fields::allocator_type>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<one_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1>>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<two_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1, Arg2>>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<two_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1, Arg2>, std::tuple<fields::allocator_type>>::value);
|
||||
|
||||
{
|
||||
Arg1 arg1;
|
||||
request<one_arg_body>{std::move(arg1)};
|
||||
request<one_arg_body>{verb::get, "/", 11, std::move(arg1)};
|
||||
BEAST_EXPECT(arg1.moved);
|
||||
}
|
||||
|
||||
{
|
||||
header<true> h;
|
||||
h.insert(field::user_agent, "test");
|
||||
request<one_arg_body> m{Arg1{}, h};
|
||||
BEAST_EXPECT(h["User-Agent"] == "test");
|
||||
BEAST_EXPECT(m["User-Agent"] == "test");
|
||||
h.set(field::user_agent, "test");
|
||||
BEAST_EXPECT(h[field::user_agent] == "test");
|
||||
request<default_body> m{std::move(h)};
|
||||
BEAST_EXPECT(m[field::user_agent] == "test");
|
||||
BEAST_EXPECT(h.count(field::user_agent) == 0);
|
||||
}
|
||||
{
|
||||
header<true> h;
|
||||
h.insert(field::user_agent, "test");
|
||||
request<one_arg_body> m{Arg1{}, std::move(h)};
|
||||
BEAST_EXPECT(! h.count(http::field::user_agent));
|
||||
request<empty_body> h{verb::get, "/", 10};
|
||||
h.set(field::user_agent, "test");
|
||||
request<one_arg_body> m{std::move(h.base()), Arg1{}};
|
||||
BEAST_EXPECT(m["User-Agent"] == "test");
|
||||
BEAST_EXPECT(h.count(http::field::user_agent) == 0);
|
||||
BEAST_EXPECT(m.method() == verb::get);
|
||||
BEAST_EXPECT(m.target() == "/");
|
||||
BEAST_EXPECT(m.version == 10);
|
||||
}
|
||||
|
||||
// swap
|
||||
@@ -166,31 +183,79 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
struct token {};
|
||||
|
||||
struct test_fields
|
||||
{
|
||||
std::string target;
|
||||
|
||||
test_fields() = delete;
|
||||
test_fields(token) {}
|
||||
void set_method_impl(string_view) {}
|
||||
void set_target_impl(string_view s) { target = s.to_string(); }
|
||||
void set_reason_impl(string_view) {}
|
||||
string_view get_method_impl() const { return {}; }
|
||||
string_view get_target_impl() const { return target; }
|
||||
string_view get_reason_impl() const { return {}; }
|
||||
void prepare_payload_impl(bool, boost::optional<std::uint64_t>) {}
|
||||
};
|
||||
|
||||
void
|
||||
testHeaders()
|
||||
testMessageCtors()
|
||||
{
|
||||
{
|
||||
using req_type = header<true>;
|
||||
BOOST_STATIC_ASSERT(std::is_copy_constructible<req_type>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_constructible<req_type>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_copy_assignable<req_type>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_assignable<req_type>::value);
|
||||
|
||||
using res_type = header<false>;
|
||||
BOOST_STATIC_ASSERT(std::is_copy_constructible<res_type>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_constructible<res_type>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_copy_assignable<res_type>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_assignable<res_type>::value);
|
||||
request<empty_body> req;
|
||||
BEAST_EXPECT(req.version == 11);
|
||||
BEAST_EXPECT(req.method() == verb::unknown);
|
||||
BEAST_EXPECT(req.target() == "");
|
||||
}
|
||||
|
||||
{
|
||||
MoveFields h;
|
||||
header<true, MoveFields> r{std::move(h)};
|
||||
BEAST_EXPECT(h.moved_from);
|
||||
BEAST_EXPECT(r.moved_to);
|
||||
request<string_body, MoveFields> m{std::move(r)};
|
||||
BEAST_EXPECT(r.moved_from);
|
||||
BEAST_EXPECT(m.moved_to);
|
||||
request<empty_body> req{verb::get, "/", 11};
|
||||
BEAST_EXPECT(req.version == 11);
|
||||
BEAST_EXPECT(req.method() == verb::get);
|
||||
BEAST_EXPECT(req.target() == "/");
|
||||
}
|
||||
{
|
||||
request<string_body> req{verb::get, "/", 11, "Hello"};
|
||||
BEAST_EXPECT(req.version == 11);
|
||||
BEAST_EXPECT(req.method() == verb::get);
|
||||
BEAST_EXPECT(req.target() == "/");
|
||||
BEAST_EXPECT(req.body == "Hello");
|
||||
}
|
||||
{
|
||||
request<string_body, test_fields> req{
|
||||
verb::get, "/", 11, "Hello", token{}};
|
||||
BEAST_EXPECT(req.version == 11);
|
||||
BEAST_EXPECT(req.method() == verb::get);
|
||||
BEAST_EXPECT(req.target() == "/");
|
||||
BEAST_EXPECT(req.body == "Hello");
|
||||
}
|
||||
{
|
||||
response<string_body> res;
|
||||
BEAST_EXPECT(res.version == 11);
|
||||
BEAST_EXPECT(res.result() == status::ok);
|
||||
BEAST_EXPECT(res.reason() == "OK");
|
||||
}
|
||||
{
|
||||
response<string_body> res{status::bad_request, 10};
|
||||
BEAST_EXPECT(res.version == 10);
|
||||
BEAST_EXPECT(res.result() == status::bad_request);
|
||||
BEAST_EXPECT(res.reason() == "Bad Request");
|
||||
}
|
||||
{
|
||||
response<string_body> res{status::bad_request, 10, "Hello"};
|
||||
BEAST_EXPECT(res.version == 10);
|
||||
BEAST_EXPECT(res.result() == status::bad_request);
|
||||
BEAST_EXPECT(res.reason() == "Bad Request");
|
||||
BEAST_EXPECT(res.body == "Hello");
|
||||
}
|
||||
{
|
||||
response<string_body, test_fields> res{
|
||||
status::bad_request, 10, "Hello", token{}};
|
||||
BEAST_EXPECT(res.version == 10);
|
||||
BEAST_EXPECT(res.result() == status::bad_request);
|
||||
BEAST_EXPECT(res.reason() == "Bad Request");
|
||||
BEAST_EXPECT(res.body == "Hello");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -292,7 +357,7 @@ public:
|
||||
run() override
|
||||
{
|
||||
testMessage();
|
||||
testHeaders();
|
||||
testMessageCtors();
|
||||
testSwap();
|
||||
testSpecialMembers();
|
||||
testMethod();
|
||||
|
||||
@@ -27,10 +27,11 @@ public:
|
||||
{
|
||||
static_assert(is_body_reader<string_view_body>::value, "");
|
||||
static_assert(! is_body_writer<string_view_body>::value, "");
|
||||
request<string_view_body> req{"Hello, world!"};
|
||||
request<string_view_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::post);
|
||||
req.target("/");
|
||||
req.body = "Hello, world!";
|
||||
req.prepare_payload();
|
||||
static_buffer_n<512> b;
|
||||
ostream(b) << req;
|
||||
|
||||
+25
-42
@@ -278,9 +278,8 @@ public:
|
||||
response<string_body> m;
|
||||
m.version = 10;
|
||||
m.result(status::ok);
|
||||
m.reason("OK");
|
||||
m.insert(field::server, "test");
|
||||
m.insert("Content-Length", "5");
|
||||
m.set(field::server, "test");
|
||||
m.set(field::content_length, "5");
|
||||
m.body = "*****";
|
||||
error_code ec;
|
||||
test::string_ostream ss{ios_};
|
||||
@@ -297,9 +296,8 @@ public:
|
||||
response<string_body> m;
|
||||
m.version = 11;
|
||||
m.result(status::ok);
|
||||
m.reason("OK");
|
||||
m.insert(field::server, "test");
|
||||
m.insert("Transfer-Encoding", "chunked");
|
||||
m.set(field::server, "test");
|
||||
m.set(field::transfer_encoding, "chunked");
|
||||
m.body = "*****";
|
||||
error_code ec;
|
||||
test::string_ostream ss(ios_);
|
||||
@@ -327,11 +325,8 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
request<fail_body> m{fc};
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.insert(field::user_agent, "test");
|
||||
request<fail_body> m(verb::get, "/", 10, fc);
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::connection, "keep-alive");
|
||||
m.set(field::content_length, "5");
|
||||
m.body = "*****";
|
||||
@@ -360,12 +355,9 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
request<fail_body> m{fc};
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.insert(field::user_agent, "test");
|
||||
m.insert("Transfer-Encoding", "chunked");
|
||||
request<fail_body> m{verb::get, "/", 10, fc};
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::transfer_encoding, "chunked");
|
||||
m.body = "*****";
|
||||
error_code ec = test::error::fail_error;
|
||||
write(fs, m, ec);
|
||||
@@ -393,12 +385,9 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
request<fail_body> m{fc};
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.insert(field::user_agent, "test");
|
||||
m.insert("Transfer-Encoding", "chunked");
|
||||
request<fail_body> m{verb::get, "/", 10, fc};
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::transfer_encoding, "chunked");
|
||||
m.body = "*****";
|
||||
error_code ec = test::error::fail_error;
|
||||
async_write(fs, m, do_yield[ec]);
|
||||
@@ -426,11 +415,8 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
request<fail_body> m{fc};
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.insert(field::user_agent, "test");
|
||||
request<fail_body> m{verb::get, "/", 10, fc};
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::connection, "keep-alive");
|
||||
m.set(field::content_length, "5");
|
||||
m.body = "*****";
|
||||
@@ -456,11 +442,8 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
request<fail_body> m{fc};
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.insert(field::user_agent, "test");
|
||||
request<fail_body> m{verb::get, "/", 10, fc};
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::connection, "keep-alive");
|
||||
m.set(field::content_length, "5");
|
||||
m.body = "*****";
|
||||
@@ -491,7 +474,7 @@ public:
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.insert(field::user_agent, "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
m.prepare_payload();
|
||||
BEAST_EXPECT(str(m) ==
|
||||
@@ -508,7 +491,7 @@ public:
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.insert(field::user_agent, "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
m.prepare_payload();
|
||||
test::string_ostream ss(ios_);
|
||||
@@ -528,7 +511,7 @@ public:
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.insert(field::user_agent, "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
m.prepare_payload();
|
||||
BEAST_EXPECT(str(m) ==
|
||||
@@ -545,7 +528,7 @@ public:
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.insert(field::user_agent, "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
m.prepare_payload();
|
||||
test::string_ostream ss(ios_);
|
||||
@@ -570,7 +553,7 @@ public:
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.insert(field::user_agent, "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
BEAST_EXPECT(boost::lexical_cast<std::string>(m) ==
|
||||
"GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n*");
|
||||
@@ -600,7 +583,7 @@ public:
|
||||
m.method(verb::get);
|
||||
m.version = 11;
|
||||
m.target("/");
|
||||
m.insert("Content-Length", 5);
|
||||
m.set("Content-Length", 5);
|
||||
m.body = "*****";
|
||||
async_write(os, m, handler{});
|
||||
BEAST_EXPECT(handler::count() > 0);
|
||||
@@ -622,7 +605,7 @@ public:
|
||||
m.method(verb::get);
|
||||
m.version = 11;
|
||||
m.target("/");
|
||||
m.insert("Content-Length", 5);
|
||||
m.set("Content-Length", 5);
|
||||
m.body = "*****";
|
||||
async_write(is, m, handler{});
|
||||
BEAST_EXPECT(handler::count() > 0);
|
||||
@@ -704,7 +687,7 @@ public:
|
||||
m0.version = 11;
|
||||
m0.result(status::ok);
|
||||
m0.reason("OK");
|
||||
m0.insert(field::server, "test");
|
||||
m0.set(field::server, "test");
|
||||
m0.body.s = "Hello, world!\n";
|
||||
|
||||
{
|
||||
@@ -761,7 +744,7 @@ public:
|
||||
}
|
||||
}
|
||||
{
|
||||
m0.insert("Transfer-Encoding", "chunked");
|
||||
m0.set("Transfer-Encoding", "chunked");
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
|
||||
Reference in New Issue
Block a user