test.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include <gtest/gtest.h>
  2. #include <httplib.h>
  3. #include <future>
  4. #include <iostream>
  5. #ifdef _WIN32
  6. #include <process.h>
  7. #define msleep(n) ::Sleep(n)
  8. #else
  9. #define msleep(n) ::usleep(n * 1000)
  10. #endif
  11. using namespace std;
  12. using namespace httplib;
  13. const char* HOST = "localhost";
  14. const int PORT = 1234;
  15. #ifdef _WIN32
  16. TEST(StartupTest, WSAStartup)
  17. {
  18. WSADATA wsaData;
  19. int ret = WSAStartup(0x0002, &wsaData);
  20. ASSERT_EQ(0, ret);
  21. }
  22. #endif
  23. TEST(SplitTest, ParseQueryString)
  24. {
  25. string s = "key1=val1&key2=val2&key3=val3";
  26. map<string, string> dic;
  27. detail::split(s.c_str(), s.c_str() + s.size(), '&', [&](const char* b, const char* e) {
  28. string key, val;
  29. detail::split(b, e, '=', [&](const char* b, const char* e) {
  30. if (key.empty()) {
  31. key.assign(b, e);
  32. } else {
  33. val.assign(b, e);
  34. }
  35. });
  36. dic[key] = val;
  37. });
  38. EXPECT_EQ("val1", dic["key1"]);
  39. EXPECT_EQ("val2", dic["key2"]);
  40. EXPECT_EQ("val3", dic["key3"]);
  41. }
  42. TEST(ParseQueryTest, ParseQueryString)
  43. {
  44. string s = "key1=val1&key2=val2&key3=val3";
  45. map<string, string> dic;
  46. detail::parse_query_text(s, dic);
  47. EXPECT_EQ("val1", dic["key1"]);
  48. EXPECT_EQ("val2", dic["key2"]);
  49. EXPECT_EQ("val3", dic["key3"]);
  50. }
  51. TEST(SocketTest, OpenClose)
  52. {
  53. socket_t sock = detail::create_server_socket(HOST, PORT);
  54. ASSERT_NE(-1, sock);
  55. auto ret = detail::close_socket(sock);
  56. EXPECT_EQ(0, ret);
  57. }
  58. TEST(GetHeaderValueTest, DefaultValue)
  59. {
  60. MultiMap map = {{"Dummy","Dummy"}};
  61. auto val = detail::get_header_value(map, "Content-Type", "text/plain");
  62. ASSERT_STREQ("text/plain", val);
  63. }
  64. TEST(GetHeaderValueTest, DefaultValueInt)
  65. {
  66. MultiMap map = {{"Dummy","Dummy"}};
  67. auto val = detail::get_header_value_int(map, "Content-Length", 100);
  68. EXPECT_EQ(100, val);
  69. }
  70. TEST(GetHeaderValueTest, RegularValue)
  71. {
  72. MultiMap map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
  73. auto val = detail::get_header_value(map, "Content-Type", "text/plain");
  74. ASSERT_STREQ("text/html", val);
  75. }
  76. TEST(GetHeaderValueTest, RegularValueInt)
  77. {
  78. MultiMap map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
  79. auto val = detail::get_header_value_int(map, "Content-Length", 0);
  80. EXPECT_EQ(100, val);
  81. }
  82. class ServerTest : public ::testing::Test {
  83. protected:
  84. ServerTest() : cli_(HOST, PORT), up_(false) {
  85. }
  86. virtual void SetUp() {
  87. svr_.get("/hi", [&](const Request& req, Response& res) {
  88. res.set_content("Hello World!", "text/plain");
  89. });
  90. svr_.get("/", [&](const Request& req, Response& res) {
  91. res.set_redirect("/hi");
  92. });
  93. svr_.post("/person", [&](const Request& req, Response& res) {
  94. if (req.has_param("name") && req.has_param("note")) {
  95. persons_[req.params.at("name")] = req.params.at("note");
  96. } else {
  97. res.status = 400;
  98. }
  99. });
  100. svr_.get("/person/(.*)", [&](const Request& req, Response& res) {
  101. string name = req.matches[1];
  102. if (persons_.find(name) != persons_.end()) {
  103. auto note = persons_[name];
  104. res.set_content(note, "text/plain");
  105. } else {
  106. res.status = 404;
  107. }
  108. });
  109. svr_.get("/stop", [&](const Request& req, Response& res) {
  110. svr_.stop();
  111. });
  112. persons_["john"] = "programmer";
  113. f_ = async([&](){
  114. up_ = true;
  115. svr_.listen(HOST, PORT);
  116. });
  117. while (!up_) {
  118. msleep(1);
  119. }
  120. }
  121. virtual void TearDown() {
  122. //svr_.stop(); // NOTE: This causes dead lock on Windows.
  123. cli_.get("/stop");
  124. f_.get();
  125. }
  126. map<string, string> persons_;
  127. Server svr_;
  128. Client cli_;
  129. future<void> f_;
  130. bool up_;
  131. };
  132. TEST_F(ServerTest, GetMethod200)
  133. {
  134. auto res = cli_.get("/hi");
  135. ASSERT_TRUE(res != nullptr);
  136. EXPECT_EQ(200, res->status);
  137. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  138. EXPECT_EQ("Hello World!", res->body);
  139. }
  140. TEST_F(ServerTest, GetMethod302)
  141. {
  142. auto res = cli_.get("/");
  143. ASSERT_TRUE(res != nullptr);
  144. EXPECT_EQ(302, res->status);
  145. EXPECT_EQ("/hi", res->get_header_value("Location"));
  146. }
  147. TEST_F(ServerTest, GetMethod404)
  148. {
  149. auto res = cli_.get("/invalid");
  150. ASSERT_TRUE(res != nullptr);
  151. EXPECT_EQ(404, res->status);
  152. }
  153. TEST_F(ServerTest, HeadMethod200)
  154. {
  155. auto res = cli_.head("/hi");
  156. ASSERT_TRUE(res != nullptr);
  157. EXPECT_EQ(200, res->status);
  158. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  159. EXPECT_EQ("", res->body);
  160. }
  161. TEST_F(ServerTest, HeadMethod404)
  162. {
  163. auto res = cli_.head("/invalid");
  164. ASSERT_TRUE(res != nullptr);
  165. EXPECT_EQ(404, res->status);
  166. EXPECT_EQ("", res->body);
  167. }
  168. TEST_F(ServerTest, GetMethodPersonJohn)
  169. {
  170. auto res = cli_.get("/person/john");
  171. ASSERT_TRUE(res != nullptr);
  172. EXPECT_EQ(200, res->status);
  173. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  174. EXPECT_EQ("programmer", res->body);
  175. }
  176. TEST_F(ServerTest, PostMethod1)
  177. {
  178. auto res = cli_.get("/person/john1");
  179. ASSERT_TRUE(res != nullptr);
  180. ASSERT_EQ(404, res->status);
  181. res = cli_.post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
  182. ASSERT_TRUE(res != nullptr);
  183. ASSERT_EQ(200, res->status);
  184. res = cli_.get("/person/john1");
  185. ASSERT_TRUE(res != nullptr);
  186. ASSERT_EQ(200, res->status);
  187. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  188. ASSERT_EQ("coder", res->body);
  189. }
  190. TEST_F(ServerTest, PostMethod2)
  191. {
  192. auto res = cli_.get("/person/john2");
  193. ASSERT_TRUE(res != nullptr);
  194. ASSERT_EQ(404, res->status);
  195. Map params;
  196. params["name"] = "john2";
  197. params["note"] = "coder";
  198. res = cli_.post("/person", params);
  199. ASSERT_TRUE(res != nullptr);
  200. ASSERT_EQ(200, res->status);
  201. res = cli_.get("/person/john2");
  202. ASSERT_TRUE(res != nullptr);
  203. ASSERT_EQ(200, res->status);
  204. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  205. ASSERT_EQ("coder", res->body);
  206. }
  207. #ifdef _WIN32
  208. TEST(CleanupTest, WSACleanup)
  209. {
  210. int ret = WSACleanup();
  211. ASSERT_EQ(0, ret);
  212. }
  213. #endif
  214. // vim: et ts=4 sw=4 cin cino={1s ff=unix