2017-07-20 08:01:46 -07:00
|
|
|
//
|
|
|
|
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
|
|
|
|
// Test that header file is self-contained.
|
|
|
|
#include <beast/http/write.hpp>
|
2016-04-29 06:04:40 -04:00
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
#include <beast/http/fields.hpp>
|
2016-04-29 06:04:40 -04:00
|
|
|
#include <beast/http/message.hpp>
|
|
|
|
#include <beast/http/empty_body.hpp>
|
|
|
|
#include <beast/http/string_body.hpp>
|
|
|
|
#include <beast/http/write.hpp>
|
2016-05-07 14:57:15 -04:00
|
|
|
#include <beast/core/error.hpp>
|
|
|
|
#include <beast/core/streambuf.hpp>
|
|
|
|
#include <beast/core/to_string.hpp>
|
2016-05-07 17:06:46 -04:00
|
|
|
#include <beast/test/fail_stream.hpp>
|
|
|
|
#include <beast/test/yield_to.hpp>
|
2016-05-06 19:14:17 -04:00
|
|
|
#include <beast/unit_test/suite.hpp>
|
2016-04-29 06:04:40 -04:00
|
|
|
#include <boost/asio/error.hpp>
|
2016-05-07 17:06:46 -04:00
|
|
|
#include <sstream>
|
2016-04-29 06:04:40 -04:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace beast {
|
|
|
|
namespace http {
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
class write_test
|
|
|
|
: public beast::unit_test::suite
|
|
|
|
, public test::enable_yield_to
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
|
|
|
public:
|
2016-05-07 17:06:46 -04:00
|
|
|
class string_write_stream
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
boost::asio::io_service& ios_;
|
2016-05-10 21:21:19 -04:00
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
public:
|
2016-04-29 06:04:40 -04:00
|
|
|
std::string str;
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
explicit
|
|
|
|
string_write_stream(boost::asio::io_service& ios)
|
|
|
|
: ios_(ios)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::asio::io_service&
|
|
|
|
get_io_service()
|
|
|
|
{
|
|
|
|
return ios_;
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:04:40 -04:00
|
|
|
template<class ConstBufferSequence>
|
|
|
|
std::size_t
|
|
|
|
write_some(ConstBufferSequence const& buffers)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
auto const n = write_some(buffers, ec);
|
|
|
|
if(ec)
|
2016-05-07 14:57:15 -04:00
|
|
|
throw system_error{ec};
|
2016-04-29 06:04:40 -04:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class ConstBufferSequence>
|
2016-05-07 17:06:46 -04:00
|
|
|
std::size_t
|
|
|
|
write_some(
|
2016-10-13 12:32:01 +10:00
|
|
|
ConstBufferSequence const& buffers, error_code&)
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
template<class ConstBufferSequence, class WriteHandler>
|
|
|
|
typename async_completion<
|
|
|
|
WriteHandler, void(error_code)>::result_type
|
|
|
|
async_write_some(ConstBufferSequence const& buffers,
|
|
|
|
WriteHandler&& handler)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
auto const bytes_transferred = write_some(buffers, ec);
|
|
|
|
async_completion<
|
|
|
|
WriteHandler, void(error_code, std::size_t)
|
|
|
|
> completion(handler);
|
|
|
|
get_io_service().post(
|
|
|
|
bind_handler(completion.handler, ec, bytes_transferred));
|
|
|
|
return completion.result.get();
|
|
|
|
}
|
2016-04-29 06:04:40 -04:00
|
|
|
};
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
struct unsized_body
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
|
|
|
using value_type = std::string;
|
|
|
|
|
|
|
|
class writer
|
|
|
|
{
|
|
|
|
value_type const& body_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<bool isRequest, class Allocator>
|
|
|
|
explicit
|
2016-10-15 13:40:17 -04:00
|
|
|
writer(message<isRequest, unsized_body, Allocator> const& msg) noexcept
|
2016-05-06 21:51:43 -04:00
|
|
|
: body_(msg.body)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-15 13:40:17 -04:00
|
|
|
init(error_code& ec) noexcept
|
2016-05-06 21:51:43 -04:00
|
|
|
{
|
2016-10-13 12:32:01 +10:00
|
|
|
beast::detail::ignore_unused(ec);
|
2016-05-06 21:51:43 -04:00
|
|
|
}
|
|
|
|
|
2016-10-15 13:40:17 -04:00
|
|
|
template<class WriteFunction>
|
2016-05-06 21:51:43 -04:00
|
|
|
boost::tribool
|
2016-10-15 13:40:17 -04:00
|
|
|
write(resume_context&&, error_code&,
|
|
|
|
WriteFunction&& wf) noexcept
|
2016-05-06 21:51:43 -04:00
|
|
|
{
|
2016-10-15 13:40:17 -04:00
|
|
|
wf(boost::asio::buffer(body_));
|
2016-05-06 21:51:43 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
struct fail_body
|
2016-05-06 21:51:43 -04:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
class writer;
|
|
|
|
|
|
|
|
class value_type
|
|
|
|
{
|
|
|
|
friend class writer;
|
|
|
|
|
|
|
|
std::string s_;
|
|
|
|
test::fail_counter& fc_;
|
|
|
|
boost::asio::io_service& ios_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
value_type(test::fail_counter& fc,
|
|
|
|
boost::asio::io_service& ios)
|
|
|
|
: fc_(fc)
|
|
|
|
, ios_(ios)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::asio::io_service&
|
|
|
|
get_io_service() const
|
|
|
|
{
|
|
|
|
return ios_;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_type&
|
|
|
|
operator=(std::string s)
|
|
|
|
{
|
|
|
|
s_ = std::move(s);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
2016-05-06 21:51:43 -04:00
|
|
|
|
|
|
|
class writer
|
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
std::size_t n_ = 0;
|
2016-05-06 21:51:43 -04:00
|
|
|
value_type const& body_;
|
2016-05-07 17:06:46 -04:00
|
|
|
bool suspend_ = false;
|
|
|
|
enable_yield_to yt_;
|
2016-05-06 21:51:43 -04:00
|
|
|
|
|
|
|
public:
|
|
|
|
template<bool isRequest, class Allocator>
|
|
|
|
explicit
|
2016-10-15 13:40:17 -04:00
|
|
|
writer(message<isRequest, fail_body, Allocator> const& msg) noexcept
|
2016-04-29 06:04:40 -04:00
|
|
|
: body_(msg.body)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-10-15 13:40:17 -04:00
|
|
|
init(error_code& ec) noexcept
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
body_.fc_.fail(ec);
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
class do_resume
|
|
|
|
{
|
|
|
|
resume_context rc_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit
|
|
|
|
do_resume(resume_context&& rc)
|
|
|
|
: rc_(std::move(rc))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
operator()()
|
|
|
|
{
|
|
|
|
rc_();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-15 13:40:17 -04:00
|
|
|
template<class WriteFunction>
|
2016-04-29 06:04:40 -04:00
|
|
|
boost::tribool
|
2016-10-15 13:40:17 -04:00
|
|
|
write(resume_context&& rc, error_code& ec,
|
|
|
|
WriteFunction&& wf) noexcept
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
if(body_.fc_.fail(ec))
|
|
|
|
return false;
|
|
|
|
suspend_ = ! suspend_;
|
|
|
|
if(suspend_)
|
|
|
|
{
|
|
|
|
yt_.get_io_service().post(do_resume{std::move(rc)});
|
|
|
|
return boost::indeterminate;
|
|
|
|
}
|
|
|
|
if(n_ >= body_.s_.size())
|
|
|
|
return true;
|
2016-10-15 13:40:17 -04:00
|
|
|
wf(boost::asio::buffer(body_.s_.data() + n_, 1));
|
2016-05-07 17:06:46 -04:00
|
|
|
++n_;
|
|
|
|
return n_ == body_.s_.size();
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-11-10 05:34:49 -05:00
|
|
|
template<bool isRequest, class Body, class Fields>
|
2016-04-29 06:04:40 -04:00
|
|
|
std::string
|
2016-11-10 05:34:49 -05:00
|
|
|
str(message<isRequest, Body, Fields> const& m)
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
2016-05-07 17:06:46 -04:00
|
|
|
string_write_stream ss(ios_);
|
2016-04-29 06:04:40 -04:00
|
|
|
write(ss, m);
|
|
|
|
return ss.str;
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:51:10 -05:00
|
|
|
void
|
|
|
|
testAsyncWriteHeaders(yield_context do_yield)
|
|
|
|
{
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
header<true, fields> m;
|
2016-11-07 13:51:10 -05:00
|
|
|
m.version = 11;
|
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-11-07 13:51:10 -05:00
|
|
|
error_code ec;
|
|
|
|
string_write_stream ss{ios_};
|
|
|
|
async_write(ss, m, do_yield[ec]);
|
|
|
|
if(BEAST_EXPECTS(! ec, ec.message()))
|
|
|
|
BEAST_EXPECT(ss.str ==
|
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"\r\n");
|
|
|
|
}
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
header<false, fields> m;
|
2016-11-07 13:51:10 -05:00
|
|
|
m.version = 10;
|
|
|
|
m.status = 200;
|
|
|
|
m.reason = "OK";
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("Server", "test");
|
|
|
|
m.fields.insert("Content-Length", "5");
|
2016-11-07 13:51:10 -05:00
|
|
|
error_code ec;
|
|
|
|
string_write_stream ss{ios_};
|
|
|
|
async_write(ss, m, do_yield[ec]);
|
|
|
|
if(BEAST_EXPECTS(! ec, ec.message()))
|
|
|
|
BEAST_EXPECT(ss.str ==
|
|
|
|
"HTTP/1.0 200 OK\r\n"
|
|
|
|
"Server: test\r\n"
|
|
|
|
"Content-Length: 5\r\n"
|
|
|
|
"\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:04:40 -04:00
|
|
|
void
|
2016-05-07 17:06:46 -04:00
|
|
|
testAsyncWrite(yield_context do_yield)
|
|
|
|
{
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<false, string_body, fields> m;
|
2016-05-07 17:06:46 -04:00
|
|
|
m.version = 10;
|
|
|
|
m.status = 200;
|
|
|
|
m.reason = "OK";
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("Server", "test");
|
|
|
|
m.fields.insert("Content-Length", "5");
|
2016-05-07 17:06:46 -04:00
|
|
|
m.body = "*****";
|
|
|
|
error_code ec;
|
2016-11-07 13:51:10 -05:00
|
|
|
string_write_stream ss{ios_};
|
2016-05-07 17:06:46 -04:00
|
|
|
async_write(ss, m, do_yield[ec]);
|
2016-08-29 13:28:08 -04:00
|
|
|
if(BEAST_EXPECTS(! ec, ec.message()))
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(ss.str ==
|
2016-05-07 17:06:46 -04:00
|
|
|
"HTTP/1.0 200 OK\r\n"
|
|
|
|
"Server: test\r\n"
|
|
|
|
"Content-Length: 5\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"*****");
|
|
|
|
}
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<false, string_body, fields> m;
|
2016-05-07 17:06:46 -04:00
|
|
|
m.version = 11;
|
|
|
|
m.status = 200;
|
|
|
|
m.reason = "OK";
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("Server", "test");
|
|
|
|
m.fields.insert("Transfer-Encoding", "chunked");
|
2016-05-07 17:06:46 -04:00
|
|
|
m.body = "*****";
|
|
|
|
error_code ec;
|
|
|
|
string_write_stream ss(ios_);
|
|
|
|
async_write(ss, m, do_yield[ec]);
|
2016-08-29 13:28:08 -04:00
|
|
|
if(BEAST_EXPECTS(! ec, ec.message()))
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(ss.str ==
|
2016-05-07 17:06:46 -04:00
|
|
|
"HTTP/1.1 200 OK\r\n"
|
|
|
|
"Server: test\r\n"
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"5\r\n"
|
|
|
|
"*****\r\n"
|
|
|
|
"0\r\n\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
testFailures(yield_context do_yield)
|
|
|
|
{
|
|
|
|
static std::size_t constexpr limit = 100;
|
|
|
|
std::size_t n;
|
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
{
|
|
|
|
test::fail_counter fc(n);
|
|
|
|
test::fail_stream<
|
|
|
|
string_write_stream> fs(fc, ios_);
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, fail_body, fields> m(
|
2016-05-07 17:06:46 -04:00
|
|
|
std::piecewise_construct,
|
|
|
|
std::forward_as_tuple(fc, ios_));
|
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
|
|
|
m.fields.insert("Content-Length", "5");
|
2016-05-07 17:06:46 -04:00
|
|
|
m.body = "*****";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
write(fs, m);
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(fs.next_layer().str ==
|
2016-05-07 17:06:46 -04:00
|
|
|
"GET / HTTP/1.0\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"Content-Length: 5\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"*****"
|
|
|
|
);
|
|
|
|
pass();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch(std::exception const&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
{
|
|
|
|
test::fail_counter fc(n);
|
|
|
|
test::fail_stream<
|
|
|
|
string_write_stream> fs(fc, ios_);
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, fail_body, fields> m(
|
2016-05-07 17:06:46 -04:00
|
|
|
std::piecewise_construct,
|
|
|
|
std::forward_as_tuple(fc, ios_));
|
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
|
|
|
m.fields.insert("Transfer-Encoding", "chunked");
|
2016-05-07 17:06:46 -04:00
|
|
|
m.body = "*****";
|
|
|
|
error_code ec;
|
|
|
|
write(fs, m, ec);
|
|
|
|
if(ec == boost::asio::error::eof)
|
|
|
|
{
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(fs.next_layer().str ==
|
2016-05-07 17:06:46 -04:00
|
|
|
"GET / HTTP/1.0\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"0\r\n\r\n"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
{
|
|
|
|
test::fail_counter fc(n);
|
|
|
|
test::fail_stream<
|
|
|
|
string_write_stream> fs(fc, ios_);
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, fail_body, fields> m(
|
2016-05-07 17:06:46 -04:00
|
|
|
std::piecewise_construct,
|
|
|
|
std::forward_as_tuple(fc, ios_));
|
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
|
|
|
m.fields.insert("Transfer-Encoding", "chunked");
|
2016-05-07 17:06:46 -04:00
|
|
|
m.body = "*****";
|
|
|
|
error_code ec;
|
|
|
|
async_write(fs, m, do_yield[ec]);
|
|
|
|
if(ec == boost::asio::error::eof)
|
|
|
|
{
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(fs.next_layer().str ==
|
2016-05-07 17:06:46 -04:00
|
|
|
"GET / HTTP/1.0\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"Transfer-Encoding: chunked\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"1\r\n*\r\n"
|
|
|
|
"0\r\n\r\n"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
{
|
|
|
|
test::fail_counter fc(n);
|
|
|
|
test::fail_stream<
|
|
|
|
string_write_stream> fs(fc, ios_);
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, fail_body, fields> m(
|
2016-05-07 17:06:46 -04:00
|
|
|
std::piecewise_construct,
|
|
|
|
std::forward_as_tuple(fc, ios_));
|
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
|
|
|
m.fields.insert("Content-Length", "5");
|
2016-05-07 17:06:46 -04:00
|
|
|
m.body = "*****";
|
|
|
|
error_code ec;
|
|
|
|
write(fs, m, ec);
|
|
|
|
if(! ec)
|
|
|
|
{
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(fs.next_layer().str ==
|
2016-05-07 17:06:46 -04:00
|
|
|
"GET / HTTP/1.0\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"Content-Length: 5\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"*****"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-05-07 17:06:46 -04:00
|
|
|
|
|
|
|
for(n = 0; n < limit; ++n)
|
|
|
|
{
|
|
|
|
test::fail_counter fc(n);
|
|
|
|
test::fail_stream<
|
|
|
|
string_write_stream> fs(fc, ios_);
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, fail_body, fields> m(
|
2016-05-07 17:06:46 -04:00
|
|
|
std::piecewise_construct,
|
|
|
|
std::forward_as_tuple(fc, ios_));
|
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
|
|
|
m.fields.insert("Content-Length", "5");
|
2016-05-07 17:06:46 -04:00
|
|
|
m.body = "*****";
|
|
|
|
error_code ec;
|
|
|
|
async_write(fs, m, do_yield[ec]);
|
|
|
|
if(! ec)
|
|
|
|
{
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(fs.next_layer().str ==
|
2016-05-07 17:06:46 -04:00
|
|
|
"GET / HTTP/1.0\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"Content-Length: 5\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"*****"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(n < limit);
|
2016-05-07 17:06:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
testOutput()
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
|
|
|
// auto content-length HTTP/1.0
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, string_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
m.body = "*";
|
|
|
|
prepare(m);
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(str(m) ==
|
2016-04-29 06:04:40 -04:00
|
|
|
"GET / HTTP/1.0\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"Content-Length: 1\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"*"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// keep-alive HTTP/1.0
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, string_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
m.body = "*";
|
|
|
|
prepare(m, connection::keep_alive);
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(str(m) ==
|
2016-04-29 06:04:40 -04:00
|
|
|
"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
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, string_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
m.body = "*";
|
|
|
|
try
|
|
|
|
{
|
|
|
|
prepare(m, connection::upgrade);
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
catch(std::exception const&)
|
|
|
|
{
|
|
|
|
pass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no content-length HTTP/1.0
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, unsized_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 10;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
m.body = "*";
|
|
|
|
prepare(m);
|
2016-05-07 17:06:46 -04:00
|
|
|
string_write_stream ss(ios_);
|
2016-04-29 06:04:40 -04:00
|
|
|
error_code ec;
|
|
|
|
write(ss, m, ec);
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(ec == boost::asio::error::eof);
|
|
|
|
BEAST_EXPECT(ss.str ==
|
2016-04-29 06:04:40 -04:00
|
|
|
"GET / HTTP/1.0\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"*"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// auto content-length HTTP/1.1
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, string_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 11;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
m.body = "*";
|
|
|
|
prepare(m);
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(str(m) ==
|
2016-04-29 06:04:40 -04:00
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"Content-Length: 1\r\n"
|
|
|
|
"\r\n"
|
|
|
|
"*"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// close HTTP/1.1
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, string_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 11;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
m.body = "*";
|
|
|
|
prepare(m, connection::close);
|
2016-05-07 17:06:46 -04:00
|
|
|
string_write_stream ss(ios_);
|
2016-04-29 06:04:40 -04:00
|
|
|
error_code ec;
|
|
|
|
write(ss, m, ec);
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(ec == boost::asio::error::eof);
|
|
|
|
BEAST_EXPECT(ss.str ==
|
2016-04-29 06:04:40 -04:00
|
|
|
"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
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, empty_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 11;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
prepare(m, connection::upgrade);
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(str(m) ==
|
2016-04-29 06:04:40 -04:00
|
|
|
"GET / HTTP/1.1\r\n"
|
|
|
|
"User-Agent: test\r\n"
|
|
|
|
"Connection: upgrade\r\n"
|
|
|
|
"\r\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// no content-length HTTP/1.1
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, unsized_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 11;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
m.body = "*";
|
|
|
|
prepare(m);
|
2016-05-07 17:06:46 -04:00
|
|
|
string_write_stream ss(ios_);
|
2016-04-29 06:04:40 -04:00
|
|
|
error_code ec;
|
|
|
|
write(ss, m, ec);
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(ss.str ==
|
2016-04-29 06:04:40 -04:00
|
|
|
"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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:51:10 -05:00
|
|
|
void test_std_ostream()
|
2016-04-29 06:04:40 -04:00
|
|
|
{
|
2016-11-07 13:51:10 -05:00
|
|
|
// Conversion to std::string via operator<<
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, string_body, fields> m;
|
2016-05-07 15:18:22 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 11;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-04-29 06:04:40 -04:00
|
|
|
m.body = "*";
|
2016-08-03 15:34:23 -04:00
|
|
|
BEAST_EXPECT(boost::lexical_cast<std::string>(m) ==
|
2016-11-07 13:51:10 -05:00
|
|
|
"GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n*");
|
|
|
|
BEAST_EXPECT(boost::lexical_cast<std::string>(m.base()) ==
|
|
|
|
"GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n");
|
|
|
|
// Cause exceptions in operator<<
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss.setstate(ss.rdstate() |
|
|
|
|
std::stringstream::failbit);
|
|
|
|
try
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
// header
|
2016-11-07 13:51:10 -05:00
|
|
|
ss << m.base();
|
|
|
|
fail("", __FILE__, __LINE__);
|
|
|
|
}
|
|
|
|
catch(std::exception const&)
|
|
|
|
{
|
|
|
|
pass();
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// message
|
|
|
|
ss << m;
|
|
|
|
fail("", __FILE__, __LINE__);
|
|
|
|
}
|
|
|
|
catch(std::exception const&)
|
|
|
|
{
|
|
|
|
pass();
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:06:46 -04:00
|
|
|
void testOstream()
|
|
|
|
{
|
2016-11-10 05:34:49 -05:00
|
|
|
message<true, string_body, fields> m;
|
2016-05-07 17:06:46 -04:00
|
|
|
m.method = "GET";
|
|
|
|
m.url = "/";
|
|
|
|
m.version = 11;
|
2016-11-10 05:34:49 -05:00
|
|
|
m.fields.insert("User-Agent", "test");
|
2016-05-07 17:06:46 -04:00
|
|
|
m.body = "*";
|
|
|
|
prepare(m);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss.setstate(ss.rdstate() |
|
|
|
|
std::stringstream::failbit);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ss << m;
|
|
|
|
fail();
|
|
|
|
}
|
|
|
|
catch(std::exception const&)
|
|
|
|
{
|
|
|
|
pass();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 06:04:40 -04:00
|
|
|
void run() override
|
|
|
|
{
|
2017-01-19 12:05:56 -05:00
|
|
|
yield_to(&write_test::testAsyncWriteHeaders, this);
|
|
|
|
yield_to(&write_test::testAsyncWrite, this);
|
|
|
|
yield_to(&write_test::testFailures, this);
|
2016-05-07 17:06:46 -04:00
|
|
|
testOutput();
|
2016-11-07 13:51:10 -05:00
|
|
|
test_std_ostream();
|
2016-05-07 17:06:46 -04:00
|
|
|
testOstream();
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BEAST_DEFINE_TESTSUITE(write,http,beast);
|
|
|
|
|
|
|
|
} // http
|
|
|
|
} // beast
|