test.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include <gtest/gtest.h>
  2. #include <httplib.h>
  3. #include <future>
  4. #include <iostream>
  5. using namespace std;
  6. using namespace httplib;
  7. TEST(SplitTest, ParseQueryString)
  8. {
  9. string s = "key1=val1&key2=val2&key3=val3";
  10. map<string, string> dic;
  11. split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) {
  12. string key, val;
  13. split(b, e, '=', [&](const char* b, const char* e) {
  14. if (key.empty()) {
  15. key.assign(b, e);
  16. } else {
  17. val.assign(b, e);
  18. }
  19. });
  20. dic[key] = val;
  21. });
  22. EXPECT_EQ("val1", dic["key1"]);
  23. EXPECT_EQ("val2", dic["key2"]);
  24. EXPECT_EQ("val3", dic["key3"]);
  25. }
  26. TEST(ParseQueryTest, ParseQueryString)
  27. {
  28. string s = "key1=val1&key2=val2&key3=val3";
  29. map<string, string> dic;
  30. parse_query_text(&s[0], &s[s.size()], dic);
  31. EXPECT_EQ("val1", dic["key1"]);
  32. EXPECT_EQ("val2", dic["key2"]);
  33. EXPECT_EQ("val3", dic["key3"]);
  34. }
  35. TEST(SocketTest, OpenClose)
  36. {
  37. socket_t sock = create_server_socket("localhost", 1914);
  38. ASSERT_NE(-1, sock);
  39. auto ret = close_socket(sock);
  40. EXPECT_EQ(0, ret);
  41. }
  42. TEST(GetHeaderValueTest, DefaultValue)
  43. {
  44. MultiMap map = {{"Dummy","Dummy"}};
  45. auto val = get_header_value_text(map, "Content-Type", "text/plain");
  46. ASSERT_STREQ("text/plain", val);
  47. }
  48. TEST(GetHeaderValueTest, DefaultValueInt)
  49. {
  50. MultiMap map = {{"Dummy","Dummy"}};
  51. auto val = get_header_value_int(map, "Content-Length", 100);
  52. EXPECT_EQ(100, val);
  53. }
  54. TEST(GetHeaderValueTest, RegularValue)
  55. {
  56. MultiMap map = {{"Content-Type","text/html"}, {"Dummy", "Dummy"}};
  57. auto val = get_header_value_text(map, "Content-Type", "text/plain");
  58. ASSERT_STREQ("text/html", val);
  59. }
  60. TEST(GetHeaderValueTest, RegularValueInt)
  61. {
  62. MultiMap map = {{"Content-Length","100"}, {"Dummy", "Dummy"}};
  63. auto val = get_header_value_int(map, "Content-Length", 0);
  64. EXPECT_EQ(100, val);
  65. }
  66. class ServerTest : public ::testing::Test {
  67. protected:
  68. ServerTest() : svr_(host_, port_) {
  69. persons_["john"] = "programmer";
  70. }
  71. virtual void SetUp() {
  72. svr_.get("/hi", [&](httplib::Connection& c) {
  73. c.response.set_content("Hello World!", "text/plain");
  74. });
  75. svr_.get("/", [&](httplib::Connection& c) {
  76. c.response.set_redirect("/hi");
  77. });
  78. svr_.post("/person", [&](httplib::Connection& c) {
  79. const auto& req = c.request;
  80. if (req.has_param("name") && req.has_param("note")) {
  81. persons_[req.params.at("name")] = req.params.at("note");
  82. } else {
  83. c.response.status = 400;
  84. }
  85. });
  86. svr_.get("/person/(.*)", [&](httplib::Connection& c) {
  87. const auto& req = c.request;
  88. std::string name = req.matches[1];
  89. if (persons_.find(name) != persons_.end()) {
  90. auto note = persons_[name];
  91. c.response.set_content(note, "text/plain");
  92. } else {
  93. c.response.status = 404;
  94. }
  95. });
  96. f_ = async([&](){ svr_.run(); });
  97. }
  98. virtual void TearDown() {
  99. svr_.stop();
  100. f_.get();
  101. }
  102. const char* host_ = "localhost";
  103. int port_ = 1914;
  104. std::map<std::string, std::string> persons_;
  105. Server svr_;
  106. std::future<void> f_;
  107. };
  108. TEST_F(ServerTest, GetMethod200)
  109. {
  110. Response res;
  111. bool ret = Client(host_, port_).get("/hi", res);
  112. ASSERT_EQ(true, ret);
  113. EXPECT_EQ(200, res.status);
  114. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  115. EXPECT_EQ("Hello World!", res.body);
  116. }
  117. TEST_F(ServerTest, GetMethod302)
  118. {
  119. Response res;
  120. bool ret = Client(host_, port_).get("/", res);
  121. ASSERT_EQ(true, ret);
  122. EXPECT_EQ(302, res.status);
  123. EXPECT_EQ("/hi", res.get_header_value("Location"));
  124. }
  125. TEST_F(ServerTest, GetMethod404)
  126. {
  127. Response res;
  128. bool ret = Client(host_, port_).get("/invalid", res);
  129. ASSERT_EQ(true, ret);
  130. EXPECT_EQ(404, res.status);
  131. }
  132. TEST_F(ServerTest, GetMethodPersonJohn)
  133. {
  134. Response res;
  135. bool ret = Client(host_, port_).get("/person/john", res);
  136. ASSERT_EQ(true, ret);
  137. EXPECT_EQ(200, res.status);
  138. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  139. EXPECT_EQ("programmer", res.body);
  140. }
  141. TEST_F(ServerTest, PostMethod)
  142. {
  143. {
  144. Response res;
  145. bool ret = Client(host_, port_).get("/person/john2", res);
  146. ASSERT_EQ(true, ret);
  147. ASSERT_EQ(404, res.status);
  148. }
  149. {
  150. auto content = "name=john2&note=coder";
  151. auto content_type = "application/x-www-form-urlencoded";
  152. Response res;
  153. bool ret = Client(host_, port_).post("/person", content, content_type, res);
  154. ASSERT_EQ(true, ret);
  155. ASSERT_EQ(200, res.status);
  156. }
  157. {
  158. Response res;
  159. bool ret = Client(host_, port_).get("/person/john2", res);
  160. ASSERT_EQ(true, ret);
  161. ASSERT_EQ(200, res.status);
  162. ASSERT_EQ("text/plain", res.get_header_value("Content-Type"));
  163. ASSERT_EQ("coder", res.body);
  164. }
  165. }
  166. // vim: et ts=4 sw=4 cin cino={1s ff=unix