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