test.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(url_, [&](httplib::Connection& c) {
  63. c.response.set_content(content_, mime_);
  64. });
  65. f_ = async([&](){ svr_.run(); });
  66. }
  67. virtual void TearDown() {
  68. svr_.stop();
  69. f_.get();
  70. }
  71. const char* host_ = "localhost";
  72. int port_ = 1914;
  73. const char* url_ = "/hi";
  74. const char* content_ = "Hello World!";
  75. const char* mime_ = "text/plain";
  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(url_, res);
  83. ASSERT_EQ(true, ret);
  84. ASSERT_EQ(200, res.status);
  85. ASSERT_EQ(content_, res.body);
  86. }
  87. TEST_F(ServerTest, GetMethod404)
  88. {
  89. Response res;
  90. bool ret = Client(host_, port_).get("/invalid", res);
  91. ASSERT_EQ(false, ret);
  92. ASSERT_EQ(404, res.status);
  93. }
  94. // vim: et ts=4 sw=4 cin cino={1s ff=unix