test.cc 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674
  1. #include <httplib.h>
  2. #include <gtest/gtest.h>
  3. #include <chrono>
  4. #include <future>
  5. #include <thread>
  6. #define SERVER_CERT_FILE "./cert.pem"
  7. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  8. #define CA_CERT_FILE "./ca-bundle.crt"
  9. #define CLIENT_CA_CERT_FILE "./rootCA.cert.pem"
  10. #define CLIENT_CA_CERT_DIR "."
  11. #define CLIENT_CERT_FILE "./client.cert.pem"
  12. #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem"
  13. using namespace std;
  14. using namespace httplib;
  15. const char *HOST = "localhost";
  16. const int PORT = 1234;
  17. const string LONG_QUERY_VALUE = string(25000, '@');
  18. const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
  19. const std::string JSON_DATA = "{\"hello\":\"world\"}";
  20. const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB
  21. MultipartFormData &get_file_value(MultipartFormDataItems &files,
  22. const char *key) {
  23. auto it = std::find_if(
  24. files.begin(), files.end(),
  25. [&](const MultipartFormData &file) { return file.name == key; });
  26. if (it != files.end()) { return *it; }
  27. throw std::runtime_error("invalid mulitpart form data name error");
  28. }
  29. #ifdef _WIN32
  30. TEST(StartupTest, WSAStartup) {
  31. WSADATA wsaData;
  32. int ret = WSAStartup(0x0002, &wsaData);
  33. ASSERT_EQ(0, ret);
  34. }
  35. #endif
  36. TEST(SplitTest, ParseQueryString) {
  37. string s = "key1=val1&key2=val2&key3=val3";
  38. Params dic;
  39. detail::split(s.c_str(), s.c_str() + s.size(), '&',
  40. [&](const char *b, const char *e) {
  41. string key, val;
  42. detail::split(b, e, '=', [&](const char *b, const char *e) {
  43. if (key.empty()) {
  44. key.assign(b, e);
  45. } else {
  46. val.assign(b, e);
  47. }
  48. });
  49. dic.emplace(key, val);
  50. });
  51. EXPECT_EQ("val1", dic.find("key1")->second);
  52. EXPECT_EQ("val2", dic.find("key2")->second);
  53. EXPECT_EQ("val3", dic.find("key3")->second);
  54. }
  55. TEST(ParseQueryTest, ParseQueryString) {
  56. string s = "key1=val1&key2=val2&key3=val3";
  57. Params dic;
  58. detail::parse_query_text(s, dic);
  59. EXPECT_EQ("val1", dic.find("key1")->second);
  60. EXPECT_EQ("val2", dic.find("key2")->second);
  61. EXPECT_EQ("val3", dic.find("key3")->second);
  62. }
  63. TEST(ParamsToQueryTest, ConvertParamsToQuery) {
  64. Params dic;
  65. EXPECT_EQ(detail::params_to_query_str(dic), "");
  66. dic.emplace("key1", "val1");
  67. EXPECT_EQ(detail::params_to_query_str(dic), "key1=val1");
  68. dic.emplace("key2", "val2");
  69. dic.emplace("key3", "val3");
  70. EXPECT_EQ(detail::params_to_query_str(dic), "key1=val1&key2=val2&key3=val3");
  71. }
  72. TEST(GetHeaderValueTest, DefaultValue) {
  73. Headers headers = {{"Dummy", "Dummy"}};
  74. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  75. EXPECT_STREQ("text/plain", val);
  76. }
  77. TEST(GetHeaderValueTest, DefaultValueInt) {
  78. Headers headers = {{"Dummy", "Dummy"}};
  79. auto val = detail::get_header_value_uint64(headers, "Content-Length", 100);
  80. EXPECT_EQ(100ull, val);
  81. }
  82. TEST(GetHeaderValueTest, RegularValue) {
  83. Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
  84. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  85. EXPECT_STREQ("text/html", val);
  86. }
  87. TEST(GetHeaderValueTest, RegularValueInt) {
  88. Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
  89. auto val = detail::get_header_value_uint64(headers, "Content-Length", 0);
  90. EXPECT_EQ(100ull, val);
  91. }
  92. TEST(GetHeaderValueTest, Range) {
  93. {
  94. Headers headers = {make_range_header({{1, -1}})};
  95. auto val = detail::get_header_value(headers, "Range", 0, 0);
  96. EXPECT_STREQ("bytes=1-", val);
  97. }
  98. {
  99. Headers headers = {make_range_header({{-1, 1}})};
  100. auto val = detail::get_header_value(headers, "Range", 0, 0);
  101. EXPECT_STREQ("bytes=-1", val);
  102. }
  103. {
  104. Headers headers = {make_range_header({{1, 10}})};
  105. auto val = detail::get_header_value(headers, "Range", 0, 0);
  106. EXPECT_STREQ("bytes=1-10", val);
  107. }
  108. {
  109. Headers headers = {make_range_header({{1, 10}, {100, -1}})};
  110. auto val = detail::get_header_value(headers, "Range", 0, 0);
  111. EXPECT_STREQ("bytes=1-10, 100-", val);
  112. }
  113. {
  114. Headers headers = {make_range_header({{1, 10}, {100, 200}})};
  115. auto val = detail::get_header_value(headers, "Range", 0, 0);
  116. EXPECT_STREQ("bytes=1-10, 100-200", val);
  117. }
  118. {
  119. Headers headers = {make_range_header({{0, 0}, {-1, 1}})};
  120. auto val = detail::get_header_value(headers, "Range", 0, 0);
  121. EXPECT_STREQ("bytes=0-0, -1", val);
  122. }
  123. }
  124. TEST(ParseHeaderValueTest, Range) {
  125. {
  126. Ranges ranges;
  127. auto ret = detail::parse_range_header("bytes=1-", ranges);
  128. EXPECT_TRUE(ret);
  129. EXPECT_EQ(1u, ranges.size());
  130. EXPECT_EQ(1u, ranges[0].first);
  131. EXPECT_EQ(-1, ranges[0].second);
  132. }
  133. {
  134. Ranges ranges;
  135. auto ret = detail::parse_range_header("bytes=-1", ranges);
  136. EXPECT_TRUE(ret);
  137. EXPECT_EQ(1u, ranges.size());
  138. EXPECT_EQ(-1, ranges[0].first);
  139. EXPECT_EQ(1u, ranges[0].second);
  140. }
  141. {
  142. Ranges ranges;
  143. auto ret = detail::parse_range_header("bytes=1-10", ranges);
  144. EXPECT_TRUE(ret);
  145. EXPECT_EQ(1u, ranges.size());
  146. EXPECT_EQ(1u, ranges[0].first);
  147. EXPECT_EQ(10u, ranges[0].second);
  148. }
  149. {
  150. Ranges ranges;
  151. auto ret = detail::parse_range_header("bytes=10-1", ranges);
  152. EXPECT_FALSE(ret);
  153. }
  154. {
  155. Ranges ranges;
  156. auto ret = detail::parse_range_header("bytes=1-10, 100-", ranges);
  157. EXPECT_TRUE(ret);
  158. EXPECT_EQ(2u, ranges.size());
  159. EXPECT_EQ(1u, ranges[0].first);
  160. EXPECT_EQ(10u, ranges[0].second);
  161. EXPECT_EQ(100u, ranges[1].first);
  162. EXPECT_EQ(-1, ranges[1].second);
  163. }
  164. {
  165. Ranges ranges;
  166. auto ret =
  167. detail::parse_range_header("bytes=1-10, 100-200, 300-400", ranges);
  168. EXPECT_TRUE(ret);
  169. EXPECT_EQ(3u, ranges.size());
  170. EXPECT_EQ(1u, ranges[0].first);
  171. EXPECT_EQ(10u, ranges[0].second);
  172. EXPECT_EQ(100u, ranges[1].first);
  173. EXPECT_EQ(200u, ranges[1].second);
  174. EXPECT_EQ(300u, ranges[2].first);
  175. EXPECT_EQ(400u, ranges[2].second);
  176. }
  177. }
  178. TEST(BufferStreamTest, read) {
  179. detail::BufferStream strm1;
  180. Stream &strm = strm1;
  181. EXPECT_EQ(5, strm.write("hello"));
  182. char buf[512];
  183. EXPECT_EQ(2, strm.read(buf, 2));
  184. EXPECT_EQ('h', buf[0]);
  185. EXPECT_EQ('e', buf[1]);
  186. EXPECT_EQ(2, strm.read(buf, 2));
  187. EXPECT_EQ('l', buf[0]);
  188. EXPECT_EQ('l', buf[1]);
  189. EXPECT_EQ(1, strm.read(buf, 1));
  190. EXPECT_EQ('o', buf[0]);
  191. EXPECT_EQ(0, strm.read(buf, 1));
  192. }
  193. TEST(ChunkedEncodingTest, FromHTTPWatch) {
  194. auto host = "www.httpwatch.com";
  195. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  196. auto port = 443;
  197. httplib::SSLClient cli(host, port);
  198. #else
  199. auto port = 80;
  200. httplib::Client cli(host, port);
  201. #endif
  202. cli.set_timeout_sec(2);
  203. auto res =
  204. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
  205. ASSERT_TRUE(res != nullptr);
  206. std::string out;
  207. httplib::detail::read_file("./image.jpg", out);
  208. EXPECT_EQ(200, res->status);
  209. EXPECT_EQ(out, res->body);
  210. }
  211. TEST(ChunkedEncodingTest, WithContentReceiver) {
  212. auto host = "www.httpwatch.com";
  213. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  214. auto port = 443;
  215. httplib::SSLClient cli(host, port);
  216. #else
  217. auto port = 80;
  218. httplib::Client cli(host, port);
  219. #endif
  220. cli.set_timeout_sec(2);
  221. std::string body;
  222. auto res =
  223. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
  224. [&](const char *data, size_t data_length) {
  225. body.append(data, data_length);
  226. return true;
  227. });
  228. ASSERT_TRUE(res != nullptr);
  229. std::string out;
  230. httplib::detail::read_file("./image.jpg", out);
  231. EXPECT_EQ(200, res->status);
  232. EXPECT_EQ(out, body);
  233. }
  234. TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
  235. auto host = "www.httpwatch.com";
  236. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  237. auto port = 443;
  238. httplib::SSLClient cli(host, port);
  239. #else
  240. auto port = 80;
  241. httplib::Client cli(host, port);
  242. #endif
  243. cli.set_timeout_sec(2);
  244. std::string body;
  245. auto res = cli.Get(
  246. "/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", Headers(),
  247. [&](const Response &response) {
  248. EXPECT_EQ(200, response.status);
  249. return true;
  250. },
  251. [&](const char *data, size_t data_length) {
  252. body.append(data, data_length);
  253. return true;
  254. });
  255. ASSERT_TRUE(res != nullptr);
  256. std::string out;
  257. httplib::detail::read_file("./image.jpg", out);
  258. EXPECT_EQ(200, res->status);
  259. EXPECT_EQ(out, body);
  260. }
  261. TEST(RangeTest, FromHTTPBin) {
  262. auto host = "httpbin.org";
  263. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  264. auto port = 443;
  265. httplib::SSLClient cli(host, port);
  266. #else
  267. auto port = 80;
  268. httplib::Client cli(host, port);
  269. #endif
  270. cli.set_timeout_sec(5);
  271. {
  272. httplib::Headers headers;
  273. auto res = cli.Get("/range/32", headers);
  274. ASSERT_TRUE(res != nullptr);
  275. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  276. EXPECT_EQ(200, res->status);
  277. }
  278. {
  279. httplib::Headers headers = {httplib::make_range_header({{1, -1}})};
  280. auto res = cli.Get("/range/32", headers);
  281. ASSERT_TRUE(res != nullptr);
  282. EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef");
  283. EXPECT_EQ(206, res->status);
  284. }
  285. {
  286. httplib::Headers headers = {httplib::make_range_header({{1, 10}})};
  287. auto res = cli.Get("/range/32", headers);
  288. ASSERT_TRUE(res != nullptr);
  289. EXPECT_EQ(res->body, "bcdefghijk");
  290. EXPECT_EQ(206, res->status);
  291. }
  292. {
  293. httplib::Headers headers = {httplib::make_range_header({{0, 31}})};
  294. auto res = cli.Get("/range/32", headers);
  295. ASSERT_TRUE(res != nullptr);
  296. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  297. EXPECT_EQ(200, res->status);
  298. }
  299. {
  300. httplib::Headers headers = {httplib::make_range_header({{0, -1}})};
  301. auto res = cli.Get("/range/32", headers);
  302. ASSERT_TRUE(res != nullptr);
  303. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  304. EXPECT_EQ(200, res->status);
  305. }
  306. {
  307. httplib::Headers headers = {httplib::make_range_header({{0, 32}})};
  308. auto res = cli.Get("/range/32", headers);
  309. ASSERT_TRUE(res != nullptr);
  310. EXPECT_EQ(416, res->status);
  311. }
  312. }
  313. TEST(ConnectionErrorTest, InvalidHost) {
  314. auto host = "-abcde.com";
  315. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  316. auto port = 443;
  317. httplib::SSLClient cli(host, port);
  318. #else
  319. auto port = 80;
  320. httplib::Client cli(host, port);
  321. #endif
  322. cli.set_timeout_sec(2);
  323. auto res = cli.Get("/");
  324. ASSERT_TRUE(res == nullptr);
  325. }
  326. TEST(ConnectionErrorTest, InvalidPort) {
  327. auto host = "localhost";
  328. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  329. auto port = 44380;
  330. httplib::SSLClient cli(host, port);
  331. #else
  332. auto port = 8080;
  333. httplib::Client cli(host, port);
  334. #endif
  335. cli.set_timeout_sec(2);
  336. auto res = cli.Get("/");
  337. ASSERT_TRUE(res == nullptr);
  338. }
  339. TEST(ConnectionErrorTest, Timeout) {
  340. auto host = "google.com";
  341. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  342. auto port = 44380;
  343. httplib::SSLClient cli(host, port);
  344. #else
  345. auto port = 8080;
  346. httplib::Client cli(host, port);
  347. #endif
  348. cli.set_timeout_sec(2);
  349. auto res = cli.Get("/");
  350. ASSERT_TRUE(res == nullptr);
  351. }
  352. TEST(CancelTest, NoCancel) {
  353. auto host = "httpbin.org";
  354. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  355. auto port = 443;
  356. httplib::SSLClient cli(host, port);
  357. #else
  358. auto port = 80;
  359. httplib::Client cli(host, port);
  360. #endif
  361. cli.set_timeout_sec(5);
  362. auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; });
  363. ASSERT_TRUE(res != nullptr);
  364. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  365. EXPECT_EQ(200, res->status);
  366. }
  367. TEST(CancelTest, WithCancelSmallPayload) {
  368. auto host = "httpbin.org";
  369. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  370. auto port = 443;
  371. httplib::SSLClient cli(host, port);
  372. #else
  373. auto port = 80;
  374. httplib::Client cli(host, port);
  375. #endif
  376. auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return false; });
  377. cli.set_timeout_sec(5);
  378. ASSERT_TRUE(res == nullptr);
  379. }
  380. TEST(CancelTest, WithCancelLargePayload) {
  381. auto host = "httpbin.org";
  382. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  383. auto port = 443;
  384. httplib::SSLClient cli(host, port);
  385. #else
  386. auto port = 80;
  387. httplib::Client cli(host, port);
  388. #endif
  389. cli.set_timeout_sec(5);
  390. uint32_t count = 0;
  391. httplib::Headers headers;
  392. auto res = cli.Get("/range/65536", headers,
  393. [&count](uint64_t, uint64_t) { return (count++ == 0); });
  394. ASSERT_TRUE(res == nullptr);
  395. }
  396. TEST(BaseAuthTest, FromHTTPWatch) {
  397. auto host = "httpbin.org";
  398. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  399. auto port = 443;
  400. httplib::SSLClient cli(host, port);
  401. #else
  402. auto port = 80;
  403. httplib::Client cli(host, port);
  404. #endif
  405. {
  406. auto res = cli.Get("/basic-auth/hello/world");
  407. ASSERT_TRUE(res != nullptr);
  408. EXPECT_EQ(401, res->status);
  409. }
  410. {
  411. auto res =
  412. cli.Get("/basic-auth/hello/world",
  413. {httplib::make_basic_authentication_header("hello", "world")});
  414. ASSERT_TRUE(res != nullptr);
  415. EXPECT_EQ(res->body,
  416. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  417. EXPECT_EQ(200, res->status);
  418. }
  419. {
  420. cli.set_basic_auth("hello", "world");
  421. auto res = cli.Get("/basic-auth/hello/world");
  422. ASSERT_TRUE(res != nullptr);
  423. EXPECT_EQ(res->body,
  424. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  425. EXPECT_EQ(200, res->status);
  426. }
  427. {
  428. cli.set_basic_auth("hello", "bad");
  429. auto res = cli.Get("/basic-auth/hello/world");
  430. ASSERT_TRUE(res != nullptr);
  431. EXPECT_EQ(401, res->status);
  432. }
  433. {
  434. cli.set_basic_auth("bad", "world");
  435. auto res = cli.Get("/basic-auth/hello/world");
  436. ASSERT_TRUE(res != nullptr);
  437. EXPECT_EQ(401, res->status);
  438. }
  439. }
  440. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  441. TEST(DigestAuthTest, FromHTTPWatch) {
  442. auto host = "httpbin.org";
  443. auto port = 443;
  444. httplib::SSLClient cli(host, port);
  445. {
  446. auto res = cli.Get("/digest-auth/auth/hello/world");
  447. ASSERT_TRUE(res != nullptr);
  448. EXPECT_EQ(401, res->status);
  449. }
  450. {
  451. std::vector<std::string> paths = {
  452. "/digest-auth/auth/hello/world/MD5",
  453. "/digest-auth/auth/hello/world/SHA-256",
  454. "/digest-auth/auth/hello/world/SHA-512",
  455. "/digest-auth/auth-int/hello/world/MD5",
  456. };
  457. cli.set_digest_auth("hello", "world");
  458. for (auto path : paths) {
  459. auto res = cli.Get(path.c_str());
  460. ASSERT_TRUE(res != nullptr);
  461. EXPECT_EQ(res->body,
  462. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  463. EXPECT_EQ(200, res->status);
  464. }
  465. cli.set_digest_auth("hello", "bad");
  466. for (auto path : paths) {
  467. auto res = cli.Get(path.c_str());
  468. ASSERT_TRUE(res != nullptr);
  469. EXPECT_EQ(400, res->status);
  470. }
  471. cli.set_digest_auth("bad", "world");
  472. for (auto path : paths) {
  473. auto res = cli.Get(path.c_str());
  474. ASSERT_TRUE(res != nullptr);
  475. EXPECT_EQ(400, res->status);
  476. }
  477. }
  478. }
  479. #endif
  480. TEST(AbsoluteRedirectTest, Redirect) {
  481. auto host = "httpbin.org";
  482. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  483. httplib::SSLClient cli(host);
  484. #else
  485. httplib::Client cli(host);
  486. #endif
  487. cli.set_follow_location(true);
  488. auto res = cli.Get("/absolute-redirect/3");
  489. ASSERT_TRUE(res != nullptr);
  490. EXPECT_EQ(200, res->status);
  491. }
  492. TEST(RedirectTest, Redirect) {
  493. auto host = "httpbin.org";
  494. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  495. httplib::SSLClient cli(host);
  496. #else
  497. httplib::Client cli(host);
  498. #endif
  499. cli.set_follow_location(true);
  500. auto res = cli.Get("/redirect/3");
  501. ASSERT_TRUE(res != nullptr);
  502. EXPECT_EQ(200, res->status);
  503. }
  504. TEST(RelativeRedirectTest, Redirect) {
  505. auto host = "httpbin.org";
  506. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  507. httplib::SSLClient cli(host);
  508. #else
  509. httplib::Client cli(host);
  510. #endif
  511. cli.set_follow_location(true);
  512. auto res = cli.Get("/relative-redirect/3");
  513. ASSERT_TRUE(res != nullptr);
  514. EXPECT_EQ(200, res->status);
  515. }
  516. TEST(TooManyRedirectTest, Redirect) {
  517. auto host = "httpbin.org";
  518. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  519. httplib::SSLClient cli(host);
  520. #else
  521. httplib::Client cli(host);
  522. #endif
  523. cli.set_follow_location(true);
  524. auto res = cli.Get("/redirect/21");
  525. ASSERT_TRUE(res == nullptr);
  526. }
  527. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  528. TEST(YahooRedirectTest, Redirect) {
  529. httplib::Client cli("yahoo.com");
  530. auto res = cli.Get("/");
  531. ASSERT_TRUE(res != nullptr);
  532. EXPECT_EQ(301, res->status);
  533. cli.set_follow_location(true);
  534. res = cli.Get("/");
  535. ASSERT_TRUE(res != nullptr);
  536. EXPECT_EQ(200, res->status);
  537. }
  538. TEST(YahooRedirectTestWithURL, Redirect) {
  539. auto res = httplib::url::Get("http://yahoo.com");
  540. ASSERT_TRUE(res != nullptr);
  541. EXPECT_EQ(301, res->status);
  542. httplib::url::options options;
  543. options.follow_location = true;
  544. res = httplib::url::Get("http://yahoo.com", options);
  545. ASSERT_TRUE(res != nullptr);
  546. EXPECT_EQ(200, res->status);
  547. }
  548. TEST(HttpsToHttpRedirectTest, Redirect) {
  549. httplib::SSLClient cli("httpbin.org");
  550. cli.set_follow_location(true);
  551. auto res =
  552. cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
  553. ASSERT_TRUE(res != nullptr);
  554. }
  555. #endif
  556. TEST(Server, BindAndListenSeparately) {
  557. Server svr;
  558. int port = svr.bind_to_any_port("0.0.0.0");
  559. ASSERT_TRUE(svr.is_valid());
  560. ASSERT_TRUE(port > 0);
  561. svr.stop();
  562. }
  563. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  564. TEST(SSLServer, BindAndListenSeparately) {
  565. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  566. CLIENT_CA_CERT_DIR);
  567. int port = svr.bind_to_any_port("0.0.0.0");
  568. ASSERT_TRUE(svr.is_valid());
  569. ASSERT_TRUE(port > 0);
  570. svr.stop();
  571. }
  572. #endif
  573. class ServerTest : public ::testing::Test {
  574. protected:
  575. ServerTest()
  576. : cli_(HOST, PORT)
  577. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  578. ,
  579. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  580. #endif
  581. {
  582. }
  583. virtual void SetUp() {
  584. svr_.set_mount_point("/", "./www");
  585. svr_.set_mount_point("/mount", "./www2");
  586. svr_.set_file_extension_and_mimetype_mapping("abcde", "text/abcde");
  587. svr_.Get("/hi",
  588. [&](const Request & /*req*/, Response &res) {
  589. res.set_content("Hello World!", "text/plain");
  590. })
  591. .Get("/http_response_splitting",
  592. [&](const Request & /*req*/, Response &res) {
  593. res.set_header("a", "1\r\nSet-Cookie: a=1");
  594. EXPECT_EQ(0, res.headers.size());
  595. EXPECT_FALSE(res.has_header("a"));
  596. res.set_header("a", "1\nSet-Cookie: a=1");
  597. EXPECT_EQ(0, res.headers.size());
  598. EXPECT_FALSE(res.has_header("a"));
  599. res.set_header("a", "1\rSet-Cookie: a=1");
  600. EXPECT_EQ(0, res.headers.size());
  601. EXPECT_FALSE(res.has_header("a"));
  602. res.set_header("a\r\nb", "0");
  603. EXPECT_EQ(0, res.headers.size());
  604. EXPECT_FALSE(res.has_header("a"));
  605. res.set_header("a\rb", "0");
  606. EXPECT_EQ(0, res.headers.size());
  607. EXPECT_FALSE(res.has_header("a"));
  608. res.set_header("a\nb", "0");
  609. EXPECT_EQ(0, res.headers.size());
  610. EXPECT_FALSE(res.has_header("a"));
  611. res.set_redirect("1\r\nSet-Cookie: a=1");
  612. EXPECT_EQ(0, res.headers.size());
  613. EXPECT_FALSE(res.has_header("Location"));
  614. })
  615. .Get("/slow",
  616. [&](const Request & /*req*/, Response &res) {
  617. std::this_thread::sleep_for(std::chrono::seconds(2));
  618. res.set_content("slow", "text/plain");
  619. })
  620. .Get("/remote_addr",
  621. [&](const Request &req, Response &res) {
  622. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  623. res.set_content(remote_addr.c_str(), "text/plain");
  624. })
  625. .Get("/endwith%",
  626. [&](const Request & /*req*/, Response &res) {
  627. res.set_content("Hello World!", "text/plain");
  628. })
  629. .Get("/a\\+\\+b",
  630. [&](const Request &req, Response &res) {
  631. ASSERT_TRUE(req.has_param("a +b"));
  632. auto val = req.get_param_value("a +b");
  633. res.set_content(val, "text/plain");
  634. })
  635. .Get("/", [&](const Request & /*req*/,
  636. Response &res) { res.set_redirect("/hi"); })
  637. .Post("/1", [](const Request & /*req*/,
  638. Response &res) { res.set_redirect("/2", 303); })
  639. .Get("/2",
  640. [](const Request & /*req*/, Response &res) {
  641. res.set_content("redirected.", "text/plain");
  642. res.status = 200;
  643. })
  644. .Post("/person",
  645. [&](const Request &req, Response &res) {
  646. if (req.has_param("name") && req.has_param("note")) {
  647. persons_[req.get_param_value("name")] =
  648. req.get_param_value("note");
  649. } else {
  650. res.status = 400;
  651. }
  652. })
  653. .Put("/person",
  654. [&](const Request &req, Response &res) {
  655. if (req.has_param("name") && req.has_param("note")) {
  656. persons_[req.get_param_value("name")] =
  657. req.get_param_value("note");
  658. } else {
  659. res.status = 400;
  660. }
  661. })
  662. .Get("/person/(.*)",
  663. [&](const Request &req, Response &res) {
  664. string name = req.matches[1];
  665. if (persons_.find(name) != persons_.end()) {
  666. auto note = persons_[name];
  667. res.set_content(note, "text/plain");
  668. } else {
  669. res.status = 404;
  670. }
  671. })
  672. .Post("/x-www-form-urlencoded-json",
  673. [&](const Request &req, Response &res) {
  674. auto json = req.get_param_value("json");
  675. ASSERT_EQ(JSON_DATA, json);
  676. res.set_content(json, "appliation/json");
  677. res.status = 200;
  678. })
  679. .Get("/streamed-chunked",
  680. [&](const Request & /*req*/, Response &res) {
  681. res.set_chunked_content_provider(
  682. [](uint64_t /*offset*/, DataSink &sink) {
  683. ASSERT_TRUE(sink.is_writable());
  684. sink.write("123", 3);
  685. sink.write("456", 3);
  686. sink.write("789", 3);
  687. sink.done();
  688. });
  689. })
  690. .Get("/streamed-chunked2",
  691. [&](const Request & /*req*/, Response &res) {
  692. auto i = new int(0);
  693. res.set_chunked_content_provider(
  694. [i](uint64_t /*offset*/, DataSink &sink) {
  695. ASSERT_TRUE(sink.is_writable());
  696. switch (*i) {
  697. case 0: sink.write("123", 3); break;
  698. case 1: sink.write("456", 3); break;
  699. case 2: sink.write("789", 3); break;
  700. case 3: sink.done(); break;
  701. }
  702. (*i)++;
  703. },
  704. [i] { delete i; });
  705. })
  706. .Get("/streamed",
  707. [&](const Request & /*req*/, Response &res) {
  708. res.set_content_provider(
  709. 6, [](uint64_t offset, uint64_t /*length*/, DataSink &sink) {
  710. sink.write(offset < 3 ? "a" : "b", 1);
  711. });
  712. })
  713. .Get("/streamed-with-range",
  714. [&](const Request & /*req*/, Response &res) {
  715. auto data = new std::string("abcdefg");
  716. res.set_content_provider(
  717. data->size(),
  718. [data](uint64_t offset, uint64_t length, DataSink &sink) {
  719. ASSERT_TRUE(sink.is_writable());
  720. size_t DATA_CHUNK_SIZE = 4;
  721. const auto &d = *data;
  722. auto out_len =
  723. std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
  724. sink.write(&d[static_cast<size_t>(offset)], out_len);
  725. },
  726. [data] { delete data; });
  727. })
  728. .Get("/streamed-cancel",
  729. [&](const Request & /*req*/, Response &res) {
  730. res.set_content_provider(size_t(-1), [](uint64_t /*offset*/,
  731. uint64_t /*length*/,
  732. DataSink &sink) {
  733. ASSERT_TRUE(sink.is_writable());
  734. std::string data = "data_chunk";
  735. sink.write(data.data(), data.size());
  736. });
  737. })
  738. .Get("/with-range",
  739. [&](const Request & /*req*/, Response &res) {
  740. res.set_content("abcdefg", "text/plain");
  741. })
  742. .Post("/chunked",
  743. [&](const Request &req, Response & /*res*/) {
  744. EXPECT_EQ(req.body, "dechunked post body");
  745. })
  746. .Post("/large-chunked",
  747. [&](const Request &req, Response & /*res*/) {
  748. std::string expected(6 * 30 * 1024u, 'a');
  749. EXPECT_EQ(req.body, expected);
  750. })
  751. .Post("/multipart",
  752. [&](const Request &req, Response & /*res*/) {
  753. EXPECT_EQ(5u, req.files.size());
  754. ASSERT_TRUE(!req.has_file("???"));
  755. ASSERT_TRUE(req.body.empty());
  756. {
  757. const auto &file = req.get_file_value("text1");
  758. EXPECT_EQ("", file.filename);
  759. EXPECT_EQ("text default", file.content);
  760. }
  761. {
  762. const auto &file = req.get_file_value("text2");
  763. EXPECT_EQ("", file.filename);
  764. EXPECT_EQ("aωb", file.content);
  765. }
  766. {
  767. const auto &file = req.get_file_value("file1");
  768. EXPECT_EQ("hello.txt", file.filename);
  769. EXPECT_EQ("text/plain", file.content_type);
  770. EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content);
  771. }
  772. {
  773. const auto &file = req.get_file_value("file3");
  774. EXPECT_EQ("", file.filename);
  775. EXPECT_EQ("application/octet-stream", file.content_type);
  776. EXPECT_EQ(0u, file.content.size());
  777. }
  778. })
  779. .Post("/empty",
  780. [&](const Request &req, Response &res) {
  781. EXPECT_EQ(req.body, "");
  782. res.set_content("empty", "text/plain");
  783. })
  784. .Put("/put",
  785. [&](const Request &req, Response &res) {
  786. EXPECT_EQ(req.body, "PUT");
  787. res.set_content(req.body, "text/plain");
  788. })
  789. .Put("/put-large",
  790. [&](const Request &req, Response &res) {
  791. EXPECT_EQ(req.body, LARGE_DATA);
  792. res.set_content(req.body, "text/plain");
  793. })
  794. .Patch("/patch",
  795. [&](const Request &req, Response &res) {
  796. EXPECT_EQ(req.body, "PATCH");
  797. res.set_content(req.body, "text/plain");
  798. })
  799. .Delete("/delete",
  800. [&](const Request & /*req*/, Response &res) {
  801. res.set_content("DELETE", "text/plain");
  802. })
  803. .Delete("/delete-body",
  804. [&](const Request &req, Response &res) {
  805. EXPECT_EQ(req.body, "content");
  806. res.set_content(req.body, "text/plain");
  807. })
  808. .Options(R"(\*)",
  809. [&](const Request & /*req*/, Response &res) {
  810. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  811. })
  812. .Get("/request-target",
  813. [&](const Request &req, Response & /*res*/) {
  814. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  815. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  816. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  817. })
  818. .Get("/long-query-value",
  819. [&](const Request &req, Response & /*res*/) {
  820. EXPECT_EQ(LONG_QUERY_URL, req.target);
  821. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  822. })
  823. .Get("/array-param",
  824. [&](const Request &req, Response & /*res*/) {
  825. EXPECT_EQ(3u, req.get_param_value_count("array"));
  826. EXPECT_EQ("value1", req.get_param_value("array", 0));
  827. EXPECT_EQ("value2", req.get_param_value("array", 1));
  828. EXPECT_EQ("value3", req.get_param_value("array", 2));
  829. })
  830. .Post("/validate-no-multiple-headers",
  831. [&](const Request &req, Response & /*res*/) {
  832. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  833. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  834. })
  835. .Post("/content_receiver",
  836. [&](const Request &req, Response &res,
  837. const ContentReader &content_reader) {
  838. if (req.is_multipart_form_data()) {
  839. MultipartFormDataItems files;
  840. content_reader(
  841. [&](const MultipartFormData &file) {
  842. files.push_back(file);
  843. return true;
  844. },
  845. [&](const char *data, size_t data_length) {
  846. files.back().content.append(data, data_length);
  847. return true;
  848. });
  849. EXPECT_EQ(5u, files.size());
  850. {
  851. const auto &file = get_file_value(files, "text1");
  852. EXPECT_EQ("", file.filename);
  853. EXPECT_EQ("text default", file.content);
  854. }
  855. {
  856. const auto &file = get_file_value(files, "text2");
  857. EXPECT_EQ("", file.filename);
  858. EXPECT_EQ("aωb", file.content);
  859. }
  860. {
  861. const auto &file = get_file_value(files, "file1");
  862. EXPECT_EQ("hello.txt", file.filename);
  863. EXPECT_EQ("text/plain", file.content_type);
  864. EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content);
  865. }
  866. {
  867. const auto &file = get_file_value(files, "file3");
  868. EXPECT_EQ("", file.filename);
  869. EXPECT_EQ("application/octet-stream", file.content_type);
  870. EXPECT_EQ(0u, file.content.size());
  871. }
  872. } else {
  873. std::string body;
  874. content_reader([&](const char *data, size_t data_length) {
  875. EXPECT_EQ(data_length, 7);
  876. body.append(data, data_length);
  877. return true;
  878. });
  879. EXPECT_EQ(body, "content");
  880. res.set_content(body, "text/plain");
  881. }
  882. })
  883. .Put("/content_receiver",
  884. [&](const Request & /*req*/, Response &res,
  885. const ContentReader &content_reader) {
  886. std::string body;
  887. content_reader([&](const char *data, size_t data_length) {
  888. body.append(data, data_length);
  889. return true;
  890. });
  891. EXPECT_EQ(body, "content");
  892. res.set_content(body, "text/plain");
  893. })
  894. .Patch("/content_receiver",
  895. [&](const Request & /*req*/, Response &res,
  896. const ContentReader &content_reader) {
  897. std::string body;
  898. content_reader([&](const char *data, size_t data_length) {
  899. body.append(data, data_length);
  900. return true;
  901. });
  902. EXPECT_EQ(body, "content");
  903. res.set_content(body, "text/plain");
  904. })
  905. .Post("/query-string-and-body",
  906. [&](const Request &req, Response & /*res*/) {
  907. ASSERT_TRUE(req.has_param("key"));
  908. EXPECT_EQ(req.get_param_value("key"), "value");
  909. EXPECT_EQ(req.body, "content");
  910. })
  911. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  912. .Get("/gzip",
  913. [&](const Request & /*req*/, Response &res) {
  914. res.set_content(
  915. "12345678901234567890123456789012345678901234567890123456789"
  916. "01234567890123456789012345678901234567890",
  917. "text/plain");
  918. })
  919. .Get("/nogzip",
  920. [&](const Request & /*req*/, Response &res) {
  921. res.set_content(
  922. "12345678901234567890123456789012345678901234567890123456789"
  923. "01234567890123456789012345678901234567890",
  924. "application/octet-stream");
  925. })
  926. .Post("/gzipmultipart",
  927. [&](const Request &req, Response & /*res*/) {
  928. EXPECT_EQ(2u, req.files.size());
  929. ASSERT_TRUE(!req.has_file("???"));
  930. {
  931. const auto &file = req.get_file_value("key1");
  932. EXPECT_EQ("", file.filename);
  933. EXPECT_EQ("test", file.content);
  934. }
  935. {
  936. const auto &file = req.get_file_value("key2");
  937. EXPECT_EQ("", file.filename);
  938. EXPECT_EQ("--abcdefg123", file.content);
  939. }
  940. })
  941. #endif
  942. ;
  943. persons_["john"] = "programmer";
  944. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  945. while (!svr_.is_running()) {
  946. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  947. }
  948. }
  949. virtual void TearDown() {
  950. svr_.stop();
  951. for (auto &t : request_threads_) {
  952. t.join();
  953. }
  954. t_.join();
  955. }
  956. map<string, string> persons_;
  957. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  958. SSLClient cli_;
  959. SSLServer svr_;
  960. #else
  961. Client cli_;
  962. Server svr_;
  963. #endif
  964. thread t_;
  965. std::vector<thread> request_threads_;
  966. };
  967. TEST_F(ServerTest, GetMethod200) {
  968. auto res = cli_.Get("/hi");
  969. ASSERT_TRUE(res != nullptr);
  970. EXPECT_EQ("HTTP/1.1", res->version);
  971. EXPECT_EQ(200, res->status);
  972. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  973. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  974. EXPECT_EQ("Hello World!", res->body);
  975. }
  976. TEST_F(ServerTest, GetMethod200withPercentEncoding) {
  977. auto res = cli_.Get("/%68%69"); // auto res = cli_.Get("/hi");
  978. ASSERT_TRUE(res != nullptr);
  979. EXPECT_EQ("HTTP/1.1", res->version);
  980. EXPECT_EQ(200, res->status);
  981. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  982. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  983. EXPECT_EQ("Hello World!", res->body);
  984. }
  985. TEST_F(ServerTest, GetMethod302) {
  986. auto res = cli_.Get("/");
  987. ASSERT_TRUE(res != nullptr);
  988. EXPECT_EQ(302, res->status);
  989. EXPECT_EQ("/hi", res->get_header_value("Location"));
  990. }
  991. TEST_F(ServerTest, GetMethod302Redirect) {
  992. cli_.set_follow_location(true);
  993. auto res = cli_.Get("/");
  994. ASSERT_TRUE(res != nullptr);
  995. EXPECT_EQ(200, res->status);
  996. EXPECT_EQ("Hello World!", res->body);
  997. }
  998. TEST_F(ServerTest, GetMethod404) {
  999. auto res = cli_.Get("/invalid");
  1000. ASSERT_TRUE(res != nullptr);
  1001. EXPECT_EQ(404, res->status);
  1002. }
  1003. TEST_F(ServerTest, HeadMethod200) {
  1004. auto res = cli_.Head("/hi");
  1005. ASSERT_TRUE(res != nullptr);
  1006. EXPECT_EQ(200, res->status);
  1007. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1008. EXPECT_EQ("", res->body);
  1009. }
  1010. TEST_F(ServerTest, HeadMethod200Static) {
  1011. auto res = cli_.Head("/mount/dir/index.html");
  1012. ASSERT_TRUE(res != nullptr);
  1013. EXPECT_EQ(200, res->status);
  1014. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1015. EXPECT_EQ(104, std::stoi(res->get_header_value("Content-Length")));
  1016. EXPECT_EQ("", res->body);
  1017. }
  1018. TEST_F(ServerTest, HeadMethod404) {
  1019. auto res = cli_.Head("/invalid");
  1020. ASSERT_TRUE(res != nullptr);
  1021. EXPECT_EQ(404, res->status);
  1022. EXPECT_EQ("", res->body);
  1023. }
  1024. TEST_F(ServerTest, GetMethodPersonJohn) {
  1025. auto res = cli_.Get("/person/john");
  1026. ASSERT_TRUE(res != nullptr);
  1027. EXPECT_EQ(200, res->status);
  1028. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1029. EXPECT_EQ("programmer", res->body);
  1030. }
  1031. TEST_F(ServerTest, PostMethod1) {
  1032. auto res = cli_.Get("/person/john1");
  1033. ASSERT_TRUE(res != nullptr);
  1034. ASSERT_EQ(404, res->status);
  1035. res = cli_.Post("/person", "name=john1&note=coder",
  1036. "application/x-www-form-urlencoded");
  1037. ASSERT_TRUE(res != nullptr);
  1038. ASSERT_EQ(200, res->status);
  1039. res = cli_.Get("/person/john1");
  1040. ASSERT_TRUE(res != nullptr);
  1041. ASSERT_EQ(200, res->status);
  1042. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  1043. ASSERT_EQ("coder", res->body);
  1044. }
  1045. TEST_F(ServerTest, PostMethod2) {
  1046. auto res = cli_.Get("/person/john2");
  1047. ASSERT_TRUE(res != nullptr);
  1048. ASSERT_EQ(404, res->status);
  1049. Params params;
  1050. params.emplace("name", "john2");
  1051. params.emplace("note", "coder");
  1052. res = cli_.Post("/person", params);
  1053. ASSERT_TRUE(res != nullptr);
  1054. ASSERT_EQ(200, res->status);
  1055. res = cli_.Get("/person/john2");
  1056. ASSERT_TRUE(res != nullptr);
  1057. ASSERT_EQ(200, res->status);
  1058. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  1059. ASSERT_EQ("coder", res->body);
  1060. }
  1061. TEST_F(ServerTest, PutMethod3) {
  1062. auto res = cli_.Get("/person/john3");
  1063. ASSERT_TRUE(res != nullptr);
  1064. ASSERT_EQ(404, res->status);
  1065. Params params;
  1066. params.emplace("name", "john3");
  1067. params.emplace("note", "coder");
  1068. res = cli_.Put("/person", params);
  1069. ASSERT_TRUE(res != nullptr);
  1070. ASSERT_EQ(200, res->status);
  1071. res = cli_.Get("/person/john3");
  1072. ASSERT_TRUE(res != nullptr);
  1073. ASSERT_EQ(200, res->status);
  1074. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  1075. ASSERT_EQ("coder", res->body);
  1076. }
  1077. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  1078. Params params;
  1079. params.emplace("json", JSON_DATA);
  1080. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  1081. ASSERT_TRUE(res != nullptr);
  1082. ASSERT_EQ(200, res->status);
  1083. ASSERT_EQ(JSON_DATA, res->body);
  1084. }
  1085. TEST_F(ServerTest, PostEmptyContent) {
  1086. auto res = cli_.Post("/empty", "", "text/plain");
  1087. ASSERT_TRUE(res != nullptr);
  1088. ASSERT_EQ(200, res->status);
  1089. ASSERT_EQ("empty", res->body);
  1090. }
  1091. TEST_F(ServerTest, GetMethodDir) {
  1092. auto res = cli_.Get("/dir/");
  1093. ASSERT_TRUE(res != nullptr);
  1094. EXPECT_EQ(200, res->status);
  1095. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1096. auto body = R"(<html>
  1097. <head>
  1098. </head>
  1099. <body>
  1100. <a href="/dir/test.html">Test</a>
  1101. <a href="/hi">hi</a>
  1102. </body>
  1103. </html>
  1104. )";
  1105. EXPECT_EQ(body, res->body);
  1106. }
  1107. TEST_F(ServerTest, GetMethodDirTest) {
  1108. auto res = cli_.Get("/dir/test.html");
  1109. ASSERT_TRUE(res != nullptr);
  1110. EXPECT_EQ(200, res->status);
  1111. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1112. EXPECT_EQ("test.html", res->body);
  1113. }
  1114. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  1115. auto res = cli_.Get("/dir/../dir/test.html");
  1116. ASSERT_TRUE(res != nullptr);
  1117. EXPECT_EQ(200, res->status);
  1118. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1119. EXPECT_EQ("test.html", res->body);
  1120. }
  1121. TEST_F(ServerTest, GetMethodInvalidPath) {
  1122. auto res = cli_.Get("/dir/../test.html");
  1123. ASSERT_TRUE(res != nullptr);
  1124. EXPECT_EQ(404, res->status);
  1125. }
  1126. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  1127. auto res = cli_.Get("/../www/dir/test.html");
  1128. ASSERT_TRUE(res != nullptr);
  1129. EXPECT_EQ(404, res->status);
  1130. }
  1131. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  1132. auto res = cli_.Get("/dir/../../www/dir/test.html");
  1133. ASSERT_TRUE(res != nullptr);
  1134. EXPECT_EQ(404, res->status);
  1135. }
  1136. TEST_F(ServerTest, GetMethodDirMountTest) {
  1137. auto res = cli_.Get("/mount/dir/test.html");
  1138. ASSERT_TRUE(res != nullptr);
  1139. EXPECT_EQ(200, res->status);
  1140. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1141. EXPECT_EQ("test.html", res->body);
  1142. }
  1143. TEST_F(ServerTest, GetMethodDirMountTestWithDoubleDots) {
  1144. auto res = cli_.Get("/mount/dir/../dir/test.html");
  1145. ASSERT_TRUE(res != nullptr);
  1146. EXPECT_EQ(200, res->status);
  1147. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1148. EXPECT_EQ("test.html", res->body);
  1149. }
  1150. TEST_F(ServerTest, GetMethodInvalidMountPath) {
  1151. auto res = cli_.Get("/mount/dir/../test.html");
  1152. ASSERT_TRUE(res != nullptr);
  1153. EXPECT_EQ(404, res->status);
  1154. }
  1155. TEST_F(ServerTest, GetMethodOutOfBaseDirMount) {
  1156. auto res = cli_.Get("/mount/../www2/dir/test.html");
  1157. ASSERT_TRUE(res != nullptr);
  1158. EXPECT_EQ(404, res->status);
  1159. }
  1160. TEST_F(ServerTest, GetMethodOutOfBaseDirMount2) {
  1161. auto res = cli_.Get("/mount/dir/../../www2/dir/test.html");
  1162. ASSERT_TRUE(res != nullptr);
  1163. EXPECT_EQ(404, res->status);
  1164. }
  1165. TEST_F(ServerTest, PostMethod303) {
  1166. auto res = cli_.Post("/1", "body", "text/plain");
  1167. ASSERT_TRUE(res != nullptr);
  1168. EXPECT_EQ(303, res->status);
  1169. EXPECT_EQ("/2", res->get_header_value("Location"));
  1170. }
  1171. TEST_F(ServerTest, PostMethod303Redirect) {
  1172. cli_.set_follow_location(true);
  1173. auto res = cli_.Post("/1", "body", "text/plain");
  1174. ASSERT_TRUE(res != nullptr);
  1175. EXPECT_EQ(200, res->status);
  1176. EXPECT_EQ("redirected.", res->body);
  1177. }
  1178. TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
  1179. auto res = cli_.Get("/dir/test.abcde");
  1180. ASSERT_TRUE(res != nullptr);
  1181. EXPECT_EQ(200, res->status);
  1182. EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
  1183. EXPECT_EQ("abcde", res->body);
  1184. }
  1185. TEST_F(ServerTest, InvalidBaseDirMount) {
  1186. EXPECT_EQ(false, svr_.set_mount_point("invalid_mount_point", "./www3"));
  1187. }
  1188. TEST_F(ServerTest, EmptyRequest) {
  1189. auto res = cli_.Get("");
  1190. ASSERT_TRUE(res == nullptr);
  1191. }
  1192. TEST_F(ServerTest, LongRequest) {
  1193. std::string request;
  1194. for (size_t i = 0; i < 545; i++) {
  1195. request += "/TooLongRequest";
  1196. }
  1197. request += "OK";
  1198. auto res = cli_.Get(request.c_str());
  1199. ASSERT_TRUE(res != nullptr);
  1200. EXPECT_EQ(404, res->status);
  1201. }
  1202. TEST_F(ServerTest, TooLongRequest) {
  1203. std::string request;
  1204. for (size_t i = 0; i < 545; i++) {
  1205. request += "/TooLongRequest";
  1206. }
  1207. request += "_NG";
  1208. auto res = cli_.Get(request.c_str());
  1209. ASSERT_TRUE(res != nullptr);
  1210. EXPECT_EQ(414, res->status);
  1211. }
  1212. TEST_F(ServerTest, LongHeader) {
  1213. Request req;
  1214. req.method = "GET";
  1215. req.path = "/hi";
  1216. std::string host_and_port;
  1217. host_and_port += HOST;
  1218. host_and_port += ":";
  1219. host_and_port += std::to_string(PORT);
  1220. req.headers.emplace("Host", host_and_port.c_str());
  1221. req.headers.emplace("Accept", "*/*");
  1222. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1223. req.headers.emplace(
  1224. "Header-Name",
  1225. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1226. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1227. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1228. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1229. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1230. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1231. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1232. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1233. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1234. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1235. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1236. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1237. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1238. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1239. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1240. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1241. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1242. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1243. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1244. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1245. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1246. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1247. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1248. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1249. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1250. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1251. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1252. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1253. "@@@@@@@@@@@@@@@@");
  1254. auto res = std::make_shared<Response>();
  1255. auto ret = cli_.send(req, *res);
  1256. ASSERT_TRUE(ret);
  1257. EXPECT_EQ(200, res->status);
  1258. }
  1259. TEST_F(ServerTest, LongQueryValue) {
  1260. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  1261. ASSERT_TRUE(res != nullptr);
  1262. EXPECT_EQ(414, res->status);
  1263. }
  1264. TEST_F(ServerTest, TooLongHeader) {
  1265. Request req;
  1266. req.method = "GET";
  1267. req.path = "/hi";
  1268. std::string host_and_port;
  1269. host_and_port += HOST;
  1270. host_and_port += ":";
  1271. host_and_port += std::to_string(PORT);
  1272. req.headers.emplace("Host", host_and_port.c_str());
  1273. req.headers.emplace("Accept", "*/*");
  1274. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1275. req.headers.emplace(
  1276. "Header-Name",
  1277. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1278. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1279. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1280. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1281. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1282. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1283. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1284. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1285. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1286. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1287. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1288. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1289. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1290. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1291. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1292. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1293. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1294. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1295. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1296. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1297. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1298. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1299. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1300. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1301. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1302. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1303. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1304. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1305. "@@@@@@@@@@@@@@@@@");
  1306. auto res = std::make_shared<Response>();
  1307. auto ret = cli_.send(req, *res);
  1308. ASSERT_TRUE(ret);
  1309. EXPECT_EQ(200, res->status);
  1310. }
  1311. TEST_F(ServerTest, PercentEncoding) {
  1312. auto res = cli_.Get("/e%6edwith%");
  1313. ASSERT_TRUE(res != nullptr);
  1314. EXPECT_EQ(200, res->status);
  1315. }
  1316. TEST_F(ServerTest, PercentEncodingUnicode) {
  1317. auto res = cli_.Get("/e%u006edwith%");
  1318. ASSERT_TRUE(res != nullptr);
  1319. EXPECT_EQ(200, res->status);
  1320. }
  1321. TEST_F(ServerTest, InvalidPercentEncoding) {
  1322. auto res = cli_.Get("/%endwith%");
  1323. ASSERT_TRUE(res != nullptr);
  1324. EXPECT_EQ(404, res->status);
  1325. }
  1326. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  1327. auto res = cli_.Get("/%uendwith%");
  1328. ASSERT_TRUE(res != nullptr);
  1329. EXPECT_EQ(404, res->status);
  1330. }
  1331. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  1332. auto res = cli_.Get("/hello?aaa=bbb%");
  1333. ASSERT_TRUE(res != nullptr);
  1334. EXPECT_EQ(404, res->status);
  1335. }
  1336. TEST_F(ServerTest, PlusSignEncoding) {
  1337. auto res = cli_.Get("/a+%2Bb?a %2bb=a %2Bb");
  1338. ASSERT_TRUE(res != nullptr);
  1339. EXPECT_EQ(200, res->status);
  1340. EXPECT_EQ("a +b", res->body);
  1341. }
  1342. TEST_F(ServerTest, MultipartFormData) {
  1343. MultipartFormDataItems items = {
  1344. {"text1", "text default", "", ""},
  1345. {"text2", "aωb", "", ""},
  1346. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1347. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1348. {"file3", "", "", "application/octet-stream"},
  1349. };
  1350. auto res = cli_.Post("/multipart", items);
  1351. ASSERT_TRUE(res != nullptr);
  1352. EXPECT_EQ(200, res->status);
  1353. }
  1354. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  1355. auto res = cli_.Get("/hi");
  1356. ASSERT_TRUE(res != nullptr);
  1357. EXPECT_EQ(200, res->status);
  1358. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  1359. EXPECT_EQ("Hello World!", res->body);
  1360. }
  1361. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  1362. Request req;
  1363. req.method = "POST";
  1364. req.path = "/chunked";
  1365. std::string host_and_port;
  1366. host_and_port += HOST;
  1367. host_and_port += ":";
  1368. host_and_port += std::to_string(PORT);
  1369. req.headers.emplace("Host", host_and_port.c_str());
  1370. req.headers.emplace("Accept", "*/*");
  1371. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1372. req.headers.emplace("Content-Type", "text/plain");
  1373. req.headers.emplace("Content-Length", "0");
  1374. req.headers.emplace(
  1375. "Transfer-Encoding",
  1376. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  1377. // Client does not chunk, so make a chunked body manually.
  1378. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  1379. auto res = std::make_shared<Response>();
  1380. auto ret = cli_.send(req, *res);
  1381. ASSERT_TRUE(ret);
  1382. EXPECT_EQ(200, res->status);
  1383. }
  1384. TEST_F(ServerTest, GetStreamed2) {
  1385. auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
  1386. ASSERT_TRUE(res != nullptr);
  1387. EXPECT_EQ(206, res->status);
  1388. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1389. EXPECT_EQ(std::string("ab"), res->body);
  1390. }
  1391. TEST_F(ServerTest, GetStreamed) {
  1392. auto res = cli_.Get("/streamed");
  1393. ASSERT_TRUE(res != nullptr);
  1394. EXPECT_EQ(200, res->status);
  1395. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1396. EXPECT_EQ(std::string("aaabbb"), res->body);
  1397. }
  1398. TEST_F(ServerTest, GetStreamedWithRange1) {
  1399. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
  1400. ASSERT_TRUE(res != nullptr);
  1401. EXPECT_EQ(206, res->status);
  1402. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1403. EXPECT_EQ(true, res->has_header("Content-Range"));
  1404. EXPECT_EQ(std::string("def"), res->body);
  1405. }
  1406. TEST_F(ServerTest, GetStreamedWithRange2) {
  1407. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
  1408. ASSERT_TRUE(res != nullptr);
  1409. EXPECT_EQ(206, res->status);
  1410. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1411. EXPECT_EQ(true, res->has_header("Content-Range"));
  1412. EXPECT_EQ(std::string("bcdefg"), res->body);
  1413. }
  1414. TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
  1415. auto res =
  1416. cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1417. ASSERT_TRUE(res != nullptr);
  1418. EXPECT_EQ(206, res->status);
  1419. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1420. EXPECT_EQ(false, res->has_header("Content-Range"));
  1421. EXPECT_EQ(269, res->body.size());
  1422. }
  1423. TEST_F(ServerTest, GetStreamedEndless) {
  1424. uint64_t offset = 0;
  1425. auto res = cli_.Get("/streamed-cancel",
  1426. [&](const char * /*data*/, uint64_t data_length) {
  1427. if (offset < 100) {
  1428. offset += data_length;
  1429. return true;
  1430. }
  1431. return false;
  1432. });
  1433. ASSERT_TRUE(res == nullptr);
  1434. }
  1435. TEST_F(ServerTest, GetWithRange1) {
  1436. auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
  1437. ASSERT_TRUE(res != nullptr);
  1438. EXPECT_EQ(206, res->status);
  1439. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1440. EXPECT_EQ(true, res->has_header("Content-Range"));
  1441. EXPECT_EQ(std::string("def"), res->body);
  1442. }
  1443. TEST_F(ServerTest, GetWithRange2) {
  1444. auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
  1445. ASSERT_TRUE(res != nullptr);
  1446. EXPECT_EQ(206, res->status);
  1447. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1448. EXPECT_EQ(true, res->has_header("Content-Range"));
  1449. EXPECT_EQ(std::string("bcdefg"), res->body);
  1450. }
  1451. TEST_F(ServerTest, GetWithRange3) {
  1452. auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
  1453. ASSERT_TRUE(res != nullptr);
  1454. EXPECT_EQ(206, res->status);
  1455. EXPECT_EQ("1", res->get_header_value("Content-Length"));
  1456. EXPECT_EQ(true, res->has_header("Content-Range"));
  1457. EXPECT_EQ(std::string("a"), res->body);
  1458. }
  1459. TEST_F(ServerTest, GetWithRange4) {
  1460. auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
  1461. ASSERT_TRUE(res != nullptr);
  1462. EXPECT_EQ(206, res->status);
  1463. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1464. EXPECT_EQ(true, res->has_header("Content-Range"));
  1465. EXPECT_EQ(std::string("fg"), res->body);
  1466. }
  1467. TEST_F(ServerTest, GetWithRangeMultipart) {
  1468. auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1469. ASSERT_TRUE(res != nullptr);
  1470. EXPECT_EQ(206, res->status);
  1471. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1472. EXPECT_EQ(false, res->has_header("Content-Range"));
  1473. EXPECT_EQ(269, res->body.size());
  1474. }
  1475. TEST_F(ServerTest, GetStreamedChunked) {
  1476. auto res = cli_.Get("/streamed-chunked");
  1477. ASSERT_TRUE(res != nullptr);
  1478. EXPECT_EQ(200, res->status);
  1479. EXPECT_EQ(std::string("123456789"), res->body);
  1480. }
  1481. TEST_F(ServerTest, GetStreamedChunked2) {
  1482. auto res = cli_.Get("/streamed-chunked2");
  1483. ASSERT_TRUE(res != nullptr);
  1484. EXPECT_EQ(200, res->status);
  1485. EXPECT_EQ(std::string("123456789"), res->body);
  1486. }
  1487. TEST_F(ServerTest, LargeChunkedPost) {
  1488. Request req;
  1489. req.method = "POST";
  1490. req.path = "/large-chunked";
  1491. std::string host_and_port;
  1492. host_and_port += HOST;
  1493. host_and_port += ":";
  1494. host_and_port += std::to_string(PORT);
  1495. req.headers.emplace("Host", host_and_port.c_str());
  1496. req.headers.emplace("Accept", "*/*");
  1497. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1498. req.headers.emplace("Content-Type", "text/plain");
  1499. req.headers.emplace("Content-Length", "0");
  1500. req.headers.emplace("Transfer-Encoding", "chunked");
  1501. std::string long_string(30 * 1024u, 'a');
  1502. std::string chunk = "7800\r\n" + long_string + "\r\n";
  1503. // Attempt to make a large enough post to exceed OS buffers, to test that
  1504. // the server handles short reads if the full chunk data isn't available.
  1505. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  1506. auto res = std::make_shared<Response>();
  1507. auto ret = cli_.send(req, *res);
  1508. ASSERT_TRUE(ret);
  1509. EXPECT_EQ(200, res->status);
  1510. }
  1511. TEST_F(ServerTest, GetMethodRemoteAddr) {
  1512. auto res = cli_.Get("/remote_addr");
  1513. ASSERT_TRUE(res != nullptr);
  1514. EXPECT_EQ(200, res->status);
  1515. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1516. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  1517. }
  1518. TEST_F(ServerTest, HTTPResponseSplitting) {
  1519. auto res = cli_.Get("/http_response_splitting");
  1520. ASSERT_TRUE(res != nullptr);
  1521. EXPECT_EQ(200, res->status);
  1522. }
  1523. TEST_F(ServerTest, SlowRequest) {
  1524. request_threads_.push_back(
  1525. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1526. request_threads_.push_back(
  1527. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1528. request_threads_.push_back(
  1529. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1530. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1531. }
  1532. TEST_F(ServerTest, Put) {
  1533. auto res = cli_.Put("/put", "PUT", "text/plain");
  1534. ASSERT_TRUE(res != nullptr);
  1535. EXPECT_EQ(200, res->status);
  1536. EXPECT_EQ("PUT", res->body);
  1537. }
  1538. TEST_F(ServerTest, PutWithContentProvider) {
  1539. auto res = cli_.Put(
  1540. "/put", 3,
  1541. [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
  1542. ASSERT_TRUE(sink.is_writable());
  1543. sink.write("PUT", 3);
  1544. },
  1545. "text/plain");
  1546. ASSERT_TRUE(res != nullptr);
  1547. EXPECT_EQ(200, res->status);
  1548. EXPECT_EQ("PUT", res->body);
  1549. }
  1550. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1551. TEST_F(ServerTest, PutWithContentProviderWithGzip) {
  1552. cli_.set_compress(true);
  1553. auto res = cli_.Put(
  1554. "/put", 3,
  1555. [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
  1556. ASSERT_TRUE(sink.is_writable());
  1557. sink.write("PUT", 3);
  1558. },
  1559. "text/plain");
  1560. ASSERT_TRUE(res != nullptr);
  1561. EXPECT_EQ(200, res->status);
  1562. EXPECT_EQ("PUT", res->body);
  1563. }
  1564. TEST_F(ServerTest, PutLargeFileWithGzip) {
  1565. cli_.set_compress(true);
  1566. auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain");
  1567. ASSERT_TRUE(res != nullptr);
  1568. EXPECT_EQ(200, res->status);
  1569. EXPECT_EQ(LARGE_DATA, res->body);
  1570. }
  1571. TEST_F(ServerTest, PutContentWithDeflate) {
  1572. cli_.set_compress(false);
  1573. httplib::Headers headers;
  1574. headers.emplace("Content-Encoding", "deflate");
  1575. // PUT in deflate format:
  1576. auto res = cli_.Put("/put", headers,
  1577. "\170\234\013\010\015\001\0\001\361\0\372", "text/plain");
  1578. ASSERT_TRUE(res != nullptr);
  1579. EXPECT_EQ(200, res->status);
  1580. EXPECT_EQ("PUT", res->body);
  1581. }
  1582. #endif
  1583. TEST_F(ServerTest, Patch) {
  1584. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  1585. ASSERT_TRUE(res != nullptr);
  1586. EXPECT_EQ(200, res->status);
  1587. EXPECT_EQ("PATCH", res->body);
  1588. }
  1589. TEST_F(ServerTest, Delete) {
  1590. auto res = cli_.Delete("/delete");
  1591. ASSERT_TRUE(res != nullptr);
  1592. EXPECT_EQ(200, res->status);
  1593. EXPECT_EQ("DELETE", res->body);
  1594. }
  1595. TEST_F(ServerTest, DeleteContentReceiver) {
  1596. auto res = cli_.Delete("/delete-body", "content", "text/plain");
  1597. ASSERT_TRUE(res != nullptr);
  1598. EXPECT_EQ(200, res->status);
  1599. EXPECT_EQ("content", res->body);
  1600. }
  1601. TEST_F(ServerTest, Options) {
  1602. auto res = cli_.Options("*");
  1603. ASSERT_TRUE(res != nullptr);
  1604. EXPECT_EQ(200, res->status);
  1605. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  1606. EXPECT_TRUE(res->body.empty());
  1607. }
  1608. TEST_F(ServerTest, URL) {
  1609. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  1610. ASSERT_TRUE(res != nullptr);
  1611. EXPECT_EQ(200, res->status);
  1612. }
  1613. TEST_F(ServerTest, ArrayParam) {
  1614. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  1615. ASSERT_TRUE(res != nullptr);
  1616. EXPECT_EQ(200, res->status);
  1617. }
  1618. TEST_F(ServerTest, NoMultipleHeaders) {
  1619. Headers headers = {{"Content-Length", "5"}};
  1620. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1621. "text/plain");
  1622. ASSERT_TRUE(res != nullptr);
  1623. EXPECT_EQ(200, res->status);
  1624. }
  1625. TEST_F(ServerTest, PostContentReceiver) {
  1626. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1627. ASSERT_TRUE(res != nullptr);
  1628. ASSERT_EQ(200, res->status);
  1629. ASSERT_EQ("content", res->body);
  1630. }
  1631. TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
  1632. MultipartFormDataItems items = {
  1633. {"text1", "text default", "", ""},
  1634. {"text2", "aωb", "", ""},
  1635. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1636. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1637. {"file3", "", "", "application/octet-stream"},
  1638. };
  1639. auto res = cli_.Post("/content_receiver", items);
  1640. ASSERT_TRUE(res != nullptr);
  1641. EXPECT_EQ(200, res->status);
  1642. }
  1643. TEST_F(ServerTest, PostContentReceiverGzip) {
  1644. cli_.set_compress(true);
  1645. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1646. ASSERT_TRUE(res != nullptr);
  1647. ASSERT_EQ(200, res->status);
  1648. ASSERT_EQ("content", res->body);
  1649. }
  1650. TEST_F(ServerTest, PutContentReceiver) {
  1651. auto res = cli_.Put("/content_receiver", "content", "text/plain");
  1652. ASSERT_TRUE(res != nullptr);
  1653. ASSERT_EQ(200, res->status);
  1654. ASSERT_EQ("content", res->body);
  1655. }
  1656. TEST_F(ServerTest, PatchContentReceiver) {
  1657. auto res = cli_.Patch("/content_receiver", "content", "text/plain");
  1658. ASSERT_TRUE(res != nullptr);
  1659. ASSERT_EQ(200, res->status);
  1660. ASSERT_EQ("content", res->body);
  1661. }
  1662. TEST_F(ServerTest, PostQueryStringAndBody) {
  1663. auto res =
  1664. cli_.Post("/query-string-and-body?key=value", "content", "text/plain");
  1665. ASSERT_TRUE(res != nullptr);
  1666. ASSERT_EQ(200, res->status);
  1667. }
  1668. TEST_F(ServerTest, HTTP2Magic) {
  1669. Request req;
  1670. req.method = "PRI";
  1671. req.path = "*";
  1672. req.body = "SM";
  1673. auto res = std::make_shared<Response>();
  1674. auto ret = cli_.send(req, *res);
  1675. ASSERT_TRUE(ret);
  1676. EXPECT_EQ(400, res->status);
  1677. }
  1678. TEST_F(ServerTest, KeepAlive) {
  1679. cli_.set_keep_alive_max_count(4);
  1680. std::vector<Request> requests;
  1681. Get(requests, "/hi");
  1682. Get(requests, "/hi");
  1683. Get(requests, "/hi");
  1684. Get(requests, "/not-exist");
  1685. Post(requests, "/empty", "", "text/plain");
  1686. std::vector<Response> responses;
  1687. auto ret = cli_.send(requests, responses);
  1688. ASSERT_TRUE(ret == true);
  1689. ASSERT_TRUE(requests.size() == responses.size());
  1690. for (size_t i = 0; i < 3; i++) {
  1691. auto &res = responses[i];
  1692. EXPECT_EQ(200, res.status);
  1693. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1694. EXPECT_EQ("Hello World!", res.body);
  1695. }
  1696. {
  1697. auto &res = responses[3];
  1698. EXPECT_EQ(404, res.status);
  1699. }
  1700. {
  1701. auto &res = responses[4];
  1702. EXPECT_EQ(200, res.status);
  1703. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1704. EXPECT_EQ("empty", res.body);
  1705. }
  1706. }
  1707. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1708. TEST_F(ServerTest, Gzip) {
  1709. Headers headers;
  1710. headers.emplace("Accept-Encoding", "gzip, deflate");
  1711. auto res = cli_.Get("/gzip", headers);
  1712. ASSERT_TRUE(res != nullptr);
  1713. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1714. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1715. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1716. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1717. "7890123456789012345678901234567890",
  1718. res->body);
  1719. EXPECT_EQ(200, res->status);
  1720. }
  1721. TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
  1722. Headers headers;
  1723. auto res = cli_.Get("/gzip", headers);
  1724. ASSERT_TRUE(res != nullptr);
  1725. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1726. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1727. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1728. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1729. "7890123456789012345678901234567890",
  1730. res->body);
  1731. EXPECT_EQ(200, res->status);
  1732. }
  1733. TEST_F(ServerTest, GzipWithContentReceiver) {
  1734. Headers headers;
  1735. headers.emplace("Accept-Encoding", "gzip, deflate");
  1736. std::string body;
  1737. auto res =
  1738. cli_.Get("/gzip", headers, [&](const char *data, uint64_t data_length) {
  1739. EXPECT_EQ(data_length, 100);
  1740. body.append(data, data_length);
  1741. return true;
  1742. });
  1743. ASSERT_TRUE(res != nullptr);
  1744. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1745. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1746. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1747. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1748. "7890123456789012345678901234567890",
  1749. body);
  1750. EXPECT_EQ(200, res->status);
  1751. }
  1752. TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
  1753. Headers headers;
  1754. std::string body;
  1755. auto res =
  1756. cli_.Get("/gzip", headers, [&](const char *data, uint64_t data_length) {
  1757. EXPECT_EQ(data_length, 100);
  1758. body.append(data, data_length);
  1759. return true;
  1760. });
  1761. ASSERT_TRUE(res != nullptr);
  1762. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1763. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1764. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1765. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1766. "7890123456789012345678901234567890",
  1767. body);
  1768. EXPECT_EQ(200, res->status);
  1769. }
  1770. TEST_F(ServerTest, NoGzip) {
  1771. Headers headers;
  1772. headers.emplace("Accept-Encoding", "gzip, deflate");
  1773. auto res = cli_.Get("/nogzip", headers);
  1774. ASSERT_TRUE(res != nullptr);
  1775. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1776. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1777. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1778. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1779. "7890123456789012345678901234567890",
  1780. res->body);
  1781. EXPECT_EQ(200, res->status);
  1782. }
  1783. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1784. Headers headers;
  1785. headers.emplace("Accept-Encoding", "gzip, deflate");
  1786. std::string body;
  1787. auto res =
  1788. cli_.Get("/nogzip", headers, [&](const char *data, uint64_t data_length) {
  1789. EXPECT_EQ(data_length, 100);
  1790. body.append(data, data_length);
  1791. return true;
  1792. });
  1793. ASSERT_TRUE(res != nullptr);
  1794. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1795. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1796. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1797. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1798. "7890123456789012345678901234567890",
  1799. body);
  1800. EXPECT_EQ(200, res->status);
  1801. }
  1802. TEST_F(ServerTest, MultipartFormDataGzip) {
  1803. MultipartFormDataItems items = {
  1804. {"key1", "test", "", ""},
  1805. {"key2", "--abcdefg123", "", ""},
  1806. };
  1807. cli_.set_compress(true);
  1808. auto res = cli_.Post("/gzipmultipart", items);
  1809. ASSERT_TRUE(res != nullptr);
  1810. EXPECT_EQ(200, res->status);
  1811. }
  1812. #endif
  1813. // Sends a raw request to a server listening at HOST:PORT.
  1814. static bool send_request(time_t read_timeout_sec, const std::string &req) {
  1815. auto client_sock = detail::create_client_socket(HOST, PORT, /*timeout_sec=*/5,
  1816. std::string());
  1817. if (client_sock == INVALID_SOCKET) { return false; }
  1818. return detail::process_and_close_socket(
  1819. true, client_sock, 1, read_timeout_sec, 0,
  1820. [&](Stream &strm, bool /*last_connection*/, bool &
  1821. /*connection_close*/) -> bool {
  1822. if (req.size() !=
  1823. static_cast<size_t>(strm.write(req.data(), req.size()))) {
  1824. return false;
  1825. }
  1826. char buf[512];
  1827. detail::stream_line_reader line_reader(strm, buf, sizeof(buf));
  1828. while (line_reader.getline()) {}
  1829. return true;
  1830. });
  1831. }
  1832. TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
  1833. Server svr;
  1834. std::string header_value;
  1835. svr.Get("/validate-ws-in-headers", [&](const Request &req, Response &res) {
  1836. header_value = req.get_header_value("foo");
  1837. res.set_content("ok", "text/plain");
  1838. });
  1839. thread t = thread([&] { svr.listen(HOST, PORT); });
  1840. while (!svr.is_running()) {
  1841. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  1842. }
  1843. // Only space and horizontal tab are whitespace. Make sure other whitespace-
  1844. // like characters are not treated the same - use vertical tab and escape.
  1845. const std::string req = "GET /validate-ws-in-headers HTTP/1.1\r\n"
  1846. "foo: \t \v bar \e\t \r\n"
  1847. "Connection: close\r\n"
  1848. "\r\n";
  1849. ASSERT_TRUE(send_request(5, req));
  1850. svr.stop();
  1851. t.join();
  1852. EXPECT_EQ(header_value, "\v bar \e");
  1853. }
  1854. // Sends a raw request and verifies that there isn't a crash or exception.
  1855. static void test_raw_request(const std::string &req) {
  1856. Server svr;
  1857. svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1858. res.set_content("ok", "text/plain");
  1859. });
  1860. // Server read timeout must be longer than the client read timeout for the
  1861. // bug to reproduce, probably to force the server to process a request
  1862. // without a trailing blank line.
  1863. const time_t client_read_timeout_sec = 1;
  1864. svr.set_read_timeout(client_read_timeout_sec + 1, 0);
  1865. bool listen_thread_ok = false;
  1866. thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
  1867. while (!svr.is_running()) {
  1868. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  1869. }
  1870. ASSERT_TRUE(send_request(client_read_timeout_sec, req));
  1871. svr.stop();
  1872. t.join();
  1873. EXPECT_TRUE(listen_thread_ok);
  1874. }
  1875. TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
  1876. // A certain header line causes an exception if the header property is parsed
  1877. // naively with a single regex. This occurs with libc++ but not libstdc++.
  1878. test_raw_request(
  1879. "GET /hi HTTP/1.1\r\n"
  1880. " : "
  1881. " "
  1882. " ");
  1883. }
  1884. TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity2) {
  1885. // A certain header line causes an exception if the header property *name* is
  1886. // parsed with a regular expression starting with "(.+?):" - this is a non-
  1887. // greedy matcher and requires backtracking when there are a lot of ":"
  1888. // characters.
  1889. // This occurs with libc++ but not libstdc++.
  1890. test_raw_request(
  1891. "GET /hi HTTP/1.1\r\n"
  1892. ":-:::::::::::::::::::::::::::-::::::::::::::::::::::::@-&&&&&&&&&&&"
  1893. "--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&"
  1894. "&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-:::::"
  1895. "::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-::::::::::::::::::::::::"
  1896. ":::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::"
  1897. "::::::::-:::::::::::::::::@-&&&&&&&--:::::::-::::::::::::::::::::::"
  1898. ":::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::"
  1899. "::::::::::-:::::::::::::::::@-&&&&&::::::::::::-:::::::::::::::::@-"
  1900. "&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::::::"
  1901. ":@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::"
  1902. "::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::@-&&"
  1903. "&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@"
  1904. "::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&"
  1905. "--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&"
  1906. "&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&"
  1907. "&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&"
  1908. "&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@"
  1909. "-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::"
  1910. "::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::"
  1911. ":::::@-&&&&&&&&&&&::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-::::::"
  1912. ":::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::"
  1913. "::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-"
  1914. ":::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&---&&:&"
  1915. "&&.0------------:-:::::::::::::::::::::::::::::-:::::::::::::::::@-"
  1916. "&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::::::"
  1917. ":@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::"
  1918. "::::@-&&&&&&&&&&&---&&:&&&.0------------O--------\rH PUTHTTP/1.1\r\n"
  1919. "&&&%%%");
  1920. }
  1921. TEST(ServerStopTest, StopServerWithChunkedTransmission) {
  1922. Server svr;
  1923. svr.Get("/events", [](const Request & /*req*/, Response &res) {
  1924. res.set_header("Content-Type", "text/event-stream");
  1925. res.set_header("Cache-Control", "no-cache");
  1926. res.set_chunked_content_provider([](size_t offset, const DataSink &sink) {
  1927. char buffer[27];
  1928. auto size = static_cast<size_t>(sprintf(buffer, "data:%ld\n\n", offset));
  1929. sink.write(buffer, size);
  1930. std::this_thread::sleep_for(std::chrono::seconds(1));
  1931. });
  1932. });
  1933. auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
  1934. while (!svr.is_running()) {
  1935. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  1936. }
  1937. Client client(HOST, PORT);
  1938. const Headers headers = {{"Accept", "text/event-stream"},
  1939. {"Connection", "Keep-Alive"}};
  1940. auto get_thread = std::thread([&client, &headers]() {
  1941. std::shared_ptr<Response> res = client.Get(
  1942. "/events", headers,
  1943. [](const char * /*data*/, size_t /*len*/) -> bool { return true; });
  1944. });
  1945. // Give GET time to get a few messages.
  1946. std::this_thread::sleep_for(std::chrono::seconds(2));
  1947. svr.stop();
  1948. listen_thread.join();
  1949. get_thread.join();
  1950. ASSERT_FALSE(svr.is_running());
  1951. }
  1952. TEST(MountTest, Unmount) {
  1953. Server svr;
  1954. auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
  1955. while (!svr.is_running()) {
  1956. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  1957. }
  1958. // Give GET time to get a few messages.
  1959. std::this_thread::sleep_for(std::chrono::seconds(1));
  1960. Client cli("localhost", PORT);
  1961. svr.set_mount_point("/mount2", "./www2");
  1962. auto res = cli.Get("/");
  1963. ASSERT_TRUE(res != nullptr);
  1964. EXPECT_EQ(404, res->status);
  1965. res = cli.Get("/mount2/dir/test.html");
  1966. ASSERT_TRUE(res != nullptr);
  1967. EXPECT_EQ(200, res->status);
  1968. svr.set_mount_point("/", "./www");
  1969. res = cli.Get("/dir/");
  1970. ASSERT_TRUE(res != nullptr);
  1971. EXPECT_EQ(200, res->status);
  1972. svr.remove_mount_point("/");
  1973. res = cli.Get("/dir/");
  1974. ASSERT_TRUE(res != nullptr);
  1975. EXPECT_EQ(404, res->status);
  1976. svr.remove_mount_point("/mount2");
  1977. res = cli.Get("/mount2/dir/test.html");
  1978. ASSERT_TRUE(res != nullptr);
  1979. EXPECT_EQ(404, res->status);
  1980. svr.stop();
  1981. listen_thread.join();
  1982. ASSERT_FALSE(svr.is_running());
  1983. }
  1984. TEST(ExceptionTest, ThrowExceptionInHandler) {
  1985. Server svr;
  1986. svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1987. throw std::runtime_error("exception...");
  1988. res.set_content("Hello World!", "text/plain");
  1989. });
  1990. auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
  1991. while (!svr.is_running()) {
  1992. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  1993. }
  1994. // Give GET time to get a few messages.
  1995. std::this_thread::sleep_for(std::chrono::seconds(1));
  1996. Client cli("localhost", PORT);
  1997. auto res = cli.Get("/hi");
  1998. ASSERT_TRUE(res != nullptr);
  1999. EXPECT_EQ(500, res->status);
  2000. ASSERT_FALSE(res->has_header("EXCEPTION_WHAT"));
  2001. svr.stop();
  2002. listen_thread.join();
  2003. ASSERT_FALSE(svr.is_running());
  2004. }
  2005. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  2006. protected:
  2007. ServerTestWithAI_PASSIVE()
  2008. : cli_(HOST, PORT)
  2009. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2010. ,
  2011. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  2012. #endif
  2013. {
  2014. }
  2015. virtual void SetUp() {
  2016. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  2017. res.set_content("Hello World!", "text/plain");
  2018. });
  2019. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  2020. while (!svr_.is_running()) {
  2021. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2022. }
  2023. }
  2024. virtual void TearDown() {
  2025. svr_.stop();
  2026. t_.join();
  2027. }
  2028. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2029. SSLClient cli_;
  2030. SSLServer svr_;
  2031. #else
  2032. Client cli_;
  2033. Server svr_;
  2034. #endif
  2035. thread t_;
  2036. };
  2037. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  2038. auto res = cli_.Get("/hi");
  2039. ASSERT_TRUE(res != nullptr);
  2040. EXPECT_EQ(200, res->status);
  2041. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  2042. EXPECT_EQ("Hello World!", res->body);
  2043. }
  2044. class ServerUpDownTest : public ::testing::Test {
  2045. protected:
  2046. ServerUpDownTest() : cli_(HOST, PORT) {}
  2047. virtual void SetUp() {
  2048. t_ = thread([&]() {
  2049. svr_.bind_to_any_port(HOST);
  2050. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  2051. ASSERT_TRUE(svr_.listen_after_bind());
  2052. });
  2053. while (!svr_.is_running()) {
  2054. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2055. }
  2056. }
  2057. virtual void TearDown() {
  2058. svr_.stop();
  2059. t_.join();
  2060. }
  2061. Client cli_;
  2062. Server svr_;
  2063. thread t_;
  2064. };
  2065. TEST_F(ServerUpDownTest, QuickStartStop) {
  2066. // Should not crash, especially when run with
  2067. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  2068. }
  2069. class PayloadMaxLengthTest : public ::testing::Test {
  2070. protected:
  2071. PayloadMaxLengthTest()
  2072. : cli_(HOST, PORT)
  2073. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2074. ,
  2075. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  2076. #endif
  2077. {
  2078. }
  2079. virtual void SetUp() {
  2080. svr_.set_payload_max_length(8);
  2081. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  2082. res.set_content("test", "text/plain");
  2083. });
  2084. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  2085. while (!svr_.is_running()) {
  2086. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2087. }
  2088. }
  2089. virtual void TearDown() {
  2090. svr_.stop();
  2091. t_.join();
  2092. }
  2093. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2094. SSLClient cli_;
  2095. SSLServer svr_;
  2096. #else
  2097. Client cli_;
  2098. Server svr_;
  2099. #endif
  2100. thread t_;
  2101. };
  2102. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  2103. auto res = cli_.Post("/test", "123456789", "text/plain");
  2104. ASSERT_TRUE(res != nullptr);
  2105. EXPECT_EQ(413, res->status);
  2106. res = cli_.Post("/test", "12345678", "text/plain");
  2107. ASSERT_TRUE(res != nullptr);
  2108. EXPECT_EQ(200, res->status);
  2109. }
  2110. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2111. TEST(SSLClientTest, ServerNameIndication) {
  2112. SSLClient cli("httpbin.org", 443);
  2113. auto res = cli.Get("/get");
  2114. ASSERT_TRUE(res != nullptr);
  2115. ASSERT_EQ(200, res->status);
  2116. }
  2117. TEST(SSLClientTest, ServerCertificateVerification) {
  2118. SSLClient cli("google.com");
  2119. auto res = cli.Get("/");
  2120. ASSERT_TRUE(res != nullptr);
  2121. ASSERT_EQ(301, res->status);
  2122. cli.enable_server_certificate_verification(true);
  2123. res = cli.Get("/");
  2124. ASSERT_TRUE(res == nullptr);
  2125. cli.set_ca_cert_path(CA_CERT_FILE);
  2126. res = cli.Get("/");
  2127. ASSERT_TRUE(res != nullptr);
  2128. ASSERT_EQ(301, res->status);
  2129. }
  2130. TEST(SSLClientTest, WildcardHostNameMatch) {
  2131. SSLClient cli("www.youtube.com");
  2132. cli.set_ca_cert_path(CA_CERT_FILE);
  2133. cli.enable_server_certificate_verification(true);
  2134. auto res = cli.Get("/");
  2135. ASSERT_TRUE(res != nullptr);
  2136. ASSERT_EQ(200, res->status);
  2137. }
  2138. TEST(SSLClientServerTest, ClientCertPresent) {
  2139. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  2140. CLIENT_CA_CERT_DIR);
  2141. ASSERT_TRUE(svr.is_valid());
  2142. svr.Get("/test", [&](const Request &req, Response &res) {
  2143. res.set_content("test", "text/plain");
  2144. svr.stop();
  2145. ASSERT_TRUE(true);
  2146. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  2147. ASSERT_TRUE(peer_cert != nullptr);
  2148. auto subject_name = X509_get_subject_name(peer_cert);
  2149. ASSERT_TRUE(subject_name != nullptr);
  2150. std::string common_name;
  2151. {
  2152. char name[BUFSIZ];
  2153. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  2154. name, sizeof(name));
  2155. common_name.assign(name, static_cast<size_t>(name_len));
  2156. }
  2157. EXPECT_EQ("Common Name", common_name);
  2158. X509_free(peer_cert);
  2159. });
  2160. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  2161. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2162. httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
  2163. auto res = cli.Get("/test");
  2164. cli.set_timeout_sec(30);
  2165. ASSERT_TRUE(res != nullptr);
  2166. ASSERT_EQ(200, res->status);
  2167. t.join();
  2168. }
  2169. TEST(SSLClientServerTest, MemoryClientCertPresent) {
  2170. X509 *server_cert;
  2171. EVP_PKEY *server_private_key;
  2172. X509_STORE *client_ca_cert_store;
  2173. X509 *client_cert;
  2174. EVP_PKEY *client_private_key;
  2175. FILE *f = fopen(SERVER_CERT_FILE, "r+");
  2176. server_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
  2177. fclose(f);
  2178. f = fopen(SERVER_PRIVATE_KEY_FILE, "r+");
  2179. server_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr);
  2180. fclose(f);
  2181. f = fopen(CLIENT_CA_CERT_FILE, "r+");
  2182. client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
  2183. client_ca_cert_store = X509_STORE_new();
  2184. X509_STORE_add_cert(client_ca_cert_store, client_cert);
  2185. X509_free(client_cert);
  2186. fclose(f);
  2187. f = fopen(CLIENT_CERT_FILE, "r+");
  2188. client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
  2189. fclose(f);
  2190. f = fopen(CLIENT_PRIVATE_KEY_FILE, "r+");
  2191. client_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr);
  2192. fclose(f);
  2193. SSLServer svr(server_cert, server_private_key, client_ca_cert_store);
  2194. ASSERT_TRUE(svr.is_valid());
  2195. svr.Get("/test", [&](const Request &req, Response &res) {
  2196. res.set_content("test", "text/plain");
  2197. svr.stop();
  2198. ASSERT_TRUE(true);
  2199. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  2200. ASSERT_TRUE(peer_cert != nullptr);
  2201. auto subject_name = X509_get_subject_name(peer_cert);
  2202. ASSERT_TRUE(subject_name != nullptr);
  2203. std::string common_name;
  2204. {
  2205. char name[BUFSIZ];
  2206. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  2207. name, sizeof(name));
  2208. common_name.assign(name, static_cast<size_t>(name_len));
  2209. }
  2210. EXPECT_EQ("Common Name", common_name);
  2211. X509_free(peer_cert);
  2212. });
  2213. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  2214. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2215. httplib::SSLClient cli(HOST, PORT, client_cert, client_private_key);
  2216. auto res = cli.Get("/test");
  2217. cli.set_timeout_sec(30);
  2218. ASSERT_TRUE(res != nullptr);
  2219. ASSERT_EQ(200, res->status);
  2220. X509_free(server_cert);
  2221. EVP_PKEY_free(server_private_key);
  2222. X509_free(client_cert);
  2223. EVP_PKEY_free(client_private_key);
  2224. t.join();
  2225. }
  2226. TEST(SSLClientServerTest, ClientCertMissing) {
  2227. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  2228. CLIENT_CA_CERT_DIR);
  2229. ASSERT_TRUE(svr.is_valid());
  2230. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  2231. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  2232. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2233. httplib::SSLClient cli(HOST, PORT);
  2234. auto res = cli.Get("/test");
  2235. cli.set_timeout_sec(30);
  2236. ASSERT_TRUE(res == nullptr);
  2237. svr.stop();
  2238. t.join();
  2239. }
  2240. TEST(SSLClientServerTest, TrustDirOptional) {
  2241. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  2242. ASSERT_TRUE(svr.is_valid());
  2243. svr.Get("/test", [&](const Request &, Response &res) {
  2244. res.set_content("test", "text/plain");
  2245. svr.stop();
  2246. });
  2247. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  2248. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2249. httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
  2250. auto res = cli.Get("/test");
  2251. cli.set_timeout_sec(30);
  2252. ASSERT_TRUE(res != nullptr);
  2253. ASSERT_EQ(200, res->status);
  2254. t.join();
  2255. }
  2256. /* Cannot test this case as there is no external access to SSL object to check
  2257. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  2258. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  2259. nullptr, CLIENT_CA_CERT_DIR);
  2260. }
  2261. */
  2262. #endif
  2263. #ifdef _WIN32
  2264. TEST(CleanupTest, WSACleanup) {
  2265. int ret = WSACleanup();
  2266. ASSERT_EQ(0, ret);
  2267. }
  2268. #endif