test.cc 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  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-int/hello/world/MD5",
  436. };
  437. cli.set_digest_auth("hello", "world");
  438. for (auto path : paths) {
  439. auto res = cli.Get(path.c_str());
  440. ASSERT_TRUE(res != nullptr);
  441. EXPECT_EQ(res->body,
  442. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  443. EXPECT_EQ(200, res->status);
  444. }
  445. cli.set_digest_auth("hello", "bad");
  446. for (auto path : paths) {
  447. auto res = cli.Get(path.c_str());
  448. ASSERT_TRUE(res != nullptr);
  449. EXPECT_EQ(400, res->status);
  450. }
  451. cli.set_digest_auth("bad", "world");
  452. for (auto path : paths) {
  453. auto res = cli.Get(path.c_str());
  454. ASSERT_TRUE(res != nullptr);
  455. EXPECT_EQ(400, res->status);
  456. }
  457. }
  458. }
  459. #endif
  460. TEST(AbsoluteRedirectTest, Redirect) {
  461. auto host = "httpbin.org";
  462. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  463. httplib::SSLClient cli(host);
  464. #else
  465. httplib::Client cli(host);
  466. #endif
  467. cli.set_follow_location(true);
  468. auto res = cli.Get("/absolute-redirect/3");
  469. ASSERT_TRUE(res != nullptr);
  470. EXPECT_EQ(200, res->status);
  471. }
  472. TEST(RedirectTest, Redirect) {
  473. auto host = "httpbin.org";
  474. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  475. httplib::SSLClient cli(host);
  476. #else
  477. httplib::Client cli(host);
  478. #endif
  479. cli.set_follow_location(true);
  480. auto res = cli.Get("/redirect/3");
  481. ASSERT_TRUE(res != nullptr);
  482. EXPECT_EQ(200, res->status);
  483. }
  484. TEST(RelativeRedirectTest, Redirect) {
  485. auto host = "httpbin.org";
  486. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  487. httplib::SSLClient cli(host);
  488. #else
  489. httplib::Client cli(host);
  490. #endif
  491. cli.set_follow_location(true);
  492. auto res = cli.Get("/relative-redirect/3");
  493. ASSERT_TRUE(res != nullptr);
  494. EXPECT_EQ(200, res->status);
  495. }
  496. TEST(TooManyRedirectTest, Redirect) {
  497. auto host = "httpbin.org";
  498. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  499. httplib::SSLClient cli(host);
  500. #else
  501. httplib::Client cli(host);
  502. #endif
  503. cli.set_follow_location(true);
  504. auto res = cli.Get("/redirect/21");
  505. ASSERT_TRUE(res == nullptr);
  506. }
  507. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  508. TEST(YahooRedirectTest, Redirect) {
  509. httplib::Client cli("yahoo.com");
  510. auto res = cli.Get("/");
  511. ASSERT_TRUE(res != nullptr);
  512. EXPECT_EQ(301, res->status);
  513. cli.set_follow_location(true);
  514. res = cli.Get("/");
  515. ASSERT_TRUE(res != nullptr);
  516. EXPECT_EQ(200, res->status);
  517. }
  518. TEST(HttpsToHttpRedirectTest, Redirect) {
  519. httplib::SSLClient cli("httpbin.org");
  520. cli.set_follow_location(true);
  521. auto res =
  522. cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
  523. ASSERT_TRUE(res != nullptr);
  524. }
  525. #endif
  526. TEST(Server, BindAndListenSeparately) {
  527. Server svr;
  528. int port = svr.bind_to_any_port("localhost");
  529. ASSERT_TRUE(port > 0);
  530. svr.stop();
  531. }
  532. class ServerTest : public ::testing::Test {
  533. protected:
  534. ServerTest()
  535. : cli_(HOST, PORT)
  536. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  537. ,
  538. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  539. #endif
  540. {
  541. }
  542. virtual void SetUp() {
  543. svr_.set_base_dir("./www");
  544. svr_.set_base_dir("./www2", "/mount");
  545. svr_.set_file_extension_and_mimetype_mapping("abcde", "text/abcde");
  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. .Post("/query-string-and-body",
  794. [&](const Request &req, Response & /*res*/) {
  795. ASSERT_TRUE(req.has_param("key"));
  796. EXPECT_EQ(req.get_param_value("key"), "value");
  797. EXPECT_EQ(req.body, "content");
  798. })
  799. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  800. .Get("/gzip",
  801. [&](const Request & /*req*/, Response &res) {
  802. res.set_content(
  803. "12345678901234567890123456789012345678901234567890123456789"
  804. "01234567890123456789012345678901234567890",
  805. "text/plain");
  806. })
  807. .Get("/nogzip",
  808. [&](const Request & /*req*/, Response &res) {
  809. res.set_content(
  810. "12345678901234567890123456789012345678901234567890123456789"
  811. "01234567890123456789012345678901234567890",
  812. "application/octet-stream");
  813. })
  814. .Post("/gzipmultipart",
  815. [&](const Request &req, Response & /*res*/) {
  816. EXPECT_EQ(2u, req.files.size());
  817. ASSERT_TRUE(!req.has_file("???"));
  818. {
  819. const auto &file = req.get_file_value("key1");
  820. EXPECT_EQ("", file.filename);
  821. EXPECT_EQ("test", file.content);
  822. }
  823. {
  824. const auto &file = req.get_file_value("key2");
  825. EXPECT_EQ("", file.filename);
  826. EXPECT_EQ("--abcdefg123", file.content);
  827. }
  828. })
  829. #endif
  830. ;
  831. persons_["john"] = "programmer";
  832. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  833. while (!svr_.is_running()) {
  834. msleep(1);
  835. }
  836. }
  837. virtual void TearDown() {
  838. svr_.stop();
  839. for (auto &t : request_threads_) {
  840. t.join();
  841. }
  842. t_.join();
  843. }
  844. map<string, string> persons_;
  845. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  846. SSLClient cli_;
  847. SSLServer svr_;
  848. #else
  849. Client cli_;
  850. Server svr_;
  851. #endif
  852. thread t_;
  853. std::vector<thread> request_threads_;
  854. };
  855. TEST_F(ServerTest, GetMethod200) {
  856. auto res = cli_.Get("/hi");
  857. ASSERT_TRUE(res != nullptr);
  858. EXPECT_EQ("HTTP/1.1", res->version);
  859. EXPECT_EQ(200, res->status);
  860. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  861. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  862. EXPECT_EQ("Hello World!", res->body);
  863. }
  864. TEST_F(ServerTest, GetMethod302) {
  865. auto res = cli_.Get("/");
  866. ASSERT_TRUE(res != nullptr);
  867. EXPECT_EQ(302, res->status);
  868. EXPECT_EQ("/hi", res->get_header_value("Location"));
  869. }
  870. TEST_F(ServerTest, GetMethod404) {
  871. auto res = cli_.Get("/invalid");
  872. ASSERT_TRUE(res != nullptr);
  873. EXPECT_EQ(404, res->status);
  874. }
  875. TEST_F(ServerTest, HeadMethod200) {
  876. auto res = cli_.Head("/hi");
  877. ASSERT_TRUE(res != nullptr);
  878. EXPECT_EQ(200, res->status);
  879. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  880. EXPECT_EQ("", res->body);
  881. }
  882. TEST_F(ServerTest, HeadMethod404) {
  883. auto res = cli_.Head("/invalid");
  884. ASSERT_TRUE(res != nullptr);
  885. EXPECT_EQ(404, res->status);
  886. EXPECT_EQ("", res->body);
  887. }
  888. TEST_F(ServerTest, GetMethodPersonJohn) {
  889. auto res = cli_.Get("/person/john");
  890. ASSERT_TRUE(res != nullptr);
  891. EXPECT_EQ(200, res->status);
  892. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  893. EXPECT_EQ("programmer", res->body);
  894. }
  895. TEST_F(ServerTest, PostMethod1) {
  896. auto res = cli_.Get("/person/john1");
  897. ASSERT_TRUE(res != nullptr);
  898. ASSERT_EQ(404, res->status);
  899. res = cli_.Post("/person", "name=john1&note=coder",
  900. "application/x-www-form-urlencoded");
  901. ASSERT_TRUE(res != nullptr);
  902. ASSERT_EQ(200, res->status);
  903. res = cli_.Get("/person/john1");
  904. ASSERT_TRUE(res != nullptr);
  905. ASSERT_EQ(200, res->status);
  906. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  907. ASSERT_EQ("coder", res->body);
  908. }
  909. TEST_F(ServerTest, PostMethod2) {
  910. auto res = cli_.Get("/person/john2");
  911. ASSERT_TRUE(res != nullptr);
  912. ASSERT_EQ(404, res->status);
  913. Params params;
  914. params.emplace("name", "john2");
  915. params.emplace("note", "coder");
  916. res = cli_.Post("/person", params);
  917. ASSERT_TRUE(res != nullptr);
  918. ASSERT_EQ(200, res->status);
  919. res = cli_.Get("/person/john2");
  920. ASSERT_TRUE(res != nullptr);
  921. ASSERT_EQ(200, res->status);
  922. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  923. ASSERT_EQ("coder", res->body);
  924. }
  925. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  926. Params params;
  927. params.emplace("json", JSON_DATA);
  928. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  929. ASSERT_TRUE(res != nullptr);
  930. ASSERT_EQ(200, res->status);
  931. ASSERT_EQ(JSON_DATA, res->body);
  932. }
  933. TEST_F(ServerTest, PostEmptyContent) {
  934. auto res = cli_.Post("/empty", "", "text/plain");
  935. ASSERT_TRUE(res != nullptr);
  936. ASSERT_EQ(200, res->status);
  937. ASSERT_EQ("empty", res->body);
  938. }
  939. TEST_F(ServerTest, GetMethodDir) {
  940. auto res = cli_.Get("/dir/");
  941. ASSERT_TRUE(res != nullptr);
  942. EXPECT_EQ(200, res->status);
  943. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  944. auto body = R"(<html>
  945. <head>
  946. </head>
  947. <body>
  948. <a href="/dir/test.html">Test</a>
  949. <a href="/hi">hi</a>
  950. </body>
  951. </html>
  952. )";
  953. EXPECT_EQ(body, res->body);
  954. }
  955. TEST_F(ServerTest, GetMethodDirTest) {
  956. auto res = cli_.Get("/dir/test.html");
  957. ASSERT_TRUE(res != nullptr);
  958. EXPECT_EQ(200, res->status);
  959. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  960. EXPECT_EQ("test.html", res->body);
  961. }
  962. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  963. auto res = cli_.Get("/dir/../dir/test.html");
  964. ASSERT_TRUE(res != nullptr);
  965. EXPECT_EQ(200, res->status);
  966. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  967. EXPECT_EQ("test.html", res->body);
  968. }
  969. TEST_F(ServerTest, GetMethodInvalidPath) {
  970. auto res = cli_.Get("/dir/../test.html");
  971. ASSERT_TRUE(res != nullptr);
  972. EXPECT_EQ(404, res->status);
  973. }
  974. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  975. auto res = cli_.Get("/../www/dir/test.html");
  976. ASSERT_TRUE(res != nullptr);
  977. EXPECT_EQ(404, res->status);
  978. }
  979. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  980. auto res = cli_.Get("/dir/../../www/dir/test.html");
  981. ASSERT_TRUE(res != nullptr);
  982. EXPECT_EQ(404, res->status);
  983. }
  984. TEST_F(ServerTest, GetMethodDirMountTest) {
  985. auto res = cli_.Get("/mount/dir/test.html");
  986. ASSERT_TRUE(res != nullptr);
  987. EXPECT_EQ(200, res->status);
  988. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  989. EXPECT_EQ("test.html", res->body);
  990. }
  991. TEST_F(ServerTest, GetMethodDirMountTestWithDoubleDots) {
  992. auto res = cli_.Get("/mount/dir/../dir/test.html");
  993. ASSERT_TRUE(res != nullptr);
  994. EXPECT_EQ(200, res->status);
  995. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  996. EXPECT_EQ("test.html", res->body);
  997. }
  998. TEST_F(ServerTest, GetMethodInvalidMountPath) {
  999. auto res = cli_.Get("/mount/dir/../test.html");
  1000. ASSERT_TRUE(res != nullptr);
  1001. EXPECT_EQ(404, res->status);
  1002. }
  1003. TEST_F(ServerTest, GetMethodOutOfBaseDirMount) {
  1004. auto res = cli_.Get("/mount/../www2/dir/test.html");
  1005. ASSERT_TRUE(res != nullptr);
  1006. EXPECT_EQ(404, res->status);
  1007. }
  1008. TEST_F(ServerTest, GetMethodOutOfBaseDirMount2) {
  1009. auto res = cli_.Get("/mount/dir/../../www2/dir/test.html");
  1010. ASSERT_TRUE(res != nullptr);
  1011. EXPECT_EQ(404, res->status);
  1012. }
  1013. TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
  1014. auto res = cli_.Get("/dir/test.abcde");
  1015. ASSERT_TRUE(res != nullptr);
  1016. EXPECT_EQ(200, res->status);
  1017. EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
  1018. EXPECT_EQ("abcde\n", res->body);
  1019. }
  1020. TEST_F(ServerTest, InvalidBaseDirMount) {
  1021. EXPECT_EQ(false, svr_.set_base_dir("./www3", "invalid_mount_point"));
  1022. }
  1023. TEST_F(ServerTest, EmptyRequest) {
  1024. auto res = cli_.Get("");
  1025. ASSERT_TRUE(res == nullptr);
  1026. }
  1027. TEST_F(ServerTest, LongRequest) {
  1028. std::string request;
  1029. for (size_t i = 0; i < 545; i++) {
  1030. request += "/TooLongRequest";
  1031. }
  1032. request += "OK";
  1033. auto res = cli_.Get(request.c_str());
  1034. ASSERT_TRUE(res != nullptr);
  1035. EXPECT_EQ(404, res->status);
  1036. }
  1037. TEST_F(ServerTest, TooLongRequest) {
  1038. std::string request;
  1039. for (size_t i = 0; i < 545; i++) {
  1040. request += "/TooLongRequest";
  1041. }
  1042. request += "_NG";
  1043. auto res = cli_.Get(request.c_str());
  1044. ASSERT_TRUE(res != nullptr);
  1045. EXPECT_EQ(414, res->status);
  1046. }
  1047. TEST_F(ServerTest, LongHeader) {
  1048. Request req;
  1049. req.method = "GET";
  1050. req.path = "/hi";
  1051. std::string host_and_port;
  1052. host_and_port += HOST;
  1053. host_and_port += ":";
  1054. host_and_port += std::to_string(PORT);
  1055. req.headers.emplace("Host", host_and_port.c_str());
  1056. req.headers.emplace("Accept", "*/*");
  1057. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1058. req.headers.emplace(
  1059. "Header-Name",
  1060. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1061. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1062. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1063. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1064. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1065. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1066. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1067. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1068. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1069. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1070. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1071. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1072. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1073. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1074. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1075. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1076. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1077. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1078. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1079. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1080. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1081. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1082. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1083. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1084. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1085. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1086. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1087. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1088. "@@@@@@@@@@@@@@@@");
  1089. auto res = std::make_shared<Response>();
  1090. auto ret = cli_.send(req, *res);
  1091. ASSERT_TRUE(ret);
  1092. EXPECT_EQ(200, res->status);
  1093. }
  1094. TEST_F(ServerTest, LongQueryValue) {
  1095. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  1096. ASSERT_TRUE(res != nullptr);
  1097. EXPECT_EQ(414, res->status);
  1098. }
  1099. TEST_F(ServerTest, TooLongHeader) {
  1100. Request req;
  1101. req.method = "GET";
  1102. req.path = "/hi";
  1103. std::string host_and_port;
  1104. host_and_port += HOST;
  1105. host_and_port += ":";
  1106. host_and_port += std::to_string(PORT);
  1107. req.headers.emplace("Host", host_and_port.c_str());
  1108. req.headers.emplace("Accept", "*/*");
  1109. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1110. req.headers.emplace(
  1111. "Header-Name",
  1112. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1113. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1114. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1115. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1116. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1117. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1118. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1119. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1120. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1121. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1122. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1123. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1124. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1125. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1126. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1127. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1128. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1129. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1130. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1131. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1132. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1133. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1134. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1135. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1136. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1137. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1138. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1139. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1140. "@@@@@@@@@@@@@@@@@");
  1141. auto res = std::make_shared<Response>();
  1142. auto ret = cli_.send(req, *res);
  1143. ASSERT_TRUE(ret);
  1144. EXPECT_EQ(200, res->status);
  1145. }
  1146. TEST_F(ServerTest, PercentEncoding) {
  1147. auto res = cli_.Get("/e%6edwith%");
  1148. ASSERT_TRUE(res != nullptr);
  1149. EXPECT_EQ(200, res->status);
  1150. }
  1151. TEST_F(ServerTest, PercentEncodingUnicode) {
  1152. auto res = cli_.Get("/e%u006edwith%");
  1153. ASSERT_TRUE(res != nullptr);
  1154. EXPECT_EQ(200, res->status);
  1155. }
  1156. TEST_F(ServerTest, InvalidPercentEncoding) {
  1157. auto res = cli_.Get("/%endwith%");
  1158. ASSERT_TRUE(res != nullptr);
  1159. EXPECT_EQ(404, res->status);
  1160. }
  1161. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  1162. auto res = cli_.Get("/%uendwith%");
  1163. ASSERT_TRUE(res != nullptr);
  1164. EXPECT_EQ(404, res->status);
  1165. }
  1166. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  1167. auto res = cli_.Get("/hello?aaa=bbb%");
  1168. ASSERT_TRUE(res != nullptr);
  1169. EXPECT_EQ(404, res->status);
  1170. }
  1171. TEST_F(ServerTest, MultipartFormData) {
  1172. MultipartFormDataItems items = {
  1173. {"text1", "text default", "", ""},
  1174. {"text2", "aωb", "", ""},
  1175. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1176. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1177. {"file3", "", "", "application/octet-stream"},
  1178. };
  1179. auto res = cli_.Post("/multipart", items);
  1180. ASSERT_TRUE(res != nullptr);
  1181. EXPECT_EQ(200, res->status);
  1182. }
  1183. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  1184. auto res = cli_.Get("/hi");
  1185. ASSERT_TRUE(res != nullptr);
  1186. EXPECT_EQ(200, res->status);
  1187. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  1188. EXPECT_EQ("Hello World!", res->body);
  1189. }
  1190. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  1191. Request req;
  1192. req.method = "POST";
  1193. req.path = "/chunked";
  1194. std::string host_and_port;
  1195. host_and_port += HOST;
  1196. host_and_port += ":";
  1197. host_and_port += std::to_string(PORT);
  1198. req.headers.emplace("Host", host_and_port.c_str());
  1199. req.headers.emplace("Accept", "*/*");
  1200. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1201. req.headers.emplace("Content-Type", "text/plain");
  1202. req.headers.emplace("Content-Length", "0");
  1203. req.headers.emplace(
  1204. "Transfer-Encoding",
  1205. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  1206. // Client does not chunk, so make a chunked body manually.
  1207. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  1208. auto res = std::make_shared<Response>();
  1209. auto ret = cli_.send(req, *res);
  1210. ASSERT_TRUE(ret);
  1211. EXPECT_EQ(200, res->status);
  1212. }
  1213. TEST_F(ServerTest, GetStreamed2) {
  1214. auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
  1215. ASSERT_TRUE(res != nullptr);
  1216. EXPECT_EQ(206, res->status);
  1217. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1218. EXPECT_EQ(std::string("ab"), res->body);
  1219. }
  1220. TEST_F(ServerTest, GetStreamed) {
  1221. auto res = cli_.Get("/streamed");
  1222. ASSERT_TRUE(res != nullptr);
  1223. EXPECT_EQ(200, res->status);
  1224. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1225. EXPECT_EQ(std::string("aaabbb"), res->body);
  1226. }
  1227. TEST_F(ServerTest, GetStreamedWithRange1) {
  1228. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
  1229. ASSERT_TRUE(res != nullptr);
  1230. EXPECT_EQ(206, res->status);
  1231. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1232. EXPECT_EQ(true, res->has_header("Content-Range"));
  1233. EXPECT_EQ(std::string("def"), res->body);
  1234. }
  1235. TEST_F(ServerTest, GetStreamedWithRange2) {
  1236. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
  1237. ASSERT_TRUE(res != nullptr);
  1238. EXPECT_EQ(206, res->status);
  1239. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1240. EXPECT_EQ(true, res->has_header("Content-Range"));
  1241. EXPECT_EQ(std::string("bcdefg"), res->body);
  1242. }
  1243. TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
  1244. auto res =
  1245. cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1246. ASSERT_TRUE(res != nullptr);
  1247. EXPECT_EQ(206, res->status);
  1248. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1249. EXPECT_EQ(false, res->has_header("Content-Range"));
  1250. EXPECT_EQ(269, res->body.size());
  1251. }
  1252. TEST_F(ServerTest, GetStreamedEndless) {
  1253. uint64_t offset = 0;
  1254. auto res = cli_.Get("/streamed-cancel",
  1255. [&](const char * /*data*/, uint64_t data_length) {
  1256. if (offset < 100) {
  1257. offset += data_length;
  1258. return true;
  1259. }
  1260. return false;
  1261. });
  1262. ASSERT_TRUE(res == nullptr);
  1263. }
  1264. TEST_F(ServerTest, GetWithRange1) {
  1265. auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
  1266. ASSERT_TRUE(res != nullptr);
  1267. EXPECT_EQ(206, res->status);
  1268. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1269. EXPECT_EQ(true, res->has_header("Content-Range"));
  1270. EXPECT_EQ(std::string("def"), res->body);
  1271. }
  1272. TEST_F(ServerTest, GetWithRange2) {
  1273. auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
  1274. ASSERT_TRUE(res != nullptr);
  1275. EXPECT_EQ(206, res->status);
  1276. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1277. EXPECT_EQ(true, res->has_header("Content-Range"));
  1278. EXPECT_EQ(std::string("bcdefg"), res->body);
  1279. }
  1280. TEST_F(ServerTest, GetWithRange3) {
  1281. auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
  1282. ASSERT_TRUE(res != nullptr);
  1283. EXPECT_EQ(206, res->status);
  1284. EXPECT_EQ("1", res->get_header_value("Content-Length"));
  1285. EXPECT_EQ(true, res->has_header("Content-Range"));
  1286. EXPECT_EQ(std::string("a"), res->body);
  1287. }
  1288. TEST_F(ServerTest, GetWithRange4) {
  1289. auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
  1290. ASSERT_TRUE(res != nullptr);
  1291. EXPECT_EQ(206, res->status);
  1292. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1293. EXPECT_EQ(true, res->has_header("Content-Range"));
  1294. EXPECT_EQ(std::string("fg"), res->body);
  1295. }
  1296. TEST_F(ServerTest, GetWithRangeMultipart) {
  1297. auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1298. ASSERT_TRUE(res != nullptr);
  1299. EXPECT_EQ(206, res->status);
  1300. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1301. EXPECT_EQ(false, res->has_header("Content-Range"));
  1302. EXPECT_EQ(269, res->body.size());
  1303. }
  1304. TEST_F(ServerTest, GetStreamedChunked) {
  1305. auto res = cli_.Get("/streamed-chunked");
  1306. ASSERT_TRUE(res != nullptr);
  1307. EXPECT_EQ(200, res->status);
  1308. EXPECT_EQ(std::string("123456789"), res->body);
  1309. }
  1310. TEST_F(ServerTest, LargeChunkedPost) {
  1311. Request req;
  1312. req.method = "POST";
  1313. req.path = "/large-chunked";
  1314. std::string host_and_port;
  1315. host_and_port += HOST;
  1316. host_and_port += ":";
  1317. host_and_port += std::to_string(PORT);
  1318. req.headers.emplace("Host", host_and_port.c_str());
  1319. req.headers.emplace("Accept", "*/*");
  1320. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1321. req.headers.emplace("Content-Type", "text/plain");
  1322. req.headers.emplace("Content-Length", "0");
  1323. req.headers.emplace("Transfer-Encoding", "chunked");
  1324. std::string long_string(30 * 1024u, 'a');
  1325. std::string chunk = "7800\r\n" + long_string + "\r\n";
  1326. // Attempt to make a large enough post to exceed OS buffers, to test that
  1327. // the server handles short reads if the full chunk data isn't available.
  1328. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  1329. auto res = std::make_shared<Response>();
  1330. auto ret = cli_.send(req, *res);
  1331. ASSERT_TRUE(ret);
  1332. EXPECT_EQ(200, res->status);
  1333. }
  1334. TEST_F(ServerTest, GetMethodRemoteAddr) {
  1335. auto res = cli_.Get("/remote_addr");
  1336. ASSERT_TRUE(res != nullptr);
  1337. EXPECT_EQ(200, res->status);
  1338. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1339. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  1340. }
  1341. TEST_F(ServerTest, SlowRequest) {
  1342. request_threads_.push_back(
  1343. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1344. request_threads_.push_back(
  1345. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1346. request_threads_.push_back(
  1347. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1348. msleep(100);
  1349. }
  1350. TEST_F(ServerTest, Put) {
  1351. auto res = cli_.Put("/put", "PUT", "text/plain");
  1352. ASSERT_TRUE(res != nullptr);
  1353. EXPECT_EQ(200, res->status);
  1354. EXPECT_EQ("PUT", res->body);
  1355. }
  1356. TEST_F(ServerTest, PutWithContentProvider) {
  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. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1368. TEST_F(ServerTest, PutWithContentProviderWithGzip) {
  1369. cli_.set_compress(true);
  1370. auto res = cli_.Put(
  1371. "/put", 3,
  1372. [](size_t /*offset*/, size_t /*length*/, DataSink sink) {
  1373. sink("PUT", 3);
  1374. },
  1375. "text/plain");
  1376. ASSERT_TRUE(res != nullptr);
  1377. EXPECT_EQ(200, res->status);
  1378. EXPECT_EQ("PUT", res->body);
  1379. }
  1380. TEST_F(ServerTest, PutLargeFileWithGzip) {
  1381. cli_.set_compress(true);
  1382. auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain");
  1383. ASSERT_TRUE(res != nullptr);
  1384. EXPECT_EQ(200, res->status);
  1385. EXPECT_EQ(LARGE_DATA, res->body);
  1386. }
  1387. #endif
  1388. TEST_F(ServerTest, Patch) {
  1389. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  1390. ASSERT_TRUE(res != nullptr);
  1391. EXPECT_EQ(200, res->status);
  1392. EXPECT_EQ("PATCH", res->body);
  1393. }
  1394. TEST_F(ServerTest, Delete) {
  1395. auto res = cli_.Delete("/delete");
  1396. ASSERT_TRUE(res != nullptr);
  1397. EXPECT_EQ(200, res->status);
  1398. EXPECT_EQ("DELETE", res->body);
  1399. }
  1400. TEST_F(ServerTest, Options) {
  1401. auto res = cli_.Options("*");
  1402. ASSERT_TRUE(res != nullptr);
  1403. EXPECT_EQ(200, res->status);
  1404. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  1405. EXPECT_TRUE(res->body.empty());
  1406. }
  1407. TEST_F(ServerTest, URL) {
  1408. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  1409. ASSERT_TRUE(res != nullptr);
  1410. EXPECT_EQ(200, res->status);
  1411. }
  1412. TEST_F(ServerTest, ArrayParam) {
  1413. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  1414. ASSERT_TRUE(res != nullptr);
  1415. EXPECT_EQ(200, res->status);
  1416. }
  1417. TEST_F(ServerTest, NoMultipleHeaders) {
  1418. Headers headers = {{"Content-Length", "5"}};
  1419. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1420. "text/plain");
  1421. ASSERT_TRUE(res != nullptr);
  1422. EXPECT_EQ(200, res->status);
  1423. }
  1424. TEST_F(ServerTest, PostContentReceiver) {
  1425. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1426. ASSERT_TRUE(res != nullptr);
  1427. ASSERT_EQ(200, res->status);
  1428. ASSERT_EQ("content", res->body);
  1429. }
  1430. TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
  1431. MultipartFormDataItems items = {
  1432. {"text1", "text default", "", ""},
  1433. {"text2", "aωb", "", ""},
  1434. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1435. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1436. {"file3", "", "", "application/octet-stream"},
  1437. };
  1438. auto res = cli_.Post("/content_receiver", items);
  1439. ASSERT_TRUE(res != nullptr);
  1440. EXPECT_EQ(200, res->status);
  1441. }
  1442. TEST_F(ServerTest, PostContentReceiverGzip) {
  1443. cli_.set_compress(true);
  1444. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1445. ASSERT_TRUE(res != nullptr);
  1446. ASSERT_EQ(200, res->status);
  1447. ASSERT_EQ("content", res->body);
  1448. }
  1449. TEST_F(ServerTest, PutContentReceiver) {
  1450. auto res = cli_.Put("/content_receiver", "content", "text/plain");
  1451. ASSERT_TRUE(res != nullptr);
  1452. ASSERT_EQ(200, res->status);
  1453. ASSERT_EQ("content", res->body);
  1454. }
  1455. TEST_F(ServerTest, PatchContentReceiver) {
  1456. auto res = cli_.Patch("/content_receiver", "content", "text/plain");
  1457. ASSERT_TRUE(res != nullptr);
  1458. ASSERT_EQ(200, res->status);
  1459. ASSERT_EQ("content", res->body);
  1460. }
  1461. TEST_F(ServerTest, PostQueryStringAndBody) {
  1462. auto res = cli_.Post("/query-string-and-body?key=value", "content", "text/plain");
  1463. ASSERT_TRUE(res != nullptr);
  1464. ASSERT_EQ(200, res->status);
  1465. }
  1466. TEST_F(ServerTest, HTTP2Magic) {
  1467. Request req;
  1468. req.method = "PRI";
  1469. req.path = "*";
  1470. req.body = "SM";
  1471. auto res = std::make_shared<Response>();
  1472. auto ret = cli_.send(req, *res);
  1473. ASSERT_TRUE(ret);
  1474. EXPECT_EQ(400, res->status);
  1475. }
  1476. TEST_F(ServerTest, KeepAlive) {
  1477. cli_.set_keep_alive_max_count(4);
  1478. std::vector<Request> requests;
  1479. Get(requests, "/hi");
  1480. Get(requests, "/hi");
  1481. Get(requests, "/hi");
  1482. Get(requests, "/not-exist");
  1483. Post(requests, "/empty", "", "text/plain");
  1484. std::vector<Response> responses;
  1485. auto ret = cli_.send(requests, responses);
  1486. ASSERT_TRUE(ret == true);
  1487. ASSERT_TRUE(requests.size() == responses.size());
  1488. for (int i = 0; i < 3; i++) {
  1489. auto &res = responses[i];
  1490. EXPECT_EQ(200, res.status);
  1491. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1492. EXPECT_EQ("Hello World!", res.body);
  1493. }
  1494. {
  1495. auto &res = responses[3];
  1496. EXPECT_EQ(404, res.status);
  1497. }
  1498. {
  1499. auto &res = responses[4];
  1500. EXPECT_EQ(200, res.status);
  1501. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1502. EXPECT_EQ("empty", res.body);
  1503. }
  1504. }
  1505. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1506. TEST_F(ServerTest, Gzip) {
  1507. Headers headers;
  1508. headers.emplace("Accept-Encoding", "gzip, deflate");
  1509. auto res = cli_.Get("/gzip", headers);
  1510. ASSERT_TRUE(res != nullptr);
  1511. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1512. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1513. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1514. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1515. "7890123456789012345678901234567890",
  1516. res->body);
  1517. EXPECT_EQ(200, res->status);
  1518. }
  1519. TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
  1520. Headers headers;
  1521. auto res = cli_.Get("/gzip", headers);
  1522. ASSERT_TRUE(res != nullptr);
  1523. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1524. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1525. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1526. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1527. "7890123456789012345678901234567890",
  1528. res->body);
  1529. EXPECT_EQ(200, res->status);
  1530. }
  1531. TEST_F(ServerTest, GzipWithContentReceiver) {
  1532. Headers headers;
  1533. headers.emplace("Accept-Encoding", "gzip, deflate");
  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("gzip", res->get_header_value("Content-Encoding"));
  1543. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1544. EXPECT_EQ("33", 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, GzipWithContentReceiverWithoutAcceptEncoding) {
  1551. Headers headers;
  1552. std::string body;
  1553. auto res =
  1554. cli_.Get("/gzip", headers, [&](const char *data, uint64_t data_length) {
  1555. EXPECT_EQ(data_length, 100);
  1556. body.append(data, data_length);
  1557. return true;
  1558. });
  1559. ASSERT_TRUE(res != nullptr);
  1560. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1561. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1562. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1563. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1564. "7890123456789012345678901234567890",
  1565. body);
  1566. EXPECT_EQ(200, res->status);
  1567. }
  1568. TEST_F(ServerTest, NoGzip) {
  1569. Headers headers;
  1570. headers.emplace("Accept-Encoding", "gzip, deflate");
  1571. auto res = cli_.Get("/nogzip", headers);
  1572. ASSERT_TRUE(res != nullptr);
  1573. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1574. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1575. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1576. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1577. "7890123456789012345678901234567890",
  1578. res->body);
  1579. EXPECT_EQ(200, res->status);
  1580. }
  1581. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1582. Headers headers;
  1583. headers.emplace("Accept-Encoding", "gzip, deflate");
  1584. std::string body;
  1585. auto res =
  1586. cli_.Get("/nogzip", headers, [&](const char *data, uint64_t data_length) {
  1587. EXPECT_EQ(data_length, 100);
  1588. body.append(data, data_length);
  1589. return true;
  1590. });
  1591. ASSERT_TRUE(res != nullptr);
  1592. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1593. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1594. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1595. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1596. "7890123456789012345678901234567890",
  1597. body);
  1598. EXPECT_EQ(200, res->status);
  1599. }
  1600. TEST_F(ServerTest, MultipartFormDataGzip) {
  1601. MultipartFormDataItems items = {
  1602. {"key1", "test", "", ""},
  1603. {"key2", "--abcdefg123", "", ""},
  1604. };
  1605. cli_.set_compress(true);
  1606. auto res = cli_.Post("/gzipmultipart", items);
  1607. ASSERT_TRUE(res != nullptr);
  1608. EXPECT_EQ(200, res->status);
  1609. }
  1610. #endif
  1611. // Sends a raw request to a server listening at HOST:PORT.
  1612. static bool send_request(time_t read_timeout_sec, const std::string &req) {
  1613. auto client_sock = detail::create_client_socket(HOST, PORT, /*timeout_sec=*/5,
  1614. std::string());
  1615. if (client_sock == INVALID_SOCKET) { return false; }
  1616. return detail::process_and_close_socket(
  1617. true, client_sock, 1, read_timeout_sec, 0,
  1618. [&](Stream &strm, bool /*last_connection*/, bool &
  1619. /*connection_close*/) -> bool {
  1620. if (req.size() !=
  1621. static_cast<size_t>(strm.write(req.data(), req.size()))) {
  1622. return false;
  1623. }
  1624. char buf[512];
  1625. detail::stream_line_reader line_reader(strm, buf, sizeof(buf));
  1626. while (line_reader.getline()) {}
  1627. return true;
  1628. });
  1629. }
  1630. TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
  1631. Server svr;
  1632. std::string header_value;
  1633. svr.Get("/validate-ws-in-headers", [&](const Request &req, Response &res) {
  1634. header_value = req.get_header_value("foo");
  1635. res.set_content("ok", "text/plain");
  1636. });
  1637. thread t = thread([&] { svr.listen(HOST, PORT); });
  1638. while (!svr.is_running()) {
  1639. msleep(1);
  1640. }
  1641. // Only space and horizontal tab are whitespace. Make sure other whitespace-
  1642. // like characters are not treated the same - use vertical tab and escape.
  1643. const std::string req = "GET /validate-ws-in-headers HTTP/1.1\r\n"
  1644. "foo: \t \v bar \e\t \r\n"
  1645. "Connection: close\r\n"
  1646. "\r\n";
  1647. ASSERT_TRUE(send_request(5, req));
  1648. svr.stop();
  1649. t.join();
  1650. EXPECT_EQ(header_value, "\v bar \e");
  1651. }
  1652. TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
  1653. Server svr;
  1654. svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1655. res.set_content("ok", "text/plain");
  1656. });
  1657. // Server read timeout must be longer than the client read timeout for the
  1658. // bug to reproduce, probably to force the server to process a request
  1659. // without a trailing blank line.
  1660. const time_t client_read_timeout_sec = 1;
  1661. svr.set_read_timeout(client_read_timeout_sec + 1, 0);
  1662. bool listen_thread_ok = false;
  1663. thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
  1664. while (!svr.is_running()) {
  1665. msleep(1);
  1666. }
  1667. // A certain header line causes an exception if the header property is parsed
  1668. // naively with a single regex. This occurs with libc++ but not libstdc++.
  1669. const std::string req =
  1670. "GET /hi HTTP/1.1\r\n"
  1671. " : "
  1672. " ";
  1673. ASSERT_TRUE(send_request(client_read_timeout_sec, req));
  1674. svr.stop();
  1675. t.join();
  1676. EXPECT_TRUE(listen_thread_ok);
  1677. }
  1678. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1679. protected:
  1680. ServerTestWithAI_PASSIVE()
  1681. : cli_(HOST, PORT)
  1682. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1683. ,
  1684. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1685. #endif
  1686. {
  1687. }
  1688. virtual void SetUp() {
  1689. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1690. res.set_content("Hello World!", "text/plain");
  1691. });
  1692. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1693. while (!svr_.is_running()) {
  1694. msleep(1);
  1695. }
  1696. }
  1697. virtual void TearDown() {
  1698. svr_.stop();
  1699. t_.join();
  1700. }
  1701. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1702. SSLClient cli_;
  1703. SSLServer svr_;
  1704. #else
  1705. Client cli_;
  1706. Server svr_;
  1707. #endif
  1708. thread t_;
  1709. };
  1710. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1711. auto res = cli_.Get("/hi");
  1712. ASSERT_TRUE(res != nullptr);
  1713. EXPECT_EQ(200, res->status);
  1714. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1715. EXPECT_EQ("Hello World!", res->body);
  1716. }
  1717. class ServerUpDownTest : public ::testing::Test {
  1718. protected:
  1719. ServerUpDownTest() : cli_(HOST, PORT) {}
  1720. virtual void SetUp() {
  1721. t_ = thread([&]() {
  1722. svr_.bind_to_any_port(HOST);
  1723. msleep(500);
  1724. ASSERT_TRUE(svr_.listen_after_bind());
  1725. });
  1726. while (!svr_.is_running()) {
  1727. msleep(1);
  1728. }
  1729. }
  1730. virtual void TearDown() {
  1731. svr_.stop();
  1732. t_.join();
  1733. }
  1734. Client cli_;
  1735. Server svr_;
  1736. thread t_;
  1737. };
  1738. TEST_F(ServerUpDownTest, QuickStartStop) {
  1739. // Should not crash, especially when run with
  1740. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1741. }
  1742. class PayloadMaxLengthTest : public ::testing::Test {
  1743. protected:
  1744. PayloadMaxLengthTest()
  1745. : cli_(HOST, PORT)
  1746. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1747. ,
  1748. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1749. #endif
  1750. {
  1751. }
  1752. virtual void SetUp() {
  1753. svr_.set_payload_max_length(8);
  1754. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1755. res.set_content("test", "text/plain");
  1756. });
  1757. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1758. while (!svr_.is_running()) {
  1759. msleep(1);
  1760. }
  1761. }
  1762. virtual void TearDown() {
  1763. svr_.stop();
  1764. t_.join();
  1765. }
  1766. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1767. SSLClient cli_;
  1768. SSLServer svr_;
  1769. #else
  1770. Client cli_;
  1771. Server svr_;
  1772. #endif
  1773. thread t_;
  1774. };
  1775. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1776. auto res = cli_.Post("/test", "123456789", "text/plain");
  1777. ASSERT_TRUE(res != nullptr);
  1778. EXPECT_EQ(413, res->status);
  1779. res = cli_.Post("/test", "12345678", "text/plain");
  1780. ASSERT_TRUE(res != nullptr);
  1781. EXPECT_EQ(200, res->status);
  1782. }
  1783. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1784. TEST(SSLClientTest, ServerNameIndication) {
  1785. SSLClient cli("httpbin.org", 443);
  1786. auto res = cli.Get("/get");
  1787. ASSERT_TRUE(res != nullptr);
  1788. ASSERT_EQ(200, res->status);
  1789. }
  1790. TEST(SSLClientTest, ServerCertificateVerification) {
  1791. SSLClient cli("google.com");
  1792. auto res = cli.Get("/");
  1793. ASSERT_TRUE(res != nullptr);
  1794. ASSERT_EQ(301, res->status);
  1795. cli.enable_server_certificate_verification(true);
  1796. res = cli.Get("/");
  1797. ASSERT_TRUE(res == nullptr);
  1798. cli.set_ca_cert_path(CA_CERT_FILE);
  1799. res = cli.Get("/");
  1800. ASSERT_TRUE(res != nullptr);
  1801. ASSERT_EQ(301, res->status);
  1802. }
  1803. TEST(SSLClientTest, WildcardHostNameMatch) {
  1804. SSLClient cli("www.youtube.com");
  1805. cli.set_ca_cert_path(CA_CERT_FILE);
  1806. cli.enable_server_certificate_verification(true);
  1807. auto res = cli.Get("/");
  1808. ASSERT_TRUE(res != nullptr);
  1809. ASSERT_EQ(200, res->status);
  1810. }
  1811. TEST(SSLClientServerTest, ClientCertPresent) {
  1812. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1813. CLIENT_CA_CERT_DIR);
  1814. ASSERT_TRUE(svr.is_valid());
  1815. svr.Get("/test", [&](const Request &req, Response &res) {
  1816. res.set_content("test", "text/plain");
  1817. svr.stop();
  1818. ASSERT_TRUE(true);
  1819. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1820. ASSERT_TRUE(peer_cert != nullptr);
  1821. auto subject_name = X509_get_subject_name(peer_cert);
  1822. ASSERT_TRUE(subject_name != nullptr);
  1823. std::string common_name;
  1824. {
  1825. char name[BUFSIZ];
  1826. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1827. name, sizeof(name));
  1828. common_name.assign(name, name_len);
  1829. }
  1830. EXPECT_EQ("Common Name", common_name);
  1831. X509_free(peer_cert);
  1832. });
  1833. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1834. msleep(1);
  1835. httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE,
  1836. CLIENT_PRIVATE_KEY_FILE);
  1837. auto res = cli.Get("/test");
  1838. cli.set_timeout_sec(30);
  1839. ASSERT_TRUE(res != nullptr);
  1840. ASSERT_EQ(200, res->status);
  1841. t.join();
  1842. }
  1843. TEST(SSLClientServerTest, ClientCertMissing) {
  1844. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1845. CLIENT_CA_CERT_DIR);
  1846. ASSERT_TRUE(svr.is_valid());
  1847. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1848. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1849. msleep(1);
  1850. httplib::SSLClient cli(HOST, PORT);
  1851. auto res = cli.Get("/test");
  1852. cli.set_timeout_sec(30);
  1853. ASSERT_TRUE(res == nullptr);
  1854. svr.stop();
  1855. t.join();
  1856. }
  1857. TEST(SSLClientServerTest, TrustDirOptional) {
  1858. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1859. ASSERT_TRUE(svr.is_valid());
  1860. svr.Get("/test", [&](const Request &, Response &res) {
  1861. res.set_content("test", "text/plain");
  1862. svr.stop();
  1863. });
  1864. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1865. msleep(1);
  1866. httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE,
  1867. CLIENT_PRIVATE_KEY_FILE);
  1868. auto res = cli.Get("/test");
  1869. cli.set_timeout_sec(30);
  1870. ASSERT_TRUE(res != nullptr);
  1871. ASSERT_EQ(200, res->status);
  1872. t.join();
  1873. }
  1874. /* Cannot test this case as there is no external access to SSL object to check
  1875. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1876. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1877. nullptr, CLIENT_CA_CERT_DIR);
  1878. }
  1879. */
  1880. #endif
  1881. #ifdef _WIN32
  1882. TEST(CleanupTest, WSACleanup) {
  1883. int ret = WSACleanup();
  1884. ASSERT_EQ(0, ret);
  1885. }
  1886. #endif