Browse Source

Avoid hardcoded ports in RedirectToDifferentPort.Redirect test (#1012)

The RedirectToDifferentPort.Redirect test assumes that port 8080 and
8081 are available on localhost. They aren’t on my system so the test
fails. Improve this by binding to available ports instead of hardcoded
ones.
Joel Rosdahl 4 years ago
parent
commit
1b3b098329
1 changed files with 26 additions and 20 deletions
  1. 26 20
      test/test.cc

+ 26 - 20
test/test.cc

@@ -852,42 +852,49 @@ TEST(UrlWithSpace, Redirect) {
 #endif
 
 TEST(RedirectToDifferentPort, Redirect) {
-  Server svr8080;
-  Server svr8081;
-
-  svr8080.Get("/1", [&](const Request & /*req*/, Response &res) {
-    res.set_redirect("http://localhost:8081/2");
+  Server svr1;
+  svr1.Get("/1", [&](const Request & /*req*/, Response &res) {
+    res.set_content("Hello World!", "text/plain");
   });
 
-  svr8081.Get("/2", [&](const Request & /*req*/, Response &res) {
-    res.set_content("Hello World!", "text/plain");
+  int svr1_port = 0;
+  auto thread1 = std::thread([&]() {
+    svr1_port = svr1.bind_to_any_port("localhost");
+    svr1.listen_after_bind();
   });
 
-  auto thread8080 = std::thread([&]() { svr8080.listen("localhost", 8080); });
+  Server svr2;
+  svr2.Get("/2", [&](const Request & /*req*/, Response &res) {
+    res.set_redirect("http://localhost:" + std::to_string(svr1_port) + "/1");
+  });
 
-  auto thread8081 = std::thread([&]() { svr8081.listen("localhost", 8081); });
+  int svr2_port = 0;
+  auto thread2 = std::thread([&]() {
+    svr2_port = svr2.bind_to_any_port("localhost");
+    svr2.listen_after_bind();
+  });
 
-  while (!svr8080.is_running() || !svr8081.is_running()) {
+  while (!svr1.is_running() || !svr2.is_running()) {
     std::this_thread::sleep_for(std::chrono::milliseconds(1));
   }
 
   // Give GET time to get a few messages.
   std::this_thread::sleep_for(std::chrono::seconds(1));
 
-  Client cli("localhost", 8080);
+  Client cli("localhost", svr2_port);
   cli.set_follow_location(true);
 
-  auto res = cli.Get("/1");
+  auto res = cli.Get("/2");
   ASSERT_TRUE(res);
   EXPECT_EQ(200, res->status);
   EXPECT_EQ("Hello World!", res->body);
 
-  svr8080.stop();
-  svr8081.stop();
-  thread8080.join();
-  thread8081.join();
-  ASSERT_FALSE(svr8080.is_running());
-  ASSERT_FALSE(svr8081.is_running());
+  svr1.stop();
+  svr2.stop();
+  thread1.join();
+  thread2.join();
+  ASSERT_FALSE(svr1.is_running());
+  ASSERT_FALSE(svr2.is_running());
 }
 
 TEST(RedirectFromPageWithContent, Redirect) {
@@ -3769,7 +3776,7 @@ TEST(GetWithParametersTest, GetWithParameters) {
     EXPECT_EQ("world3", req.get_param_value("hello3"));
   });
 
-  svr.Get(R"(/resources/([a-z0-9\\-]+))", [&](const Request& req, Response&) {
+  svr.Get(R"(/resources/([a-z0-9\\-]+))", [&](const Request &req, Response &) {
     EXPECT_EQ("resource-id", req.matches[1]);
     EXPECT_EQ("foo", req.get_param_value("param1"));
     EXPECT_EQ("bar", req.get_param_value("param2"));
@@ -4520,7 +4527,6 @@ TEST(HttpToHttpsRedirectTest, CertFile) {
     ssl_svr.stop();
   });
 
-
   thread t = thread([&]() { ASSERT_TRUE(svr.listen("127.0.0.1", PORT)); });
   thread t2 = thread([&]() { ASSERT_TRUE(ssl_svr.listen("127.0.0.1", 1235)); });
   std::this_thread::sleep_for(std::chrono::milliseconds(1));