Skip to content

Commit d5062a7

Browse files
author
pfeatherstone
committed
more unit tests
1 parent ba04c96 commit d5062a7

File tree

1 file changed

+168
-2
lines changed

1 file changed

+168
-2
lines changed

examples/unit_tests/message.cpp

Lines changed: 168 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ TEST_SUITE("[MESSAGE]")
192192
http::request req;
193193
std::error_code ec{};
194194
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
195-
REQUIRE(ec == http::http_read_header_unsupported_field);
195+
REQUIRE(ec == http::http_read_header_unsupported_field); // (It should be "Host:" not "Host :")
196196
}
197197

198198
{
@@ -208,9 +208,175 @@ TEST_SUITE("[MESSAGE]")
208208
std::error_code ec{};
209209
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
210210
REQUIRE(!bool(ec));
211-
REQUIRE(!finished);
211+
REQUIRE(!finished); // Incorrect Content-Length
212212
REQUIRE(bad_req.empty());
213213
}
214+
215+
{
216+
std::string bad_req = "POST / HTTP/1.1\r\n";
217+
bad_req += "Host: developer.mozilla.org\r\n";
218+
bad_req += "User-Agent: curl/8.6.0\r\n";
219+
bad_req += "Content-Type: application/json\r\n";
220+
bad_req += "Content-Length: 32\r\n";
221+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
222+
223+
http::request req;
224+
std::error_code ec{};
225+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
226+
REQUIRE(!bool(ec));
227+
REQUIRE(!finished); // Waiting for \r\n to test header
228+
}
229+
230+
231+
{
232+
std::string bad_req = "POST / HTTP/1.1\r\n";
233+
bad_req += "Host: developer.mozilla.org\r\n";
234+
bad_req += "User-Agent: curl/8.6.0\r\n";
235+
bad_req += "Content-Type: application/json\r\n";
236+
bad_req += "Content-Length: 32\r\n";
237+
bad_req += "\r\n";
238+
bad_req += "{\"name\": \"Obi\", \"creed\": \"Jedi\"}";
239+
240+
http::request req;
241+
std::error_code ec{};
242+
bool finished = http::parser<http::request>{}.parse(req, bad_req, ec);
243+
REQUIRE(!bool(ec));
244+
REQUIRE(finished); // Done
245+
REQUIRE(bad_req.empty());
246+
}
247+
}
248+
249+
TEST_CASE("parse bad response")
250+
{
251+
{
252+
std::string bad_reply = "HTTP/2.1 200 ok\r\n";
253+
bad_reply += "Server: Apache\r\n";
254+
bad_reply += "Content-Type: text\r\n";
255+
bad_reply += "Content-Length: 11\r\n";
256+
bad_reply += "\r\n";
257+
bad_reply += "hello there";
258+
259+
http::response reply;
260+
std::error_code ec{};
261+
bool finished = http::parser<http::response>{}.parse(reply, bad_reply, ec);
262+
REQUIRE(ec == http::http_read_unsupported_http_version);
263+
}
264+
265+
{
266+
std::string bad_reply = "HTTP/11 200 ok\r\n";
267+
bad_reply += "Server: Apache\r\n";
268+
bad_reply += "Content-Type: text\r\n";
269+
bad_reply += "Content-Length: 11\r\n";
270+
bad_reply += "\r\n";
271+
bad_reply += "hello there";
272+
273+
http::response reply;
274+
std::error_code ec{};
275+
bool finished = http::parser<http::response>{}.parse(reply, bad_reply, ec);
276+
REQUIRE(ec == http::http_read_unsupported_http_version);
277+
}
278+
279+
{
280+
std::string bad_reply = "HTT/1.1 200 ok\r\n";
281+
bad_reply += "Server: Apache\r\n";
282+
bad_reply += "Content-Type: text\r\n";
283+
bad_reply += "Content-Length: 11\r\n";
284+
bad_reply += "\r\n";
285+
bad_reply += "hello there";
286+
287+
http::response reply;
288+
std::error_code ec{};
289+
bool finished = http::parser<http::response>{}.parse(reply, bad_reply, ec);
290+
REQUIRE(ec == http::http_read_unsupported_http_version);
291+
}
292+
293+
{
294+
std::string bad_reply = "HTTP/1.1 99 teapot\r\n";
295+
bad_reply += "Server: Apache\r\n";
296+
bad_reply += "Content-Type: text\r\n";
297+
bad_reply += "Content-Length: 11\r\n";
298+
bad_reply += "\r\n";
299+
bad_reply += "hello there";
300+
301+
http::response reply;
302+
std::error_code ec{};
303+
bool finished = http::parser<http::response>{}.parse(reply, bad_reply, ec);
304+
REQUIRE(ec == http::http_read_bad_status);
305+
}
306+
307+
{
308+
std::string bad_reply = "HTTP/1.1 200 ok\r\n";
309+
bad_reply += "Server: Apache\r\n";
310+
bad_reply += "Content-Type- text\r\n";
311+
bad_reply += "Content-Length: 11\r\n";
312+
bad_reply += "\r\n";
313+
bad_reply += "hello there";
314+
315+
http::response reply;
316+
std::error_code ec{};
317+
bool finished = http::parser<http::response>{}.parse(reply, bad_reply, ec);
318+
REQUIRE(ec == http::http_read_header_kv_delimiter_not_found);
319+
}
320+
321+
{
322+
std::string bad_reply = "HTTP/1.1 200 ok\r\n";
323+
bad_reply += "Server: Apache\r\n";
324+
bad_reply += "Content-Type : text\r\n";
325+
bad_reply += "Content-Length: 11\r\n";
326+
bad_reply += "\r\n";
327+
bad_reply += "hello there";
328+
329+
http::response reply;
330+
std::error_code ec{};
331+
bool finished = http::parser<http::response>{}.parse(reply, bad_reply, ec);
332+
REQUIRE(ec == http::http_read_header_unsupported_field);
333+
}
334+
335+
{
336+
std::string bad_reply = "HTTP/1.1 200 ok\r\n";
337+
bad_reply += "Server: Apache\r\n";
338+
bad_reply += "Content-Flavour: text\r\n";
339+
bad_reply += "Content-Length: 11\r\n";
340+
bad_reply += "\r\n";
341+
bad_reply += "hello there";
342+
343+
http::response reply;
344+
std::error_code ec{};
345+
bool finished = http::parser<http::response>{}.parse(reply, bad_reply, ec);
346+
REQUIRE(ec == http::http_read_header_unsupported_field);
347+
}
348+
349+
{
350+
std::string bad_reply = "HTTP/1.1 200 ok\r\n";
351+
bad_reply += "Server: Apache\r\n";
352+
bad_reply += "Content-Type: text\r\n";
353+
bad_reply += "Content-Length: 32\r\n";
354+
bad_reply += "\r\n";
355+
bad_reply += "hello there";
356+
357+
http::response reply;
358+
std::error_code ec{};
359+
bool finished = http::parser<http::response>{}.parse(reply, bad_reply, ec);
360+
REQUIRE(!bool(ec));
361+
REQUIRE(!finished); // Wrong content length. Waiting for rest of payload
362+
REQUIRE(bad_reply.empty());
363+
}
364+
365+
{
366+
std::string good_reply = "HTTP/1.1 200 ok\r\n";
367+
good_reply += "Server: Apache\r\n";
368+
good_reply += "Content-Type: text\r\n";
369+
good_reply += "Content-Length: 11\r\n";
370+
good_reply += "\r\n";
371+
good_reply += "hello there";
372+
373+
http::response reply;
374+
std::error_code ec{};
375+
bool finished = http::parser<http::response>{}.parse(reply, good_reply, ec);
376+
REQUIRE(!bool(ec));
377+
REQUIRE(finished); // Good
378+
REQUIRE(good_reply.empty());
379+
}
214380
}
215381

216382
TEST_CASE("serialise bad requests")

0 commit comments

Comments
 (0)