Files
boost_beast/test/beast/http/file_body.cpp
T

223 lines
6.4 KiB
C++
Raw Normal View History

2017-07-05 22:08:31 -07:00
//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-07-05 22:08:31 -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)
//
2017-07-20 13:40:34 -07:00
// Official repository: https://github.com/boostorg/beast
//
2017-07-05 22:08:31 -07:00
// Test that header file is self-contained.
2017-07-20 13:40:34 -07:00
#include <boost/beast/http/file_body.hpp>
2017-07-05 22:08:31 -07:00
2019-03-05 08:25:54 -08:00
#include <boost/beast/core/buffer_traits.hpp>
2017-09-07 07:39:52 -07:00
#include <boost/beast/core/buffers_prefix.hpp>
2017-07-20 13:40:34 -07:00
#include <boost/beast/core/file_stdio.hpp>
#include <boost/beast/core/flat_buffer.hpp>
2020-06-04 19:18:44 +02:00
#include <boost/beast/core/static_string.hpp>
2017-07-20 13:40:34 -07:00
#include <boost/beast/http/parser.hpp>
#include <boost/beast/http/serializer.hpp>
2018-11-11 14:07:55 -08:00
#include <boost/beast/_experimental/unit_test/suite.hpp>
2017-07-05 22:08:31 -07:00
#include <boost/filesystem.hpp>
2020-01-24 11:28:24 +01:00
#include <boost/asio/error.hpp>
2022-02-25 19:40:41 +01:00
#include <fstream>
2017-07-05 22:08:31 -07:00
2017-07-20 13:40:34 -07:00
namespace boost {
2017-07-05 22:08:31 -07:00
namespace beast {
namespace http {
class file_body_test : public beast::unit_test::suite
{
public:
struct lambda
{
flat_buffer buffer;
template<class ConstBufferSequence>
void
operator()(error_code&, ConstBufferSequence const& buffers)
{
2018-11-30 14:58:38 -08:00
buffer.commit(net::buffer_copy(
2019-03-05 08:47:02 -08:00
buffer.prepare(buffer_bytes(buffers)),
2017-07-05 22:08:31 -07:00
buffers));
}
};
2020-01-24 11:28:24 +01:00
struct temp_file
{
temp_file(std::ostream& logger)
: path_(boost::filesystem::unique_path())
, log_(logger)
{}
~temp_file()
{
if (!path_.empty())
{
auto ec = error_code();
boost::filesystem::remove(path_, ec);
if (ec)
{
log_ << "warning: failed to remove temporary file: " << path_ << "\n";
}
}
}
temp_file(temp_file&&) = default;
temp_file(temp_file const&) = delete;
2020-02-29 08:58:49 +01:00
temp_file& operator=(temp_file&&) = delete;
2020-01-24 11:28:24 +01:00
temp_file& operator=(temp_file const&) = delete;
boost::filesystem::path const& path() const
{
return path_;
}
private:
boost::filesystem::path path_;
std::ostream& log_;
};
2017-07-05 22:08:31 -07:00
template<class File>
void
doTestFileBody()
{
error_code ec;
string_view const s =
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Content-Length: 3\r\n"
"\r\n"
"xyz";
auto const temp = boost::filesystem::unique_path();
{
response_parser<basic_file_body<File>> p;
p.eager(true);
p.get().body().open(
2017-07-05 22:08:31 -07:00
temp.string<std::string>().c_str(), file_mode::write, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
2017-07-05 22:08:31 -07:00
2018-11-30 14:58:38 -08:00
p.put(net::buffer(s.data(), s.size()), ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
2017-07-05 22:08:31 -07:00
}
{
File f;
f.open(temp.string<std::string>().c_str(), file_mode::read, ec);
auto size = f.size(ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(size == 3);
2017-07-05 22:08:31 -07:00
std::string s1;
s1.resize(3);
f.read(&s1[0], s1.size(), ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECTS(s1 == "xyz", s);
2017-07-05 22:08:31 -07:00
}
{
lambda visit;
{
response<basic_file_body<File>> res{status::ok, 11};
res.set(field::server, "test");
res.body().open(temp.string<std::string>().c_str(),
2017-07-05 22:08:31 -07:00
file_mode::scan, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
2017-07-05 22:08:31 -07:00
res.prepare_payload();
serializer<false, basic_file_body<File>, fields> sr{res};
sr.next(ec, visit);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
2017-09-07 07:39:52 -07:00
auto const b = buffers_front(visit.buffer.data());
2017-07-05 22:08:31 -07:00
string_view const s1{
2017-09-07 07:39:52 -07:00
reinterpret_cast<char const*>(b.data()),
b.size()};
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(s1 == s, s1);
2017-07-05 22:08:31 -07:00
}
}
boost::filesystem::remove(temp, ec);
2017-07-25 12:35:54 -07:00
BEAST_EXPECTS(! ec, ec.message());
2017-07-05 22:08:31 -07:00
}
2020-01-24 11:28:24 +01:00
template<class File>
void
fileBodyUnexpectedEofOnGet()
{
auto temp = temp_file(log);
error_code ec;
string_view const ten =
"0123456789"; // 40
// create the temporary file
{
std::ofstream fstemp(temp.path().native());
std::size_t written = 0;
std::size_t to_write = 4097;
while (written < to_write)
{
2021-03-16 09:49:13 +01:00
auto size = (std::min)(ten.size(), to_write - written);
2020-01-24 11:28:24 +01:00
fstemp << ten.substr(0, size);
BEAST_EXPECT(fstemp.good());
written += size;
}
fstemp.close();
}
// open the file and read the header
{
using file_body_type = basic_file_body<File>;
typename file_body_type::value_type value;
// opened in write mode so we can truncate later
value.open(temp.path().string<std::string>().c_str(), file_mode::read, ec);
BEAST_EXPECTS(! ec, ec.message());
response_header<> header;
header.version(11);
header.result(status::accepted);
header.set(field::server, "test");
2022-04-16 10:11:24 -04:00
header.set(field::content_length, "4097");
2020-01-24 11:28:24 +01:00
typename file_body_type::writer w(header, value);
auto maybe_range = w.get(ec);
BEAST_EXPECTS(!ec, ec.message());
BEAST_EXPECTS(maybe_range.has_value(), "no range returned");
BEAST_EXPECT(maybe_range.value().second);
value.file().seek(4097, ec);
BEAST_EXPECTS(!ec, ec.message());
maybe_range = w.get(ec);
BEAST_EXPECTS(ec == error::short_read, ec.message());
BEAST_EXPECTS(!maybe_range.has_value(), "range returned on error");
}
}
2017-07-05 22:08:31 -07:00
void
run() override
{
doTestFileBody<file_stdio>();
2020-01-24 11:28:24 +01:00
#if BOOST_BEAST_USE_WIN32_FILE
2017-07-08 06:31:49 -07:00
doTestFileBody<file_win32>();
2020-01-24 11:28:24 +01:00
#endif
#if BOOST_BEAST_USE_POSIX_FILE
2017-07-08 06:31:49 -07:00
doTestFileBody<file_posix>();
2020-01-24 11:28:24 +01:00
#endif
fileBodyUnexpectedEofOnGet<file_stdio>();
#if BOOST_BEAST_USE_POSIX_FILE
fileBodyUnexpectedEofOnGet<file_posix>();
2017-07-08 06:31:49 -07:00
#endif
2020-01-24 11:28:24 +01:00
#if BOOST_BEAST_USE_WIN32_FILE
fileBodyUnexpectedEofOnGet<file_win32>();
#endif
2017-07-05 22:08:31 -07:00
}
};
2017-08-01 17:01:57 -07:00
BEAST_DEFINE_TESTSUITE(beast,http,file_body);
2017-07-05 22:08:31 -07:00
} // http
} // beast
2017-07-20 13:40:34 -07:00
} // boost