test.cc 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  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. .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, InvalidBaseDirMount) {
  1014. EXPECT_EQ(false, svr_.set_base_dir("./www3", "invalid_mount_point"));
  1015. }
  1016. TEST_F(ServerTest, EmptyRequest) {
  1017. auto res = cli_.Get("");
  1018. ASSERT_TRUE(res == nullptr);
  1019. }
  1020. TEST_F(ServerTest, LongRequest) {
  1021. std::string request;
  1022. for (size_t i = 0; i < 545; i++) {
  1023. request += "/TooLongRequest";
  1024. }
  1025. request += "OK";
  1026. auto res = cli_.Get(request.c_str());
  1027. ASSERT_TRUE(res != nullptr);
  1028. EXPECT_EQ(404, res->status);
  1029. }
  1030. TEST_F(ServerTest, TooLongRequest) {
  1031. std::string request;
  1032. for (size_t i = 0; i < 545; i++) {
  1033. request += "/TooLongRequest";
  1034. }
  1035. request += "_NG";
  1036. auto res = cli_.Get(request.c_str());
  1037. ASSERT_TRUE(res != nullptr);
  1038. EXPECT_EQ(414, res->status);
  1039. }
  1040. TEST_F(ServerTest, LongHeader) {
  1041. Request req;
  1042. req.method = "GET";
  1043. req.path = "/hi";
  1044. std::string host_and_port;
  1045. host_and_port += HOST;
  1046. host_and_port += ":";
  1047. host_and_port += std::to_string(PORT);
  1048. req.headers.emplace("Host", host_and_port.c_str());
  1049. req.headers.emplace("Accept", "*/*");
  1050. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1051. req.headers.emplace(
  1052. "Header-Name",
  1053. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1054. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1055. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1056. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1057. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1058. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1059. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1060. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1061. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1062. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1063. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1064. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1065. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1066. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1067. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1068. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1069. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1070. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1071. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1072. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1073. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1074. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1075. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1076. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1077. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1078. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1079. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1080. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1081. "@@@@@@@@@@@@@@@@");
  1082. auto res = std::make_shared<Response>();
  1083. auto ret = cli_.send(req, *res);
  1084. ASSERT_TRUE(ret);
  1085. EXPECT_EQ(200, res->status);
  1086. }
  1087. TEST_F(ServerTest, LongQueryValue) {
  1088. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  1089. ASSERT_TRUE(res != nullptr);
  1090. EXPECT_EQ(414, res->status);
  1091. }
  1092. TEST_F(ServerTest, TooLongHeader) {
  1093. Request req;
  1094. req.method = "GET";
  1095. req.path = "/hi";
  1096. std::string host_and_port;
  1097. host_and_port += HOST;
  1098. host_and_port += ":";
  1099. host_and_port += std::to_string(PORT);
  1100. req.headers.emplace("Host", host_and_port.c_str());
  1101. req.headers.emplace("Accept", "*/*");
  1102. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1103. req.headers.emplace(
  1104. "Header-Name",
  1105. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1106. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1107. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1108. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1109. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1110. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1111. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1112. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1113. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1114. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1115. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1116. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1117. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1118. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1119. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1120. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1121. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1122. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1123. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1124. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1125. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1126. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1127. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1128. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1129. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1130. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1131. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1132. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1133. "@@@@@@@@@@@@@@@@@");
  1134. auto res = std::make_shared<Response>();
  1135. auto ret = cli_.send(req, *res);
  1136. ASSERT_TRUE(ret);
  1137. EXPECT_EQ(200, res->status);
  1138. }
  1139. TEST_F(ServerTest, PercentEncoding) {
  1140. auto res = cli_.Get("/e%6edwith%");
  1141. ASSERT_TRUE(res != nullptr);
  1142. EXPECT_EQ(200, res->status);
  1143. }
  1144. TEST_F(ServerTest, PercentEncodingUnicode) {
  1145. auto res = cli_.Get("/e%u006edwith%");
  1146. ASSERT_TRUE(res != nullptr);
  1147. EXPECT_EQ(200, res->status);
  1148. }
  1149. TEST_F(ServerTest, InvalidPercentEncoding) {
  1150. auto res = cli_.Get("/%endwith%");
  1151. ASSERT_TRUE(res != nullptr);
  1152. EXPECT_EQ(404, res->status);
  1153. }
  1154. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  1155. auto res = cli_.Get("/%uendwith%");
  1156. ASSERT_TRUE(res != nullptr);
  1157. EXPECT_EQ(404, res->status);
  1158. }
  1159. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  1160. auto res = cli_.Get("/hello?aaa=bbb%");
  1161. ASSERT_TRUE(res != nullptr);
  1162. EXPECT_EQ(404, res->status);
  1163. }
  1164. TEST_F(ServerTest, MultipartFormData) {
  1165. MultipartFormDataItems items = {
  1166. {"text1", "text default", "", ""},
  1167. {"text2", "aωb", "", ""},
  1168. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1169. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1170. {"file3", "", "", "application/octet-stream"},
  1171. };
  1172. auto res = cli_.Post("/multipart", items);
  1173. ASSERT_TRUE(res != nullptr);
  1174. EXPECT_EQ(200, res->status);
  1175. }
  1176. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  1177. auto res = cli_.Get("/hi");
  1178. ASSERT_TRUE(res != nullptr);
  1179. EXPECT_EQ(200, res->status);
  1180. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  1181. EXPECT_EQ("Hello World!", res->body);
  1182. }
  1183. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  1184. Request req;
  1185. req.method = "POST";
  1186. req.path = "/chunked";
  1187. std::string host_and_port;
  1188. host_and_port += HOST;
  1189. host_and_port += ":";
  1190. host_and_port += std::to_string(PORT);
  1191. req.headers.emplace("Host", host_and_port.c_str());
  1192. req.headers.emplace("Accept", "*/*");
  1193. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1194. req.headers.emplace("Content-Type", "text/plain");
  1195. req.headers.emplace("Content-Length", "0");
  1196. req.headers.emplace(
  1197. "Transfer-Encoding",
  1198. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  1199. // Client does not chunk, so make a chunked body manually.
  1200. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  1201. auto res = std::make_shared<Response>();
  1202. auto ret = cli_.send(req, *res);
  1203. ASSERT_TRUE(ret);
  1204. EXPECT_EQ(200, res->status);
  1205. }
  1206. TEST_F(ServerTest, GetStreamed2) {
  1207. auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
  1208. ASSERT_TRUE(res != nullptr);
  1209. EXPECT_EQ(206, res->status);
  1210. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1211. EXPECT_EQ(std::string("ab"), res->body);
  1212. }
  1213. TEST_F(ServerTest, GetStreamed) {
  1214. auto res = cli_.Get("/streamed");
  1215. ASSERT_TRUE(res != nullptr);
  1216. EXPECT_EQ(200, res->status);
  1217. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1218. EXPECT_EQ(std::string("aaabbb"), res->body);
  1219. }
  1220. TEST_F(ServerTest, GetStreamedWithRange1) {
  1221. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
  1222. ASSERT_TRUE(res != nullptr);
  1223. EXPECT_EQ(206, res->status);
  1224. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1225. EXPECT_EQ(true, res->has_header("Content-Range"));
  1226. EXPECT_EQ(std::string("def"), res->body);
  1227. }
  1228. TEST_F(ServerTest, GetStreamedWithRange2) {
  1229. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
  1230. ASSERT_TRUE(res != nullptr);
  1231. EXPECT_EQ(206, res->status);
  1232. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1233. EXPECT_EQ(true, res->has_header("Content-Range"));
  1234. EXPECT_EQ(std::string("bcdefg"), res->body);
  1235. }
  1236. TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
  1237. auto res =
  1238. cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1239. ASSERT_TRUE(res != nullptr);
  1240. EXPECT_EQ(206, res->status);
  1241. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1242. EXPECT_EQ(false, res->has_header("Content-Range"));
  1243. EXPECT_EQ(269, res->body.size());
  1244. }
  1245. TEST_F(ServerTest, GetStreamedEndless) {
  1246. uint64_t offset = 0;
  1247. auto res = cli_.Get("/streamed-cancel",
  1248. [&](const char * /*data*/, uint64_t data_length) {
  1249. if (offset < 100) {
  1250. offset += data_length;
  1251. return true;
  1252. }
  1253. return false;
  1254. });
  1255. ASSERT_TRUE(res == nullptr);
  1256. }
  1257. TEST_F(ServerTest, GetWithRange1) {
  1258. auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
  1259. ASSERT_TRUE(res != nullptr);
  1260. EXPECT_EQ(206, res->status);
  1261. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1262. EXPECT_EQ(true, res->has_header("Content-Range"));
  1263. EXPECT_EQ(std::string("def"), res->body);
  1264. }
  1265. TEST_F(ServerTest, GetWithRange2) {
  1266. auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
  1267. ASSERT_TRUE(res != nullptr);
  1268. EXPECT_EQ(206, res->status);
  1269. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1270. EXPECT_EQ(true, res->has_header("Content-Range"));
  1271. EXPECT_EQ(std::string("bcdefg"), res->body);
  1272. }
  1273. TEST_F(ServerTest, GetWithRange3) {
  1274. auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
  1275. ASSERT_TRUE(res != nullptr);
  1276. EXPECT_EQ(206, res->status);
  1277. EXPECT_EQ("1", res->get_header_value("Content-Length"));
  1278. EXPECT_EQ(true, res->has_header("Content-Range"));
  1279. EXPECT_EQ(std::string("a"), res->body);
  1280. }
  1281. TEST_F(ServerTest, GetWithRange4) {
  1282. auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
  1283. ASSERT_TRUE(res != nullptr);
  1284. EXPECT_EQ(206, res->status);
  1285. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1286. EXPECT_EQ(true, res->has_header("Content-Range"));
  1287. EXPECT_EQ(std::string("fg"), res->body);
  1288. }
  1289. TEST_F(ServerTest, GetWithRangeMultipart) {
  1290. auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1291. ASSERT_TRUE(res != nullptr);
  1292. EXPECT_EQ(206, res->status);
  1293. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1294. EXPECT_EQ(false, res->has_header("Content-Range"));
  1295. EXPECT_EQ(269, res->body.size());
  1296. }
  1297. TEST_F(ServerTest, GetStreamedChunked) {
  1298. auto res = cli_.Get("/streamed-chunked");
  1299. ASSERT_TRUE(res != nullptr);
  1300. EXPECT_EQ(200, res->status);
  1301. EXPECT_EQ(std::string("123456789"), res->body);
  1302. }
  1303. TEST_F(ServerTest, LargeChunkedPost) {
  1304. Request req;
  1305. req.method = "POST";
  1306. req.path = "/large-chunked";
  1307. std::string host_and_port;
  1308. host_and_port += HOST;
  1309. host_and_port += ":";
  1310. host_and_port += std::to_string(PORT);
  1311. req.headers.emplace("Host", host_and_port.c_str());
  1312. req.headers.emplace("Accept", "*/*");
  1313. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1314. req.headers.emplace("Content-Type", "text/plain");
  1315. req.headers.emplace("Content-Length", "0");
  1316. req.headers.emplace("Transfer-Encoding", "chunked");
  1317. std::string long_string(30 * 1024u, 'a');
  1318. std::string chunk = "7800\r\n" + long_string + "\r\n";
  1319. // Attempt to make a large enough post to exceed OS buffers, to test that
  1320. // the server handles short reads if the full chunk data isn't available.
  1321. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  1322. auto res = std::make_shared<Response>();
  1323. auto ret = cli_.send(req, *res);
  1324. ASSERT_TRUE(ret);
  1325. EXPECT_EQ(200, res->status);
  1326. }
  1327. TEST_F(ServerTest, GetMethodRemoteAddr) {
  1328. auto res = cli_.Get("/remote_addr");
  1329. ASSERT_TRUE(res != nullptr);
  1330. EXPECT_EQ(200, res->status);
  1331. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1332. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  1333. }
  1334. TEST_F(ServerTest, SlowRequest) {
  1335. request_threads_.push_back(
  1336. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1337. request_threads_.push_back(
  1338. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1339. request_threads_.push_back(
  1340. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1341. msleep(100);
  1342. }
  1343. TEST_F(ServerTest, Put) {
  1344. auto res = cli_.Put("/put", "PUT", "text/plain");
  1345. ASSERT_TRUE(res != nullptr);
  1346. EXPECT_EQ(200, res->status);
  1347. EXPECT_EQ("PUT", res->body);
  1348. }
  1349. TEST_F(ServerTest, PutWithContentProvider) {
  1350. auto res = cli_.Put(
  1351. "/put", 3,
  1352. [](size_t /*offset*/, size_t /*length*/, DataSink sink) {
  1353. sink("PUT", 3);
  1354. },
  1355. "text/plain");
  1356. ASSERT_TRUE(res != nullptr);
  1357. EXPECT_EQ(200, res->status);
  1358. EXPECT_EQ("PUT", res->body);
  1359. }
  1360. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1361. TEST_F(ServerTest, PutWithContentProviderWithGzip) {
  1362. cli_.set_compress(true);
  1363. auto res = cli_.Put(
  1364. "/put", 3,
  1365. [](size_t /*offset*/, size_t /*length*/, DataSink sink) {
  1366. sink("PUT", 3);
  1367. },
  1368. "text/plain");
  1369. ASSERT_TRUE(res != nullptr);
  1370. EXPECT_EQ(200, res->status);
  1371. EXPECT_EQ("PUT", res->body);
  1372. }
  1373. TEST_F(ServerTest, PutLargeFileWithGzip) {
  1374. cli_.set_compress(true);
  1375. auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain");
  1376. ASSERT_TRUE(res != nullptr);
  1377. EXPECT_EQ(200, res->status);
  1378. EXPECT_EQ(LARGE_DATA, res->body);
  1379. }
  1380. #endif
  1381. TEST_F(ServerTest, Patch) {
  1382. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  1383. ASSERT_TRUE(res != nullptr);
  1384. EXPECT_EQ(200, res->status);
  1385. EXPECT_EQ("PATCH", res->body);
  1386. }
  1387. TEST_F(ServerTest, Delete) {
  1388. auto res = cli_.Delete("/delete");
  1389. ASSERT_TRUE(res != nullptr);
  1390. EXPECT_EQ(200, res->status);
  1391. EXPECT_EQ("DELETE", res->body);
  1392. }
  1393. TEST_F(ServerTest, Options) {
  1394. auto res = cli_.Options("*");
  1395. ASSERT_TRUE(res != nullptr);
  1396. EXPECT_EQ(200, res->status);
  1397. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  1398. EXPECT_TRUE(res->body.empty());
  1399. }
  1400. TEST_F(ServerTest, URL) {
  1401. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  1402. ASSERT_TRUE(res != nullptr);
  1403. EXPECT_EQ(200, res->status);
  1404. }
  1405. TEST_F(ServerTest, ArrayParam) {
  1406. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  1407. ASSERT_TRUE(res != nullptr);
  1408. EXPECT_EQ(200, res->status);
  1409. }
  1410. TEST_F(ServerTest, NoMultipleHeaders) {
  1411. Headers headers = {{"Content-Length", "5"}};
  1412. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1413. "text/plain");
  1414. ASSERT_TRUE(res != nullptr);
  1415. EXPECT_EQ(200, res->status);
  1416. }
  1417. TEST_F(ServerTest, PostContentReceiver) {
  1418. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1419. ASSERT_TRUE(res != nullptr);
  1420. ASSERT_EQ(200, res->status);
  1421. ASSERT_EQ("content", res->body);
  1422. }
  1423. TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
  1424. MultipartFormDataItems items = {
  1425. {"text1", "text default", "", ""},
  1426. {"text2", "aωb", "", ""},
  1427. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1428. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1429. {"file3", "", "", "application/octet-stream"},
  1430. };
  1431. auto res = cli_.Post("/content_receiver", items);
  1432. ASSERT_TRUE(res != nullptr);
  1433. EXPECT_EQ(200, res->status);
  1434. }
  1435. TEST_F(ServerTest, PostContentReceiverGzip) {
  1436. cli_.set_compress(true);
  1437. auto res = cli_.Post("/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, PutContentReceiver) {
  1443. auto res = cli_.Put("/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, PatchContentReceiver) {
  1449. auto res = cli_.Patch("/content_receiver", "content", "text/plain");
  1450. ASSERT_TRUE(res != nullptr);
  1451. ASSERT_EQ(200, res->status);
  1452. ASSERT_EQ("content", res->body);
  1453. }
  1454. TEST_F(ServerTest, PostQueryStringAndBody) {
  1455. auto res = cli_.Post("/query-string-and-body?key=value", "content", "text/plain");
  1456. ASSERT_TRUE(res != nullptr);
  1457. ASSERT_EQ(200, res->status);
  1458. }
  1459. TEST_F(ServerTest, HTTP2Magic) {
  1460. Request req;
  1461. req.method = "PRI";
  1462. req.path = "*";
  1463. req.body = "SM";
  1464. auto res = std::make_shared<Response>();
  1465. auto ret = cli_.send(req, *res);
  1466. ASSERT_TRUE(ret);
  1467. EXPECT_EQ(400, res->status);
  1468. }
  1469. TEST_F(ServerTest, KeepAlive) {
  1470. cli_.set_keep_alive_max_count(4);
  1471. std::vector<Request> requests;
  1472. Get(requests, "/hi");
  1473. Get(requests, "/hi");
  1474. Get(requests, "/hi");
  1475. Get(requests, "/not-exist");
  1476. Post(requests, "/empty", "", "text/plain");
  1477. std::vector<Response> responses;
  1478. auto ret = cli_.send(requests, responses);
  1479. ASSERT_TRUE(ret == true);
  1480. ASSERT_TRUE(requests.size() == responses.size());
  1481. for (int i = 0; i < 3; i++) {
  1482. auto &res = responses[i];
  1483. EXPECT_EQ(200, res.status);
  1484. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1485. EXPECT_EQ("Hello World!", res.body);
  1486. }
  1487. {
  1488. auto &res = responses[3];
  1489. EXPECT_EQ(404, res.status);
  1490. }
  1491. {
  1492. auto &res = responses[4];
  1493. EXPECT_EQ(200, res.status);
  1494. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1495. EXPECT_EQ("empty", res.body);
  1496. }
  1497. }
  1498. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1499. TEST_F(ServerTest, Gzip) {
  1500. Headers headers;
  1501. headers.emplace("Accept-Encoding", "gzip, deflate");
  1502. auto res = cli_.Get("/gzip", headers);
  1503. ASSERT_TRUE(res != nullptr);
  1504. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1505. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1506. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1507. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1508. "7890123456789012345678901234567890",
  1509. res->body);
  1510. EXPECT_EQ(200, res->status);
  1511. }
  1512. TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
  1513. Headers headers;
  1514. auto res = cli_.Get("/gzip", headers);
  1515. ASSERT_TRUE(res != nullptr);
  1516. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1517. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1518. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1519. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1520. "7890123456789012345678901234567890",
  1521. res->body);
  1522. EXPECT_EQ(200, res->status);
  1523. }
  1524. TEST_F(ServerTest, GzipWithContentReceiver) {
  1525. Headers headers;
  1526. headers.emplace("Accept-Encoding", "gzip, deflate");
  1527. std::string body;
  1528. auto res =
  1529. cli_.Get("/gzip", headers, [&](const char *data, uint64_t data_length) {
  1530. EXPECT_EQ(data_length, 100);
  1531. body.append(data, data_length);
  1532. return true;
  1533. });
  1534. ASSERT_TRUE(res != nullptr);
  1535. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1536. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1537. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1538. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1539. "7890123456789012345678901234567890",
  1540. body);
  1541. EXPECT_EQ(200, res->status);
  1542. }
  1543. TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
  1544. Headers headers;
  1545. std::string body;
  1546. auto res =
  1547. cli_.Get("/gzip", headers, [&](const char *data, uint64_t data_length) {
  1548. EXPECT_EQ(data_length, 100);
  1549. body.append(data, data_length);
  1550. return true;
  1551. });
  1552. ASSERT_TRUE(res != nullptr);
  1553. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1554. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1555. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1556. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1557. "7890123456789012345678901234567890",
  1558. body);
  1559. EXPECT_EQ(200, res->status);
  1560. }
  1561. TEST_F(ServerTest, NoGzip) {
  1562. Headers headers;
  1563. headers.emplace("Accept-Encoding", "gzip, deflate");
  1564. auto res = cli_.Get("/nogzip", headers);
  1565. ASSERT_TRUE(res != nullptr);
  1566. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1567. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1568. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1569. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1570. "7890123456789012345678901234567890",
  1571. res->body);
  1572. EXPECT_EQ(200, res->status);
  1573. }
  1574. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1575. Headers headers;
  1576. headers.emplace("Accept-Encoding", "gzip, deflate");
  1577. std::string body;
  1578. auto res =
  1579. cli_.Get("/nogzip", headers, [&](const char *data, uint64_t data_length) {
  1580. EXPECT_EQ(data_length, 100);
  1581. body.append(data, data_length);
  1582. return true;
  1583. });
  1584. ASSERT_TRUE(res != nullptr);
  1585. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1586. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1587. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1588. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1589. "7890123456789012345678901234567890",
  1590. body);
  1591. EXPECT_EQ(200, res->status);
  1592. }
  1593. TEST_F(ServerTest, MultipartFormDataGzip) {
  1594. MultipartFormDataItems items = {
  1595. {"key1", "test", "", ""},
  1596. {"key2", "--abcdefg123", "", ""},
  1597. };
  1598. cli_.set_compress(true);
  1599. auto res = cli_.Post("/gzipmultipart", items);
  1600. ASSERT_TRUE(res != nullptr);
  1601. EXPECT_EQ(200, res->status);
  1602. }
  1603. #endif
  1604. // Sends a raw request to a server listening at HOST:PORT.
  1605. static bool send_request(time_t read_timeout_sec, const std::string &req) {
  1606. auto client_sock = detail::create_client_socket(HOST, PORT, /*timeout_sec=*/5,
  1607. std::string());
  1608. if (client_sock == INVALID_SOCKET) { return false; }
  1609. return detail::process_and_close_socket(
  1610. true, client_sock, 1, read_timeout_sec, 0,
  1611. [&](Stream &strm, bool /*last_connection*/, bool &
  1612. /*connection_close*/) -> bool {
  1613. if (req.size() !=
  1614. static_cast<size_t>(strm.write(req.data(), req.size()))) {
  1615. return false;
  1616. }
  1617. char buf[512];
  1618. detail::stream_line_reader line_reader(strm, buf, sizeof(buf));
  1619. while (line_reader.getline()) {}
  1620. return true;
  1621. });
  1622. }
  1623. TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
  1624. Server svr;
  1625. std::string header_value;
  1626. svr.Get("/validate-ws-in-headers", [&](const Request &req, Response &res) {
  1627. header_value = req.get_header_value("foo");
  1628. res.set_content("ok", "text/plain");
  1629. });
  1630. thread t = thread([&] { svr.listen(HOST, PORT); });
  1631. while (!svr.is_running()) {
  1632. msleep(1);
  1633. }
  1634. // Only space and horizontal tab are whitespace. Make sure other whitespace-
  1635. // like characters are not treated the same - use vertical tab and escape.
  1636. const std::string req = "GET /validate-ws-in-headers HTTP/1.1\r\n"
  1637. "foo: \t \v bar \e\t \r\n"
  1638. "Connection: close\r\n"
  1639. "\r\n";
  1640. ASSERT_TRUE(send_request(5, req));
  1641. svr.stop();
  1642. t.join();
  1643. EXPECT_EQ(header_value, "\v bar \e");
  1644. }
  1645. TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
  1646. Server svr;
  1647. svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1648. res.set_content("ok", "text/plain");
  1649. });
  1650. // Server read timeout must be longer than the client read timeout for the
  1651. // bug to reproduce, probably to force the server to process a request
  1652. // without a trailing blank line.
  1653. const time_t client_read_timeout_sec = 1;
  1654. svr.set_read_timeout(client_read_timeout_sec + 1, 0);
  1655. bool listen_thread_ok = false;
  1656. thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
  1657. while (!svr.is_running()) {
  1658. msleep(1);
  1659. }
  1660. // A certain header line causes an exception if the header property is parsed
  1661. // naively with a single regex. This occurs with libc++ but not libstdc++.
  1662. const std::string req =
  1663. "GET /hi HTTP/1.1\r\n"
  1664. " : "
  1665. " ";
  1666. ASSERT_TRUE(send_request(client_read_timeout_sec, req));
  1667. svr.stop();
  1668. t.join();
  1669. EXPECT_TRUE(listen_thread_ok);
  1670. }
  1671. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1672. protected:
  1673. ServerTestWithAI_PASSIVE()
  1674. : cli_(HOST, PORT)
  1675. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1676. ,
  1677. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1678. #endif
  1679. {
  1680. }
  1681. virtual void SetUp() {
  1682. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1683. res.set_content("Hello World!", "text/plain");
  1684. });
  1685. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1686. while (!svr_.is_running()) {
  1687. msleep(1);
  1688. }
  1689. }
  1690. virtual void TearDown() {
  1691. svr_.stop();
  1692. t_.join();
  1693. }
  1694. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1695. SSLClient cli_;
  1696. SSLServer svr_;
  1697. #else
  1698. Client cli_;
  1699. Server svr_;
  1700. #endif
  1701. thread t_;
  1702. };
  1703. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1704. auto res = cli_.Get("/hi");
  1705. ASSERT_TRUE(res != nullptr);
  1706. EXPECT_EQ(200, res->status);
  1707. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1708. EXPECT_EQ("Hello World!", res->body);
  1709. }
  1710. class ServerUpDownTest : public ::testing::Test {
  1711. protected:
  1712. ServerUpDownTest() : cli_(HOST, PORT) {}
  1713. virtual void SetUp() {
  1714. t_ = thread([&]() {
  1715. svr_.bind_to_any_port(HOST);
  1716. msleep(500);
  1717. ASSERT_TRUE(svr_.listen_after_bind());
  1718. });
  1719. while (!svr_.is_running()) {
  1720. msleep(1);
  1721. }
  1722. }
  1723. virtual void TearDown() {
  1724. svr_.stop();
  1725. t_.join();
  1726. }
  1727. Client cli_;
  1728. Server svr_;
  1729. thread t_;
  1730. };
  1731. TEST_F(ServerUpDownTest, QuickStartStop) {
  1732. // Should not crash, especially when run with
  1733. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1734. }
  1735. class PayloadMaxLengthTest : public ::testing::Test {
  1736. protected:
  1737. PayloadMaxLengthTest()
  1738. : cli_(HOST, PORT)
  1739. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1740. ,
  1741. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1742. #endif
  1743. {
  1744. }
  1745. virtual void SetUp() {
  1746. svr_.set_payload_max_length(8);
  1747. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1748. res.set_content("test", "text/plain");
  1749. });
  1750. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1751. while (!svr_.is_running()) {
  1752. msleep(1);
  1753. }
  1754. }
  1755. virtual void TearDown() {
  1756. svr_.stop();
  1757. t_.join();
  1758. }
  1759. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1760. SSLClient cli_;
  1761. SSLServer svr_;
  1762. #else
  1763. Client cli_;
  1764. Server svr_;
  1765. #endif
  1766. thread t_;
  1767. };
  1768. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1769. auto res = cli_.Post("/test", "123456789", "text/plain");
  1770. ASSERT_TRUE(res != nullptr);
  1771. EXPECT_EQ(413, res->status);
  1772. res = cli_.Post("/test", "12345678", "text/plain");
  1773. ASSERT_TRUE(res != nullptr);
  1774. EXPECT_EQ(200, res->status);
  1775. }
  1776. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1777. TEST(SSLClientTest, ServerNameIndication) {
  1778. SSLClient cli("httpbin.org", 443);
  1779. auto res = cli.Get("/get");
  1780. ASSERT_TRUE(res != nullptr);
  1781. ASSERT_EQ(200, res->status);
  1782. }
  1783. TEST(SSLClientTest, ServerCertificateVerification) {
  1784. SSLClient cli("google.com");
  1785. auto res = cli.Get("/");
  1786. ASSERT_TRUE(res != nullptr);
  1787. ASSERT_EQ(301, res->status);
  1788. cli.enable_server_certificate_verification(true);
  1789. res = cli.Get("/");
  1790. ASSERT_TRUE(res == nullptr);
  1791. cli.set_ca_cert_path(CA_CERT_FILE);
  1792. res = cli.Get("/");
  1793. ASSERT_TRUE(res != nullptr);
  1794. ASSERT_EQ(301, res->status);
  1795. }
  1796. TEST(SSLClientTest, WildcardHostNameMatch) {
  1797. SSLClient cli("www.youtube.com");
  1798. cli.set_ca_cert_path(CA_CERT_FILE);
  1799. cli.enable_server_certificate_verification(true);
  1800. auto res = cli.Get("/");
  1801. ASSERT_TRUE(res != nullptr);
  1802. ASSERT_EQ(200, res->status);
  1803. }
  1804. TEST(SSLClientServerTest, ClientCertPresent) {
  1805. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1806. CLIENT_CA_CERT_DIR);
  1807. ASSERT_TRUE(svr.is_valid());
  1808. svr.Get("/test", [&](const Request &req, Response &res) {
  1809. res.set_content("test", "text/plain");
  1810. svr.stop();
  1811. ASSERT_TRUE(true);
  1812. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1813. ASSERT_TRUE(peer_cert != nullptr);
  1814. auto subject_name = X509_get_subject_name(peer_cert);
  1815. ASSERT_TRUE(subject_name != nullptr);
  1816. std::string common_name;
  1817. {
  1818. char name[BUFSIZ];
  1819. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1820. name, sizeof(name));
  1821. common_name.assign(name, name_len);
  1822. }
  1823. EXPECT_EQ("Common Name", common_name);
  1824. X509_free(peer_cert);
  1825. });
  1826. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1827. msleep(1);
  1828. httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE,
  1829. CLIENT_PRIVATE_KEY_FILE);
  1830. auto res = cli.Get("/test");
  1831. cli.set_timeout_sec(30);
  1832. ASSERT_TRUE(res != nullptr);
  1833. ASSERT_EQ(200, res->status);
  1834. t.join();
  1835. }
  1836. TEST(SSLClientServerTest, ClientCertMissing) {
  1837. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1838. CLIENT_CA_CERT_DIR);
  1839. ASSERT_TRUE(svr.is_valid());
  1840. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1841. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1842. msleep(1);
  1843. httplib::SSLClient cli(HOST, PORT);
  1844. auto res = cli.Get("/test");
  1845. cli.set_timeout_sec(30);
  1846. ASSERT_TRUE(res == nullptr);
  1847. svr.stop();
  1848. t.join();
  1849. }
  1850. TEST(SSLClientServerTest, TrustDirOptional) {
  1851. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1852. ASSERT_TRUE(svr.is_valid());
  1853. svr.Get("/test", [&](const Request &, Response &res) {
  1854. res.set_content("test", "text/plain");
  1855. svr.stop();
  1856. });
  1857. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1858. msleep(1);
  1859. httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE,
  1860. CLIENT_PRIVATE_KEY_FILE);
  1861. auto res = cli.Get("/test");
  1862. cli.set_timeout_sec(30);
  1863. ASSERT_TRUE(res != nullptr);
  1864. ASSERT_EQ(200, res->status);
  1865. t.join();
  1866. }
  1867. /* Cannot test this case as there is no external access to SSL object to check
  1868. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1869. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1870. nullptr, CLIENT_CA_CERT_DIR);
  1871. }
  1872. */
  1873. #endif
  1874. #ifdef _WIN32
  1875. TEST(CleanupTest, WSACleanup) {
  1876. int ret = WSACleanup();
  1877. ASSERT_EQ(0, ret);
  1878. }
  1879. #endif