test.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(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(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);
  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. Server svr;
  76. std::future<void> f;
  77. };
  78. TEST_F(ServerTest, GetMethod200)
  79. {
  80. Response res;
  81. bool ret = Client(host, port).get(url, res);
  82. ASSERT_EQ(true, ret);
  83. ASSERT_EQ(200, res.status);
  84. ASSERT_EQ(content, res.body);
  85. }
  86. TEST_F(ServerTest, GetMethod404)
  87. {
  88. Response res;
  89. bool ret = Client(host, port).get("/invalid", res);
  90. ASSERT_EQ(false, ret);
  91. ASSERT_EQ(404, res.status);
  92. }
  93. // vim: et ts=4 sw=4 cin cino={1s ff=unix