]>
git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/test/http/message_parser.cpp
2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 // Test that header file is self-contained.
9 #include <beast/http/message_parser.hpp>
11 #include "test_parser.hpp"
13 #include <beast/unit_test/suite.hpp>
14 #include <beast/test/string_istream.hpp>
15 #include <beast/test/string_ostream.hpp>
16 #include <beast/test/yield_to.hpp>
17 #include <beast/core/flat_streambuf.hpp>
18 #include <beast/core/streambuf.hpp>
19 #include <beast/http/header_parser.hpp>
20 #include <beast/http/read.hpp>
21 #include <beast/http/read.hpp>
22 #include <beast/http/string_body.hpp>
23 #include <boost/system/system_error.hpp>
28 class message_parser_test
29 : public beast::unit_test::suite
30 , public beast::test::enable_yield_to
33 template<bool isRequest
, class Pred
>
35 testMatrix(std::string
const& s
, Pred
const& pred
)
37 beast::test::string_istream ss
{get_io_service(), s
};
42 flat_streambuf dynabuf
{1024};
44 message
<isRequest
, string_body
, fields
> m
;
45 read(ss
, dynabuf
, m
, ec
);
46 if(! BEAST_EXPECTS(! ec
, ec
.message()))
59 [&](message
<false, string_body
, fields
> const& m
)
61 BEAST_EXPECTS(m
.body
== "*******",
62 "body='" + m
.body
+ "'");
68 "Transfer-Encoding: chunked\r\n"
76 "MD5-Fingerprint: -\r\n"
78 [&](message
<false, string_body
, fields
> const& m
)
80 BEAST_EXPECT(m
.body
== "*****--");
86 "Content-Length: 5\r\n"
89 [&](message
<false, string_body
, fields
> const& m
)
91 BEAST_EXPECT(m
.body
== "*****");
96 "User-Agent: test\r\n"
98 [&](message
<true, string_body
, fields
> const& m
)
104 "User-Agent: test\r\n"
107 [&](message
<true, string_body
, fields
> const& m
)
109 BEAST_EXPECT(m
.fields
["X"] == "x");
117 using boost::asio::buffer
;
120 beast::test::string_istream is
{
123 "User-Agent: test\r\n"
124 "Content-Length: 1\r\n"
127 flat_streambuf sb
{1024};
128 message_parser
<true, string_body
, fields
> p
;
130 auto const& m
= p
.get();
131 BEAST_EXPECTS(! ec
, ec
.message());
132 BEAST_EXPECT(p
.is_complete());
133 BEAST_EXPECT(m
.method
== "GET");
134 BEAST_EXPECT(m
.url
== "/");
135 BEAST_EXPECT(m
.version
== 11);
136 BEAST_EXPECT(m
.fields
["User-Agent"] == "test");
137 BEAST_EXPECT(m
.body
== "*");
141 // test partial parsing of final chunk
142 // parse through the chunk body
143 beast::test::string_istream is
{
144 get_io_service(), ""};
148 "Transfer-Encoding: chunked\r\n"
153 message_parser
<true, string_body
, fields
> p
;
155 BEAST_EXPECT(sb
.size() == 0);
156 BEAST_EXPECTS(! ec
, ec
.message());
157 BEAST_EXPECT(!p
.is_complete());
158 BEAST_EXPECT(p
.get().body
== "*");
159 sb
<< "\r\n0;d;e=3;f=\"4\"\r\n"
161 "MD5-Fingerprint: -\r\n";
162 // incomplete parse, missing the final crlf
163 BEAST_EXPECT(p
.write(sb
.data(), ec
) == 0);
164 BEAST_EXPECTS(! ec
, ec
.message());
165 BEAST_EXPECT(!p
.is_complete());
166 sb
<< "\r\n"; // final crlf to end message
167 BEAST_EXPECT(p
.write(sb
.data(), ec
) == sb
.size());
168 BEAST_EXPECTS(! ec
, ec
.message());
169 BEAST_EXPECT(p
.is_complete());
173 message_parser
<false, string_body
, fields
> p
;
174 std::string
const s
=
175 "HTTP/1.1 200 OK\r\n"
177 "Content-Length: 1\r\n"
180 p
.write(buffer(s
), ec
);
181 auto const& m
= p
.get();
182 BEAST_EXPECTS(! ec
, ec
.message());
183 BEAST_EXPECT(p
.is_complete());
184 BEAST_EXPECT(m
.status
== 200);
185 BEAST_EXPECT(m
.reason
== "OK");
186 BEAST_EXPECT(m
.version
== 11);
187 BEAST_EXPECT(m
.fields
["Server"] == "test");
188 BEAST_EXPECT(m
.body
== "*");
193 message_parser
<false, string_body
, fields
> p
;
194 std::string
const s
=
195 "HTTP/1.1 200 Connection Established\r\n"
196 "Proxy-Agent: Zscaler/5.1\r\n"
199 p
.write(buffer(s
), ec
);
200 BEAST_EXPECTS(! ec
, ec
.message());
201 BEAST_EXPECT(p
.is_complete());
207 testExpect100Continue()
209 test::string_istream ss
{ios_
,
210 "POST / HTTP/1.1\r\n"
211 "Expect: 100-continue\r\n"
212 "Content-Length: 5\r\n"
217 header_parser
<true, fields
> p0
;
218 auto const bytes_used
=
219 read_some(ss
, sb
, p0
, ec
);
220 sb
.consume(bytes_used
);
221 BEAST_EXPECTS(! ec
, ec
.message());
222 BEAST_EXPECT(p0
.state() != parse_state::header
);
223 BEAST_EXPECT(! p0
.is_complete());
225 string_body
, fields
> p1
{std::move(p0
)};
226 read(ss
, sb
, p1
, ec
);
227 BEAST_EXPECTS(! ec
, ec
.message());
228 BEAST_EXPECT(p1
.get().body
== "*****");
236 testExpect100Continue();
240 BEAST_DEFINE_TESTSUITE(message_parser
,http
,beast
);