More basic_parser tests

This commit is contained in:
Vinnie Falco
2017-06-13 07:41:04 -07:00
parent ca109693fb
commit 9939320c83
2 changed files with 71 additions and 85 deletions

View File

@ -6,6 +6,7 @@ Version 56:
* HTTP/1.1 is the default version
* Try harder to find Boost (cmake)
* Reset error codes
* More basic_parser tests
--------------------------------------------------------------------------------

View File

@ -148,71 +148,13 @@ public:
return {s, N-1};
}
template<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
template<class ConstBufferSequence, bool isRequest, class Derived>
std::size_t
feed(ConstBufferSequence const& buffers,
basic_parser<isRequest, Derived>& parser,
error_code& ec)
basic_parser<isRequest, Derived>& p, error_code& ec)
{
using boost::asio::buffer_size;
consuming_buffers<
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);
p.eager(true);
return p.put(buffers, ec);
}
template<bool isRequest, class Pred>
@ -222,11 +164,12 @@ public:
{
using boost::asio::buffer;
test_parser<isRequest> p;
p.eager(true);
if(skipBody)
p.skip(true);
error_code ec;
auto const n = feed(buffer(
s.data(), s.size()), p, ec);
auto const n = p.put(
buffer(s.data(), s.size()), ec);
if(! BEAST_EXPECTS(! ec, ec.message()))
return;
if(! BEAST_EXPECT(n == s.size()))
@ -254,11 +197,11 @@ public:
{
using boost::asio::buffer;
test_parser<isRequest> p;
p.eager(true);
if(skipBody)
p.skip(true);
error_code ec;
feed(buffer(
s.data(), s.size()), p, ec);
p.put(buffer(s.data(), s.size()), ec);
if(! ec && ev)
p.put_eof(ec);
BEAST_EXPECTS(ec == ev, ec.message());
@ -267,7 +210,6 @@ public:
void
testFlatten()
{
#if 0
using boost::asio::buffer;
{
std::string const s =
@ -283,10 +225,12 @@ public:
auto const b2 = buffer(
s.data() + i, s.size() - i);
test_parser<true> p;
p.eager(true);
error_code ec;
feed(b1, p, ec);
BEAST_EXPECTS(! ec, ec.message());
feed(buffer_cat(b1, b2), p, ec);
p.put(b1, ec);
BEAST_EXPECTS(ec == error::need_more, ec.message());
ec = {};
p.put(boost::asio::buffer(s.data(), s.size()), ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.is_done());
}
@ -303,16 +247,16 @@ public:
auto const b2 = buffer(
s.data() + i, s.size() - i);
test_parser<false> p;
p.eager(true);
error_code ec;
feed(b1, p, ec);
BEAST_EXPECTS(! ec, ec.message());
p.put(b1, ec);
BEAST_EXPECTS(ec == error::need_more, ec.message());
ec = {};
feed(buffer_cat(b1, b2), p, ec);
p.put(buffer_cat(b1, b2), ec);
BEAST_EXPECTS(! ec, ec.message());
p.put_eof(ec);
}
}
#endif
}
// Check that all callbacks are invoked
@ -322,6 +266,7 @@ public:
using boost::asio::buffer;
{
test_parser<true> p;
p.eager(true);
error_code ec;
std::string const s =
"GET / HTTP/1.1\r\n"
@ -329,7 +274,7 @@ public:
"Content-Length: 1\r\n"
"\r\n"
"*";
feed(buffer(s), p, ec);
p.put(buffer(s), ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.is_done());
BEAST_EXPECT(p.got_on_begin);
@ -341,6 +286,7 @@ public:
}
{
test_parser<false> p;
p.eager(true);
error_code ec;
std::string const s =
"HTTP/1.1 200 OK\r\n"
@ -348,7 +294,7 @@ public:
"Content-Length: 1\r\n"
"\r\n"
"*";
feed(buffer(s), p, ec);
p.put(buffer(s), ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.is_done());
BEAST_EXPECT(p.got_on_begin);
@ -358,6 +304,48 @@ public:
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\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
@ -745,9 +733,9 @@ public:
});
}
void testBody()
void
testBody()
{
#if 0
using boost::asio::buffer;
good<true>(
"GET / HTTP/1.1\r\n"
@ -804,6 +792,7 @@ public:
// response without Content-Length or
// Transfer-Encoding: chunked requires eof.
{
#if 0
error_code ec;
test_parser<false> p;
feed(buf(
@ -822,6 +811,7 @@ public:
p.put_eof(ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.is_done());
#endif
}
// 304 "Not Modified" response does not require eof
@ -883,7 +873,6 @@ public:
"Content-Length: 1\r\n"
"\r\n",
error::partial_message);
#endif
}
#if 0
@ -906,7 +895,6 @@ public:
void
testSplit()
{
#if 0
multi_buffer b;
ostream(b) <<
"POST / HTTP/1.1\r\n"
@ -915,8 +903,7 @@ public:
"*****";
error_code ec;
test_parser<true> p;
p.pause();
auto n = feed(b.data(), p, ec);
auto n = p.put(b.data(), ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.got_on_begin);
BEAST_EXPECT(p.got_on_field);
@ -924,11 +911,10 @@ public:
BEAST_EXPECT(! p.got_on_body);
BEAST_EXPECT(! p.got_on_chunk);
BEAST_EXPECT(! p.got_on_complete);
BEAST_EXPECT(p.state() != parse_state::header);
BEAST_EXPECT(! p.is_done());
BEAST_EXPECT(p.is_header_done());
BEAST_EXPECT(p.body.empty());
b.consume(n);
p.resume();
n = feed(b.data(), p, ec);
BEAST_EXPECTS(! ec, ec.message());
BEAST_EXPECT(p.got_on_begin);
@ -939,7 +925,6 @@ public:
BEAST_EXPECT(p.got_on_complete);
BEAST_EXPECT(p.is_done());
BEAST_EXPECT(p.body == "*****");
#endif
}
//--------------------------------------------------------------------------