|
10 | 10 | // Test that header file is self-contained. |
11 | 11 | #include <boost/http/server/http_worker.hpp> |
12 | 12 |
|
13 | | -#include "test_helpers.hpp" |
| 13 | +#include "test_worker.hpp" |
14 | 14 |
|
15 | 15 | namespace boost { |
16 | 16 | namespace http { |
17 | 17 |
|
18 | 18 | struct http_worker_test |
| 19 | + : test_worker |
19 | 20 | { |
| 21 | + void |
| 22 | + testBasicRoute() |
| 23 | + { |
| 24 | + // Route returns 200 with body |
| 25 | + check(GET, "/hello", |
| 26 | + h_text("world"), |
| 27 | + status::ok, "world"); |
| 28 | + |
| 29 | + // Unmatched path returns 404 |
| 30 | + { |
| 31 | + test_router r; |
| 32 | + r.add(GET, "/hello", h_text("x")); |
| 33 | + check(r, GET, "/missing", |
| 34 | + status::not_found); |
| 35 | + } |
| 36 | + |
| 37 | + // Wrong method returns 405 |
| 38 | + { |
| 39 | + test_router r; |
| 40 | + r.add(GET, "/hello", h_text("x")); |
| 41 | + check(r, POST, "/hello", |
| 42 | + status::method_not_allowed); |
| 43 | + } |
| 44 | + |
| 45 | + // Custom status code |
| 46 | + check(GET, "/nc", |
| 47 | + h_stat(status::no_content), |
| 48 | + status::no_content); |
| 49 | + |
| 50 | + // Multiple routes, correct dispatch |
| 51 | + { |
| 52 | + test_router r; |
| 53 | + r.add(GET, "/a", h_text("A")); |
| 54 | + r.add(GET, "/b", h_text("B")); |
| 55 | + check(r, GET, "/b", |
| 56 | + status::ok, "B"); |
| 57 | + } |
| 58 | + |
| 59 | + // HTML body sniffs content-type |
| 60 | + { |
| 61 | + test_router r; |
| 62 | + r.add(GET, "/page", |
| 63 | + h_text("<h1>hi</h1>")); |
| 64 | + auto res = exchange(r, GET, "/page"); |
| 65 | + BOOST_TEST(res.status() == status::ok); |
| 66 | + BOOST_TEST(res.body == "<h1>hi</h1>"); |
| 67 | + BOOST_TEST(res.res.exists( |
| 68 | + field::content_type)); |
| 69 | + } |
| 70 | + |
| 71 | + // Custom content-type |
| 72 | + check(GET, "/json", |
| 73 | + h_typed("application/json", |
| 74 | + R"({"ok":true})"), |
| 75 | + status::ok, R"({"ok":true})"); |
| 76 | + } |
| 77 | + |
20 | 78 | void |
21 | 79 | run() |
22 | 80 | { |
| 81 | + testBasicRoute(); |
23 | 82 | } |
24 | 83 | }; |
25 | 84 |
|
|
0 commit comments