-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathhttp_methods.cpp
More file actions
42 lines (35 loc) · 1.63 KB
/
http_methods.cpp
File metadata and controls
42 lines (35 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "expresscpp/console.hpp"
#include "expresscpp/expresscpp.hpp"
#include "expresscpp/fetch.hpp"
#include "expresscpp/http_method.hpp"
#include "expresscpp/types.hpp"
#include "gtest/gtest.h"
using namespace expresscpp;
TEST(HttpMethods, TestDiffentMethods) {
auto expresscpp = std::make_shared<ExpressCpp>();
expresscpp->Get("/", [](auto /*req*/, auto res, auto /*next*/) { res->Send("get"); });
expresscpp->Post("/", [](auto /*req*/, auto res, auto /*next*/) { res->Send("post"); });
expresscpp->Delete("/", [](auto /*req*/, auto res, auto /*next*/) { res->Send("delete"); });
expresscpp->Patch("/", [](auto /*req*/, auto res, auto /*next*/) { res->Send("patch"); });
constexpr uint16_t port = 8081u;
const std::string target = fmt::format("http://localhost:{}/", port);
expresscpp->Listen(port, [&](auto ec) {
EXPECT_FALSE(ec);
const auto s_get = fetch(target, { HttpMethod::Get});
const auto s_post = fetch(target, { HttpMethod::Post});
const auto s_delete = fetch(target, { HttpMethod::Delete});
const auto s_patch = fetch(target, { HttpMethod::Patch});
EXPECT_EQ(s_get, "get");
EXPECT_EQ(s_post, "post");
EXPECT_EQ(s_patch, "patch");
EXPECT_EQ(s_delete, "delete");
});
}
TEST(HttpMethods, TestStringToEnumConvertions) {
EXPECT_EQ(getHttpMethodFromName("all"), HttpMethod::All);
EXPECT_EQ(getHttpMethodFromName("GET"), HttpMethod::Get);
EXPECT_EQ(getHttpMethodFromName("Post"), HttpMethod::Post);
EXPECT_NE(getHttpMethodFromName("Post"), HttpMethod::Patch);
EXPECT_EQ(getHttpMethodFromName("pUt"), HttpMethod::Put);
EXPECT_THROW(getHttpMethodFromName("pasdt"), std::runtime_error);
}