test.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. MultiMap map;
  62. map.insert(make_pair("Dummy", "Dummy"));
  63. auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
  64. ASSERT_STREQ("text/plain", val);
  65. }
  66. TEST(GetHeaderValueTest, DefaultValueInt)
  67. {
  68. //MultiMap map = {{"Dummy","Dummy"}};
  69. MultiMap map;
  70. map.insert(make_pair("Dummy", "Dummy"));
  71. auto val = detail::get_header_value_int(map, "Content-Length", 100);
  72. EXPECT_EQ(100, val);
  73. }
  74. TEST(GetHeaderValueTest, RegularValue)
  75. {
  76. //MultiMap map = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
  77. MultiMap map;
  78. map.insert(make_pair("Content-Type","text/html"));
  79. map.insert(make_pair("Dummy", "Dummy"));
  80. auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
  81. ASSERT_STREQ("text/html", val);
  82. }
  83. TEST(GetHeaderValueTest, RegularValueInt)
  84. {
  85. //MultiMap map = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
  86. MultiMap map;
  87. map.insert(make_pair("Content-Length", "100"));
  88. map.insert(make_pair("Dummy", "Dummy"));
  89. auto val = detail::get_header_value_int(map, "Content-Length", 0);
  90. EXPECT_EQ(100, val);
  91. }
  92. class ServerTest : public ::testing::Test {
  93. protected:
  94. ServerTest() : cli_(HOST, PORT), up_(false) {
  95. }
  96. virtual void SetUp() {
  97. svr_.get("/hi", [&](const Request& req, Response& res) {
  98. res.set_content("Hello World!", "text/plain");
  99. });
  100. svr_.get("/", [&](const Request& req, Response& res) {
  101. res.set_redirect("/hi");
  102. });
  103. svr_.post("/person", [&](const Request& req, Response& res) {
  104. if (req.has_param("name") && req.has_param("note")) {
  105. persons_[req.params.at("name")] = req.params.at("note");
  106. } else {
  107. res.status = 400;
  108. }
  109. });
  110. svr_.get("/person/(.*)", [&](const Request& req, Response& res) {
  111. string name = req.matches[1];
  112. if (persons_.find(name) != persons_.end()) {
  113. auto note = persons_[name];
  114. res.set_content(note, "text/plain");
  115. } else {
  116. res.status = 404;
  117. }
  118. });
  119. svr_.get("/stop", [&](const Request& req, Response& res) {
  120. svr_.stop();
  121. });
  122. persons_["john"] = "programmer";
  123. f_ = async([&](){
  124. up_ = true;
  125. svr_.listen(HOST, PORT);
  126. });
  127. while (!up_) {
  128. msleep(1);
  129. }
  130. }
  131. virtual void TearDown() {
  132. //svr_.stop(); // NOTE: This causes dead lock on Windows.
  133. cli_.get("/stop");
  134. f_.get();
  135. }
  136. map<string, string> persons_;
  137. Server svr_;
  138. Client cli_;
  139. future<void> f_;
  140. bool up_;
  141. };
  142. TEST_F(ServerTest, GetMethod200)
  143. {
  144. auto res = cli_.get("/hi");
  145. ASSERT_TRUE(res != nullptr);
  146. EXPECT_EQ(200, res->status);
  147. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  148. EXPECT_EQ("Hello World!", res->body);
  149. }
  150. TEST_F(ServerTest, GetMethod302)
  151. {
  152. auto res = cli_.get("/");
  153. ASSERT_TRUE(res != nullptr);
  154. EXPECT_EQ(302, res->status);
  155. EXPECT_EQ("/hi", res->get_header_value("Location"));
  156. }
  157. TEST_F(ServerTest, GetMethod404)
  158. {
  159. auto res = cli_.get("/invalid");
  160. ASSERT_TRUE(res != nullptr);
  161. EXPECT_EQ(404, res->status);
  162. }
  163. TEST_F(ServerTest, HeadMethod200)
  164. {
  165. auto res = cli_.head("/hi");
  166. ASSERT_TRUE(res != nullptr);
  167. EXPECT_EQ(200, res->status);
  168. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  169. EXPECT_EQ("", res->body);
  170. }
  171. TEST_F(ServerTest, HeadMethod404)
  172. {
  173. auto res = cli_.head("/invalid");
  174. ASSERT_TRUE(res != nullptr);
  175. EXPECT_EQ(404, res->status);
  176. EXPECT_EQ("", res->body);
  177. }
  178. TEST_F(ServerTest, GetMethodPersonJohn)
  179. {
  180. auto res = cli_.get("/person/john");
  181. ASSERT_TRUE(res != nullptr);
  182. EXPECT_EQ(200, res->status);
  183. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  184. EXPECT_EQ("programmer", res->body);
  185. }
  186. TEST_F(ServerTest, PostMethod1)
  187. {
  188. auto res = cli_.get("/person/john1");
  189. ASSERT_TRUE(res != nullptr);
  190. ASSERT_EQ(404, res->status);
  191. res = cli_.post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
  192. ASSERT_TRUE(res != nullptr);
  193. ASSERT_EQ(200, res->status);
  194. res = cli_.get("/person/john1");
  195. ASSERT_TRUE(res != nullptr);
  196. ASSERT_EQ(200, res->status);
  197. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  198. ASSERT_EQ("coder", res->body);
  199. }
  200. TEST_F(ServerTest, PostMethod2)
  201. {
  202. auto res = cli_.get("/person/john2");
  203. ASSERT_TRUE(res != nullptr);
  204. ASSERT_EQ(404, res->status);
  205. Map params;
  206. params["name"] = "john2";
  207. params["note"] = "coder";
  208. res = cli_.post("/person", params);
  209. ASSERT_TRUE(res != nullptr);
  210. ASSERT_EQ(200, res->status);
  211. res = cli_.get("/person/john2");
  212. ASSERT_TRUE(res != nullptr);
  213. ASSERT_EQ(200, res->status);
  214. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  215. ASSERT_EQ("coder", res->body);
  216. }
  217. #ifdef _WIN32
  218. TEST(CleanupTest, WSACleanup)
  219. {
  220. int ret = WSACleanup();
  221. ASSERT_EQ(0, ret);
  222. }
  223. #endif
  224. // vim: et ts=4 sw=4 cin cino={1s ff=unix