Use StatusCode in tests and examples (#1743)

* Use StatusCode in tests and examples

* Use StatusCode in README
This commit is contained in:
Ilya Andreev 2023-12-21 01:28:57 +03:00 committed by GitHub
parent c86f69a105
commit 5b943d9bb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 297 additions and 292 deletions

View file

@ -235,7 +235,7 @@ svr.set_exception_handler([](const auto& req, auto& res, std::exception_ptr ep)
snprintf(buf, sizeof(buf), fmt, "Unknown Exception");
}
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
// Send a '417 Expectation Failed' response.
svr.set_expect_100_continue_handler([](const Request &req, Response &res) {
return 417;
return StatusCode::ExpectationFailed_417;
});
```
```cpp
// Send a final status without reading the message body.
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);
if (auto res = cli.Get("/hi")) {
if (res->status == 200) {
if (res->status == StatusCode::OK_200) {
std::cout << res->body << std::endl;
}
} else {
@ -623,7 +623,7 @@ std::string body;
auto res = cli.Get(
"/stream", Headers(),
[&](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.
},
[&](const char *data, size_t data_length) {

View file

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

File diff suppressed because it is too large Load diff

View file

@ -10,7 +10,7 @@ void ProxyTest(T& cli, bool basic) {
cli.set_proxy("localhost", basic ? 3128 : 3129);
auto res = cli.Get("/httpbin/get");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(407, res->status);
EXPECT_EQ(StatusCode::ProxyAuthenticationRequired_407, res->status);
}
TEST(ProxyTest, NoSSLBasic) {
@ -51,7 +51,7 @@ void RedirectProxyText(T& cli, const char *path, bool basic) {
auto res = cli.Get(path);
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
EXPECT_EQ(StatusCode::OK_200, res->status);
}
TEST(RedirectTest, HTTPBinNoSSLBasic) {
@ -108,7 +108,7 @@ void BaseAuthTestFromHTTPWatch(T& cli) {
{
auto res = cli.Get("/basic-auth/hello/world");
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")});
ASSERT_TRUE(res != nullptr);
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");
ASSERT_TRUE(res != nullptr);
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");
auto res = cli.Get("/basic-auth/hello/world");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(401, res->status);
EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
}
{
cli.set_basic_auth("bad", "world");
auto res = cli.Get("/basic-auth/hello/world");
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");
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());
ASSERT_TRUE(res != nullptr);
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");
for (auto path : paths) {
auto res = cli.Get(path.c_str());
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
@ -198,7 +198,7 @@ void DigestAuthTestFromHTTPWatch(T& cli) {
// for (auto path : paths) {
// auto res = cli.Get(path.c_str());
// 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");
EXPECT_EQ(200, res->status);
EXPECT_EQ(StatusCode::OK_200, res->status);
}
{
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) {
auto res = cli.Get(path.c_str());
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;
while (count--) {
auto res = cli.Get("/httpbin/get");
EXPECT_EQ(200, res->status);
EXPECT_EQ(StatusCode::OK_200, res->status);
}
}
}