Skip to content

Commit 3ee2f01

Browse files
committed
Add test_worker
1 parent 26e2a87 commit 3ee2f01

File tree

2 files changed

+411
-1
lines changed

2 files changed

+411
-1
lines changed

test/unit/server/http_worker.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,75 @@
1010
// Test that header file is self-contained.
1111
#include <boost/http/server/http_worker.hpp>
1212

13-
#include "test_helpers.hpp"
13+
#include "test_worker.hpp"
1414

1515
namespace boost {
1616
namespace http {
1717

1818
struct http_worker_test
19+
: test_worker
1920
{
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+
2078
void
2179
run()
2280
{
81+
testBasicRoute();
2382
}
2483
};
2584

0 commit comments

Comments
 (0)