Browse Source

Use StatusCode in tests and examples (#1743)

* Use StatusCode in tests and examples

* Use StatusCode in README
Ilya Andreev 2 years ago
parent
commit
5b943d9bb8
4 changed files with 149 additions and 146 deletions
  1. 5 5
      README.md
  2. 1 1
      example/benchmark.cc
  3. 128 125
      test/test.cc
  4. 15 15
      test/test_proxy.cc

+ 5 - 5
README.md

@@ -235,7 +235,7 @@ svr.set_exception_handler([](const auto& req, auto& res, std::exception_ptr ep)
     snprintf(buf, sizeof(buf), fmt, "Unknown Exception");
     snprintf(buf, sizeof(buf), fmt, "Unknown Exception");
   }
   }
   res.set_content(buf, "text/html");
   res.set_content(buf, "text/html");
-  res.status = 500;
+  res.status = StatusCode::InternalServerError_500;
 });
 });
 ```
 ```
 
 
@@ -385,14 +385,14 @@ By default, the server sends a `100 Continue` response for an `Expect: 100-conti
 ```cpp
 ```cpp
 // Send a '417 Expectation Failed' response.
 // Send a '417 Expectation Failed' response.
 svr.set_expect_100_continue_handler([](const Request &req, Response &res) {
 svr.set_expect_100_continue_handler([](const Request &req, Response &res) {
-  return 417;
+  return StatusCode::ExpectationFailed_417;
 });
 });
 ```
 ```
 
 
 ```cpp
 ```cpp
 // Send a final status without reading the message body.
 // Send a final status without reading the message body.
 svr.set_expect_100_continue_handler([](const Request &req, Response &res) {
 svr.set_expect_100_continue_handler([](const Request &req, Response &res) {
-  return res.status = 401;
+  return res.status = StatusCode::Unauthorized_401;
 });
 });
 ```
 ```
 
 
@@ -473,7 +473,7 @@ int main(void)
   httplib::Client cli("localhost", 1234);
   httplib::Client cli("localhost", 1234);
 
 
   if (auto res = cli.Get("/hi")) {
   if (auto res = cli.Get("/hi")) {
-    if (res->status == 200) {
+    if (res->status == StatusCode::OK_200) {
       std::cout << res->body << std::endl;
       std::cout << res->body << std::endl;
     }
     }
   } else {
   } else {
@@ -623,7 +623,7 @@ std::string body;
 auto res = cli.Get(
 auto res = cli.Get(
   "/stream", Headers(),
   "/stream", Headers(),
   [&](const Response &response) {
   [&](const Response &response) {
-    EXPECT_EQ(200, response.status);
+    EXPECT_EQ(StatusCode::OK_200, response.status);
     return true; // return 'false' if you want to cancel the request.
     return true; // return 'false' if you want to cancel the request.
   },
   },
   [&](const char *data, size_t data_length) {
   [&](const char *data, size_t data_length) {

+ 1 - 1
example/benchmark.cc

@@ -26,7 +26,7 @@ int main(void) {
   for (int i = 0; i < 3; i++) {
   for (int i = 0; i < 3; i++) {
     StopWatch sw(to_string(i).c_str());
     StopWatch sw(to_string(i).c_str());
     auto res = cli.Post("/post", body, "application/octet-stream");
     auto res = cli.Post("/post", body, "application/octet-stream");
-    assert(res->status == 200);
+    assert(res->status == httplib::StatusCode::OK_200);
   }
   }
 
 
   return 0;
   return 0;

File diff suppressed because it is too large
+ 128 - 125
test/test.cc


+ 15 - 15
test/test_proxy.cc

@@ -10,7 +10,7 @@ void ProxyTest(T& cli, bool basic) {
   cli.set_proxy("localhost", basic ? 3128 : 3129);
   cli.set_proxy("localhost", basic ? 3128 : 3129);
   auto res = cli.Get("/httpbin/get");
   auto res = cli.Get("/httpbin/get");
   ASSERT_TRUE(res != nullptr);
   ASSERT_TRUE(res != nullptr);
-  EXPECT_EQ(407, res->status);
+  EXPECT_EQ(StatusCode::ProxyAuthenticationRequired_407, res->status);
 }
 }
 
 
 TEST(ProxyTest, NoSSLBasic) {
 TEST(ProxyTest, NoSSLBasic) {
@@ -51,7 +51,7 @@ void RedirectProxyText(T& cli, const char *path, bool basic) {
 
 
   auto res = cli.Get(path);
   auto res = cli.Get(path);
   ASSERT_TRUE(res != nullptr);
   ASSERT_TRUE(res != nullptr);
-  EXPECT_EQ(200, res->status);
+  EXPECT_EQ(StatusCode::OK_200, res->status);
 }
 }
 
 
 TEST(RedirectTest, HTTPBinNoSSLBasic) {
 TEST(RedirectTest, HTTPBinNoSSLBasic) {
@@ -108,7 +108,7 @@ void BaseAuthTestFromHTTPWatch(T& cli) {
   {
   {
     auto res = cli.Get("/basic-auth/hello/world");
     auto res = cli.Get("/basic-auth/hello/world");
     ASSERT_TRUE(res != nullptr);
     ASSERT_TRUE(res != nullptr);
-    EXPECT_EQ(401, res->status);
+    EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
   }
   }
 
 
   {
   {
@@ -117,7 +117,7 @@ void BaseAuthTestFromHTTPWatch(T& cli) {
                 {make_basic_authentication_header("hello", "world")});
                 {make_basic_authentication_header("hello", "world")});
     ASSERT_TRUE(res != nullptr);
     ASSERT_TRUE(res != nullptr);
     EXPECT_EQ("{\n  \"authenticated\": true, \n  \"user\": \"hello\"\n}\n", res->body);
     EXPECT_EQ("{\n  \"authenticated\": true, \n  \"user\": \"hello\"\n}\n", res->body);
-    EXPECT_EQ(200, res->status);
+    EXPECT_EQ(StatusCode::OK_200, res->status);
   }
   }
 
 
   {
   {
@@ -125,21 +125,21 @@ void BaseAuthTestFromHTTPWatch(T& cli) {
     auto res = cli.Get("/basic-auth/hello/world");
     auto res = cli.Get("/basic-auth/hello/world");
     ASSERT_TRUE(res != nullptr);
     ASSERT_TRUE(res != nullptr);
     EXPECT_EQ("{\n  \"authenticated\": true, \n  \"user\": \"hello\"\n}\n", res->body);
     EXPECT_EQ("{\n  \"authenticated\": true, \n  \"user\": \"hello\"\n}\n", res->body);
-    EXPECT_EQ(200, res->status);
+    EXPECT_EQ(StatusCode::OK_200, res->status);
   }
   }
 
 
   {
   {
     cli.set_basic_auth("hello", "bad");
     cli.set_basic_auth("hello", "bad");
     auto res = cli.Get("/basic-auth/hello/world");
     auto res = cli.Get("/basic-auth/hello/world");
     ASSERT_TRUE(res != nullptr);
     ASSERT_TRUE(res != nullptr);
-    EXPECT_EQ(401, res->status);
+    EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
   }
   }
 
 
   {
   {
     cli.set_basic_auth("bad", "world");
     cli.set_basic_auth("bad", "world");
     auto res = cli.Get("/basic-auth/hello/world");
     auto res = cli.Get("/basic-auth/hello/world");
     ASSERT_TRUE(res != nullptr);
     ASSERT_TRUE(res != nullptr);
-    EXPECT_EQ(401, res->status);
+    EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
   }
   }
 }
 }
 
 
@@ -166,7 +166,7 @@ void DigestAuthTestFromHTTPWatch(T& cli) {
   {
   {
     auto res = cli.Get("/digest-auth/auth/hello/world");
     auto res = cli.Get("/digest-auth/auth/hello/world");
     ASSERT_TRUE(res != nullptr);
     ASSERT_TRUE(res != nullptr);
-    EXPECT_EQ(401, res->status);
+    EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
   }
   }
 
 
   {
   {
@@ -182,14 +182,14 @@ void DigestAuthTestFromHTTPWatch(T& cli) {
       auto res = cli.Get(path.c_str());
       auto res = cli.Get(path.c_str());
       ASSERT_TRUE(res != nullptr);
       ASSERT_TRUE(res != nullptr);
       EXPECT_EQ("{\n  \"authenticated\": true, \n  \"user\": \"hello\"\n}\n", res->body);
       EXPECT_EQ("{\n  \"authenticated\": true, \n  \"user\": \"hello\"\n}\n", res->body);
-      EXPECT_EQ(200, res->status);
+      EXPECT_EQ(StatusCode::OK_200, res->status);
     }
     }
 
 
     cli.set_digest_auth("hello", "bad");
     cli.set_digest_auth("hello", "bad");
     for (auto path : paths) {
     for (auto path : paths) {
       auto res = cli.Get(path.c_str());
       auto res = cli.Get(path.c_str());
       ASSERT_TRUE(res != nullptr);
       ASSERT_TRUE(res != nullptr);
-      EXPECT_EQ(401, res->status);
+      EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
     }
     }
 
 
     // NOTE: Until httpbin.org fixes issue #46, the following test is commented
     // NOTE: Until httpbin.org fixes issue #46, the following test is commented
@@ -198,7 +198,7 @@ void DigestAuthTestFromHTTPWatch(T& cli) {
     // for (auto path : paths) {
     // for (auto path : paths) {
     //   auto res = cli.Get(path.c_str());
     //   auto res = cli.Get(path.c_str());
     //   ASSERT_TRUE(res != nullptr);
     //   ASSERT_TRUE(res != nullptr);
-    //   EXPECT_EQ(401, res->status);
+    //   EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
     // }
     // }
   }
   }
 }
 }
@@ -234,11 +234,11 @@ void KeepAliveTest(T& cli, bool basic) {
 
 
   {
   {
     auto res = cli.Get("/httpbin/get");
     auto res = cli.Get("/httpbin/get");
-    EXPECT_EQ(200, res->status);
+    EXPECT_EQ(StatusCode::OK_200, res->status);
   }
   }
   {
   {
     auto res = cli.Get("/httpbin/redirect/2");
     auto res = cli.Get("/httpbin/redirect/2");
-    EXPECT_EQ(200, res->status);
+    EXPECT_EQ(StatusCode::OK_200, res->status);
   }
   }
 
 
   {
   {
@@ -252,7 +252,7 @@ void KeepAliveTest(T& cli, bool basic) {
     for (auto path: paths) {
     for (auto path: paths) {
       auto res = cli.Get(path.c_str());
       auto res = cli.Get(path.c_str());
       EXPECT_EQ("{\n  \"authenticated\": true, \n  \"user\": \"hello\"\n}\n", res->body);
       EXPECT_EQ("{\n  \"authenticated\": true, \n  \"user\": \"hello\"\n}\n", res->body);
-      EXPECT_EQ(200, res->status);
+      EXPECT_EQ(StatusCode::OK_200, res->status);
     }
     }
   }
   }
 
 
@@ -260,7 +260,7 @@ void KeepAliveTest(T& cli, bool basic) {
     int count = 10;
     int count = 10;
     while (count--) {
     while (count--) {
       auto res = cli.Get("/httpbin/get");
       auto res = cli.Get("/httpbin/get");
-      EXPECT_EQ(200, res->status);
+      EXPECT_EQ(StatusCode::OK_200, res->status);
     }
     }
   }
   }
 }
 }

Some files were not shown because too many files changed in this diff