test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. detail::split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) {
  12. string key, val;
  13. detail::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. detail::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 = detail::create_server_socket("localhost", 1914);
  38. ASSERT_NE(-1, sock);
  39. auto ret = detail::shutdown_and_close_socket(sock);
  40. EXPECT_EQ(0, ret);
  41. }
  42. TEST(GetHeaderValueTest, DefaultValue)
  43. {
  44. MultiMap map = {{"Dummy","Dummy"}};
  45. auto val = detail::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 = detail::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 = detail::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 = detail::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), cli_(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. const int PORT = 1914;
  104. std::map<std::string, std::string> persons_;
  105. Server svr_;
  106. Client cli_;
  107. std::future<void> f_;
  108. };
  109. TEST_F(ServerTest, GetMethod200)
  110. {
  111. auto res = cli_.get("/hi");
  112. ASSERT_TRUE(res != nullptr);
  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. auto res = cli_.get("/");
  120. ASSERT_TRUE(res != nullptr);
  121. EXPECT_EQ(302, res->status);
  122. EXPECT_EQ("/hi", res->get_header_value("Location"));
  123. }
  124. TEST_F(ServerTest, GetMethod404)
  125. {
  126. auto res = cli_.get("/invalid");
  127. ASSERT_TRUE(res != nullptr);
  128. EXPECT_EQ(404, res->status);
  129. }
  130. TEST_F(ServerTest, GetMethodPersonJohn)
  131. {
  132. auto res = cli_.get("/person/john");
  133. ASSERT_TRUE(res != nullptr);
  134. EXPECT_EQ(200, res->status);
  135. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  136. EXPECT_EQ("programmer", res->body);
  137. }
  138. TEST_F(ServerTest, PostMethod)
  139. {
  140. auto res = cli_.get("/person/john3");
  141. ASSERT_TRUE(res != nullptr);
  142. ASSERT_EQ(404, res->status);
  143. res = cli_.post("/person", "name=john3&note=coder", "application/x-www-form-urlencoded");
  144. ASSERT_TRUE(res != nullptr);
  145. ASSERT_EQ(200, res->status);
  146. res = cli_.get("/person/john3");
  147. ASSERT_TRUE(res != nullptr);
  148. ASSERT_EQ(200, res->status);
  149. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  150. ASSERT_EQ("coder", res->body);
  151. }
  152. // vim: et ts=4 sw=4 cin cino={1s ff=unix