mirror of
https://github.com/boostorg/beast.git
synced 2025-07-30 12:57:31 +02:00
More basic_parser tests
This commit is contained in:
@ -6,6 +6,7 @@ Version 56:
|
|||||||
* HTTP/1.1 is the default version
|
* HTTP/1.1 is the default version
|
||||||
* Try harder to find Boost (cmake)
|
* Try harder to find Boost (cmake)
|
||||||
* Reset error codes
|
* Reset error codes
|
||||||
|
* More basic_parser tests
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -148,71 +148,13 @@ public:
|
|||||||
return {s, N-1};
|
return {s, N-1};
|
||||||
}
|
}
|
||||||
|
|
||||||
template<bool isRequest, class Derived>
|
template<class ConstBufferSequence, bool isRequest, class Derived>
|
||||||
static
|
|
||||||
std::size_t
|
|
||||||
feed(boost::asio::const_buffer buffer,
|
|
||||||
basic_parser<isRequest, Derived>& parser,
|
|
||||||
error_code& ec)
|
|
||||||
{
|
|
||||||
using boost::asio::const_buffers_1;
|
|
||||||
std::size_t used = 0;
|
|
||||||
for(;;)
|
|
||||||
{
|
|
||||||
auto const n = parser.put(
|
|
||||||
const_buffers_1{buffer}, ec);
|
|
||||||
if(ec)
|
|
||||||
return 0;
|
|
||||||
if(n == 0)
|
|
||||||
break;
|
|
||||||
buffer = buffer + n;
|
|
||||||
used += n;
|
|
||||||
if(parser.is_done())
|
|
||||||
break;
|
|
||||||
if(buffer_size(buffer) == 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return used;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<class ConstBufferSequence,
|
|
||||||
bool isRequest, class Derived>
|
|
||||||
static
|
|
||||||
std::size_t
|
std::size_t
|
||||||
feed(ConstBufferSequence const& buffers,
|
feed(ConstBufferSequence const& buffers,
|
||||||
basic_parser<isRequest, Derived>& parser,
|
basic_parser<isRequest, Derived>& p, error_code& ec)
|
||||||
error_code& ec)
|
|
||||||
{
|
{
|
||||||
using boost::asio::buffer_size;
|
p.eager(true);
|
||||||
consuming_buffers<
|
return p.put(buffers, ec);
|
||||||
ConstBufferSequence> cb{buffers};
|
|
||||||
std::size_t used = 0;
|
|
||||||
for(;;)
|
|
||||||
{
|
|
||||||
auto const n =
|
|
||||||
parser.put(cb, ec);
|
|
||||||
if(ec)
|
|
||||||
return 0;
|
|
||||||
if(n == 0)
|
|
||||||
break;
|
|
||||||
cb.consume(n);
|
|
||||||
used += n;
|
|
||||||
if(parser.is_done())
|
|
||||||
break;
|
|
||||||
if(buffer_size(cb) == 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return used;
|
|
||||||
}
|
|
||||||
|
|
||||||
template<bool isRequest, class Derived>
|
|
||||||
static
|
|
||||||
std::size_t
|
|
||||||
feed(boost::asio::const_buffers_1 buffers,
|
|
||||||
basic_parser<isRequest, Derived>& parser,
|
|
||||||
error_code& ec)
|
|
||||||
{
|
|
||||||
return feed(*buffers.begin(), parser, ec);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<bool isRequest, class Pred>
|
template<bool isRequest, class Pred>
|
||||||
@ -222,11 +164,12 @@ public:
|
|||||||
{
|
{
|
||||||
using boost::asio::buffer;
|
using boost::asio::buffer;
|
||||||
test_parser<isRequest> p;
|
test_parser<isRequest> p;
|
||||||
|
p.eager(true);
|
||||||
if(skipBody)
|
if(skipBody)
|
||||||
p.skip(true);
|
p.skip(true);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
auto const n = feed(buffer(
|
auto const n = p.put(
|
||||||
s.data(), s.size()), p, ec);
|
buffer(s.data(), s.size()), ec);
|
||||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||||
return;
|
return;
|
||||||
if(! BEAST_EXPECT(n == s.size()))
|
if(! BEAST_EXPECT(n == s.size()))
|
||||||
@ -254,11 +197,11 @@ public:
|
|||||||
{
|
{
|
||||||
using boost::asio::buffer;
|
using boost::asio::buffer;
|
||||||
test_parser<isRequest> p;
|
test_parser<isRequest> p;
|
||||||
|
p.eager(true);
|
||||||
if(skipBody)
|
if(skipBody)
|
||||||
p.skip(true);
|
p.skip(true);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
feed(buffer(
|
p.put(buffer(s.data(), s.size()), ec);
|
||||||
s.data(), s.size()), p, ec);
|
|
||||||
if(! ec && ev)
|
if(! ec && ev)
|
||||||
p.put_eof(ec);
|
p.put_eof(ec);
|
||||||
BEAST_EXPECTS(ec == ev, ec.message());
|
BEAST_EXPECTS(ec == ev, ec.message());
|
||||||
@ -267,7 +210,6 @@ public:
|
|||||||
void
|
void
|
||||||
testFlatten()
|
testFlatten()
|
||||||
{
|
{
|
||||||
#if 0
|
|
||||||
using boost::asio::buffer;
|
using boost::asio::buffer;
|
||||||
{
|
{
|
||||||
std::string const s =
|
std::string const s =
|
||||||
@ -283,10 +225,12 @@ public:
|
|||||||
auto const b2 = buffer(
|
auto const b2 = buffer(
|
||||||
s.data() + i, s.size() - i);
|
s.data() + i, s.size() - i);
|
||||||
test_parser<true> p;
|
test_parser<true> p;
|
||||||
|
p.eager(true);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
feed(b1, p, ec);
|
p.put(b1, ec);
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(ec == error::need_more, ec.message());
|
||||||
feed(buffer_cat(b1, b2), p, ec);
|
ec = {};
|
||||||
|
p.put(boost::asio::buffer(s.data(), s.size()), ec);
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
BEAST_EXPECT(p.is_done());
|
BEAST_EXPECT(p.is_done());
|
||||||
}
|
}
|
||||||
@ -303,16 +247,16 @@ public:
|
|||||||
auto const b2 = buffer(
|
auto const b2 = buffer(
|
||||||
s.data() + i, s.size() - i);
|
s.data() + i, s.size() - i);
|
||||||
test_parser<false> p;
|
test_parser<false> p;
|
||||||
|
p.eager(true);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
feed(b1, p, ec);
|
p.put(b1, ec);
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(ec == error::need_more, ec.message());
|
||||||
ec = {};
|
ec = {};
|
||||||
feed(buffer_cat(b1, b2), p, ec);
|
p.put(buffer_cat(b1, b2), ec);
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
p.put_eof(ec);
|
p.put_eof(ec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that all callbacks are invoked
|
// Check that all callbacks are invoked
|
||||||
@ -322,6 +266,7 @@ public:
|
|||||||
using boost::asio::buffer;
|
using boost::asio::buffer;
|
||||||
{
|
{
|
||||||
test_parser<true> p;
|
test_parser<true> p;
|
||||||
|
p.eager(true);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
std::string const s =
|
std::string const s =
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
@ -329,7 +274,7 @@ public:
|
|||||||
"Content-Length: 1\r\n"
|
"Content-Length: 1\r\n"
|
||||||
"\r\n"
|
"\r\n"
|
||||||
"*";
|
"*";
|
||||||
feed(buffer(s), p, ec);
|
p.put(buffer(s), ec);
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
BEAST_EXPECT(p.is_done());
|
BEAST_EXPECT(p.is_done());
|
||||||
BEAST_EXPECT(p.got_on_begin);
|
BEAST_EXPECT(p.got_on_begin);
|
||||||
@ -341,6 +286,7 @@ public:
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
test_parser<false> p;
|
test_parser<false> p;
|
||||||
|
p.eager(true);
|
||||||
error_code ec;
|
error_code ec;
|
||||||
std::string const s =
|
std::string const s =
|
||||||
"HTTP/1.1 200 OK\r\n"
|
"HTTP/1.1 200 OK\r\n"
|
||||||
@ -348,7 +294,7 @@ public:
|
|||||||
"Content-Length: 1\r\n"
|
"Content-Length: 1\r\n"
|
||||||
"\r\n"
|
"\r\n"
|
||||||
"*";
|
"*";
|
||||||
feed(buffer(s), p, ec);
|
p.put(buffer(s), ec);
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
BEAST_EXPECT(p.is_done());
|
BEAST_EXPECT(p.is_done());
|
||||||
BEAST_EXPECT(p.got_on_begin);
|
BEAST_EXPECT(p.got_on_begin);
|
||||||
@ -358,6 +304,48 @@ public:
|
|||||||
BEAST_EXPECT(! p.got_on_chunk);
|
BEAST_EXPECT(! p.got_on_chunk);
|
||||||
BEAST_EXPECT(p.got_on_complete);
|
BEAST_EXPECT(p.got_on_complete);
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
test_parser<false> p;
|
||||||
|
p.eager(true);
|
||||||
|
error_code ec;
|
||||||
|
std::string const s =
|
||||||
|
"HTTP/1.1 200 OK\r\n"
|
||||||
|
"Server: test\r\n"
|
||||||
|
"Transfer-Encoding: chunked\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"1\r\n*\r\n"
|
||||||
|
"0\r\n\r\n";
|
||||||
|
p.put(buffer(s), ec);
|
||||||
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
|
BEAST_EXPECT(p.is_done());
|
||||||
|
BEAST_EXPECT(p.got_on_begin);
|
||||||
|
BEAST_EXPECT(p.got_on_field);
|
||||||
|
BEAST_EXPECT(p.got_on_header);
|
||||||
|
BEAST_EXPECT(p.got_on_body);
|
||||||
|
BEAST_EXPECT(p.got_on_chunk);
|
||||||
|
BEAST_EXPECT(p.got_on_complete);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
test_parser<false> p;
|
||||||
|
p.eager(true);
|
||||||
|
error_code ec;
|
||||||
|
std::string const s =
|
||||||
|
"HTTP/1.1 200 OK\r\n"
|
||||||
|
"Server: test\r\n"
|
||||||
|
"Transfer-Encoding: chunked\r\n"
|
||||||
|
"\r\n"
|
||||||
|
"1;x\r\n*\r\n"
|
||||||
|
"0\r\n\r\n";
|
||||||
|
p.put(buffer(s), ec);
|
||||||
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
|
BEAST_EXPECT(p.is_done());
|
||||||
|
BEAST_EXPECT(p.got_on_begin);
|
||||||
|
BEAST_EXPECT(p.got_on_field);
|
||||||
|
BEAST_EXPECT(p.got_on_header);
|
||||||
|
BEAST_EXPECT(p.got_on_body);
|
||||||
|
BEAST_EXPECT(p.got_on_chunk);
|
||||||
|
BEAST_EXPECT(p.got_on_complete);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -745,9 +733,9 @@ public:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void testBody()
|
void
|
||||||
|
testBody()
|
||||||
{
|
{
|
||||||
#if 0
|
|
||||||
using boost::asio::buffer;
|
using boost::asio::buffer;
|
||||||
good<true>(
|
good<true>(
|
||||||
"GET / HTTP/1.1\r\n"
|
"GET / HTTP/1.1\r\n"
|
||||||
@ -804,6 +792,7 @@ public:
|
|||||||
// response without Content-Length or
|
// response without Content-Length or
|
||||||
// Transfer-Encoding: chunked requires eof.
|
// Transfer-Encoding: chunked requires eof.
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
error_code ec;
|
error_code ec;
|
||||||
test_parser<false> p;
|
test_parser<false> p;
|
||||||
feed(buf(
|
feed(buf(
|
||||||
@ -822,6 +811,7 @@ public:
|
|||||||
p.put_eof(ec);
|
p.put_eof(ec);
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
BEAST_EXPECT(p.is_done());
|
BEAST_EXPECT(p.is_done());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// 304 "Not Modified" response does not require eof
|
// 304 "Not Modified" response does not require eof
|
||||||
@ -883,7 +873,6 @@ public:
|
|||||||
"Content-Length: 1\r\n"
|
"Content-Length: 1\r\n"
|
||||||
"\r\n",
|
"\r\n",
|
||||||
error::partial_message);
|
error::partial_message);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
@ -906,7 +895,6 @@ public:
|
|||||||
void
|
void
|
||||||
testSplit()
|
testSplit()
|
||||||
{
|
{
|
||||||
#if 0
|
|
||||||
multi_buffer b;
|
multi_buffer b;
|
||||||
ostream(b) <<
|
ostream(b) <<
|
||||||
"POST / HTTP/1.1\r\n"
|
"POST / HTTP/1.1\r\n"
|
||||||
@ -915,8 +903,7 @@ public:
|
|||||||
"*****";
|
"*****";
|
||||||
error_code ec;
|
error_code ec;
|
||||||
test_parser<true> p;
|
test_parser<true> p;
|
||||||
p.pause();
|
auto n = p.put(b.data(), ec);
|
||||||
auto n = feed(b.data(), p, ec);
|
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
BEAST_EXPECT(p.got_on_begin);
|
BEAST_EXPECT(p.got_on_begin);
|
||||||
BEAST_EXPECT(p.got_on_field);
|
BEAST_EXPECT(p.got_on_field);
|
||||||
@ -924,11 +911,10 @@ public:
|
|||||||
BEAST_EXPECT(! p.got_on_body);
|
BEAST_EXPECT(! p.got_on_body);
|
||||||
BEAST_EXPECT(! p.got_on_chunk);
|
BEAST_EXPECT(! p.got_on_chunk);
|
||||||
BEAST_EXPECT(! p.got_on_complete);
|
BEAST_EXPECT(! p.got_on_complete);
|
||||||
BEAST_EXPECT(p.state() != parse_state::header);
|
|
||||||
BEAST_EXPECT(! p.is_done());
|
BEAST_EXPECT(! p.is_done());
|
||||||
|
BEAST_EXPECT(p.is_header_done());
|
||||||
BEAST_EXPECT(p.body.empty());
|
BEAST_EXPECT(p.body.empty());
|
||||||
b.consume(n);
|
b.consume(n);
|
||||||
p.resume();
|
|
||||||
n = feed(b.data(), p, ec);
|
n = feed(b.data(), p, ec);
|
||||||
BEAST_EXPECTS(! ec, ec.message());
|
BEAST_EXPECTS(! ec, ec.message());
|
||||||
BEAST_EXPECT(p.got_on_begin);
|
BEAST_EXPECT(p.got_on_begin);
|
||||||
@ -939,7 +925,6 @@ public:
|
|||||||
BEAST_EXPECT(p.got_on_complete);
|
BEAST_EXPECT(p.got_on_complete);
|
||||||
BEAST_EXPECT(p.is_done());
|
BEAST_EXPECT(p.is_done());
|
||||||
BEAST_EXPECT(p.body == "*****");
|
BEAST_EXPECT(p.body == "*****");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user