test.cc 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. httplib::Headers headers;
  220. auto res =
  221. cli.Get("/range/32", headers, [](uint64_t, uint64_t) { return true; });
  222. ASSERT_TRUE(res != nullptr);
  223. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  224. EXPECT_EQ(200, res->status);
  225. }
  226. TEST(CancelTest, WithCancelSmallPayload) {
  227. auto host = "httpbin.org";
  228. auto sec = 5;
  229. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  230. auto port = 443;
  231. httplib::SSLClient cli(host, port, sec);
  232. #else
  233. auto port = 80;
  234. httplib::Client cli(host, port, sec);
  235. #endif
  236. httplib::Headers headers;
  237. auto res =
  238. cli.Get("/range/32", headers, [](uint64_t, uint64_t) { return false; });
  239. ASSERT_TRUE(res == nullptr);
  240. }
  241. TEST(CancelTest, WithCancelLargePayload) {
  242. auto host = "httpbin.org";
  243. auto sec = 5;
  244. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  245. auto port = 443;
  246. httplib::SSLClient cli(host, port, sec);
  247. #else
  248. auto port = 80;
  249. httplib::Client cli(host, port, sec);
  250. #endif
  251. uint32_t count = 0;
  252. httplib::Headers headers;
  253. auto res = cli.Get("/range/65536", headers,
  254. [&count](uint64_t, uint64_t) { return (count++ == 0); });
  255. ASSERT_TRUE(res == nullptr);
  256. }
  257. TEST(BaseAuthTest, FromHTTPWatch) {
  258. auto host = "httpbin.org";
  259. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  260. auto port = 443;
  261. httplib::SSLClient cli(host, port);
  262. #else
  263. auto port = 80;
  264. httplib::Client cli(host, port);
  265. #endif
  266. {
  267. auto res = cli.Get("/basic-auth/hello/world");
  268. ASSERT_TRUE(res != nullptr);
  269. EXPECT_EQ(401, res->status);
  270. }
  271. {
  272. httplib::Headers headers = {{"Authorization", "Basic aGVsbG86d29ybGQ="}};
  273. auto res = cli.Get("/basic-auth/hello/world", headers);
  274. ASSERT_TRUE(res != nullptr);
  275. EXPECT_EQ(res->body,
  276. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  277. EXPECT_EQ(200, res->status);
  278. }
  279. }
  280. TEST(Server, BindAndListenSeparately) {
  281. Server svr;
  282. int port = svr.bind_to_any_port("localhost");
  283. ASSERT_TRUE(port > 0);
  284. svr.stop();
  285. }
  286. class ServerTest : public ::testing::Test {
  287. protected:
  288. ServerTest()
  289. : cli_(HOST, PORT)
  290. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  291. ,
  292. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  293. #endif
  294. {
  295. }
  296. virtual void SetUp() {
  297. svr_.set_base_dir("./www");
  298. svr_.Get("/hi",
  299. [&](const Request & /*req*/, Response &res) {
  300. res.set_content("Hello World!", "text/plain");
  301. })
  302. .Get("/slow",
  303. [&](const Request & /*req*/, Response &res) {
  304. msleep(2000);
  305. res.set_content("slow", "text/plain");
  306. })
  307. .Get("/remote_addr",
  308. [&](const Request &req, Response &res) {
  309. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  310. res.set_content(remote_addr.c_str(), "text/plain");
  311. })
  312. .Get("/endwith%",
  313. [&](const Request & /*req*/, Response &res) {
  314. res.set_content("Hello World!", "text/plain");
  315. })
  316. .Get("/", [&](const Request & /*req*/,
  317. Response &res) { res.set_redirect("/hi"); })
  318. .Post("/person",
  319. [&](const Request &req, Response &res) {
  320. if (req.has_param("name") && req.has_param("note")) {
  321. persons_[req.get_param_value("name")] =
  322. req.get_param_value("note");
  323. } else {
  324. res.status = 400;
  325. }
  326. })
  327. .Get("/person/(.*)",
  328. [&](const Request &req, Response &res) {
  329. string name = req.matches[1];
  330. if (persons_.find(name) != persons_.end()) {
  331. auto note = persons_[name];
  332. res.set_content(note, "text/plain");
  333. } else {
  334. res.status = 404;
  335. }
  336. })
  337. .Post("/x-www-form-urlencoded-json",
  338. [&](const Request &req, Response &res) {
  339. auto json = req.get_param_value("json");
  340. ASSERT_EQ(JSON_DATA, json);
  341. res.set_content(json, "appliation/json");
  342. res.status = 200;
  343. })
  344. .Get("/streamedchunked",
  345. [&](const Request & /*req*/, Response &res) {
  346. res.content_producer = [](uint64_t offset) {
  347. if (offset < 3) return "a";
  348. if (offset < 6) return "b";
  349. return "";
  350. };
  351. })
  352. .Get("/streamed",
  353. [&](const Request & /*req*/, Response &res) {
  354. res.set_header("Content-Length", "6");
  355. res.content_producer = [](uint64_t offset) {
  356. if (offset < 3) return "a";
  357. if (offset < 6) return "b";
  358. return "";
  359. };
  360. })
  361. .Post("/chunked",
  362. [&](const Request &req, Response & /*res*/) {
  363. EXPECT_EQ(req.body, "dechunked post body");
  364. })
  365. .Post("/largechunked",
  366. [&](const Request &req, Response & /*res*/) {
  367. std::string expected(6 * 30 * 1024u, 'a');
  368. EXPECT_EQ(req.body, expected);
  369. })
  370. .Post("/multipart",
  371. [&](const Request &req, Response & /*res*/) {
  372. EXPECT_EQ(5u, req.files.size());
  373. ASSERT_TRUE(!req.has_file("???"));
  374. {
  375. const auto &file = req.get_file_value("text1");
  376. EXPECT_EQ("", file.filename);
  377. EXPECT_EQ("text default",
  378. req.body.substr(file.offset, file.length));
  379. }
  380. {
  381. const auto &file = req.get_file_value("text2");
  382. EXPECT_EQ("", file.filename);
  383. EXPECT_EQ("aωb", req.body.substr(file.offset, file.length));
  384. }
  385. {
  386. const auto &file = req.get_file_value("file1");
  387. EXPECT_EQ("hello.txt", file.filename);
  388. EXPECT_EQ("text/plain", file.content_type);
  389. EXPECT_EQ("h\ne\n\nl\nl\no\n",
  390. req.body.substr(file.offset, file.length));
  391. }
  392. {
  393. const auto &file = req.get_file_value("file3");
  394. EXPECT_EQ("", file.filename);
  395. EXPECT_EQ("application/octet-stream", file.content_type);
  396. EXPECT_EQ(0u, file.length);
  397. }
  398. })
  399. .Post("/empty",
  400. [&](const Request &req, Response &res) {
  401. EXPECT_EQ(req.body, "");
  402. res.set_content("empty", "text/plain");
  403. })
  404. .Put("/put",
  405. [&](const Request &req, Response &res) {
  406. EXPECT_EQ(req.body, "PUT");
  407. res.set_content(req.body, "text/plain");
  408. })
  409. .Patch("/patch",
  410. [&](const Request &req, Response &res) {
  411. EXPECT_EQ(req.body, "PATCH");
  412. res.set_content(req.body, "text/plain");
  413. })
  414. .Delete("/delete",
  415. [&](const Request & /*req*/, Response &res) {
  416. res.set_content("DELETE", "text/plain");
  417. })
  418. .Options(R"(\*)",
  419. [&](const Request & /*req*/, Response &res) {
  420. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  421. })
  422. .Get("/request-target",
  423. [&](const Request &req, Response & /*res*/) {
  424. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  425. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  426. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  427. })
  428. .Get("/long-query-value",
  429. [&](const Request &req, Response & /*res*/) {
  430. EXPECT_EQ(LONG_QUERY_URL, req.target);
  431. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  432. })
  433. .Get("/array-param",
  434. [&](const Request &req, Response & /*res*/) {
  435. EXPECT_EQ(3u, req.get_param_value_count("array"));
  436. EXPECT_EQ("value1", req.get_param_value("array", 0));
  437. EXPECT_EQ("value2", req.get_param_value("array", 1));
  438. EXPECT_EQ("value3", req.get_param_value("array", 2));
  439. })
  440. .Post("/validate-no-multiple-headers",
  441. [&](const Request &req, Response & /*res*/) {
  442. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  443. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  444. })
  445. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  446. .Get("/gzip",
  447. [&](const Request & /*req*/, Response &res) {
  448. res.set_content(
  449. "12345678901234567890123456789012345678901234567890123456789"
  450. "01234567890123456789012345678901234567890",
  451. "text/plain");
  452. })
  453. .Get("/nogzip",
  454. [&](const Request & /*req*/, Response &res) {
  455. res.set_content(
  456. "12345678901234567890123456789012345678901234567890123456789"
  457. "01234567890123456789012345678901234567890",
  458. "application/octet-stream");
  459. })
  460. .Post("/gzipmultipart",
  461. [&](const Request &req, Response & /*res*/) {
  462. EXPECT_EQ(2u, req.files.size());
  463. ASSERT_TRUE(!req.has_file("???"));
  464. {
  465. const auto &file = req.get_file_value("key1");
  466. EXPECT_EQ("", file.filename);
  467. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  468. }
  469. {
  470. const auto &file = req.get_file_value("key2");
  471. EXPECT_EQ("", file.filename);
  472. EXPECT_EQ("--abcdefg123",
  473. req.body.substr(file.offset, file.length));
  474. }
  475. })
  476. #endif
  477. ;
  478. persons_["john"] = "programmer";
  479. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  480. while (!svr_.is_running()) {
  481. msleep(1);
  482. }
  483. }
  484. virtual void TearDown() {
  485. svr_.stop();
  486. for (auto &t : request_threads_) {
  487. t.join();
  488. }
  489. t_.join();
  490. }
  491. map<string, string> persons_;
  492. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  493. SSLClient cli_;
  494. SSLServer svr_;
  495. #else
  496. Client cli_;
  497. Server svr_;
  498. #endif
  499. thread t_;
  500. std::vector<thread> request_threads_;
  501. };
  502. TEST_F(ServerTest, GetMethod200) {
  503. auto res = cli_.Get("/hi");
  504. ASSERT_TRUE(res != nullptr);
  505. EXPECT_EQ("HTTP/1.1", res->version);
  506. EXPECT_EQ(200, res->status);
  507. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  508. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  509. EXPECT_EQ("close", res->get_header_value("Connection"));
  510. EXPECT_EQ("Hello World!", res->body);
  511. }
  512. TEST_F(ServerTest, GetMethod302) {
  513. auto res = cli_.Get("/");
  514. ASSERT_TRUE(res != nullptr);
  515. EXPECT_EQ(302, res->status);
  516. EXPECT_EQ("/hi", res->get_header_value("Location"));
  517. }
  518. TEST_F(ServerTest, GetMethod404) {
  519. auto res = cli_.Get("/invalid");
  520. ASSERT_TRUE(res != nullptr);
  521. EXPECT_EQ(404, res->status);
  522. }
  523. TEST_F(ServerTest, HeadMethod200) {
  524. auto res = cli_.Head("/hi");
  525. ASSERT_TRUE(res != nullptr);
  526. EXPECT_EQ(200, res->status);
  527. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  528. EXPECT_EQ("", res->body);
  529. }
  530. TEST_F(ServerTest, HeadMethod404) {
  531. auto res = cli_.Head("/invalid");
  532. ASSERT_TRUE(res != nullptr);
  533. EXPECT_EQ(404, res->status);
  534. EXPECT_EQ("", res->body);
  535. }
  536. TEST_F(ServerTest, GetMethodPersonJohn) {
  537. auto res = cli_.Get("/person/john");
  538. ASSERT_TRUE(res != nullptr);
  539. EXPECT_EQ(200, res->status);
  540. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  541. EXPECT_EQ("programmer", res->body);
  542. }
  543. TEST_F(ServerTest, PostMethod1) {
  544. auto res = cli_.Get("/person/john1");
  545. ASSERT_TRUE(res != nullptr);
  546. ASSERT_EQ(404, res->status);
  547. res = cli_.Post("/person", "name=john1&note=coder",
  548. "application/x-www-form-urlencoded");
  549. ASSERT_TRUE(res != nullptr);
  550. ASSERT_EQ(200, res->status);
  551. res = cli_.Get("/person/john1");
  552. ASSERT_TRUE(res != nullptr);
  553. ASSERT_EQ(200, res->status);
  554. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  555. ASSERT_EQ("coder", res->body);
  556. }
  557. TEST_F(ServerTest, PostMethod2) {
  558. auto res = cli_.Get("/person/john2");
  559. ASSERT_TRUE(res != nullptr);
  560. ASSERT_EQ(404, res->status);
  561. Params params;
  562. params.emplace("name", "john2");
  563. params.emplace("note", "coder");
  564. res = cli_.Post("/person", params);
  565. ASSERT_TRUE(res != nullptr);
  566. ASSERT_EQ(200, res->status);
  567. res = cli_.Get("/person/john2");
  568. ASSERT_TRUE(res != nullptr);
  569. ASSERT_EQ(200, res->status);
  570. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  571. ASSERT_EQ("coder", res->body);
  572. }
  573. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  574. Params params;
  575. params.emplace("json", JSON_DATA);
  576. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  577. ASSERT_TRUE(res != nullptr);
  578. ASSERT_EQ(200, res->status);
  579. ASSERT_EQ(JSON_DATA, res->body);
  580. }
  581. TEST_F(ServerTest, PostEmptyContent) {
  582. auto res = cli_.Post("/empty", "", "text/plain");
  583. ASSERT_TRUE(res != nullptr);
  584. ASSERT_EQ(200, res->status);
  585. ASSERT_EQ("empty", res->body);
  586. }
  587. TEST_F(ServerTest, GetMethodDir) {
  588. auto res = cli_.Get("/dir/");
  589. ASSERT_TRUE(res != nullptr);
  590. EXPECT_EQ(200, res->status);
  591. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  592. auto body = R"(<html>
  593. <head>
  594. </head>
  595. <body>
  596. <a href="/dir/test.html">Test</a>
  597. <a href="/hi">hi</a>
  598. </body>
  599. </html>
  600. )";
  601. EXPECT_EQ(body, res->body);
  602. }
  603. TEST_F(ServerTest, GetMethodDirTest) {
  604. auto res = cli_.Get("/dir/test.html");
  605. ASSERT_TRUE(res != nullptr);
  606. EXPECT_EQ(200, res->status);
  607. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  608. EXPECT_EQ("test.html", res->body);
  609. }
  610. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  611. auto res = cli_.Get("/dir/../dir/test.html");
  612. ASSERT_TRUE(res != nullptr);
  613. EXPECT_EQ(200, res->status);
  614. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  615. EXPECT_EQ("test.html", res->body);
  616. }
  617. TEST_F(ServerTest, GetMethodInvalidPath) {
  618. auto res = cli_.Get("/dir/../test.html");
  619. ASSERT_TRUE(res != nullptr);
  620. EXPECT_EQ(404, res->status);
  621. }
  622. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  623. auto res = cli_.Get("/../www/dir/test.html");
  624. ASSERT_TRUE(res != nullptr);
  625. EXPECT_EQ(404, res->status);
  626. }
  627. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  628. auto res = cli_.Get("/dir/../../www/dir/test.html");
  629. ASSERT_TRUE(res != nullptr);
  630. EXPECT_EQ(404, res->status);
  631. }
  632. TEST_F(ServerTest, InvalidBaseDir) {
  633. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  634. EXPECT_EQ(true, svr_.set_base_dir("."));
  635. }
  636. TEST_F(ServerTest, EmptyRequest) {
  637. auto res = cli_.Get("");
  638. ASSERT_TRUE(res == nullptr);
  639. }
  640. TEST_F(ServerTest, LongRequest) {
  641. auto res =
  642. cli_.Get("/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/TooLongRequest/"
  675. "TooLongRequest/TooLongRequest/TooLongRequest/__ok__");
  676. ASSERT_TRUE(res != nullptr);
  677. EXPECT_EQ(404, res->status);
  678. }
  679. TEST_F(ServerTest, TooLongRequest) {
  680. auto res =
  681. cli_.Get("/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/TooLongRequest/"
  714. "TooLongRequest/TooLongRequest/TooLongRequest/__ng___");
  715. ASSERT_TRUE(res != nullptr);
  716. EXPECT_EQ(404, res->status);
  717. }
  718. TEST_F(ServerTest, LongHeader) {
  719. Request req;
  720. req.method = "GET";
  721. req.path = "/hi";
  722. std::string host_and_port;
  723. host_and_port += HOST;
  724. host_and_port += ":";
  725. host_and_port += std::to_string(PORT);
  726. req.headers.emplace("Host", host_and_port.c_str());
  727. req.headers.emplace("Accept", "*/*");
  728. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  729. req.headers.emplace(
  730. "Header-Name",
  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. "@@@@@@@@@@@@@@@@");
  760. auto res = std::make_shared<Response>();
  761. auto ret = cli_.send(req, *res);
  762. ASSERT_TRUE(ret);
  763. EXPECT_EQ(200, res->status);
  764. }
  765. TEST_F(ServerTest, LongQueryValue) {
  766. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  767. ASSERT_TRUE(res != nullptr);
  768. EXPECT_EQ(414, res->status);
  769. }
  770. TEST_F(ServerTest, TooLongHeader) {
  771. Request req;
  772. req.method = "GET";
  773. req.path = "/hi";
  774. std::string host_and_port;
  775. host_and_port += HOST;
  776. host_and_port += ":";
  777. host_and_port += std::to_string(PORT);
  778. req.headers.emplace("Host", host_and_port.c_str());
  779. req.headers.emplace("Accept", "*/*");
  780. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  781. req.headers.emplace(
  782. "Header-Name",
  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. "@@@@@@@@@@@@@@@@@");
  812. auto res = std::make_shared<Response>();
  813. auto ret = cli_.send(req, *res);
  814. ASSERT_TRUE(ret);
  815. EXPECT_EQ(200, res->status);
  816. }
  817. TEST_F(ServerTest, PercentEncoding) {
  818. auto res = cli_.Get("/e%6edwith%");
  819. ASSERT_TRUE(res != nullptr);
  820. EXPECT_EQ(200, res->status);
  821. }
  822. TEST_F(ServerTest, PercentEncodingUnicode) {
  823. auto res = cli_.Get("/e%u006edwith%");
  824. ASSERT_TRUE(res != nullptr);
  825. EXPECT_EQ(200, res->status);
  826. }
  827. TEST_F(ServerTest, InvalidPercentEncoding) {
  828. auto res = cli_.Get("/%endwith%");
  829. ASSERT_TRUE(res != nullptr);
  830. EXPECT_EQ(404, res->status);
  831. }
  832. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  833. auto res = cli_.Get("/%uendwith%");
  834. ASSERT_TRUE(res != nullptr);
  835. EXPECT_EQ(404, res->status);
  836. }
  837. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  838. auto res = cli_.Get("/hello?aaa=bbb%");
  839. ASSERT_TRUE(res != nullptr);
  840. EXPECT_EQ(404, res->status);
  841. }
  842. TEST_F(ServerTest, MultipartFormData) {
  843. Request req;
  844. req.method = "POST";
  845. req.path = "/multipart";
  846. std::string host_and_port;
  847. host_and_port += HOST;
  848. host_and_port += ":";
  849. host_and_port += std::to_string(PORT);
  850. req.headers.emplace("Host", host_and_port.c_str());
  851. req.headers.emplace("Accept", "*/*");
  852. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  853. req.headers.emplace(
  854. "Content-Type",
  855. "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4");
  856. req.body =
  857. "------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: "
  858. "form-data; name=\"text1\"\r\n\r\ntext "
  859. "default\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-"
  860. "Disposition: form-data; "
  861. "name=\"text2\"\r\n\r\naωb\r\n------"
  862. "WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; "
  863. "name=\"file1\"; filename=\"hello.txt\"\r\nContent-Type: "
  864. "text/"
  865. "plain\r\n\r\nh\ne\n\nl\nl\no\n\r\n------"
  866. "WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; "
  867. "name=\"file2\"; filename=\"world.json\"\r\nContent-Type: "
  868. "application/json\r\n\r\n{\n \"world\", "
  869. "true\n}\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\ncontent-"
  870. "disposition: form-data; name=\"file3\"; filename=\"\"\r\ncontent-type: "
  871. "application/"
  872. "octet-stream\r\n\r\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4--\r\n";
  873. auto res = std::make_shared<Response>();
  874. auto ret = cli_.send(req, *res);
  875. ASSERT_TRUE(ret);
  876. EXPECT_EQ(200, res->status);
  877. }
  878. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  879. auto res = cli_.Get("/hi");
  880. ASSERT_TRUE(res != nullptr);
  881. EXPECT_EQ(200, res->status);
  882. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  883. EXPECT_EQ("Hello World!", res->body);
  884. }
  885. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  886. Request req;
  887. req.method = "POST";
  888. req.path = "/chunked";
  889. std::string host_and_port;
  890. host_and_port += HOST;
  891. host_and_port += ":";
  892. host_and_port += std::to_string(PORT);
  893. req.headers.emplace("Host", host_and_port.c_str());
  894. req.headers.emplace("Accept", "*/*");
  895. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  896. req.headers.emplace("Content-Type", "text/plain");
  897. req.headers.emplace("Content-Length", "0");
  898. req.headers.emplace(
  899. "Transfer-Encoding",
  900. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  901. // Client does not chunk, so make a chunked body manually.
  902. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  903. auto res = std::make_shared<Response>();
  904. auto ret = cli_.send(req, *res);
  905. ASSERT_TRUE(ret);
  906. EXPECT_EQ(200, res->status);
  907. }
  908. TEST_F(ServerTest, GetStreamed) {
  909. auto res = cli_.Get("/streamed");
  910. ASSERT_TRUE(res != nullptr);
  911. EXPECT_EQ(200, res->status);
  912. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  913. EXPECT_TRUE(res->body == "aaabbb");
  914. }
  915. TEST_F(ServerTest, GetStreamedChunked) {
  916. auto res = cli_.Get("/streamedchunked");
  917. ASSERT_TRUE(res != nullptr);
  918. EXPECT_EQ(200, res->status);
  919. EXPECT_TRUE(res->body == "aaabbb");
  920. }
  921. TEST_F(ServerTest, LargeChunkedPost) {
  922. Request req;
  923. req.method = "POST";
  924. req.path = "/largechunked";
  925. std::string host_and_port;
  926. host_and_port += HOST;
  927. host_and_port += ":";
  928. host_and_port += std::to_string(PORT);
  929. req.headers.emplace("Host", host_and_port.c_str());
  930. req.headers.emplace("Accept", "*/*");
  931. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  932. req.headers.emplace("Content-Type", "text/plain");
  933. req.headers.emplace("Content-Length", "0");
  934. req.headers.emplace("Transfer-Encoding", "chunked");
  935. std::string long_string(30 * 1024u, 'a');
  936. std::string chunk = "7800\r\n" + long_string + "\r\n";
  937. // Attempt to make a large enough post to exceed OS buffers, to test that
  938. // the server handles short reads if the full chunk data isn't available.
  939. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  940. auto res = std::make_shared<Response>();
  941. auto ret = cli_.send(req, *res);
  942. ASSERT_TRUE(ret);
  943. EXPECT_EQ(200, res->status);
  944. }
  945. TEST_F(ServerTest, GetMethodRemoteAddr) {
  946. auto res = cli_.Get("/remote_addr");
  947. ASSERT_TRUE(res != nullptr);
  948. EXPECT_EQ(200, res->status);
  949. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  950. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  951. }
  952. TEST_F(ServerTest, SlowRequest) {
  953. request_threads_.push_back(
  954. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  955. request_threads_.push_back(
  956. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  957. request_threads_.push_back(
  958. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  959. msleep(100);
  960. }
  961. TEST_F(ServerTest, Put) {
  962. auto res = cli_.Put("/put", "PUT", "text/plain");
  963. ASSERT_TRUE(res != nullptr);
  964. EXPECT_EQ(200, res->status);
  965. EXPECT_EQ("PUT", res->body);
  966. }
  967. TEST_F(ServerTest, Patch) {
  968. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  969. ASSERT_TRUE(res != nullptr);
  970. EXPECT_EQ(200, res->status);
  971. EXPECT_EQ("PATCH", res->body);
  972. }
  973. TEST_F(ServerTest, Delete) {
  974. auto res = cli_.Delete("/delete");
  975. ASSERT_TRUE(res != nullptr);
  976. EXPECT_EQ(200, res->status);
  977. EXPECT_EQ("DELETE", res->body);
  978. }
  979. TEST_F(ServerTest, Options) {
  980. auto res = cli_.Options("*");
  981. ASSERT_TRUE(res != nullptr);
  982. EXPECT_EQ(200, res->status);
  983. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  984. EXPECT_TRUE(res->body.empty());
  985. }
  986. TEST_F(ServerTest, URL) {
  987. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  988. ASSERT_TRUE(res != nullptr);
  989. EXPECT_EQ(200, res->status);
  990. }
  991. TEST_F(ServerTest, ArrayParam) {
  992. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  993. ASSERT_TRUE(res != nullptr);
  994. EXPECT_EQ(200, res->status);
  995. }
  996. TEST_F(ServerTest, NoMultipleHeaders) {
  997. Headers headers;
  998. headers.emplace("Content-Length", "5");
  999. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1000. "text/plain");
  1001. ASSERT_TRUE(res != nullptr);
  1002. EXPECT_EQ(200, res->status);
  1003. }
  1004. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1005. TEST_F(ServerTest, Gzip) {
  1006. Headers headers;
  1007. headers.emplace("Accept-Encoding", "gzip, deflate");
  1008. auto res = cli_.Get("/gzip", headers);
  1009. ASSERT_TRUE(res != nullptr);
  1010. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1011. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1012. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1013. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1014. "7890123456789012345678901234567890",
  1015. res->body);
  1016. EXPECT_EQ(200, res->status);
  1017. }
  1018. TEST_F(ServerTest, GzipWithContentReceiver) {
  1019. Headers headers;
  1020. headers.emplace("Accept-Encoding", "gzip, deflate");
  1021. std::string body;
  1022. auto res = cli_.Get("/gzip", headers, [&](const char *data, size_t len) {
  1023. body.append(data, len);
  1024. });
  1025. ASSERT_TRUE(res != nullptr);
  1026. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1027. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1028. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1029. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1030. "7890123456789012345678901234567890",
  1031. body);
  1032. EXPECT_EQ(200, res->status);
  1033. }
  1034. TEST_F(ServerTest, NoGzip) {
  1035. Headers headers;
  1036. headers.emplace("Accept-Encoding", "gzip, deflate");
  1037. auto res = cli_.Get("/nogzip", headers);
  1038. ASSERT_TRUE(res != nullptr);
  1039. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1040. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1041. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1042. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1043. "7890123456789012345678901234567890",
  1044. res->body);
  1045. EXPECT_EQ(200, res->status);
  1046. }
  1047. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1048. Headers headers;
  1049. headers.emplace("Accept-Encoding", "gzip, deflate");
  1050. std::string body;
  1051. auto res = cli_.Get("/nogzip", headers, [&](const char *data, size_t len) {
  1052. body.append(data, len);
  1053. });
  1054. ASSERT_TRUE(res != nullptr);
  1055. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1056. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1057. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1058. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1059. "7890123456789012345678901234567890",
  1060. body);
  1061. EXPECT_EQ(200, res->status);
  1062. }
  1063. TEST_F(ServerTest, MultipartFormDataGzip) {
  1064. Request req;
  1065. req.method = "POST";
  1066. req.path = "/gzipmultipart";
  1067. std::string host_and_port;
  1068. host_and_port += HOST;
  1069. host_and_port += ":";
  1070. host_and_port += std::to_string(PORT);
  1071. req.headers.emplace("Host", host_and_port.c_str());
  1072. req.headers.emplace("Accept", "*/*");
  1073. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1074. req.headers.emplace(
  1075. "Content-Type",
  1076. "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  1077. req.headers.emplace("Content-Encoding", "gzip");
  1078. // compressed_body generated by creating input.txt to this file:
  1079. /*
  1080. --------------------------fcba8368a9f48c0f
  1081. Content-Disposition: form-data; name="key1"
  1082. test
  1083. --------------------------fcba8368a9f48c0f
  1084. Content-Disposition: form-data; name="key2"
  1085. --abcdefg123
  1086. --------------------------fcba8368a9f48c0f--
  1087. */
  1088. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  1089. uint8_t compressed_body[] = {
  1090. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  1091. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  1092. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  1093. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  1094. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  1095. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  1096. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  1097. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  1098. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  1099. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  1100. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00};
  1101. req.body = std::string((char *)compressed_body,
  1102. sizeof(compressed_body) / sizeof(compressed_body[0]));
  1103. auto res = std::make_shared<Response>();
  1104. auto ret = cli_.send(req, *res);
  1105. ASSERT_TRUE(ret);
  1106. EXPECT_EQ(200, res->status);
  1107. }
  1108. #endif
  1109. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1110. protected:
  1111. ServerTestWithAI_PASSIVE()
  1112. : cli_(HOST, PORT)
  1113. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1114. ,
  1115. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1116. #endif
  1117. {
  1118. }
  1119. virtual void SetUp() {
  1120. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1121. res.set_content("Hello World!", "text/plain");
  1122. });
  1123. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1124. while (!svr_.is_running()) {
  1125. msleep(1);
  1126. }
  1127. }
  1128. virtual void TearDown() {
  1129. svr_.stop();
  1130. t_.join();
  1131. }
  1132. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1133. SSLClient cli_;
  1134. SSLServer svr_;
  1135. #else
  1136. Client cli_;
  1137. Server svr_;
  1138. #endif
  1139. thread t_;
  1140. };
  1141. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1142. auto res = cli_.Get("/hi");
  1143. ASSERT_TRUE(res != nullptr);
  1144. EXPECT_EQ(200, res->status);
  1145. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1146. EXPECT_EQ("Hello World!", res->body);
  1147. }
  1148. class ServerUpDownTest : public ::testing::Test {
  1149. protected:
  1150. ServerUpDownTest() : cli_(HOST, PORT) {}
  1151. virtual void SetUp() {
  1152. t_ = thread([&]() {
  1153. svr_.bind_to_any_port(HOST);
  1154. msleep(500);
  1155. ASSERT_TRUE(svr_.listen_after_bind());
  1156. });
  1157. while (!svr_.is_running()) {
  1158. msleep(1);
  1159. }
  1160. }
  1161. virtual void TearDown() {
  1162. svr_.stop();
  1163. t_.join();
  1164. }
  1165. Client cli_;
  1166. Server svr_;
  1167. thread t_;
  1168. };
  1169. TEST_F(ServerUpDownTest, QuickStartStop) {
  1170. // Should not crash, especially when run with
  1171. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1172. }
  1173. class PayloadMaxLengthTest : public ::testing::Test {
  1174. protected:
  1175. PayloadMaxLengthTest()
  1176. : cli_(HOST, PORT)
  1177. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1178. ,
  1179. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1180. #endif
  1181. {
  1182. }
  1183. virtual void SetUp() {
  1184. svr_.set_payload_max_length(8);
  1185. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1186. res.set_content("test", "text/plain");
  1187. });
  1188. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1189. while (!svr_.is_running()) {
  1190. msleep(1);
  1191. }
  1192. }
  1193. virtual void TearDown() {
  1194. svr_.stop();
  1195. t_.join();
  1196. }
  1197. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1198. SSLClient cli_;
  1199. SSLServer svr_;
  1200. #else
  1201. Client cli_;
  1202. Server svr_;
  1203. #endif
  1204. thread t_;
  1205. };
  1206. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1207. auto res = cli_.Post("/test", "123456789", "text/plain");
  1208. ASSERT_TRUE(res != nullptr);
  1209. EXPECT_EQ(413, res->status);
  1210. res = cli_.Post("/test", "12345678", "text/plain");
  1211. ASSERT_TRUE(res != nullptr);
  1212. EXPECT_EQ(200, res->status);
  1213. }
  1214. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1215. TEST(SSLClientTest, ServerNameIndication) {
  1216. SSLClient cli("httpbin.org", 443);
  1217. auto res = cli.Get("/get");
  1218. ASSERT_TRUE(res != nullptr);
  1219. ASSERT_EQ(200, res->status);
  1220. }
  1221. TEST(SSLClientTest, ServerCertificateVerification) {
  1222. SSLClient cli("google.com");
  1223. auto res = cli.Get("/");
  1224. ASSERT_TRUE(res != nullptr);
  1225. ASSERT_EQ(301, res->status);
  1226. cli.enable_server_certificate_verification(true);
  1227. res = cli.Get("/");
  1228. ASSERT_TRUE(res == nullptr);
  1229. cli.set_ca_cert_path(CA_CERT_FILE);
  1230. res = cli.Get("/");
  1231. ASSERT_TRUE(res != nullptr);
  1232. ASSERT_EQ(301, res->status);
  1233. }
  1234. TEST(SSLClientTest, WildcardHostNameMatch) {
  1235. SSLClient cli("www.youtube.com");
  1236. cli.set_ca_cert_path(CA_CERT_FILE);
  1237. cli.enable_server_certificate_verification(true);
  1238. auto res = cli.Get("/");
  1239. ASSERT_TRUE(res != nullptr);
  1240. ASSERT_EQ(200, res->status);
  1241. }
  1242. TEST(SSLClientServerTest, ClientCertPresent) {
  1243. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1244. CLIENT_CA_CERT_DIR);
  1245. ASSERT_TRUE(svr.is_valid());
  1246. svr.Get("/test", [&](const Request &req, Response &res) {
  1247. res.set_content("test", "text/plain");
  1248. svr.stop();
  1249. ASSERT_TRUE(true);
  1250. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1251. ASSERT_TRUE(peer_cert != nullptr);
  1252. auto subject_name = X509_get_subject_name(peer_cert);
  1253. ASSERT_TRUE(subject_name != nullptr);
  1254. std::string common_name;
  1255. {
  1256. char name[BUFSIZ];
  1257. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1258. name, sizeof(name));
  1259. common_name.assign(name, name_len);
  1260. }
  1261. EXPECT_EQ("Common Name", common_name);
  1262. X509_free(peer_cert);
  1263. });
  1264. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1265. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1266. CLIENT_PRIVATE_KEY_FILE);
  1267. auto res = cli.Get("/test");
  1268. ASSERT_TRUE(res != nullptr);
  1269. ASSERT_EQ(200, res->status);
  1270. t.join();
  1271. }
  1272. TEST(SSLClientServerTest, ClientCertMissing) {
  1273. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1274. CLIENT_CA_CERT_DIR);
  1275. ASSERT_TRUE(svr.is_valid());
  1276. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1277. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1278. httplib::SSLClient cli(HOST, PORT, 30);
  1279. auto res = cli.Get("/test");
  1280. ASSERT_TRUE(res == nullptr);
  1281. svr.stop();
  1282. t.join();
  1283. }
  1284. TEST(SSLClientServerTest, TrustDirOptional) {
  1285. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1286. ASSERT_TRUE(svr.is_valid());
  1287. svr.Get("/test", [&](const Request &, Response &res) {
  1288. res.set_content("test", "text/plain");
  1289. svr.stop();
  1290. });
  1291. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1292. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1293. CLIENT_PRIVATE_KEY_FILE);
  1294. auto res = cli.Get("/test");
  1295. ASSERT_TRUE(res != nullptr);
  1296. ASSERT_EQ(200, res->status);
  1297. t.join();
  1298. }
  1299. /* Cannot test this case as there is no external access to SSL object to check
  1300. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1301. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1302. nullptr, CLIENT_CA_CERT_DIR);
  1303. }
  1304. */
  1305. #endif
  1306. #ifdef _WIN32
  1307. TEST(CleanupTest, WSACleanup) {
  1308. int ret = WSACleanup();
  1309. ASSERT_EQ(0, ret);
  1310. }
  1311. #endif