|
3 | 3 | // (See accompanying file LICENSE_1_0.txt or copy at |
4 | 4 | // http://www.boost.org/LICENSE_1_0.txt) |
5 | 5 |
|
| 6 | +#include<iterator> |
6 | 7 | #include<gtest/gtest.h> |
7 | 8 | #include<boost/algorithm/string/trim.hpp> |
8 | 9 | #include"network/http/v2/client/response_parser.hpp" |
@@ -46,38 +47,105 @@ namespace { |
46 | 47 | "DEALINGS IN THE SOFTWARE.\n" |
47 | 48 | ; |
48 | 49 |
|
| 50 | +template<typename Iterator> |
49 | 51 | inline |
50 | | - std::stringtrimmed_string(std::string::const_iterator first, |
51 | | - std::string::const_iterator last) { |
52 | | -returnboost::trim_right_copy(std::string(first, last)); |
| 52 | + std::stringtrimmed_string(Iterator first, Iterator last) { |
| 53 | +returnboost::trim_copy(std::string(first, last)); |
| 54 | + } |
| 55 | + |
| 56 | +template<classRng> |
| 57 | +inline |
| 58 | + std::stringtrimmed_string(const Rng &rng) { |
| 59 | +returntrimmed_string(std::begin(rng),std::end(rng)); |
53 | 60 | } |
54 | 61 | }// namespace |
55 | 62 |
|
56 | 63 | TEST(response_parser_test, parse_version) { |
57 | 64 | 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)); |
61 | 73 | } |
62 | 74 |
|
63 | 75 | TEST(response_parser_test, parse_status_code) { |
64 | 76 | 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)); |
72 | 89 | } |
73 | 90 |
|
74 | 91 | TEST(response_parser_test, parse_status_message) { |
75 | 92 | 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 | + } |
83 | 151 | } |