test.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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_.set_base_dir("./www");
  88. svr_.get("/hi", [&](const Request& req, Response& res) {
  89. res.set_content("Hello World!", "text/plain");
  90. });
  91. svr_.get("/", [&](const Request& req, Response& res) {
  92. res.set_redirect("/hi");
  93. });
  94. svr_.post("/person", [&](const Request& req, Response& res) {
  95. if (req.has_param("name") && req.has_param("note")) {
  96. persons_[req.params.at("name")] = req.params.at("note");
  97. } else {
  98. res.status = 400;
  99. }
  100. });
  101. svr_.get("/person/(.*)", [&](const Request& req, Response& res) {
  102. string name = req.matches[1];
  103. if (persons_.find(name) != persons_.end()) {
  104. auto note = persons_[name];
  105. res.set_content(note, "text/plain");
  106. } else {
  107. res.status = 404;
  108. }
  109. });
  110. svr_.get("/stop", [&](const Request& req, Response& res) {
  111. svr_.stop();
  112. });
  113. persons_["john"] = "programmer";
  114. f_ = async([&](){
  115. up_ = true;
  116. svr_.listen(HOST, PORT);
  117. });
  118. while (!up_) {
  119. msleep(1);
  120. }
  121. }
  122. virtual void TearDown() {
  123. //svr_.stop(); // NOTE: This causes dead lock on Windows.
  124. cli_.get("/stop");
  125. f_.get();
  126. }
  127. map<string, string> persons_;
  128. Server svr_;
  129. Client cli_;
  130. future<void> f_;
  131. bool up_;
  132. };
  133. TEST_F(ServerTest, GetMethod200)
  134. {
  135. auto res = cli_.get("/hi");
  136. ASSERT_TRUE(res != nullptr);
  137. EXPECT_EQ(200, res->status);
  138. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  139. EXPECT_EQ("Hello World!", res->body);
  140. }
  141. TEST_F(ServerTest, GetMethod302)
  142. {
  143. auto res = cli_.get("/");
  144. ASSERT_TRUE(res != nullptr);
  145. EXPECT_EQ(302, res->status);
  146. EXPECT_EQ("/hi", res->get_header_value("Location"));
  147. }
  148. TEST_F(ServerTest, GetMethod404)
  149. {
  150. auto res = cli_.get("/invalid");
  151. ASSERT_TRUE(res != nullptr);
  152. EXPECT_EQ(404, res->status);
  153. }
  154. TEST_F(ServerTest, HeadMethod200)
  155. {
  156. auto res = cli_.head("/hi");
  157. ASSERT_TRUE(res != nullptr);
  158. EXPECT_EQ(200, res->status);
  159. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  160. EXPECT_EQ("", res->body);
  161. }
  162. TEST_F(ServerTest, HeadMethod404)
  163. {
  164. auto res = cli_.head("/invalid");
  165. ASSERT_TRUE(res != nullptr);
  166. EXPECT_EQ(404, res->status);
  167. EXPECT_EQ("", res->body);
  168. }
  169. TEST_F(ServerTest, GetMethodPersonJohn)
  170. {
  171. auto res = cli_.get("/person/john");
  172. ASSERT_TRUE(res != nullptr);
  173. EXPECT_EQ(200, res->status);
  174. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  175. EXPECT_EQ("programmer", res->body);
  176. }
  177. TEST_F(ServerTest, PostMethod1)
  178. {
  179. auto res = cli_.get("/person/john1");
  180. ASSERT_TRUE(res != nullptr);
  181. ASSERT_EQ(404, res->status);
  182. res = cli_.post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
  183. ASSERT_TRUE(res != nullptr);
  184. ASSERT_EQ(200, res->status);
  185. res = cli_.get("/person/john1");
  186. ASSERT_TRUE(res != nullptr);
  187. ASSERT_EQ(200, res->status);
  188. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  189. ASSERT_EQ("coder", res->body);
  190. }
  191. TEST_F(ServerTest, PostMethod2)
  192. {
  193. auto res = cli_.get("/person/john2");
  194. ASSERT_TRUE(res != nullptr);
  195. ASSERT_EQ(404, res->status);
  196. Map params;
  197. params["name"] = "john2";
  198. params["note"] = "coder";
  199. res = cli_.post("/person", params);
  200. ASSERT_TRUE(res != nullptr);
  201. ASSERT_EQ(200, res->status);
  202. res = cli_.get("/person/john2");
  203. ASSERT_TRUE(res != nullptr);
  204. ASSERT_EQ(200, res->status);
  205. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  206. ASSERT_EQ("coder", res->body);
  207. }
  208. TEST_F(ServerTest, GetMethodDir)
  209. {
  210. auto res = cli_.get("/dir/");
  211. ASSERT_TRUE(res != nullptr);
  212. EXPECT_EQ(200, res->status);
  213. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  214. EXPECT_EQ("index.html", res->body);
  215. }
  216. TEST_F(ServerTest, GetMethodDirTest)
  217. {
  218. auto res = cli_.get("/dir/test.html");
  219. ASSERT_TRUE(res != nullptr);
  220. EXPECT_EQ(200, res->status);
  221. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  222. EXPECT_EQ("test.html", res->body);
  223. }
  224. TEST_F(ServerTest, InvalidBaseDir)
  225. {
  226. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  227. EXPECT_EQ(true, svr_.set_base_dir("./"));
  228. }
  229. #ifdef _WIN32
  230. TEST(CleanupTest, WSACleanup)
  231. {
  232. int ret = WSACleanup();
  233. ASSERT_EQ(0, ret);
  234. }
  235. #endif
  236. // vim: et ts=4 sw=4 cin cino={1s ff=unix