mirror of
https://github.com/Xahau/xahaud.git
synced 2025-11-28 06:25:49 +00:00
Refactor beast core, http, tests, and examples:
* Fix warnings * Port cmake scripts to linux * Add command line options for running test suites * Add examples to CMakeLists * Return std::uint64_t from writer::content_length * basic_parser::write takes asio::const_buffer instead of pointer and size * Turn message test back on now that it passes * Rename to http::headers, use std::allocator, remove http_headers * http::message::method is now a string * Refactor to_string for ConstBufferSequence * Remove chunk_encode from the public interface * Initialize members for default constructed iterators * Disallow default construction for dependent buffer sequences Refactor http::message serialization: * Serialization no longer creates a copy of the headers and modifies them * New function prepare(), sets Connection, Transfer-Encoding, Content-Length based on the body attributes and caller options. Callers can use prepare() to have the fields set automatically, or they can set the fields manually. * Use write for operator<< * Tests for serialization
This commit is contained in:
@@ -7,3 +7,252 @@
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/write.hpp>
|
||||
|
||||
#include <beast/http/error.hpp>
|
||||
#include <beast/http/headers.hpp>
|
||||
#include <beast/http/message.hpp>
|
||||
#include <beast/http/empty_body.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/http/write.hpp>
|
||||
#include <beast/streambuf.hpp>
|
||||
#include <beast/to_string.hpp>
|
||||
#include <beast/detail/unit_test/suite.hpp>
|
||||
#include <boost/asio/error.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class write_test : public beast::detail::unit_test::suite
|
||||
{
|
||||
public:
|
||||
struct string_SyncStream
|
||||
{
|
||||
std::string str;
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t
|
||||
write_some(ConstBufferSequence const& buffers)
|
||||
{
|
||||
error_code ec;
|
||||
auto const n = write_some(buffers, ec);
|
||||
if(ec)
|
||||
throw boost::system::system_error{ec};
|
||||
return n;
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t write_some(
|
||||
ConstBufferSequence const& buffers, error_code& ec)
|
||||
{
|
||||
auto const n = buffer_size(buffers);
|
||||
using boost::asio::buffer_size;
|
||||
using boost::asio::buffer_cast;
|
||||
str.reserve(str.size() + n);
|
||||
for(auto const& buffer : buffers)
|
||||
str.append(buffer_cast<char const*>(buffer),
|
||||
buffer_size(buffer));
|
||||
return n;
|
||||
}
|
||||
};
|
||||
|
||||
struct test_Body
|
||||
{
|
||||
using value_type = std::string;
|
||||
|
||||
class writer
|
||||
{
|
||||
value_type const& body_;
|
||||
|
||||
public:
|
||||
template<bool isRequest, class Allocator>
|
||||
explicit
|
||||
writer(message<isRequest, test_Body, Allocator> const& msg)
|
||||
: body_(msg.body)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
init(error_code& ec)
|
||||
{
|
||||
}
|
||||
|
||||
template<class Write>
|
||||
boost::tribool
|
||||
operator()(resume_context&&, error_code&, Write&& write)
|
||||
{
|
||||
write(boost::asio::buffer(body_));
|
||||
return true;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template<bool isRequest, class Body, class Headers>
|
||||
std::string
|
||||
str(message<isRequest, Body, Headers> const& m)
|
||||
{
|
||||
string_SyncStream ss;
|
||||
write(ss, m);
|
||||
return ss.str;
|
||||
}
|
||||
|
||||
void
|
||||
testWrite()
|
||||
{
|
||||
// auto content-length HTTP/1.0
|
||||
{
|
||||
message<true, string_body, headers> m{{
|
||||
"GET", "/", 10}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
expect(str(m) ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"\r\n"
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// keep-alive HTTP/1.0
|
||||
{
|
||||
message<true, string_body, headers> m{{
|
||||
"GET", "/", 10}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m, connection::keep_alive);
|
||||
expect(str(m) ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"Connection: keep-alive\r\n"
|
||||
"\r\n"
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// upgrade HTTP/1.0
|
||||
{
|
||||
message<true, string_body, headers> m{{
|
||||
"GET", "/", 10}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
try
|
||||
{
|
||||
prepare(m, connection::upgrade);
|
||||
fail();
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
pass();
|
||||
}
|
||||
}
|
||||
// no content-length HTTP/1.0
|
||||
{
|
||||
message<true, test_Body, headers> m{{
|
||||
"GET", "/", 10}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
string_SyncStream ss;
|
||||
error_code ec;
|
||||
write(ss, m, ec);
|
||||
expect(ec == boost::asio::error::eof);
|
||||
expect(ss.str ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"\r\n"
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// auto content-length HTTP/1.1
|
||||
{
|
||||
message<true, string_body, headers> m{{
|
||||
"GET", "/", 11}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
expect(str(m) ==
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"\r\n"
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// close HTTP/1.1
|
||||
{
|
||||
message<true, string_body, headers> m{{
|
||||
"GET", "/", 11}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m, connection::close);
|
||||
string_SyncStream ss;
|
||||
error_code ec;
|
||||
write(ss, m, ec);
|
||||
expect(ec == boost::asio::error::eof);
|
||||
expect(ss.str ==
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// upgrade HTTP/1.1
|
||||
{
|
||||
message<true, empty_body, headers> m{{
|
||||
"GET", "/", 11}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
prepare(m, connection::upgrade);
|
||||
expect(str(m) ==
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Connection: upgrade\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
}
|
||||
// no content-length HTTP/1.1
|
||||
{
|
||||
message<true, test_Body, headers> m{{
|
||||
"GET", "/", 11}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
string_SyncStream ss;
|
||||
error_code ec;
|
||||
write(ss, m, ec);
|
||||
expect(ss.str ==
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"1\r\n"
|
||||
"*\r\n"
|
||||
"0\r\n\r\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void testConvert()
|
||||
{
|
||||
message<true, string_body, headers> m{{
|
||||
"GET", "/", 11}};
|
||||
m.headers.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
expect(boost::lexical_cast<std::string>(m) ==
|
||||
"GET / HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: 1\r\n\r\n*");
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
testWrite();
|
||||
testConvert();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(write,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
Reference in New Issue
Block a user