Skip to content

Commit 9345bed

Browse files
committed
Updated tests for the response_parser.
1 parent 58a7782 commit 9345bed

File tree

1 file changed

+88
-20
lines changed

1 file changed

+88
-20
lines changed

http/test/v2/units/client/response_parser_test.cpp

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// (See accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

6+
#include <iterator>
67
#include <gtest/gtest.h>
78
#include <boost/algorithm/string/trim.hpp>
89
#include "network/http/v2/client/response_parser.hpp"
@@ -46,38 +47,105 @@ namespace {
4647
"DEALINGS IN THE SOFTWARE.\n"
4748
;
4849

50+
template <typename Iterator>
4951
inline
50-
std::string trimmed_string(std::string::const_iterator first,
51-
std::string::const_iterator last) {
52-
return boost::trim_right_copy(std::string(first, last));
52+
std::string trimmed_string(Iterator first, Iterator last) {
53+
return boost::trim_copy(std::string(first, last));
54+
}
55+
56+
template <class Rng>
57+
inline
58+
std::string trimmed_string(const Rng &rng) {
59+
return trimmed_string(std::begin(rng), std::end(rng));
5360
}
5461
} // namespace
5562

5663
TEST(response_parser_test, parse_version) {
5764
http::response_parser parser;
58-
auto part = parser.parse_until(http::response_parser::http_version_done, input);
59-
ASSERT_TRUE(std::get<0>(part));
60-
ASSERT_EQ("HTTP/1.0", trimmed_string(std::begin(std::get<1>(part)), std::end(std::get<1>(part))));
65+
66+
boost::logic::tribool parsed_ok = false;
67+
boost::iterator_range<std::string::const_iterator> version;
68+
std::tie(parsed_ok, version) = parser.parse_until(http::response_parser::http_version_done, input);
69+
70+
ASSERT_TRUE(parsed_ok);
71+
ASSERT_EQ(http::response_parser::http_version_done, parser.state());
72+
ASSERT_EQ("HTTP/1.0", trimmed_string(version));
6173
}
6274

6375
TEST(response_parser_test, parse_status_code) {
6476
http::response_parser parser;
65-
auto part_1 = parser.parse_until(http::response_parser::http_version_done, input);
66-
ASSERT_TRUE(std::get<0>(part_1));
67-
auto part_2 = parser.parse_until(http::response_parser::http_status_done,
68-
boost::make_iterator_range(std::end(std::get<1>(part_1)),
69-
std::end(input)));
70-
ASSERT_TRUE(std::get<0>(part_2));
71-
ASSERT_EQ("200", trimmed_string(std::begin(std::get<1>(part_2)), std::end(std::get<1>(part_2))));
77+
78+
boost::logic::tribool parsed_ok = false;
79+
boost::iterator_range<std::string::const_iterator> status;
80+
std::tie(parsed_ok, status) = parser.parse_until(http::response_parser::http_version_done, input);
81+
ASSERT_TRUE(parsed_ok);
82+
83+
std::tie(parsed_ok, status) = parser.parse_until(http::response_parser::http_status_done,
84+
boost::make_iterator_range(std::end(status),
85+
std::end(input)));
86+
ASSERT_TRUE(parsed_ok);
87+
ASSERT_EQ(http::response_parser::http_status_done, parser.state());
88+
ASSERT_EQ("200", trimmed_string(status));
7289
}
7390

7491
TEST(response_parser_test, parse_status_message) {
7592
http::response_parser parser;
76-
auto part_1 = parser.parse_until(http::response_parser::http_status_done, input);
77-
ASSERT_TRUE(std::get<0>(part_1));
78-
auto part_2 = parser.parse_until(http::response_parser::http_status_message_done,
79-
boost::make_iterator_range(std::end(std::get<1>(part_1)),
80-
std::end(input)));
81-
ASSERT_TRUE(std::get<0>(part_2));
82-
ASSERT_EQ("OK", trimmed_string(std::begin(std::get<1>(part_2)), std::end(std::get<1>(part_2))));
93+
94+
boost::logic::tribool parsed_ok = false;
95+
boost::iterator_range<std::string::const_iterator> status;
96+
std::tie(parsed_ok, status) = parser.parse_until(http::response_parser::http_status_done, input);
97+
ASSERT_TRUE(parsed_ok);
98+
99+
std::tie(parsed_ok, status) = parser.parse_until(http::response_parser::http_status_message_done,
100+
boost::make_iterator_range(std::end(status),
101+
std::end(input)));
102+
ASSERT_TRUE(parsed_ok);
103+
ASSERT_EQ(http::response_parser::http_status_message_done, parser.state());
104+
ASSERT_EQ("OK", trimmed_string(status));
105+
}
106+
107+
TEST(response_parser_test, parse_first_header) {
108+
http::response_parser parser;
109+
110+
boost::logic::tribool parsed_ok = false;
111+
boost::iterator_range<std::string::const_iterator> header;
112+
std::tie(parsed_ok, header) = parser.parse_until(http::response_parser::http_status_message_done, input);
113+
ASSERT_TRUE(parsed_ok);
114+
115+
std::tie(parsed_ok, header) = parser.parse_until(http::response_parser::http_header_colon,
116+
boost::make_iterator_range(std::end(header),
117+
std::end(input)));
118+
ASSERT_TRUE(parsed_ok);
119+
ASSERT_EQ(http::response_parser::http_header_colon, parser.state());
120+
ASSERT_EQ("Date:", trimmed_string(header));
121+
122+
std::tie(parsed_ok, header) = parser.parse_until(http::response_parser::http_header_line_done,
123+
boost::make_iterator_range(std::end(header),
124+
std::end(input)));
125+
ASSERT_TRUE(parsed_ok);
126+
ASSERT_EQ(http::response_parser::http_header_line_done, parser.state());
127+
ASSERT_EQ("Wed, 11 Sep 2013 05:50:12 GMT", trimmed_string(header));
128+
}
129+
130+
TEST(response_parser_test, parse_headers) {
131+
http::response_parser parser;
132+
133+
boost::logic::tribool parsed_ok = false;
134+
boost::iterator_range<std::string::const_iterator> header;
135+
std::tie(parsed_ok, header) = parser.parse_until(http::response_parser::http_status_message_done, input);
136+
ASSERT_TRUE(parsed_ok);
137+
138+
while (!header) {
139+
std::tie(parsed_ok, header) = parser.parse_until(http::response_parser::http_header_colon,
140+
boost::make_iterator_range(std::end(header),
141+
std::end(input)));
142+
ASSERT_TRUE(parsed_ok);
143+
ASSERT_EQ(http::response_parser::http_header_colon, parser.state());
144+
145+
std::tie(parsed_ok, header) = parser.parse_until(http::response_parser::http_header_colon,
146+
boost::make_iterator_range(std::end(header),
147+
std::end(input)));
148+
ASSERT_TRUE(parsed_ok);
149+
ASSERT_EQ(http::response_parser::http_header_line_done, parser.state());
150+
}
83151
}

0 commit comments

Comments
 (0)