test.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. ASSERT_EQ("val1", dic["key1"]);
  23. ASSERT_EQ("val2", dic["key2"]);
  24. ASSERT_EQ("val3", dic["key3"]);
  25. }
  26. TEST(SocketTest, OpenClose)
  27. {
  28. socket_t sock = create_server_socket("localhost", 1914);
  29. ASSERT_NE(-1, sock);
  30. auto ret = close_server_socket(sock);
  31. ASSERT_EQ(0, ret);
  32. }
  33. TEST(GetHeaderValueTest, DefaultValue)
  34. {
  35. MultiMap map = {{"Dummy","Dummy"}};
  36. auto val = get_header_value_text(map, "Content-Type", "text/plain");
  37. ASSERT_STREQ("text/plain", val);
  38. }
  39. TEST(GetHeaderValueTest, DefaultValueInt)
  40. {
  41. MultiMap map = {{"Dummy","Dummy"}};
  42. auto val = get_header_value_int(map, "Content-Length", 100);
  43. ASSERT_EQ(100, val);
  44. }
  45. TEST(GetHeaderValueTest, RegularValue)
  46. {
  47. MultiMap map = {{"Content-Type","text/html"}, {"Dummy", "Dummy"}};
  48. auto val = get_header_value_text(map, "Content-Type", "text/plain");
  49. ASSERT_STREQ("text/html", val);
  50. }
  51. TEST(GetHeaderValueTest, RegularValueInt)
  52. {
  53. MultiMap map = {{"Content-Length","100"}, {"Dummy", "Dummy"}};
  54. auto val = get_header_value_int(map, "Content-Length", 0);
  55. ASSERT_EQ(100, val);
  56. }
  57. class ServerTest : public ::testing::Test {
  58. protected:
  59. ServerTest() : svr_(host_, port_) {
  60. }
  61. virtual void SetUp() {
  62. svr_.get("/hi", [&](httplib::Connection& c) {
  63. c.response.set_content("Hello World!", "text/plain");
  64. });
  65. svr_.get("/", [&](httplib::Connection& c) {
  66. c.response.set_redirect("/hi");
  67. });
  68. f_ = async([&](){ svr_.run(); });
  69. }
  70. virtual void TearDown() {
  71. svr_.stop();
  72. f_.get();
  73. }
  74. const char* host_ = "localhost";
  75. int port_ = 1914;
  76. Server svr_;
  77. std::future<void> f_;
  78. };
  79. TEST_F(ServerTest, GetMethod200)
  80. {
  81. Response res;
  82. bool ret = Client(host_, port_).get("/hi", res);
  83. ASSERT_EQ(true, ret);
  84. ASSERT_EQ(200, res.status);
  85. ASSERT_EQ("text/plain", res.get_header_value("Content-Type"));
  86. ASSERT_EQ("Hello World!", res.body);
  87. }
  88. TEST_F(ServerTest, GetMethod302)
  89. {
  90. Response res;
  91. bool ret = Client(host_, port_).get("/", res);
  92. ASSERT_EQ(true, ret);
  93. ASSERT_EQ(302, res.status);
  94. ASSERT_EQ("/hi", res.get_header_value("Location"));
  95. }
  96. TEST_F(ServerTest, GetMethod404)
  97. {
  98. Response res;
  99. bool ret = Client(host_, port_).get("/invalid", res);
  100. ASSERT_EQ(true, ret);
  101. ASSERT_EQ(404, res.status);
  102. }
  103. // vim: et ts=4 sw=4 cin cino={1s ff=unix