test.cc 65 KB

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