|
| 1 | + |
| 2 | +// Copyright Dean Michael Berris 2010. |
| 3 | +// Distributed under the Boost Software License, Version 1.0. |
| 4 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 5 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 6 | + |
| 7 | +#define BOOST_TEST_MODULE HTTP Incremental Parser Test |
| 8 | +#include <boost/config/warning_disable.hpp> |
| 9 | +#include <boost/test/unit_test.hpp> |
| 10 | +#include <boost/network/protocol/http/parser/incremental.hpp> |
| 11 | +#include <boost/range.hpp> |
| 12 | +#include <boost/logic/tribool.hpp> |
| 13 | +#include <string> |
| 14 | +#include <iostream> |
| 15 | + |
| 16 | +/** Synopsis |
| 17 | + * |
| 18 | + * Test for HTTP Response Incremental Parser |
| 19 | + * ----------------------------------------- |
| 20 | + * |
| 21 | + * In this test we fully intend to specify how an incremental |
| 22 | + * HTTP Response parser should be used. This defines the bare |
| 23 | + * minimum implementation for an Incremental Parser concept, |
| 24 | + * and shall follow an interface that puts a premium on simplicity. |
| 25 | + * |
| 26 | + * The motivation for coming up with a re-startable stateful |
| 27 | + * incremental parser comes from the requirement in the asynchronous |
| 28 | + * HTTP client implementation that allows for parsing an HTTP |
| 29 | + * response as the data comes in. By being able to process some |
| 30 | + * parts of the message ahead of others, we are allowed to set |
| 31 | + * the promise values bound to futures that the users of the client |
| 32 | + * would be waiting on. |
| 33 | + * |
| 34 | + * The basic interface that we're looking for is a means of providing: |
| 35 | + * - a range of input |
| 36 | + * - a completion function once a certain state is reached |
| 37 | + * - a means of resetting the parser's state |
| 38 | + * |
| 39 | + * One of the possible implementations can use the Boost.MSM library |
| 40 | + * to create the state machine. The test however does not specify what |
| 41 | + * implementation should be used, but rather that the interface and the |
| 42 | + * semantics are according to expectations. |
| 43 | + * |
| 44 | + * Date: September 9, 2010 |
| 45 | + * Author: Dean Michael Berris <mikhailberis@gmail.com> |
| 46 | + */ |
| 47 | + |
| 48 | +namespace tags = boost::network::tags; |
| 49 | +namespace logic = boost::logic; |
| 50 | +namespace fusion = boost::fusion; |
| 51 | +using namespace boost::network::http; |
| 52 | + |
| 53 | +BOOST_AUTO_TEST_CASE(incremental_parser_constructor) { |
| 54 | + response_parser<tags::default_string> p; // default constructible |
| 55 | +} |
| 56 | + |
| 57 | +/** In this test we want to be able to parse incrementally a |
| 58 | + * range passed in as input, and specify to the parser that |
| 59 | + * it should stop when we reach a certain state. In this case |
| 60 | + * we want it to parse until it either finds the HTTP version |
| 61 | + * or there is an error encountered. |
| 62 | + */ |
| 63 | +BOOST_AUTO_TEST_CASE(incremental_parser_parse_http_version) { |
| 64 | + response_parser<tags::default_string> p; // default constructible |
| 65 | + logic::tribool parsed_ok = false; |
| 66 | + typedef response_parser<tags::default_string>::range_type range_type; |
| 67 | + range_type result_range; |
| 68 | + |
| 69 | + std::string valid_http_version = "HTTP/1.0 "; |
| 70 | + fusion::tie(parsed_ok, result_range) = p.parse_until( |
| 71 | + response_parser<tags::default_string>::http_version_done, |
| 72 | + valid_http_version); |
| 73 | + BOOST_CHECK_EQUAL(parsed_ok, true); |
| 74 | + BOOST_CHECK(!boost::empty(result_range)); |
| 75 | + std::string parsed(boost::begin(result_range), boost::end(result_range)); |
| 76 | + std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; |
| 77 | + p.reset(); |
| 78 | + valid_http_version = "HTTP/1.1 "; |
| 79 | + fusion::tie(parsed_ok, result_range) = p.parse_until( |
| 80 | + response_parser<tags::default_string>::http_version_done, |
| 81 | + valid_http_version); |
| 82 | + BOOST_CHECK_EQUAL(parsed_ok, true); |
| 83 | + BOOST_CHECK(!boost::empty(result_range)); |
| 84 | + parsed = std::string(boost::begin(result_range), boost::end(result_range)); |
| 85 | + std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl; |
| 86 | +} |
0 commit comments