forked from boostorg/beast
Add file_body
This commit is contained in:
1 parent
7254cc8976
commit
8bf46ffa05
11 files changed
+596
-70
No files matched your search
@@ -22,6 +22,7 @@ add_executable (http-tests
|
||||
error.cpp
|
||||
field.cpp
|
||||
fields.cpp
|
||||
file_body.cpp
|
||||
message.cpp
|
||||
parser.cpp
|
||||
read.cpp
|
||||
|
||||
@@ -15,6 +15,7 @@ unit-test http-tests :
|
||||
error.cpp
|
||||
field.cpp
|
||||
fields.cpp
|
||||
file_body.cpp
|
||||
message.cpp
|
||||
parser.cpp
|
||||
read.cpp
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
//
|
||||
|
||||
#include "example/doc/http_examples.hpp"
|
||||
#include "example/common/file_body.hpp"
|
||||
#include "example/common/const_body.hpp"
|
||||
#include "example/common/mutable_body.hpp"
|
||||
|
||||
@@ -286,62 +285,6 @@ public:
|
||||
BEAST_EXPECT(h.body == "Hello, world!");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
doFileBody()
|
||||
{
|
||||
test::pipe c{ios_};
|
||||
|
||||
boost::filesystem::path const path = "temp.txt";
|
||||
std::string const body = "Hello, world!\n";
|
||||
{
|
||||
request<string_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::put);
|
||||
req.target("/");
|
||||
req.body = body;
|
||||
req.prepare_payload();
|
||||
write(c.client, req);
|
||||
}
|
||||
{
|
||||
flat_buffer b;
|
||||
request_parser<empty_body> p0;
|
||||
read_header(c.server, b, p0);
|
||||
BEAST_EXPECTS(p0.get().method() == verb::put,
|
||||
p0.get().method_string());
|
||||
{
|
||||
error_code ec;
|
||||
request_parser<file_body> p{std::move(p0)};
|
||||
p.get().body.open(path, "wb", ec);
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(system_error{ec});
|
||||
read(c.server, b, p);
|
||||
}
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
response<file_body> res;
|
||||
res.version = 11;
|
||||
res.result(status::ok);
|
||||
res.insert(field::server, "test");
|
||||
res.body.open(path, "rb", ec);
|
||||
if(ec)
|
||||
BOOST_THROW_EXCEPTION(system_error{ec});
|
||||
res.set(field::content_length, res.body.size());
|
||||
write(c.server, res);
|
||||
}
|
||||
{
|
||||
flat_buffer b;
|
||||
response<string_body> res;
|
||||
read(c.client, b, res);
|
||||
BEAST_EXPECTS(res.body == body, body);
|
||||
}
|
||||
error_code ec;
|
||||
boost::filesystem::remove(path, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
|
||||
void
|
||||
doConstAndMutableBody()
|
||||
{
|
||||
@@ -424,7 +367,6 @@ public:
|
||||
doCustomParser();
|
||||
doHEAD();
|
||||
doDeferredBody();
|
||||
doFileBody();
|
||||
doConstAndMutableBody();
|
||||
doIncrementalRead();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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/file_body.hpp>
|
||||
|
||||
#include <beast/core/file_stdio.hpp>
|
||||
#include <beast/core/flat_buffer.hpp>
|
||||
#include <beast/http/parser.hpp>
|
||||
#include <beast/http/serializer.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
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)
|
||||
{
|
||||
buffer.commit(boost::asio::buffer_copy(
|
||||
buffer.prepare(boost::asio::buffer_size(buffers)),
|
||||
buffers));
|
||||
}
|
||||
};
|
||||
|
||||
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(
|
||||
temp.string<std::string>().c_str(), file_mode::write, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
|
||||
p.put(boost::asio::buffer(s.data(), s.size()), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
{
|
||||
File f;
|
||||
f.open(temp.string<std::string>().c_str(), file_mode::read, ec);
|
||||
auto size = f.size(ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(size == 3);
|
||||
std::string s1;
|
||||
s1.resize(3);
|
||||
f.read(&s1[0], s1.size(), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECTS(s1 == "xyz", s);
|
||||
}
|
||||
{
|
||||
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(),
|
||||
file_mode::scan, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
res.prepare_payload();
|
||||
|
||||
serializer<false, basic_file_body<File>, fields> sr{res};
|
||||
sr.next(ec, visit);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
auto const cb = *visit.buffer.data().begin();
|
||||
string_view const s1{
|
||||
boost::asio::buffer_cast<char const*>(cb),
|
||||
boost::asio::buffer_size(cb)};
|
||||
BEAST_EXPECTS(s1 == s, s1);
|
||||
}
|
||||
}
|
||||
boost::filesystem::remove(temp, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
void
|
||||
run() override
|
||||
{
|
||||
doTestFileBody<file_stdio>();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(file_body,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
Reference in new issue
Block a user