|
| 1 | +// Copyright (C) 2013 by Glyn Matthews |
| 2 | +// Copyright 2010 Dean Michael Berris. |
| 3 | +// Copyright 2012 Google, Inc. |
| 4 | +// Distributed under the Boost Software License, Version 1.0. |
| 5 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 7 | + |
| 8 | +#include <gtest/gtest.h> |
| 9 | +#include <network/protocol/http/server/request_parser.hpp> |
| 10 | +#include <network/tags.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 the HTTP Request Incremental Parser |
| 19 | + * -------------------------------------------- |
| 20 | + * |
| 21 | + * In this test we fully intend to specify how an incremental HTTP request |
| 22 | + * parser should be used. This follows the HTTP Response Incremental Parser |
| 23 | + * example, and models the Incremental Parser Concept. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace tags = boost::network::tags; |
| 28 | +namespace logic = boost::logic; |
| 29 | +namespace fusion = boost::fusion; |
| 30 | +using namespace boost::network::http; |
| 31 | + |
| 32 | +TEST(request_test, incremental_parser_constructor) { |
| 33 | + request_parser<tags::default_string> p; // default constructible |
| 34 | +} |
| 35 | + |
| 36 | +TEST(request_test, incremental_parser_parse_http_method) { |
| 37 | + request_parser<tags::default_string> p; |
| 38 | + logic::tribool parsed_ok = false; |
| 39 | + typedef request_parser<tags::default_string> request_parser_type; |
| 40 | + typedef boost::iterator_range<std::string::const_iterator> range_type; |
| 41 | + range_type result_range; |
| 42 | + |
| 43 | + std::string valid_http_method = "GET "; |
| 44 | + fusion::tie(parsed_ok, result_range) = |
| 45 | + p.parse_until(request_parser_type::method_done, valid_http_method); |
| 46 | + ASSERT_EQ(parsed_ok, true); |
| 47 | + ASSERT_TRUE(!boost::empty(result_range)); |
| 48 | + std::string parsed(boost::begin(result_range), boost::end(result_range)); |
| 49 | + std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " |
| 50 | + << std::endl; |
| 51 | + |
| 52 | + std::string invalid_http_method = "get "; |
| 53 | + p.reset(); |
| 54 | + fusion::tie(parsed_ok, result_range) = |
| 55 | + p.parse_until(request_parser_type::method_done, invalid_http_method); |
| 56 | + ASSERT_EQ(parsed_ok, false); |
| 57 | + parsed.assign(boost::begin(result_range), boost::end(result_range)); |
| 58 | + std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " |
| 59 | + << std::endl; |
| 60 | +} |
| 61 | + |
| 62 | +TEST(request_test, incremental_parser_parse_http_uri) { |
| 63 | + request_parser<tags::default_string> p; |
| 64 | + logic::tribool parsed_ok = false; |
| 65 | + typedef request_parser<tags::default_string> request_parser_type; |
| 66 | + typedef boost::iterator_range<std::string::const_iterator> range_type; |
| 67 | + range_type result_range; |
| 68 | + |
| 69 | + std::string valid_http_request = "GET / HTTP/1.1\r\n"; |
| 70 | + fusion::tie(parsed_ok, result_range) = |
| 71 | + p.parse_until(request_parser_type::uri_done, valid_http_request); |
| 72 | + ASSERT_EQ(parsed_ok, true); |
| 73 | + ASSERT_TRUE(!boost::empty(result_range)); |
| 74 | + std::string parsed(boost::begin(result_range), boost::end(result_range)); |
| 75 | + std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " |
| 76 | + << std::endl; |
| 77 | + |
| 78 | + std::string invalid_http_request = "GET /\t HTTP/1.1\r\n"; |
| 79 | + p.reset(); |
| 80 | + fusion::tie(parsed_ok, result_range) = |
| 81 | + p.parse_until(request_parser_type::uri_done, invalid_http_request); |
| 82 | + ASSERT_EQ(parsed_ok, false); |
| 83 | + parsed.assign(boost::begin(result_range), boost::end(result_range)); |
| 84 | + std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " |
| 85 | + << std::endl; |
| 86 | +} |
| 87 | + |
| 88 | +TEST(request_test, incremental_parser_parse_http_version) { |
| 89 | + request_parser<tags::default_string> p; |
| 90 | + logic::tribool parsed_ok = false; |
| 91 | + typedef request_parser<tags::default_string> request_parser_type; |
| 92 | + typedef boost::iterator_range<std::string::const_iterator> range_type; |
| 93 | + range_type result_range; |
| 94 | + |
| 95 | + std::string valid_http_request = "GET / HTTP/1.1\r\n"; |
| 96 | + fusion::tie(parsed_ok, result_range) = |
| 97 | + p.parse_until(request_parser_type::version_done, valid_http_request); |
| 98 | + ASSERT_EQ(parsed_ok, true); |
| 99 | + ASSERT_TRUE(!boost::empty(result_range)); |
| 100 | + std::string parsed(boost::begin(result_range), boost::end(result_range)); |
| 101 | + std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " |
| 102 | + << std::endl; |
| 103 | + |
| 104 | + std::string invalid_http_request = "GET / HTTP 1.1\r\n"; |
| 105 | + p.reset(); |
| 106 | + fusion::tie(parsed_ok, result_range) = |
| 107 | + p.parse_until(request_parser_type::version_done, invalid_http_request); |
| 108 | + ASSERT_EQ(parsed_ok, false); |
| 109 | + parsed.assign(boost::begin(result_range), boost::end(result_range)); |
| 110 | + std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " |
| 111 | + << std::endl; |
| 112 | +} |
| 113 | + |
| 114 | +TEST(request_test, incremental_parser_parse_http_headers) { |
| 115 | + request_parser<tags::default_string> p; |
| 116 | + logic::tribool parsed_ok = false; |
| 117 | + typedef request_parser<tags::default_string> request_parser_type; |
| 118 | + typedef boost::iterator_range<std::string::const_iterator> range_type; |
| 119 | + range_type result_range; |
| 120 | + |
| 121 | + std::string valid_http_request = |
| 122 | + "GET / HTTP/1.1\r\nHost: cpp-netlib.org\r\n\r\n"; |
| 123 | + fusion::tie(parsed_ok, result_range) = |
| 124 | + p.parse_until(request_parser_type::headers_done, valid_http_request); |
| 125 | + ASSERT_EQ(parsed_ok, true); |
| 126 | + ASSERT_TRUE(!boost::empty(result_range)); |
| 127 | + std::string parsed(boost::begin(result_range), boost::end(result_range)); |
| 128 | + std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " |
| 129 | + << std::endl; |
| 130 | + |
| 131 | + valid_http_request = |
| 132 | + "GET / HTTP/1.1\r\nHost: cpp-netlib.org\r\nConnection: close\r\n\r\n"; |
| 133 | + p.reset(); |
| 134 | + fusion::tie(parsed_ok, result_range) = |
| 135 | + p.parse_until(request_parser_type::headers_done, valid_http_request); |
| 136 | + ASSERT_EQ(parsed_ok, true); |
| 137 | + ASSERT_TRUE(!boost::empty(result_range)); |
| 138 | + parsed.assign(boost::begin(result_range), boost::end(result_range)); |
| 139 | + std::cout << "PARSED: " << parsed << " [state:" << p.state() << "] " |
| 140 | + << std::endl; |
| 141 | +} |
| 142 | + |
0 commit comments