test.cc 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. #include <future>
  2. #include <gtest/gtest.h>
  3. #include <httplib.h>
  4. #define SERVER_CERT_FILE "./cert.pem"
  5. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  6. #define CA_CERT_FILE "./ca-bundle.crt"
  7. #define CLIENT_CA_CERT_FILE "./rootCA.cert.pem"
  8. #define CLIENT_CA_CERT_DIR "."
  9. #define CLIENT_CERT_FILE "./client.cert.pem"
  10. #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem"
  11. #ifdef _WIN32
  12. #include <process.h>
  13. #define msleep(n) ::Sleep(n)
  14. #else
  15. #define msleep(n) ::usleep(n * 1000)
  16. #endif
  17. using namespace std;
  18. using namespace httplib;
  19. const char *HOST = "localhost";
  20. const int PORT = 1234;
  21. const string LONG_QUERY_VALUE = string(25000, '@');
  22. const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
  23. const std::string JSON_DATA = "{\"hello\":\"world\"}";
  24. #ifdef _WIN32
  25. TEST(StartupTest, WSAStartup) {
  26. WSADATA wsaData;
  27. int ret = WSAStartup(0x0002, &wsaData);
  28. ASSERT_EQ(0, ret);
  29. }
  30. #endif
  31. TEST(SplitTest, ParseQueryString) {
  32. string s = "key1=val1&key2=val2&key3=val3";
  33. Params dic;
  34. detail::split(s.c_str(), s.c_str() + s.size(), '&',
  35. [&](const char *b, const char *e) {
  36. string key, val;
  37. detail::split(b, e, '=', [&](const char *b, const char *e) {
  38. if (key.empty()) {
  39. key.assign(b, e);
  40. } else {
  41. val.assign(b, e);
  42. }
  43. });
  44. dic.emplace(key, val);
  45. });
  46. EXPECT_EQ("val1", dic.find("key1")->second);
  47. EXPECT_EQ("val2", dic.find("key2")->second);
  48. EXPECT_EQ("val3", dic.find("key3")->second);
  49. }
  50. TEST(ParseQueryTest, ParseQueryString) {
  51. string s = "key1=val1&key2=val2&key3=val3";
  52. Params dic;
  53. detail::parse_query_text(s, dic);
  54. EXPECT_EQ("val1", dic.find("key1")->second);
  55. EXPECT_EQ("val2", dic.find("key2")->second);
  56. EXPECT_EQ("val3", dic.find("key3")->second);
  57. }
  58. TEST(GetHeaderValueTest, DefaultValue) {
  59. Headers headers = {{"Dummy", "Dummy"}};
  60. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  61. EXPECT_STREQ("text/plain", val);
  62. }
  63. TEST(GetHeaderValueTest, DefaultValueInt) {
  64. Headers headers = {{"Dummy", "Dummy"}};
  65. auto val = detail::get_header_value_uint64(headers, "Content-Length", 100);
  66. EXPECT_EQ(100ull, val);
  67. }
  68. TEST(GetHeaderValueTest, RegularValue) {
  69. Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
  70. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  71. EXPECT_STREQ("text/html", val);
  72. }
  73. TEST(GetHeaderValueTest, RegularValueInt) {
  74. Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
  75. auto val = detail::get_header_value_uint64(headers, "Content-Length", 0);
  76. EXPECT_EQ(100ull, val);
  77. }
  78. TEST(GetHeaderValueTest, Range) {
  79. {
  80. Headers headers = {make_range_header(1)};
  81. auto val = detail::get_header_value(headers, "Range", 0, 0);
  82. EXPECT_STREQ("bytes=1-", val);
  83. }
  84. {
  85. Headers headers = {make_range_header(1, 10)};
  86. auto val = detail::get_header_value(headers, "Range", 0, 0);
  87. EXPECT_STREQ("bytes=1-10", val);
  88. }
  89. {
  90. Headers headers = {make_range_header(1, 10, 100)};
  91. auto val = detail::get_header_value(headers, "Range", 0, 0);
  92. EXPECT_STREQ("bytes=1-10, 100-", val);
  93. }
  94. {
  95. Headers headers = {make_range_header(1, 10, 100, 200)};
  96. auto val = detail::get_header_value(headers, "Range", 0, 0);
  97. EXPECT_STREQ("bytes=1-10, 100-200", val);
  98. }
  99. }
  100. TEST(ChunkedEncodingTest, FromHTTPWatch) {
  101. auto host = "www.httpwatch.com";
  102. auto sec = 2;
  103. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  104. auto port = 443;
  105. httplib::SSLClient cli(host, port, sec);
  106. #else
  107. auto port = 80;
  108. httplib::Client cli(host, port, sec);
  109. #endif
  110. auto res =
  111. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
  112. ASSERT_TRUE(res != nullptr);
  113. std::string out;
  114. httplib::detail::read_file("./image.jpg", out);
  115. EXPECT_EQ(200, res->status);
  116. EXPECT_EQ(out, res->body);
  117. }
  118. TEST(ChunkedEncodingTest, WithContentReceiver) {
  119. auto host = "www.httpwatch.com";
  120. auto sec = 2;
  121. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  122. auto port = 443;
  123. httplib::SSLClient cli(host, port, sec);
  124. #else
  125. auto port = 80;
  126. httplib::Client cli(host, port, sec);
  127. #endif
  128. std::string body;
  129. auto res =
  130. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
  131. [&](const char *data, size_t len) { body.append(data, len); });
  132. ASSERT_TRUE(res != nullptr);
  133. std::string out;
  134. httplib::detail::read_file("./image.jpg", out);
  135. EXPECT_EQ(200, res->status);
  136. EXPECT_EQ(out, body);
  137. }
  138. TEST(RangeTest, FromHTTPBin) {
  139. auto host = "httpbin.org";
  140. auto sec = 5;
  141. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  142. auto port = 443;
  143. httplib::SSLClient cli(host, port, sec);
  144. #else
  145. auto port = 80;
  146. httplib::Client cli(host, port, sec);
  147. #endif
  148. {
  149. httplib::Headers headers;
  150. auto res = cli.Get("/range/32", headers);
  151. ASSERT_TRUE(res != nullptr);
  152. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  153. EXPECT_EQ(200, res->status);
  154. }
  155. {
  156. httplib::Headers headers = {httplib::make_range_header(1)};
  157. auto res = cli.Get("/range/32", headers);
  158. ASSERT_TRUE(res != nullptr);
  159. EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef");
  160. EXPECT_EQ(206, res->status);
  161. }
  162. {
  163. httplib::Headers headers = {httplib::make_range_header(1, 10)};
  164. auto res = cli.Get("/range/32", headers);
  165. ASSERT_TRUE(res != nullptr);
  166. EXPECT_EQ(res->body, "bcdefghijk");
  167. EXPECT_EQ(206, res->status);
  168. }
  169. }
  170. TEST(ConnectionErrorTest, InvalidHost) {
  171. auto host = "abcde.com";
  172. auto sec = 2;
  173. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  174. auto port = 443;
  175. httplib::SSLClient cli(host, port, sec);
  176. #else
  177. auto port = 80;
  178. httplib::Client cli(host, port, sec);
  179. #endif
  180. auto res = cli.Get("/");
  181. ASSERT_TRUE(res == nullptr);
  182. }
  183. TEST(ConnectionErrorTest, InvalidPort) {
  184. auto host = "localhost";
  185. auto sec = 2;
  186. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  187. auto port = 44380;
  188. httplib::SSLClient cli(host, port, sec);
  189. #else
  190. auto port = 8080;
  191. httplib::Client cli(host, port, sec);
  192. #endif
  193. auto res = cli.Get("/");
  194. ASSERT_TRUE(res == nullptr);
  195. }
  196. TEST(ConnectionErrorTest, Timeout) {
  197. auto host = "google.com";
  198. auto sec = 2;
  199. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  200. auto port = 44380;
  201. httplib::SSLClient cli(host, port, sec);
  202. #else
  203. auto port = 8080;
  204. httplib::Client cli(host, port, sec);
  205. #endif
  206. auto res = cli.Get("/");
  207. ASSERT_TRUE(res == nullptr);
  208. }
  209. TEST(CancelTest, NoCancel) {
  210. auto host = "httpbin.org";
  211. auto sec = 5;
  212. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  213. auto port = 443;
  214. httplib::SSLClient cli(host, port, sec);
  215. #else
  216. auto port = 80;
  217. httplib::Client cli(host, port, sec);
  218. #endif
  219. auto res =
  220. cli.Get("/range/32", [](uint64_t, uint64_t) { return true; });
  221. ASSERT_TRUE(res != nullptr);
  222. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  223. EXPECT_EQ(200, res->status);
  224. }
  225. TEST(CancelTest, WithCancelSmallPayload) {
  226. auto host = "httpbin.org";
  227. auto sec = 5;
  228. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  229. auto port = 443;
  230. httplib::SSLClient cli(host, port, sec);
  231. #else
  232. auto port = 80;
  233. httplib::Client cli(host, port, sec);
  234. #endif
  235. auto res =
  236. cli.Get("/range/32", [](uint64_t, uint64_t) { return false; });
  237. ASSERT_TRUE(res == nullptr);
  238. }
  239. TEST(CancelTest, WithCancelLargePayload) {
  240. auto host = "httpbin.org";
  241. auto sec = 5;
  242. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  243. auto port = 443;
  244. httplib::SSLClient cli(host, port, sec);
  245. #else
  246. auto port = 80;
  247. httplib::Client cli(host, port, sec);
  248. #endif
  249. uint32_t count = 0;
  250. httplib::Headers headers;
  251. auto res = cli.Get("/range/65536", headers,
  252. [&count](uint64_t, uint64_t) { return (count++ == 0); });
  253. ASSERT_TRUE(res == nullptr);
  254. }
  255. TEST(BaseAuthTest, FromHTTPWatch) {
  256. auto host = "httpbin.org";
  257. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  258. auto port = 443;
  259. httplib::SSLClient cli(host, port);
  260. #else
  261. auto port = 80;
  262. httplib::Client cli(host, port);
  263. #endif
  264. {
  265. auto res = cli.Get("/basic-auth/hello/world");
  266. ASSERT_TRUE(res != nullptr);
  267. EXPECT_EQ(401, res->status);
  268. }
  269. {
  270. auto res = cli.Get("/basic-auth/hello/world", {
  271. httplib::make_basic_authentication_header("hello", "world")
  272. });
  273. ASSERT_TRUE(res != nullptr);
  274. EXPECT_EQ(res->body,
  275. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  276. EXPECT_EQ(200, res->status);
  277. }
  278. }
  279. TEST(Server, BindAndListenSeparately) {
  280. Server svr;
  281. int port = svr.bind_to_any_port("localhost");
  282. ASSERT_TRUE(port > 0);
  283. svr.stop();
  284. }
  285. class ServerTest : public ::testing::Test {
  286. protected:
  287. ServerTest()
  288. : cli_(HOST, PORT)
  289. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  290. ,
  291. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  292. #endif
  293. {
  294. }
  295. virtual void SetUp() {
  296. svr_.set_base_dir("./www");
  297. svr_.Get("/hi",
  298. [&](const Request & /*req*/, Response &res) {
  299. res.set_content("Hello World!", "text/plain");
  300. })
  301. .Get("/slow",
  302. [&](const Request & /*req*/, Response &res) {
  303. msleep(2000);
  304. res.set_content("slow", "text/plain");
  305. })
  306. .Get("/remote_addr",
  307. [&](const Request &req, Response &res) {
  308. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  309. res.set_content(remote_addr.c_str(), "text/plain");
  310. })
  311. .Get("/endwith%",
  312. [&](const Request & /*req*/, Response &res) {
  313. res.set_content("Hello World!", "text/plain");
  314. })
  315. .Get("/", [&](const Request & /*req*/,
  316. Response &res) { res.set_redirect("/hi"); })
  317. .Post("/person",
  318. [&](const Request &req, Response &res) {
  319. if (req.has_param("name") && req.has_param("note")) {
  320. persons_[req.get_param_value("name")] =
  321. req.get_param_value("note");
  322. } else {
  323. res.status = 400;
  324. }
  325. })
  326. .Get("/person/(.*)",
  327. [&](const Request &req, Response &res) {
  328. string name = req.matches[1];
  329. if (persons_.find(name) != persons_.end()) {
  330. auto note = persons_[name];
  331. res.set_content(note, "text/plain");
  332. } else {
  333. res.status = 404;
  334. }
  335. })
  336. .Post("/x-www-form-urlencoded-json",
  337. [&](const Request &req, Response &res) {
  338. auto json = req.get_param_value("json");
  339. ASSERT_EQ(JSON_DATA, json);
  340. res.set_content(json, "appliation/json");
  341. res.status = 200;
  342. })
  343. .Get("/streamedchunked",
  344. [&](const Request & /*req*/, Response &res) {
  345. res.content_producer = [](uint64_t offset) {
  346. if (offset < 3) return "a";
  347. if (offset < 6) return "b";
  348. return "";
  349. };
  350. })
  351. .Get("/streamed",
  352. [&](const Request & /*req*/, Response &res) {
  353. res.set_header("Content-Length", "6");
  354. res.content_producer = [](uint64_t offset) {
  355. if (offset < 3) return "a";
  356. if (offset < 6) return "b";
  357. return "";
  358. };
  359. })
  360. .Post("/chunked",
  361. [&](const Request &req, Response & /*res*/) {
  362. EXPECT_EQ(req.body, "dechunked post body");
  363. })
  364. .Post("/largechunked",
  365. [&](const Request &req, Response & /*res*/) {
  366. std::string expected(6 * 30 * 1024u, 'a');
  367. EXPECT_EQ(req.body, expected);
  368. })
  369. .Post("/multipart",
  370. [&](const Request &req, Response & /*res*/) {
  371. EXPECT_EQ(5u, req.files.size());
  372. ASSERT_TRUE(!req.has_file("???"));
  373. {
  374. const auto &file = req.get_file_value("text1");
  375. EXPECT_EQ("", file.filename);
  376. EXPECT_EQ("text default",
  377. req.body.substr(file.offset, file.length));
  378. }
  379. {
  380. const auto &file = req.get_file_value("text2");
  381. EXPECT_EQ("", file.filename);
  382. EXPECT_EQ("aωb", req.body.substr(file.offset, file.length));
  383. }
  384. {
  385. const auto &file = req.get_file_value("file1");
  386. EXPECT_EQ("hello.txt", file.filename);
  387. EXPECT_EQ("text/plain", file.content_type);
  388. EXPECT_EQ("h\ne\n\nl\nl\no\n",
  389. req.body.substr(file.offset, file.length));
  390. }
  391. {
  392. const auto &file = req.get_file_value("file3");
  393. EXPECT_EQ("", file.filename);
  394. EXPECT_EQ("application/octet-stream", file.content_type);
  395. EXPECT_EQ(0u, file.length);
  396. }
  397. })
  398. .Post("/empty",
  399. [&](const Request &req, Response &res) {
  400. EXPECT_EQ(req.body, "");
  401. res.set_content("empty", "text/plain");
  402. })
  403. .Put("/put",
  404. [&](const Request &req, Response &res) {
  405. EXPECT_EQ(req.body, "PUT");
  406. res.set_content(req.body, "text/plain");
  407. })
  408. .Patch("/patch",
  409. [&](const Request &req, Response &res) {
  410. EXPECT_EQ(req.body, "PATCH");
  411. res.set_content(req.body, "text/plain");
  412. })
  413. .Delete("/delete",
  414. [&](const Request & /*req*/, Response &res) {
  415. res.set_content("DELETE", "text/plain");
  416. })
  417. .Options(R"(\*)",
  418. [&](const Request & /*req*/, Response &res) {
  419. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  420. })
  421. .Get("/request-target",
  422. [&](const Request &req, Response & /*res*/) {
  423. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  424. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  425. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  426. })
  427. .Get("/long-query-value",
  428. [&](const Request &req, Response & /*res*/) {
  429. EXPECT_EQ(LONG_QUERY_URL, req.target);
  430. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  431. })
  432. .Get("/array-param",
  433. [&](const Request &req, Response & /*res*/) {
  434. EXPECT_EQ(3u, req.get_param_value_count("array"));
  435. EXPECT_EQ("value1", req.get_param_value("array", 0));
  436. EXPECT_EQ("value2", req.get_param_value("array", 1));
  437. EXPECT_EQ("value3", req.get_param_value("array", 2));
  438. })
  439. .Post("/validate-no-multiple-headers",
  440. [&](const Request &req, Response & /*res*/) {
  441. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  442. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  443. })
  444. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  445. .Get("/gzip",
  446. [&](const Request & /*req*/, Response &res) {
  447. res.set_content(
  448. "12345678901234567890123456789012345678901234567890123456789"
  449. "01234567890123456789012345678901234567890",
  450. "text/plain");
  451. })
  452. .Get("/nogzip",
  453. [&](const Request & /*req*/, Response &res) {
  454. res.set_content(
  455. "12345678901234567890123456789012345678901234567890123456789"
  456. "01234567890123456789012345678901234567890",
  457. "application/octet-stream");
  458. })
  459. .Post("/gzipmultipart",
  460. [&](const Request &req, Response & /*res*/) {
  461. EXPECT_EQ(2u, req.files.size());
  462. ASSERT_TRUE(!req.has_file("???"));
  463. {
  464. const auto &file = req.get_file_value("key1");
  465. EXPECT_EQ("", file.filename);
  466. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  467. }
  468. {
  469. const auto &file = req.get_file_value("key2");
  470. EXPECT_EQ("", file.filename);
  471. EXPECT_EQ("--abcdefg123",
  472. req.body.substr(file.offset, file.length));
  473. }
  474. })
  475. #endif
  476. ;
  477. persons_["john"] = "programmer";
  478. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  479. while (!svr_.is_running()) {
  480. msleep(1);
  481. }
  482. }
  483. virtual void TearDown() {
  484. svr_.stop();
  485. for (auto &t : request_threads_) {
  486. t.join();
  487. }
  488. t_.join();
  489. }
  490. map<string, string> persons_;
  491. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  492. SSLClient cli_;
  493. SSLServer svr_;
  494. #else
  495. Client cli_;
  496. Server svr_;
  497. #endif
  498. thread t_;
  499. std::vector<thread> request_threads_;
  500. };
  501. TEST_F(ServerTest, GetMethod200) {
  502. auto res = cli_.Get("/hi");
  503. ASSERT_TRUE(res != nullptr);
  504. EXPECT_EQ("HTTP/1.1", res->version);
  505. EXPECT_EQ(200, res->status);
  506. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  507. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  508. EXPECT_EQ("close", res->get_header_value("Connection"));
  509. EXPECT_EQ("Hello World!", res->body);
  510. }
  511. TEST_F(ServerTest, GetMethod302) {
  512. auto res = cli_.Get("/");
  513. ASSERT_TRUE(res != nullptr);
  514. EXPECT_EQ(302, res->status);
  515. EXPECT_EQ("/hi", res->get_header_value("Location"));
  516. }
  517. TEST_F(ServerTest, GetMethod404) {
  518. auto res = cli_.Get("/invalid");
  519. ASSERT_TRUE(res != nullptr);
  520. EXPECT_EQ(404, res->status);
  521. }
  522. TEST_F(ServerTest, HeadMethod200) {
  523. auto res = cli_.Head("/hi");
  524. ASSERT_TRUE(res != nullptr);
  525. EXPECT_EQ(200, res->status);
  526. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  527. EXPECT_EQ("", res->body);
  528. }
  529. TEST_F(ServerTest, HeadMethod404) {
  530. auto res = cli_.Head("/invalid");
  531. ASSERT_TRUE(res != nullptr);
  532. EXPECT_EQ(404, res->status);
  533. EXPECT_EQ("", res->body);
  534. }
  535. TEST_F(ServerTest, GetMethodPersonJohn) {
  536. auto res = cli_.Get("/person/john");
  537. ASSERT_TRUE(res != nullptr);
  538. EXPECT_EQ(200, res->status);
  539. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  540. EXPECT_EQ("programmer", res->body);
  541. }
  542. TEST_F(ServerTest, PostMethod1) {
  543. auto res = cli_.Get("/person/john1");
  544. ASSERT_TRUE(res != nullptr);
  545. ASSERT_EQ(404, res->status);
  546. res = cli_.Post("/person", "name=john1&note=coder",
  547. "application/x-www-form-urlencoded");
  548. ASSERT_TRUE(res != nullptr);
  549. ASSERT_EQ(200, res->status);
  550. res = cli_.Get("/person/john1");
  551. ASSERT_TRUE(res != nullptr);
  552. ASSERT_EQ(200, res->status);
  553. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  554. ASSERT_EQ("coder", res->body);
  555. }
  556. TEST_F(ServerTest, PostMethod2) {
  557. auto res = cli_.Get("/person/john2");
  558. ASSERT_TRUE(res != nullptr);
  559. ASSERT_EQ(404, res->status);
  560. Params params;
  561. params.emplace("name", "john2");
  562. params.emplace("note", "coder");
  563. res = cli_.Post("/person", params);
  564. ASSERT_TRUE(res != nullptr);
  565. ASSERT_EQ(200, res->status);
  566. res = cli_.Get("/person/john2");
  567. ASSERT_TRUE(res != nullptr);
  568. ASSERT_EQ(200, res->status);
  569. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  570. ASSERT_EQ("coder", res->body);
  571. }
  572. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  573. Params params;
  574. params.emplace("json", JSON_DATA);
  575. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  576. ASSERT_TRUE(res != nullptr);
  577. ASSERT_EQ(200, res->status);
  578. ASSERT_EQ(JSON_DATA, res->body);
  579. }
  580. TEST_F(ServerTest, PostEmptyContent) {
  581. auto res = cli_.Post("/empty", "", "text/plain");
  582. ASSERT_TRUE(res != nullptr);
  583. ASSERT_EQ(200, res->status);
  584. ASSERT_EQ("empty", res->body);
  585. }
  586. TEST_F(ServerTest, GetMethodDir) {
  587. auto res = cli_.Get("/dir/");
  588. ASSERT_TRUE(res != nullptr);
  589. EXPECT_EQ(200, res->status);
  590. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  591. auto body = R"(<html>
  592. <head>
  593. </head>
  594. <body>
  595. <a href="/dir/test.html">Test</a>
  596. <a href="/hi">hi</a>
  597. </body>
  598. </html>
  599. )";
  600. EXPECT_EQ(body, res->body);
  601. }
  602. TEST_F(ServerTest, GetMethodDirTest) {
  603. auto res = cli_.Get("/dir/test.html");
  604. ASSERT_TRUE(res != nullptr);
  605. EXPECT_EQ(200, res->status);
  606. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  607. EXPECT_EQ("test.html", res->body);
  608. }
  609. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  610. auto res = cli_.Get("/dir/../dir/test.html");
  611. ASSERT_TRUE(res != nullptr);
  612. EXPECT_EQ(200, res->status);
  613. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  614. EXPECT_EQ("test.html", res->body);
  615. }
  616. TEST_F(ServerTest, GetMethodInvalidPath) {
  617. auto res = cli_.Get("/dir/../test.html");
  618. ASSERT_TRUE(res != nullptr);
  619. EXPECT_EQ(404, res->status);
  620. }
  621. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  622. auto res = cli_.Get("/../www/dir/test.html");
  623. ASSERT_TRUE(res != nullptr);
  624. EXPECT_EQ(404, res->status);
  625. }
  626. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  627. auto res = cli_.Get("/dir/../../www/dir/test.html");
  628. ASSERT_TRUE(res != nullptr);
  629. EXPECT_EQ(404, res->status);
  630. }
  631. TEST_F(ServerTest, InvalidBaseDir) {
  632. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  633. EXPECT_EQ(true, svr_.set_base_dir("."));
  634. }
  635. TEST_F(ServerTest, EmptyRequest) {
  636. auto res = cli_.Get("");
  637. ASSERT_TRUE(res == nullptr);
  638. }
  639. TEST_F(ServerTest, LongRequest) {
  640. auto res =
  641. cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  642. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  643. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  644. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  645. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  646. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  647. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  648. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  649. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  650. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  651. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  652. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  653. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  654. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  655. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  656. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  657. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  658. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  659. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  660. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  661. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  662. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  663. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  664. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  665. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  666. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  667. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  668. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  669. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  670. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  671. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  672. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  673. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  674. "TooLongRequest/TooLongRequest/TooLongRequest/__ok__");
  675. ASSERT_TRUE(res != nullptr);
  676. EXPECT_EQ(404, res->status);
  677. }
  678. TEST_F(ServerTest, TooLongRequest) {
  679. auto res =
  680. cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  681. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  682. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  683. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  684. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  685. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  686. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  687. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  688. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  689. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  690. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  691. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  692. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  693. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  694. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  695. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  696. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  697. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  698. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  699. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  700. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  701. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  702. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  703. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  704. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  705. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  706. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  707. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  708. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  709. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  710. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  711. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  712. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  713. "TooLongRequest/TooLongRequest/TooLongRequest/__ng___");
  714. ASSERT_TRUE(res != nullptr);
  715. EXPECT_EQ(404, res->status);
  716. }
  717. TEST_F(ServerTest, LongHeader) {
  718. Request req;
  719. req.method = "GET";
  720. req.path = "/hi";
  721. std::string host_and_port;
  722. host_and_port += HOST;
  723. host_and_port += ":";
  724. host_and_port += std::to_string(PORT);
  725. req.headers.emplace("Host", host_and_port.c_str());
  726. req.headers.emplace("Accept", "*/*");
  727. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  728. req.headers.emplace(
  729. "Header-Name",
  730. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  731. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  732. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  733. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  734. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  735. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  736. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  737. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  738. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  739. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  740. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  741. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  742. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  743. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  744. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  745. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  746. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  747. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  748. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  749. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  750. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  751. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  752. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  753. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  754. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  755. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  756. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  757. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  758. "@@@@@@@@@@@@@@@@");
  759. auto res = std::make_shared<Response>();
  760. auto ret = cli_.send(req, *res);
  761. ASSERT_TRUE(ret);
  762. EXPECT_EQ(200, res->status);
  763. }
  764. TEST_F(ServerTest, LongQueryValue) {
  765. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  766. ASSERT_TRUE(res != nullptr);
  767. EXPECT_EQ(414, res->status);
  768. }
  769. TEST_F(ServerTest, TooLongHeader) {
  770. Request req;
  771. req.method = "GET";
  772. req.path = "/hi";
  773. std::string host_and_port;
  774. host_and_port += HOST;
  775. host_and_port += ":";
  776. host_and_port += std::to_string(PORT);
  777. req.headers.emplace("Host", host_and_port.c_str());
  778. req.headers.emplace("Accept", "*/*");
  779. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  780. req.headers.emplace(
  781. "Header-Name",
  782. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  783. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  784. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  785. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  786. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  787. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  788. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  789. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  790. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  791. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  792. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  793. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  794. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  795. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  796. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  797. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  798. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  799. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  800. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  801. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  802. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  803. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  804. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  805. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  806. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  807. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  808. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  809. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  810. "@@@@@@@@@@@@@@@@@");
  811. auto res = std::make_shared<Response>();
  812. auto ret = cli_.send(req, *res);
  813. ASSERT_TRUE(ret);
  814. EXPECT_EQ(200, res->status);
  815. }
  816. TEST_F(ServerTest, PercentEncoding) {
  817. auto res = cli_.Get("/e%6edwith%");
  818. ASSERT_TRUE(res != nullptr);
  819. EXPECT_EQ(200, res->status);
  820. }
  821. TEST_F(ServerTest, PercentEncodingUnicode) {
  822. auto res = cli_.Get("/e%u006edwith%");
  823. ASSERT_TRUE(res != nullptr);
  824. EXPECT_EQ(200, res->status);
  825. }
  826. TEST_F(ServerTest, InvalidPercentEncoding) {
  827. auto res = cli_.Get("/%endwith%");
  828. ASSERT_TRUE(res != nullptr);
  829. EXPECT_EQ(404, res->status);
  830. }
  831. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  832. auto res = cli_.Get("/%uendwith%");
  833. ASSERT_TRUE(res != nullptr);
  834. EXPECT_EQ(404, res->status);
  835. }
  836. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  837. auto res = cli_.Get("/hello?aaa=bbb%");
  838. ASSERT_TRUE(res != nullptr);
  839. EXPECT_EQ(404, res->status);
  840. }
  841. TEST_F(ServerTest, MultipartFormData) {
  842. MultipartFormDataItems items = {
  843. { "text1", "text default", "", "" },
  844. { "text2", "aωb", "", "" },
  845. { "file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain" },
  846. { "file2", "{\n \"world\", true\n}\n", "world.json", "application/json" },
  847. { "file3", "", "", "application/octet-stream" },
  848. };
  849. auto res = cli_.Post("/multipart", items);
  850. ASSERT_TRUE(res != nullptr);
  851. EXPECT_EQ(200, res->status);
  852. }
  853. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  854. auto res = cli_.Get("/hi");
  855. ASSERT_TRUE(res != nullptr);
  856. EXPECT_EQ(200, res->status);
  857. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  858. EXPECT_EQ("Hello World!", res->body);
  859. }
  860. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  861. Request req;
  862. req.method = "POST";
  863. req.path = "/chunked";
  864. std::string host_and_port;
  865. host_and_port += HOST;
  866. host_and_port += ":";
  867. host_and_port += std::to_string(PORT);
  868. req.headers.emplace("Host", host_and_port.c_str());
  869. req.headers.emplace("Accept", "*/*");
  870. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  871. req.headers.emplace("Content-Type", "text/plain");
  872. req.headers.emplace("Content-Length", "0");
  873. req.headers.emplace(
  874. "Transfer-Encoding",
  875. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  876. // Client does not chunk, so make a chunked body manually.
  877. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  878. auto res = std::make_shared<Response>();
  879. auto ret = cli_.send(req, *res);
  880. ASSERT_TRUE(ret);
  881. EXPECT_EQ(200, res->status);
  882. }
  883. TEST_F(ServerTest, GetStreamed) {
  884. auto res = cli_.Get("/streamed");
  885. ASSERT_TRUE(res != nullptr);
  886. EXPECT_EQ(200, res->status);
  887. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  888. EXPECT_TRUE(res->body == "aaabbb");
  889. }
  890. TEST_F(ServerTest, GetStreamedChunked) {
  891. auto res = cli_.Get("/streamedchunked");
  892. ASSERT_TRUE(res != nullptr);
  893. EXPECT_EQ(200, res->status);
  894. EXPECT_TRUE(res->body == "aaabbb");
  895. }
  896. TEST_F(ServerTest, LargeChunkedPost) {
  897. Request req;
  898. req.method = "POST";
  899. req.path = "/largechunked";
  900. std::string host_and_port;
  901. host_and_port += HOST;
  902. host_and_port += ":";
  903. host_and_port += std::to_string(PORT);
  904. req.headers.emplace("Host", host_and_port.c_str());
  905. req.headers.emplace("Accept", "*/*");
  906. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  907. req.headers.emplace("Content-Type", "text/plain");
  908. req.headers.emplace("Content-Length", "0");
  909. req.headers.emplace("Transfer-Encoding", "chunked");
  910. std::string long_string(30 * 1024u, 'a');
  911. std::string chunk = "7800\r\n" + long_string + "\r\n";
  912. // Attempt to make a large enough post to exceed OS buffers, to test that
  913. // the server handles short reads if the full chunk data isn't available.
  914. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  915. auto res = std::make_shared<Response>();
  916. auto ret = cli_.send(req, *res);
  917. ASSERT_TRUE(ret);
  918. EXPECT_EQ(200, res->status);
  919. }
  920. TEST_F(ServerTest, GetMethodRemoteAddr) {
  921. auto res = cli_.Get("/remote_addr");
  922. ASSERT_TRUE(res != nullptr);
  923. EXPECT_EQ(200, res->status);
  924. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  925. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  926. }
  927. TEST_F(ServerTest, SlowRequest) {
  928. request_threads_.push_back(
  929. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  930. request_threads_.push_back(
  931. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  932. request_threads_.push_back(
  933. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  934. msleep(100);
  935. }
  936. TEST_F(ServerTest, Put) {
  937. auto res = cli_.Put("/put", "PUT", "text/plain");
  938. ASSERT_TRUE(res != nullptr);
  939. EXPECT_EQ(200, res->status);
  940. EXPECT_EQ("PUT", res->body);
  941. }
  942. TEST_F(ServerTest, Patch) {
  943. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  944. ASSERT_TRUE(res != nullptr);
  945. EXPECT_EQ(200, res->status);
  946. EXPECT_EQ("PATCH", res->body);
  947. }
  948. TEST_F(ServerTest, Delete) {
  949. auto res = cli_.Delete("/delete");
  950. ASSERT_TRUE(res != nullptr);
  951. EXPECT_EQ(200, res->status);
  952. EXPECT_EQ("DELETE", res->body);
  953. }
  954. TEST_F(ServerTest, Options) {
  955. auto res = cli_.Options("*");
  956. ASSERT_TRUE(res != nullptr);
  957. EXPECT_EQ(200, res->status);
  958. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  959. EXPECT_TRUE(res->body.empty());
  960. }
  961. TEST_F(ServerTest, URL) {
  962. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  963. ASSERT_TRUE(res != nullptr);
  964. EXPECT_EQ(200, res->status);
  965. }
  966. TEST_F(ServerTest, ArrayParam) {
  967. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  968. ASSERT_TRUE(res != nullptr);
  969. EXPECT_EQ(200, res->status);
  970. }
  971. TEST_F(ServerTest, NoMultipleHeaders) {
  972. Headers headers = {
  973. { "Content-Length", "5" }
  974. };
  975. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  976. "text/plain");
  977. ASSERT_TRUE(res != nullptr);
  978. EXPECT_EQ(200, res->status);
  979. }
  980. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  981. TEST_F(ServerTest, Gzip) {
  982. Headers headers;
  983. headers.emplace("Accept-Encoding", "gzip, deflate");
  984. auto res = cli_.Get("/gzip", headers);
  985. ASSERT_TRUE(res != nullptr);
  986. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  987. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  988. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  989. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  990. "7890123456789012345678901234567890",
  991. res->body);
  992. EXPECT_EQ(200, res->status);
  993. }
  994. TEST_F(ServerTest, GzipWithContentReceiver) {
  995. Headers headers;
  996. headers.emplace("Accept-Encoding", "gzip, deflate");
  997. std::string body;
  998. auto res = cli_.Get("/gzip", headers, [&](const char *data, size_t len) {
  999. body.append(data, len);
  1000. });
  1001. ASSERT_TRUE(res != nullptr);
  1002. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1003. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1004. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1005. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1006. "7890123456789012345678901234567890",
  1007. body);
  1008. EXPECT_EQ(200, res->status);
  1009. }
  1010. TEST_F(ServerTest, NoGzip) {
  1011. Headers headers;
  1012. headers.emplace("Accept-Encoding", "gzip, deflate");
  1013. auto res = cli_.Get("/nogzip", headers);
  1014. ASSERT_TRUE(res != nullptr);
  1015. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1016. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1017. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1018. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1019. "7890123456789012345678901234567890",
  1020. res->body);
  1021. EXPECT_EQ(200, res->status);
  1022. }
  1023. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1024. Headers headers;
  1025. headers.emplace("Accept-Encoding", "gzip, deflate");
  1026. std::string body;
  1027. auto res = cli_.Get("/nogzip", headers, [&](const char *data, size_t len) {
  1028. body.append(data, len);
  1029. });
  1030. ASSERT_TRUE(res != nullptr);
  1031. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1032. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1033. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1034. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1035. "7890123456789012345678901234567890",
  1036. body);
  1037. EXPECT_EQ(200, res->status);
  1038. }
  1039. TEST_F(ServerTest, MultipartFormDataGzip) {
  1040. Request req;
  1041. req.method = "POST";
  1042. req.path = "/gzipmultipart";
  1043. std::string host_and_port;
  1044. host_and_port += HOST;
  1045. host_and_port += ":";
  1046. host_and_port += std::to_string(PORT);
  1047. req.headers.emplace("Host", host_and_port.c_str());
  1048. req.headers.emplace("Accept", "*/*");
  1049. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1050. req.headers.emplace(
  1051. "Content-Type",
  1052. "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  1053. req.headers.emplace("Content-Encoding", "gzip");
  1054. // compressed_body generated by creating input.txt to this file:
  1055. /*
  1056. --------------------------fcba8368a9f48c0f
  1057. Content-Disposition: form-data; name="key1"
  1058. test
  1059. --------------------------fcba8368a9f48c0f
  1060. Content-Disposition: form-data; name="key2"
  1061. --abcdefg123
  1062. --------------------------fcba8368a9f48c0f--
  1063. */
  1064. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  1065. uint8_t compressed_body[] = {
  1066. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  1067. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  1068. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  1069. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  1070. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  1071. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  1072. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  1073. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  1074. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  1075. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  1076. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00};
  1077. req.body = std::string((char *)compressed_body,
  1078. sizeof(compressed_body) / sizeof(compressed_body[0]));
  1079. auto res = std::make_shared<Response>();
  1080. auto ret = cli_.send(req, *res);
  1081. ASSERT_TRUE(ret);
  1082. EXPECT_EQ(200, res->status);
  1083. }
  1084. #endif
  1085. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1086. protected:
  1087. ServerTestWithAI_PASSIVE()
  1088. : cli_(HOST, PORT)
  1089. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1090. ,
  1091. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1092. #endif
  1093. {
  1094. }
  1095. virtual void SetUp() {
  1096. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1097. res.set_content("Hello World!", "text/plain");
  1098. });
  1099. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1100. while (!svr_.is_running()) {
  1101. msleep(1);
  1102. }
  1103. }
  1104. virtual void TearDown() {
  1105. svr_.stop();
  1106. t_.join();
  1107. }
  1108. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1109. SSLClient cli_;
  1110. SSLServer svr_;
  1111. #else
  1112. Client cli_;
  1113. Server svr_;
  1114. #endif
  1115. thread t_;
  1116. };
  1117. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1118. auto res = cli_.Get("/hi");
  1119. ASSERT_TRUE(res != nullptr);
  1120. EXPECT_EQ(200, res->status);
  1121. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1122. EXPECT_EQ("Hello World!", res->body);
  1123. }
  1124. class ServerUpDownTest : public ::testing::Test {
  1125. protected:
  1126. ServerUpDownTest() : cli_(HOST, PORT) {}
  1127. virtual void SetUp() {
  1128. t_ = thread([&]() {
  1129. svr_.bind_to_any_port(HOST);
  1130. msleep(500);
  1131. ASSERT_TRUE(svr_.listen_after_bind());
  1132. });
  1133. while (!svr_.is_running()) {
  1134. msleep(1);
  1135. }
  1136. }
  1137. virtual void TearDown() {
  1138. svr_.stop();
  1139. t_.join();
  1140. }
  1141. Client cli_;
  1142. Server svr_;
  1143. thread t_;
  1144. };
  1145. TEST_F(ServerUpDownTest, QuickStartStop) {
  1146. // Should not crash, especially when run with
  1147. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1148. }
  1149. class PayloadMaxLengthTest : public ::testing::Test {
  1150. protected:
  1151. PayloadMaxLengthTest()
  1152. : cli_(HOST, PORT)
  1153. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1154. ,
  1155. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1156. #endif
  1157. {
  1158. }
  1159. virtual void SetUp() {
  1160. svr_.set_payload_max_length(8);
  1161. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1162. res.set_content("test", "text/plain");
  1163. });
  1164. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1165. while (!svr_.is_running()) {
  1166. msleep(1);
  1167. }
  1168. }
  1169. virtual void TearDown() {
  1170. svr_.stop();
  1171. t_.join();
  1172. }
  1173. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1174. SSLClient cli_;
  1175. SSLServer svr_;
  1176. #else
  1177. Client cli_;
  1178. Server svr_;
  1179. #endif
  1180. thread t_;
  1181. };
  1182. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1183. auto res = cli_.Post("/test", "123456789", "text/plain");
  1184. ASSERT_TRUE(res != nullptr);
  1185. EXPECT_EQ(413, res->status);
  1186. res = cli_.Post("/test", "12345678", "text/plain");
  1187. ASSERT_TRUE(res != nullptr);
  1188. EXPECT_EQ(200, res->status);
  1189. }
  1190. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1191. TEST(SSLClientTest, ServerNameIndication) {
  1192. SSLClient cli("httpbin.org", 443);
  1193. auto res = cli.Get("/get");
  1194. ASSERT_TRUE(res != nullptr);
  1195. ASSERT_EQ(200, res->status);
  1196. }
  1197. TEST(SSLClientTest, ServerCertificateVerification) {
  1198. SSLClient cli("google.com");
  1199. auto res = cli.Get("/");
  1200. ASSERT_TRUE(res != nullptr);
  1201. ASSERT_EQ(301, res->status);
  1202. cli.enable_server_certificate_verification(true);
  1203. res = cli.Get("/");
  1204. ASSERT_TRUE(res == nullptr);
  1205. cli.set_ca_cert_path(CA_CERT_FILE);
  1206. res = cli.Get("/");
  1207. ASSERT_TRUE(res != nullptr);
  1208. ASSERT_EQ(301, res->status);
  1209. }
  1210. TEST(SSLClientTest, WildcardHostNameMatch) {
  1211. SSLClient cli("www.youtube.com");
  1212. cli.set_ca_cert_path(CA_CERT_FILE);
  1213. cli.enable_server_certificate_verification(true);
  1214. auto res = cli.Get("/");
  1215. ASSERT_TRUE(res != nullptr);
  1216. ASSERT_EQ(200, res->status);
  1217. }
  1218. TEST(SSLClientServerTest, ClientCertPresent) {
  1219. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1220. CLIENT_CA_CERT_DIR);
  1221. ASSERT_TRUE(svr.is_valid());
  1222. svr.Get("/test", [&](const Request &req, Response &res) {
  1223. res.set_content("test", "text/plain");
  1224. svr.stop();
  1225. ASSERT_TRUE(true);
  1226. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1227. ASSERT_TRUE(peer_cert != nullptr);
  1228. auto subject_name = X509_get_subject_name(peer_cert);
  1229. ASSERT_TRUE(subject_name != nullptr);
  1230. std::string common_name;
  1231. {
  1232. char name[BUFSIZ];
  1233. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1234. name, sizeof(name));
  1235. common_name.assign(name, name_len);
  1236. }
  1237. EXPECT_EQ("Common Name", common_name);
  1238. X509_free(peer_cert);
  1239. });
  1240. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1241. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1242. CLIENT_PRIVATE_KEY_FILE);
  1243. auto res = cli.Get("/test");
  1244. ASSERT_TRUE(res != nullptr);
  1245. ASSERT_EQ(200, res->status);
  1246. t.join();
  1247. }
  1248. TEST(SSLClientServerTest, ClientCertMissing) {
  1249. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1250. CLIENT_CA_CERT_DIR);
  1251. ASSERT_TRUE(svr.is_valid());
  1252. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1253. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1254. httplib::SSLClient cli(HOST, PORT, 30);
  1255. auto res = cli.Get("/test");
  1256. ASSERT_TRUE(res == nullptr);
  1257. svr.stop();
  1258. t.join();
  1259. }
  1260. TEST(SSLClientServerTest, TrustDirOptional) {
  1261. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1262. ASSERT_TRUE(svr.is_valid());
  1263. svr.Get("/test", [&](const Request &, Response &res) {
  1264. res.set_content("test", "text/plain");
  1265. svr.stop();
  1266. });
  1267. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1268. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1269. CLIENT_PRIVATE_KEY_FILE);
  1270. auto res = cli.Get("/test");
  1271. ASSERT_TRUE(res != nullptr);
  1272. ASSERT_EQ(200, res->status);
  1273. t.join();
  1274. }
  1275. /* Cannot test this case as there is no external access to SSL object to check
  1276. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1277. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1278. nullptr, CLIENT_CA_CERT_DIR);
  1279. }
  1280. */
  1281. #endif
  1282. #ifdef _WIN32
  1283. TEST(CleanupTest, WSACleanup) {
  1284. int ret = WSACleanup();
  1285. ASSERT_EQ(0, ret);
  1286. }
  1287. #endif