JayBeams  0.1
Another project to have fun coding.
ut_request_dispatcher.cpp
Go to the documentation of this file.
2 #include <jb/strtonum.hpp>
3 
4 #include <boost/test/unit_test.hpp>
5 
6 /**
7  * @test Verify that jb::ehs::request_dispatcher works as expected.
8  */
9 BOOST_AUTO_TEST_CASE(request_dispatcher_base) {
10  using namespace jb::ehs;
11  request_dispatcher tested("test");
12  request_type req;
13  req.method(beast::http::verb::get);
14  req.target("/");
15  req.version = 11;
16  req.insert("host", "example.com:80");
17  req.insert("user-agent", "unit test");
18 
19  response_type res = tested.process(req);
20  BOOST_CHECK_EQUAL(res.result_int(), (int)beast::http::status::not_found);
21  BOOST_CHECK_EQUAL(res.version, 11);
22  BOOST_CHECK_EQUAL(res["server"], "test");
23 
24  tested.add_handler("/", [](request_type const& req, response_type& res) {
25  res.insert("content-type", "text/plain");
26  res.body = "OK\r\n";
27  });
28  res = tested.process(req);
29  BOOST_CHECK_EQUAL(res.result_int(), (int)beast::http::status::ok);
30  BOOST_CHECK_EQUAL(res.version, 11);
31  BOOST_CHECK_EQUAL(res["server"], "test");
32  BOOST_CHECK_EQUAL(res.body, "OK\r\n");
33 
34  req.target("/not-there");
35  res = tested.process(req);
36  BOOST_CHECK_EQUAL(res.result_int(), (int)beast::http::status::not_found);
37  BOOST_CHECK_EQUAL(res.version, 11);
38  BOOST_CHECK_EQUAL(res["server"], "test");
39 
40  tested.add_handler(
41  "/not-there", [](request_type const& req, response_type& res) {
42  res.insert("content-type", "text/plain");
43  res.body = "Fine I guess\r\n";
44  });
45 
46  res = tested.process(req);
47  BOOST_CHECK_EQUAL(res.result_int(), (int)beast::http::status::ok);
48  BOOST_CHECK_EQUAL(res.version, 11);
49  BOOST_CHECK_EQUAL(res["server"], "test");
50  BOOST_CHECK_EQUAL(res.body, "Fine I guess\r\n");
51 }
52 
53 /**
54  * @test Verify that jb::ehs::request_dispatcher works as expected for errors.
55  */
56 BOOST_AUTO_TEST_CASE(request_dispatcher_error) {
57  using namespace jb::ehs;
58  request_dispatcher tested("test");
59  auto thrower = [](request_type const& req, response_type& res) {
60  throw std::runtime_error("bad stuff happens");
61  };
62  tested.add_handler("/error", thrower);
63 
64  request_type req;
65  req.method(beast::http::verb::get);
66  req.target("/error");
67  req.version = 11;
68  req.insert("host", "example.com:80");
69  req.insert("user-agent", "unit test");
70 
71  response_type res = tested.process(req);
72  BOOST_CHECK_EQUAL(
73  res.result_int(), (int)beast::http::status::internal_server_error);
74  BOOST_CHECK_EQUAL(res.version, 11);
75  BOOST_CHECK_EQUAL(res["server"], "test");
76 
77  BOOST_CHECK_THROW(tested.add_handler("/error", thrower), std::runtime_error);
78  BOOST_CHECK_EQUAL(tested.get_write_500(), 1);
79 }
80 
81 /**
82  * @test Verify that jb::ehs::request_dispatcher counters work as expected.
83  */
84 BOOST_AUTO_TEST_CASE(request_dispatcher_counter) {
85  using namespace jb::ehs;
86  request_dispatcher tested("test");
87  tested.add_handler("/path", [](request_type const& req, response_type& res) {
88  if (req.count("x-return-status") > 0) {
89  std::string val(req["x-return-status"]);
90  int r;
91  (void)jb::strtonum(val, r);
92  res.result(r);
93  } else {
94  BOOST_CHECK_MESSAGE(false, "x-return-status not set");
95  }
96  res.insert("content-type", "text/plain");
97  res.body = "OK\r\n";
98  });
99 
100  request_type req;
101  req.method(beast::http::verb::get);
102  req.target("/path");
103  req.version = 11;
104  req.insert("host", "example.com:80");
105  req.insert("user-agent", "unit test");
106 
107  req.insert("x-return-status", "200");
108  response_type res = tested.process(req);
109  BOOST_CHECK_EQUAL(res.result_int(), 200);
110  BOOST_CHECK_EQUAL(tested.get_write_200(), 1);
111 
112  req.set("x-return-status", "100");
113  res = tested.process(req);
114  BOOST_CHECK_EQUAL(res.result_int(), 100);
115  BOOST_CHECK_EQUAL(tested.get_write_100(), 1);
116 
117  req.set("x-return-status", "204");
118  res = tested.process(req);
119  BOOST_CHECK_EQUAL(res.result_int(), 204);
120  BOOST_CHECK_EQUAL(tested.get_write_200(), 2);
121 
122  req.set("x-return-status", "300");
123  res = tested.process(req);
124  BOOST_CHECK_EQUAL(res.result_int(), 300);
125  BOOST_CHECK_EQUAL(tested.get_write_300(), 1);
126 
127  req.set("x-return-status", "400");
128  res = tested.process(req);
129  BOOST_CHECK_EQUAL(res.result_int(), 400);
130  BOOST_CHECK_EQUAL(tested.get_write_400(), 1);
131 
132  req.set("x-return-status", "500");
133  res = tested.process(req);
134  BOOST_CHECK_EQUAL(res.result_int(), 500);
135  BOOST_CHECK_EQUAL(tested.get_write_500(), 1);
136 
137  req.set("x-return-status", "600");
138  res = tested.process(req);
139  BOOST_CHECK_EQUAL(res.result_int(), 600);
140  BOOST_CHECK_EQUAL(tested.get_write_invalid(), 1);
141 
142  // ... verify that the counters were only updated for the right
143  // event ...
144  BOOST_CHECK_EQUAL(tested.get_write_100(), 1);
145  BOOST_CHECK_EQUAL(tested.get_write_200(), 2);
146  BOOST_CHECK_EQUAL(tested.get_write_300(), 1);
147  BOOST_CHECK_EQUAL(tested.get_write_400(), 1);
148  BOOST_CHECK_EQUAL(tested.get_write_500(), 1);
149 }
150 
151 /**
152  * @test Verify that jb::ehs::request_dispatcher counters work as expected.
153  */
154 BOOST_AUTO_TEST_CASE(request_dispatcher_network_counter) {
155  using namespace jb::ehs;
156  request_dispatcher tested("test");
157  BOOST_CHECK_EQUAL(tested.get_accept_error(), 0);
158  tested.count_accept_error();
159  BOOST_CHECK_EQUAL(tested.get_accept_error(), 1);
160 
161  BOOST_CHECK_EQUAL(tested.get_accept_ok(), 0);
162  tested.count_accept_ok();
163  BOOST_CHECK_EQUAL(tested.get_accept_ok(), 1);
164 
165  BOOST_CHECK_EQUAL(tested.get_write_error(), 0);
166  tested.count_write_error();
167  BOOST_CHECK_EQUAL(tested.get_write_error(), 1);
168 
169  BOOST_CHECK_EQUAL(tested.get_write_ok(), 0);
170  tested.count_write_ok();
171  BOOST_CHECK_EQUAL(tested.get_write_ok(), 1);
172 
173  BOOST_CHECK_EQUAL(tested.get_read_error(), 0);
174  tested.count_read_error();
175  BOOST_CHECK_EQUAL(tested.get_read_error(), 1);
176 
177  BOOST_CHECK_EQUAL(tested.get_read_ok(), 0);
178  tested.count_read_ok();
179  BOOST_CHECK_EQUAL(tested.get_read_ok(), 1);
180 
181  BOOST_CHECK_EQUAL(tested.get_open_connection(), 0);
182  tested.count_open_connection();
183  BOOST_CHECK_EQUAL(tested.get_open_connection(), 1);
184 
185  BOOST_CHECK_EQUAL(tested.get_close_connection(), 0);
186  tested.count_close_connection();
187  BOOST_CHECK_EQUAL(tested.get_close_connection(), 1);
188 
189  // ... verify that no counters get accidentally updated by other
190  // calls ...
191  BOOST_CHECK_EQUAL(tested.get_open_connection(), 1);
192  BOOST_CHECK_EQUAL(tested.get_close_connection(), 1);
193  BOOST_CHECK_EQUAL(tested.get_read_ok(), 1);
194  BOOST_CHECK_EQUAL(tested.get_read_error(), 1);
195  BOOST_CHECK_EQUAL(tested.get_write_ok(), 1);
196  BOOST_CHECK_EQUAL(tested.get_write_error(), 1);
197  BOOST_CHECK_EQUAL(tested.get_accept_ok(), 1);
198  BOOST_CHECK_EQUAL(tested.get_accept_error(), 1);
199 }
200 
201 /**
202  * @test Verify that jb::ehs::request_dispatcher::append_metrics works
203  * as expected
204  */
205 BOOST_AUTO_TEST_CASE(request_dispatcher_append_metrics) {
206  using namespace jb::ehs;
207  request_dispatcher tested("test");
208 
209  response_type res;
210  tested.append_metrics(res);
211  BOOST_CHECK_NE(res.body, "");
212 }
long get_open_connection() const
Returns the count of open connections.
beast::http::request< beast::http::string_body > request_type
The request type used for JayBeams Embedded HTTP Servers.
Definition: base_types.hpp:17
void count_write_error()
Count a write errors.
void count_accept_ok()
Count accept successes.
void count_open_connection()
Event counters.
long get_read_ok() const
Get the count of successful reads.
long get_close_connection() const
Get the count of close connections.
void append_metrics(response_type &res) const
Append the internal metrics to the body of res.
void count_write_ok()
Count a write successes.
Holds a collection of HTTP request handlers and forwards requests.
long get_write_400() const
Get the count write in the 400 range.
beast::http::response< beast::http::string_body > response_type
The response type used for JayBeams Embedded HTTP Servers.
Definition: base_types.hpp:20
void count_close_connection()
Count a new connection closed.
void count_read_error()
Count a write errors.
long get_write_invalid() const
Get count of responses with invalid codes (outside the [100,600) range).
bool strtonum(std::string const &s, T &r)
Generic string to number conversion with validation.
Definition: strtonum.hpp:40
void count_read_ok()
Count a successful read.
long get_write_100() const
Get the count write in the 100 range.
Contains types and classes to implemented Embedded HTTP Servers.
Definition: acceptor.cpp:6
long get_write_300() const
Get the count write in the 300 range.
long get_write_500() const
Get the count write in the 500 range.
long get_read_error() const
Get the count write errors.
long get_write_200() const
Get the count write in the 200 range.
long get_write_error() const
Get the count write errors.
void count_accept_error()
Count accept errors.
response_type process(request_type const &request)
Process a new request using the right handler.
long get_write_ok() const
Get the count write successes.
BOOST_AUTO_TEST_CASE(request_dispatcher_base)
void add_handler(std::string const &path, request_handler &&handler)
Add a new handler.