2017-07-20 08:01:46 -07:00
|
|
|
//
|
2017-02-06 20:07:03 -05:00
|
|
|
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-07-20 08:01:46 -07:00
|
|
|
//
|
|
|
|
// 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/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>
|
2017-01-09 11:13:19 -05:00
|
|
|
#include <beast/test/string_ostream.hpp>
|
2016-05-07 17:06:46 -04:00
|
|
|
#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
|
|
|
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>
|
2017-03-31 11:15:27 -04:00
|
|
|
bool
|
|
|
|
write(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_;
|
|
|
|
|
|
|
|
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-10-15 13:40:17 -04:00
|
|
|
template<class WriteFunction>
|
2017-03-31 11:15:27 -04:00
|
|
|
bool
|
|
|
|
write(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;
|
|
|
|
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
|
|
|
{
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream 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;
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream ss{ios_};
|
2016-11-07 13:51:10 -05:00
|
|
|
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;
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream ss{ios_};
|
2016-11-07 13:51:10 -05:00
|
|
|
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;
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream 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;
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream 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.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<
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream> 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<
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream> 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<
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream> 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<
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream> 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<
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream> 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);
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream 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);
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream 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-20 07:32:41 -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
|
|
|
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);
|
2017-01-09 11:13:19 -05:00
|
|
|
test::string_ostream 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 10:12:58 -04:00
|
|
|
// Ensure completion handlers are not leaked
|
|
|
|
struct handler
|
|
|
|
{
|
|
|
|
static std::atomic<std::size_t>&
|
|
|
|
count() { static std::atomic<std::size_t> n; return n; }
|
|
|
|
handler() { ++count(); }
|
|
|
|
~handler() { --count(); }
|
|
|
|
handler(handler const&) { ++count(); }
|
|
|
|
void operator()(error_code const&) const {}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
testIoService()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Make sure handlers are not destroyed
|
|
|
|
// after calling io_service::stop
|
|
|
|
boost::asio::io_service ios;
|
|
|
|
test::string_ostream os{ios};
|
|
|
|
BEAST_EXPECT(handler::count() == 0);
|
|
|
|
message<true, string_body, fields> m;
|
|
|
|
m.method = "GET";
|
|
|
|
m.version = 11;
|
|
|
|
m.url = "/";
|
2017-05-02 12:03:34 -07:00
|
|
|
m.fields.insert("Content-Length", 5);
|
2017-03-31 10:12:58 -04:00
|
|
|
m.body = "*****";
|
|
|
|
async_write(os, m, handler{});
|
|
|
|
BEAST_EXPECT(handler::count() > 0);
|
|
|
|
ios.stop();
|
|
|
|
BEAST_EXPECT(handler::count() > 0);
|
|
|
|
ios.reset();
|
|
|
|
BEAST_EXPECT(handler::count() > 0);
|
|
|
|
ios.run_one();
|
|
|
|
BEAST_EXPECT(handler::count() == 0);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Make sure uninvoked handlers are
|
|
|
|
// destroyed when calling ~io_service
|
|
|
|
{
|
|
|
|
boost::asio::io_service ios;
|
|
|
|
test::string_ostream is{ios};
|
|
|
|
BEAST_EXPECT(handler::count() == 0);
|
|
|
|
message<true, string_body, fields> m;
|
|
|
|
m.method = "GET";
|
|
|
|
m.version = 11;
|
|
|
|
m.url = "/";
|
2017-05-02 12:03:34 -07:00
|
|
|
m.fields.insert("Content-Length", 5);
|
2017-03-31 10:12:58 -04:00
|
|
|
m.body = "*****";
|
|
|
|
async_write(is, m, handler{});
|
|
|
|
BEAST_EXPECT(handler::count() > 0);
|
|
|
|
}
|
|
|
|
BEAST_EXPECT(handler::count() == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2017-03-31 10:12:58 -04:00
|
|
|
testIoService();
|
2016-04-29 06:04:40 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
BEAST_DEFINE_TESTSUITE(write,http,beast);
|
|
|
|
|
|
|
|
} // http
|
|
|
|
} // beast
|