1 /*
2 	restinio
3 */
4 
5 #include <catch2/catch.hpp>
6 
7 #include <restinio/helpers/http_field_parsers/try_parse_field.hpp>
8 #include <restinio/helpers/http_field_parsers/content-encoding.hpp>
9 
10 #include <test/common/dummy_connection.hpp>
11 
12 using namespace std::string_literals;
13 
14 RESTINIO_NODISCARD
15 auto
make_dummy_endpoint()16 make_dummy_endpoint()
17 {
18 	return restinio::endpoint_t{
19 			restinio::asio_ns::ip::address::from_string("127.0.0.1"),
20 			12345u
21 	};
22 }
23 
24 TEST_CASE( "No Content-Encoding field", "[try_parse_field]" )
25 {
26 	using namespace restinio::http_field_parsers;
27 
28 	restinio::no_extra_data_factory_t extra_data_factory;
29 	auto req = std::make_shared< restinio::request_t >(
30 			restinio::request_id_t{1},
31 			restinio::http_request_header_t{},
32 			"Body"s,
33 			dummy_connection_t::make(1u),
34 			make_dummy_endpoint(),
35 			extra_data_factory );
36 
37 	struct handler_t
38 	{
operator ()handler_t39 		void operator()(const content_encoding_value_t &) const {
40 			REQUIRE( false );
41 		}
42 
operator ()handler_t43 		void operator()(field_not_found_t) const {
44 			REQUIRE( true );
45 		}
46 
operator ()handler_t47 		void operator()(restinio::easy_parser::parse_error_t) const {
48 			REQUIRE( false );
49 		}
50 	};
51 
52 	restinio::visit( handler_t{},
53 			try_parse_field< content_encoding_value_t >(
54 					*req,
55 					restinio::http_field::content_encoding) );
56 }
57 
58 TEST_CASE( "Empty Content-Encoding field", "[try_parse_field]" )
59 {
60 	using namespace restinio::http_field_parsers;
61 
62 	restinio::http_request_header_t dummy_header{
63 			restinio::http_method_post(),
64 			"/"
65 	};
66 	dummy_header.set_field(
67 			restinio::http_field::content_encoding,
68 			""s );
69 
70 	restinio::no_extra_data_factory_t extra_data_factory;
71 	auto req = std::make_shared< restinio::request_t >(
72 			restinio::request_id_t{1},
73 			std::move(dummy_header),
74 			"Body"s,
75 			dummy_connection_t::make(1u),
76 			make_dummy_endpoint(),
77 			extra_data_factory );
78 
79 	struct handler_t
80 	{
operator ()handler_t81 		void operator()(const content_encoding_value_t &) const {
82 			REQUIRE( false );
83 		}
84 
operator ()handler_t85 		void operator()(field_not_found_t) const {
86 			REQUIRE( false );
87 		}
88 
operator ()handler_t89 		void operator()(restinio::easy_parser::parse_error_t err) const {
90 			REQUIRE( restinio::easy_parser::error_reason_t::unexpected_eof
91 					== err.reason() );
92 		}
93 	};
94 
95 	restinio::visit( handler_t{},
96 			try_parse_field< content_encoding_value_t >(
97 					*req,
98 					restinio::http_field::content_encoding) );
99 }
100 
101 TEST_CASE( "Normal Content-Encoding field", "[try_parse_field]" )
102 {
103 	using namespace restinio::http_field_parsers;
104 
105 	restinio::http_request_header_t dummy_header{
106 			restinio::http_method_post(),
107 			"/"
108 	};
109 	dummy_header.set_field(
110 			restinio::http_field::content_encoding,
111 			"UTF-8"s );
112 
113 	restinio::no_extra_data_factory_t extra_data_factory;
114 	auto req = std::make_shared< restinio::request_t >(
115 			restinio::request_id_t{1},
116 			std::move(dummy_header),
117 			"Body"s,
118 			dummy_connection_t::make(1u),
119 			make_dummy_endpoint(),
120 			extra_data_factory );
121 
122 	struct handler_t
123 	{
operator ()handler_t124 		void operator()(const content_encoding_value_t & v) const {
125 			REQUIRE( std::vector<std::string>{ "utf-8"s } == v.values);
126 		}
127 
operator ()handler_t128 		void operator()(field_not_found_t) const {
129 			REQUIRE( false );
130 		}
131 
operator ()handler_t132 		void operator()(restinio::easy_parser::parse_error_t) const {
133 			REQUIRE( false );
134 		}
135 	};
136 
137 	restinio::visit( handler_t{},
138 			try_parse_field< content_encoding_value_t >(
139 					*req,
140 					restinio::http_field::content_encoding) );
141 }
142 
143 TEST_CASE( "Default value for Content-Encoding", "[try_parse_field]" )
144 {
145 	using namespace restinio::http_field_parsers;
146 
147 	restinio::http_request_header_t dummy_header{
148 			restinio::http_method_post(),
149 			"/"
150 	};
151 	restinio::no_extra_data_factory_t extra_data_factory;
152 	auto req = std::make_shared< restinio::request_t >(
153 			restinio::request_id_t{1},
154 			std::move(dummy_header),
155 			"Body"s,
156 			dummy_connection_t::make(1u),
157 			make_dummy_endpoint(),
158 			extra_data_factory );
159 
160 	struct handler_t
161 	{
operator ()handler_t162 		void operator()(const content_encoding_value_t & v) const {
163 			REQUIRE( std::vector<std::string>{ "utf-8"s } == v.values);
164 		}
165 
operator ()handler_t166 		void operator()(field_not_found_t) const {
167 			REQUIRE( false );
168 		}
169 
operator ()handler_t170 		void operator()(restinio::easy_parser::parse_error_t) const {
171 			REQUIRE( false );
172 		}
173 	};
174 
175 	restinio::visit( handler_t{},
176 			try_parse_field< content_encoding_value_t >(
177 					*req,
178 					restinio::http_field::content_encoding,
179 					"UTF-8") );
180 }
181 
182 TEST_CASE( "Normal Content-Encoding field with custom name", "[try_parse_field]" )
183 {
184 	using namespace restinio::http_field_parsers;
185 
186 	restinio::http_request_header_t dummy_header{
187 			restinio::http_method_post(),
188 			"/"
189 	};
190 	dummy_header.set_field(
191 			"My-Content-Encoding",
192 			"UTF-8"s );
193 
194 	restinio::no_extra_data_factory_t extra_data_factory;
195 	auto req = std::make_shared< restinio::request_t >(
196 			restinio::request_id_t{1},
197 			std::move(dummy_header),
198 			"Body"s,
199 			dummy_connection_t::make(1u),
200 			make_dummy_endpoint(),
201 			extra_data_factory );
202 
203 	struct handler_t
204 	{
operator ()handler_t205 		void operator()(const content_encoding_value_t & v) const {
206 			REQUIRE( std::vector<std::string>{ "utf-8"s } == v.values);
207 		}
208 
operator ()handler_t209 		void operator()(field_not_found_t) const {
210 			REQUIRE( false );
211 		}
212 
operator ()handler_t213 		void operator()(restinio::easy_parser::parse_error_t) const {
214 			REQUIRE( false );
215 		}
216 	};
217 
218 	restinio::visit( handler_t{},
219 			try_parse_field< content_encoding_value_t >(
220 					*req,
221 					"my-content-encoding") );
222 }
223 
224 TEST_CASE( "Default value for Content-Encoding with custom name", "[try_parse_field]" )
225 {
226 	using namespace restinio::http_field_parsers;
227 
228 	restinio::http_request_header_t dummy_header{
229 			restinio::http_method_post(),
230 			"/"
231 	};
232 	restinio::no_extra_data_factory_t extra_data_factory;
233 	auto req = std::make_shared< restinio::request_t >(
234 			restinio::request_id_t{1},
235 			std::move(dummy_header),
236 			"Body"s,
237 			dummy_connection_t::make(1u),
238 			make_dummy_endpoint(),
239 			extra_data_factory );
240 
241 	struct handler_t
242 	{
operator ()handler_t243 		void operator()(const content_encoding_value_t & v) const {
244 			REQUIRE( std::vector<std::string>{ "utf-8"s } == v.values);
245 		}
246 
operator ()handler_t247 		void operator()(field_not_found_t) const {
248 			REQUIRE( false );
249 		}
250 
operator ()handler_t251 		void operator()(restinio::easy_parser::parse_error_t) const {
252 			REQUIRE( false );
253 		}
254 	};
255 
256 	restinio::visit( handler_t{},
257 			try_parse_field< content_encoding_value_t >(
258 					*req,
259 					"My-Content-Encoding",
260 					"UTF-8") );
261 }
262 
263 TEST_CASE( "Normal Content-Encoding field with get_if", "[try_parse_field]" )
264 {
265 	using namespace restinio::http_field_parsers;
266 
267 	restinio::http_request_header_t dummy_header{
268 			restinio::http_method_post(),
269 			"/"
270 	};
271 	dummy_header.set_field(
272 			restinio::http_field::content_encoding,
273 			"UTF-8"s );
274 
275 	restinio::no_extra_data_factory_t extra_data_factory;
276 	auto req = std::make_shared< restinio::request_t >(
277 			restinio::request_id_t{1},
278 			std::move(dummy_header),
279 			"Body"s,
280 			dummy_connection_t::make(1u),
281 			make_dummy_endpoint(),
282 			extra_data_factory );
283 
284 	const auto parse_result = try_parse_field< content_encoding_value_t >(
285 			*req,
286 			restinio::http_field::content_encoding );
287 	if( const auto * v = restinio::get_if< content_encoding_value_t >(
288 			&parse_result ) )
289 	{
290 		REQUIRE( std::vector<std::string>{ "utf-8"s } == v->values);
291 	}
292 	else
293 	{
294 		REQUIRE( false );
295 	}
296 }
297 
298