Skip to content

Commit

Permalink
Added a test case for #996
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Jul 15, 2021
1 parent 06bfa7e commit 215b813
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3706,25 +3706,59 @@ TEST(GetWithParametersTest, GetWithParameters) {
Server svr;

svr.Get("/", [&](const Request &req, Response &res) {
auto text = req.get_param_value("hello");
res.set_content(text, "text/plain");
EXPECT_EQ("world", req.get_param_value("hello"));
EXPECT_EQ("world2", req.get_param_value("hello2"));
EXPECT_EQ("world3", req.get_param_value("hello3"));
});

auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
svr.Get("/params", [&](const Request &req, Response &res) {
EXPECT_EQ("world", req.get_param_value("hello"));
EXPECT_EQ("world2", req.get_param_value("hello2"));
EXPECT_EQ("world3", req.get_param_value("hello3"));
});

svr.Get(R"(/resources/([a-z0-9\\-]+))", [&](const Request& req, Response& res) {
EXPECT_EQ("resource-id", req.matches[1]);
EXPECT_EQ("foo", req.get_param_value("param1"));
EXPECT_EQ("bar", req.get_param_value("param2"));
});

auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); });
while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
std::this_thread::sleep_for(std::chrono::seconds(1));

Client cli("localhost", PORT);
{
Client cli(HOST, PORT);

Params params;
params.emplace("hello", "world");
auto res = cli.Get("/", params, Headers{});
Params params;
params.emplace("hello", "world");
params.emplace("hello2", "world2");
params.emplace("hello3", "world3");
auto res = cli.Get("/", params, Headers{});

ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
EXPECT_EQ("world", res->body);
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
}

{
Client cli(HOST, PORT);

auto res = cli.Get("/params?hello=world&hello2=world2&hello3=world3");

ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
}

{
Client cli(HOST, PORT);

auto res = cli.Get("/resources/resource-id?param1=foo&param2=bar");

ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
}

svr.stop();
listen_thread.join();
Expand Down

0 comments on commit 215b813

Please sign in to comment.