1
0

util_tests.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include <gtest/gtest.h>
  2. #include <string>
  3. #include <cpr/cprtypes.h>
  4. #include <cpr/util.h>
  5. using namespace cpr;
  6. TEST(UtilParseCookiesTests, BasicParseTest) {
  7. Cookies expectedCookies{{Cookie("status", "on", "127.0.0.1", false, "/", false, std::chrono::system_clock::from_time_t(1656908640)), Cookie("name", "debug", "127.0.0.1", false, "/", false, std::chrono::system_clock::from_time_t(0))}};
  8. curl_slist* raw_cookies = new curl_slist{
  9. (char*) "127.0.0.1\tFALSE\t/\tFALSE\t1656908640\tstatus\ton",
  10. new curl_slist{
  11. (char*) "127.0.0.1\tFALSE\t/\tFALSE\t0\tname\tdebug",
  12. nullptr,
  13. },
  14. };
  15. Cookies cookies = util::parseCookies(raw_cookies);
  16. for (auto cookie = cookies.begin(), expectedCookie = expectedCookies.begin(); cookie != cookies.end() && expectedCookie != expectedCookies.end(); cookie++, expectedCookie++) {
  17. EXPECT_EQ(expectedCookie->GetName(), cookie->GetName());
  18. EXPECT_EQ(expectedCookie->GetValue(), cookie->GetValue());
  19. EXPECT_EQ(expectedCookie->GetDomain(), cookie->GetDomain());
  20. EXPECT_EQ(expectedCookie->IsIncludingSubdomains(), cookie->IsIncludingSubdomains());
  21. EXPECT_EQ(expectedCookie->GetPath(), cookie->GetPath());
  22. EXPECT_EQ(expectedCookie->IsHttpsOnly(), cookie->IsHttpsOnly());
  23. EXPECT_EQ(expectedCookie->GetExpires(), cookie->GetExpires());
  24. }
  25. delete raw_cookies->next;
  26. delete raw_cookies;
  27. }
  28. TEST(UtilParseHeaderTests, BasicParseTest) {
  29. std::string header_string{
  30. "HTTP/1.1 200 OK\r\n"
  31. "Server: nginx\r\n"
  32. "Date: Sun, 05 Mar 2017 00:34:54 GMT\r\n"
  33. "Content-Type: application/json\r\n"
  34. "Content-Length: 351\r\n"
  35. "Connection: keep-alive\r\n"
  36. "Access-Control-Allow-Origin: *\r\n"
  37. "Access-Control-Allow-Credentials: true\r\n"
  38. "\r\n"};
  39. Header header = util::parseHeader(header_string);
  40. EXPECT_EQ(std::string{"nginx"}, header["Server"]);
  41. EXPECT_EQ(std::string{"Sun, 05 Mar 2017 00:34:54 GMT"}, header["Date"]);
  42. EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
  43. EXPECT_EQ(std::string{"351"}, header["Content-Length"]);
  44. EXPECT_EQ(std::string{"keep-alive"}, header["Connection"]);
  45. EXPECT_EQ(std::string{"*"}, header["Access-Control-Allow-Origin"]);
  46. EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
  47. }
  48. TEST(UtilParseHeaderTests, NewlineTest) {
  49. std::string header_string{
  50. "HTTP/1.1 200 OK\r\n"
  51. "Auth:\n"
  52. "Access-Control-Allow-Credentials: true\r\n"
  53. "\r\n"};
  54. Header header = util::parseHeader(header_string);
  55. EXPECT_EQ(std::string{""}, header["Server"]);
  56. EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
  57. }
  58. TEST(UtilParseHeaderTests, SpaceNewlineTest) {
  59. std::string header_string{
  60. "HTTP/1.1 200 OK\r\n"
  61. "Auth: \n"
  62. "Access-Control-Allow-Credentials: true\r\n"
  63. "\r\n"};
  64. Header header = util::parseHeader(header_string);
  65. EXPECT_EQ(std::string{""}, header["Server"]);
  66. EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
  67. }
  68. TEST(UtilParseHeaderTests, CarriageReturnNewlineTest) {
  69. std::string header_string{
  70. "HTTP/1.1 200 OK\n"
  71. "Auth:\r\n"
  72. "Access-Control-Allow-Credentials: true\r\n"
  73. "\r\n"};
  74. Header header = util::parseHeader(header_string);
  75. EXPECT_EQ(std::string{""}, header["Server"]);
  76. EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
  77. }
  78. TEST(UtilParseHeaderTests, SpaceCarriageReturnNewlineTest) {
  79. std::string header_string{
  80. "HTTP/1.1 200 OK\n"
  81. "Auth: \r\n"
  82. "Access-Control-Allow-Credentials: true\r\n"
  83. "\r\n"};
  84. Header header = util::parseHeader(header_string);
  85. EXPECT_EQ(std::string{""}, header["Server"]);
  86. EXPECT_EQ(std::string{"true"}, header["Access-Control-Allow-Credentials"]);
  87. }
  88. TEST(UtilParseHeaderTests, BasicStatusLineTest) {
  89. std::string header_string{
  90. "HTTP/1.1 200 OK\r\n"
  91. "Server: nginx\r\n"
  92. "Content-Type: application/json\r\n"
  93. "\r\n"};
  94. std::string status_line;
  95. std::string reason;
  96. Header header = util::parseHeader(header_string, &status_line, &reason);
  97. EXPECT_EQ(std::string{"HTTP/1.1 200 OK"}, status_line);
  98. EXPECT_EQ(std::string{"OK"}, reason);
  99. EXPECT_EQ(std::string{"nginx"}, header["Server"]);
  100. EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
  101. }
  102. TEST(UtilParseHeaderTests, NewlineStatusLineTest) {
  103. std::string header_string{
  104. "HTTP/1.1 407 Proxy Authentication Required\n"
  105. "Server: nginx\r\n"
  106. "Content-Type: application/json\r\n"
  107. "\r\n"};
  108. std::string status_line;
  109. std::string reason;
  110. Header header = util::parseHeader(header_string, &status_line, &reason);
  111. EXPECT_EQ(std::string{"HTTP/1.1 407 Proxy Authentication Required"}, status_line);
  112. EXPECT_EQ(std::string{"Proxy Authentication Required"}, reason);
  113. EXPECT_EQ(std::string{"nginx"}, header["Server"]);
  114. EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
  115. }
  116. TEST(UtilParseHeaderTests, NoReasonSpaceTest) {
  117. std::string header_string{
  118. "HTTP/1.1 200 \n"
  119. "Server: nginx\r\n"
  120. "Content-Type: application/json\r\n"
  121. "\r\n"};
  122. std::string status_line;
  123. std::string reason;
  124. Header header = util::parseHeader(header_string, &status_line, &reason);
  125. EXPECT_EQ(std::string{"HTTP/1.1 200"}, status_line);
  126. EXPECT_EQ(std::string{""}, reason);
  127. EXPECT_EQ(std::string{"nginx"}, header["Server"]);
  128. EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
  129. }
  130. TEST(UtilParseHeaderTests, NoReasonTest) {
  131. std::string header_string{
  132. "HTTP/1.1 200\n"
  133. "Server: nginx\r\n"
  134. "Content-Type: application/json\r\n"
  135. "\r\n"};
  136. std::string status_line;
  137. std::string reason;
  138. Header header = util::parseHeader(header_string, &status_line, &reason);
  139. EXPECT_EQ(std::string{"HTTP/1.1 200"}, status_line);
  140. EXPECT_EQ(std::string{""}, reason);
  141. EXPECT_EQ(std::string{"nginx"}, header["Server"]);
  142. EXPECT_EQ(std::string{"application/json"}, header["Content-Type"]);
  143. }
  144. TEST(UtilUrlEncodeTests, UnicodeEncoderTest) {
  145. std::string input = "一二三";
  146. std::string result = util::urlEncode(input);
  147. std::string expected = "%E4%B8%80%E4%BA%8C%E4%B8%89";
  148. EXPECT_EQ(result, expected);
  149. }
  150. TEST(UtilUrlEncodeTests, AsciiEncoderTest) {
  151. std::string input = "Hello World!";
  152. std::string result = util::urlEncode(input);
  153. std::string expected = "Hello%20World%21";
  154. EXPECT_EQ(result, expected);
  155. }
  156. TEST(UtilUrlDecodeTests, UnicodeDecoderTest) {
  157. std::string input = "%E4%B8%80%E4%BA%8C%E4%B8%89";
  158. std::string result = util::urlDecode(input);
  159. std::string expected = "一二三";
  160. EXPECT_EQ(result, expected);
  161. }
  162. TEST(UtilUrlDecodeTests, AsciiDecoderTest) {
  163. std::string input = "Hello%20World%21";
  164. std::string result = util::urlDecode(input);
  165. std::string expected = "Hello World!";
  166. EXPECT_EQ(result, expected);
  167. }
  168. TEST(UtilSecureStringClearTests, EmptyStringTest) {
  169. std::string input;
  170. util::secureStringClear(input);
  171. EXPECT_TRUE(input.empty());
  172. }
  173. TEST(UtilSecureStringClearTests, NotEmptyStringTest) {
  174. std::string input = "Hello World!";
  175. util::secureStringClear(input);
  176. EXPECT_TRUE(input.empty());
  177. }
  178. TEST(UtilIsTrueTests, TrueTest) {
  179. {
  180. std::string input = "TRUE";
  181. bool output = util::isTrue(input);
  182. EXPECT_TRUE(output);
  183. }
  184. {
  185. std::string input = "True";
  186. bool output = util::isTrue(input);
  187. EXPECT_TRUE(output);
  188. }
  189. {
  190. std::string input = "true";
  191. bool output = util::isTrue(input);
  192. EXPECT_TRUE(output);
  193. }
  194. }
  195. TEST(UtilIsTrueTests, FalseTest) {
  196. {
  197. std::string input = "FALSE";
  198. bool output = util::isTrue(input);
  199. EXPECT_FALSE(output);
  200. }
  201. {
  202. std::string input = "False";
  203. bool output = util::isTrue(input);
  204. EXPECT_FALSE(output);
  205. }
  206. {
  207. std::string input = "false";
  208. bool output = util::isTrue(input);
  209. EXPECT_FALSE(output);
  210. }
  211. }
  212. int main(int argc, char** argv) {
  213. ::testing::InitGoogleTest(&argc, argv);
  214. return RUN_ALL_TESTS();
  215. }