test.cc 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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_.set_file_extension_and_mimetype_mapping("abcde", "text/abcde");
  547. svr_.Get("/hi",
  548. [&](const Request & /*req*/, Response &res) {
  549. res.set_content("Hello World!", "text/plain");
  550. })
  551. .Get("/slow",
  552. [&](const Request & /*req*/, Response &res) {
  553. msleep(2000);
  554. res.set_content("slow", "text/plain");
  555. })
  556. .Get("/remote_addr",
  557. [&](const Request &req, Response &res) {
  558. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  559. res.set_content(remote_addr.c_str(), "text/plain");
  560. })
  561. .Get("/endwith%",
  562. [&](const Request & /*req*/, Response &res) {
  563. res.set_content("Hello World!", "text/plain");
  564. })
  565. .Get("/", [&](const Request & /*req*/,
  566. Response &res) { res.set_redirect("/hi"); })
  567. .Post("/person",
  568. [&](const Request &req, Response &res) {
  569. if (req.has_param("name") && req.has_param("note")) {
  570. persons_[req.get_param_value("name")] =
  571. req.get_param_value("note");
  572. } else {
  573. res.status = 400;
  574. }
  575. })
  576. .Get("/person/(.*)",
  577. [&](const Request &req, Response &res) {
  578. string name = req.matches[1];
  579. if (persons_.find(name) != persons_.end()) {
  580. auto note = persons_[name];
  581. res.set_content(note, "text/plain");
  582. } else {
  583. res.status = 404;
  584. }
  585. })
  586. .Post("/x-www-form-urlencoded-json",
  587. [&](const Request &req, Response &res) {
  588. auto json = req.get_param_value("json");
  589. ASSERT_EQ(JSON_DATA, json);
  590. res.set_content(json, "appliation/json");
  591. res.status = 200;
  592. })
  593. .Get("/streamed-chunked",
  594. [&](const Request & /*req*/, Response &res) {
  595. res.set_chunked_content_provider(
  596. [](uint64_t /*offset*/, DataSink sink, Done done) {
  597. sink("123", 3);
  598. sink("456", 3);
  599. sink("789", 3);
  600. done();
  601. });
  602. })
  603. .Get("/streamed",
  604. [&](const Request & /*req*/, Response &res) {
  605. res.set_content_provider(
  606. 6, [](uint64_t offset, uint64_t /*length*/, DataSink sink) {
  607. sink(offset < 3 ? "a" : "b", 1);
  608. });
  609. })
  610. .Get("/streamed-with-range",
  611. [&](const Request & /*req*/, Response &res) {
  612. auto data = new std::string("abcdefg");
  613. res.set_content_provider(
  614. data->size(),
  615. [data](uint64_t offset, uint64_t length, DataSink sink) {
  616. size_t DATA_CHUNK_SIZE = 4;
  617. const auto &d = *data;
  618. auto out_len =
  619. std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
  620. sink(&d[static_cast<size_t>(offset)], out_len);
  621. },
  622. [data] { delete data; });
  623. })
  624. .Get("/streamed-cancel",
  625. [&](const Request & /*req*/, Response &res) {
  626. res.set_content_provider(
  627. size_t(-1),
  628. [](uint64_t /*offset*/, uint64_t /*length*/, DataSink sink) {
  629. std::string data = "data_chunk";
  630. sink(data.data(), data.size());
  631. });
  632. })
  633. .Get("/with-range",
  634. [&](const Request & /*req*/, Response &res) {
  635. res.set_content("abcdefg", "text/plain");
  636. })
  637. .Post("/chunked",
  638. [&](const Request &req, Response & /*res*/) {
  639. EXPECT_EQ(req.body, "dechunked post body");
  640. })
  641. .Post("/large-chunked",
  642. [&](const Request &req, Response & /*res*/) {
  643. std::string expected(6 * 30 * 1024u, 'a');
  644. EXPECT_EQ(req.body, expected);
  645. })
  646. .Post("/multipart",
  647. [&](const Request &req, Response & /*res*/) {
  648. EXPECT_EQ(5u, req.files.size());
  649. ASSERT_TRUE(!req.has_file("???"));
  650. {
  651. const auto &file = req.get_file_value("text1");
  652. EXPECT_EQ("", file.filename);
  653. EXPECT_EQ("text default", file.content);
  654. }
  655. {
  656. const auto &file = req.get_file_value("text2");
  657. EXPECT_EQ("", file.filename);
  658. EXPECT_EQ("aωb", file.content);
  659. }
  660. {
  661. const auto &file = req.get_file_value("file1");
  662. EXPECT_EQ("hello.txt", file.filename);
  663. EXPECT_EQ("text/plain", file.content_type);
  664. EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content);
  665. }
  666. {
  667. const auto &file = req.get_file_value("file3");
  668. EXPECT_EQ("", file.filename);
  669. EXPECT_EQ("application/octet-stream", file.content_type);
  670. EXPECT_EQ(0u, file.content.size());
  671. }
  672. })
  673. .Post("/empty",
  674. [&](const Request &req, Response &res) {
  675. EXPECT_EQ(req.body, "");
  676. res.set_content("empty", "text/plain");
  677. })
  678. .Put("/put",
  679. [&](const Request &req, Response &res) {
  680. EXPECT_EQ(req.body, "PUT");
  681. res.set_content(req.body, "text/plain");
  682. })
  683. .Put("/put-large",
  684. [&](const Request &req, Response &res) {
  685. EXPECT_EQ(req.body, LARGE_DATA);
  686. res.set_content(req.body, "text/plain");
  687. })
  688. .Patch("/patch",
  689. [&](const Request &req, Response &res) {
  690. EXPECT_EQ(req.body, "PATCH");
  691. res.set_content(req.body, "text/plain");
  692. })
  693. .Delete("/delete",
  694. [&](const Request & /*req*/, Response &res) {
  695. res.set_content("DELETE", "text/plain");
  696. })
  697. .Options(R"(\*)",
  698. [&](const Request & /*req*/, Response &res) {
  699. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  700. })
  701. .Get("/request-target",
  702. [&](const Request &req, Response & /*res*/) {
  703. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  704. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  705. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  706. })
  707. .Get("/long-query-value",
  708. [&](const Request &req, Response & /*res*/) {
  709. EXPECT_EQ(LONG_QUERY_URL, req.target);
  710. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  711. })
  712. .Get("/array-param",
  713. [&](const Request &req, Response & /*res*/) {
  714. EXPECT_EQ(3u, req.get_param_value_count("array"));
  715. EXPECT_EQ("value1", req.get_param_value("array", 0));
  716. EXPECT_EQ("value2", req.get_param_value("array", 1));
  717. EXPECT_EQ("value3", req.get_param_value("array", 2));
  718. })
  719. .Post("/validate-no-multiple-headers",
  720. [&](const Request &req, Response & /*res*/) {
  721. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  722. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  723. })
  724. .Post("/content_receiver",
  725. [&](const Request &req, Response &res,
  726. const ContentReader &content_reader) {
  727. if (req.is_multipart_form_data()) {
  728. MultipartFormDataItems files;
  729. content_reader(
  730. [&](const MultipartFormData &file) {
  731. files.push_back(file);
  732. return true;
  733. },
  734. [&](const char *data, size_t data_length) {
  735. files.back().content.append(data, data_length);
  736. return true;
  737. });
  738. EXPECT_EQ(5u, files.size());
  739. {
  740. const auto &file = get_file_value(files, "text1");
  741. EXPECT_EQ("", file.filename);
  742. EXPECT_EQ("text default", file.content);
  743. }
  744. {
  745. const auto &file = get_file_value(files, "text2");
  746. EXPECT_EQ("", file.filename);
  747. EXPECT_EQ("aωb", file.content);
  748. }
  749. {
  750. const auto &file = get_file_value(files, "file1");
  751. EXPECT_EQ("hello.txt", file.filename);
  752. EXPECT_EQ("text/plain", file.content_type);
  753. EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content);
  754. }
  755. {
  756. const auto &file = get_file_value(files, "file3");
  757. EXPECT_EQ("", file.filename);
  758. EXPECT_EQ("application/octet-stream", file.content_type);
  759. EXPECT_EQ(0u, file.content.size());
  760. }
  761. } else {
  762. std::string body;
  763. content_reader([&](const char *data, size_t data_length) {
  764. EXPECT_EQ(data_length, 7);
  765. body.append(data, data_length);
  766. return true;
  767. });
  768. EXPECT_EQ(body, "content");
  769. res.set_content(body, "text/plain");
  770. }
  771. })
  772. .Put("/content_receiver",
  773. [&](const Request & /*req*/, Response &res,
  774. const ContentReader &content_reader) {
  775. std::string body;
  776. content_reader([&](const char *data, size_t data_length) {
  777. body.append(data, data_length);
  778. return true;
  779. });
  780. EXPECT_EQ(body, "content");
  781. res.set_content(body, "text/plain");
  782. })
  783. .Patch("/content_receiver",
  784. [&](const Request & /*req*/, Response &res,
  785. const ContentReader &content_reader) {
  786. std::string body;
  787. content_reader([&](const char *data, size_t data_length) {
  788. body.append(data, data_length);
  789. return true;
  790. });
  791. EXPECT_EQ(body, "content");
  792. res.set_content(body, "text/plain");
  793. })
  794. .Post("/query-string-and-body",
  795. [&](const Request &req, Response & /*res*/) {
  796. ASSERT_TRUE(req.has_param("key"));
  797. EXPECT_EQ(req.get_param_value("key"), "value");
  798. EXPECT_EQ(req.body, "content");
  799. })
  800. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  801. .Get("/gzip",
  802. [&](const Request & /*req*/, Response &res) {
  803. res.set_content(
  804. "12345678901234567890123456789012345678901234567890123456789"
  805. "01234567890123456789012345678901234567890",
  806. "text/plain");
  807. })
  808. .Get("/nogzip",
  809. [&](const Request & /*req*/, Response &res) {
  810. res.set_content(
  811. "12345678901234567890123456789012345678901234567890123456789"
  812. "01234567890123456789012345678901234567890",
  813. "application/octet-stream");
  814. })
  815. .Post("/gzipmultipart",
  816. [&](const Request &req, Response & /*res*/) {
  817. EXPECT_EQ(2u, req.files.size());
  818. ASSERT_TRUE(!req.has_file("???"));
  819. {
  820. const auto &file = req.get_file_value("key1");
  821. EXPECT_EQ("", file.filename);
  822. EXPECT_EQ("test", file.content);
  823. }
  824. {
  825. const auto &file = req.get_file_value("key2");
  826. EXPECT_EQ("", file.filename);
  827. EXPECT_EQ("--abcdefg123", file.content);
  828. }
  829. })
  830. #endif
  831. ;
  832. persons_["john"] = "programmer";
  833. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  834. while (!svr_.is_running()) {
  835. msleep(1);
  836. }
  837. }
  838. virtual void TearDown() {
  839. svr_.stop();
  840. for (auto &t : request_threads_) {
  841. t.join();
  842. }
  843. t_.join();
  844. }
  845. map<string, string> persons_;
  846. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  847. SSLClient cli_;
  848. SSLServer svr_;
  849. #else
  850. Client cli_;
  851. Server svr_;
  852. #endif
  853. thread t_;
  854. std::vector<thread> request_threads_;
  855. };
  856. TEST_F(ServerTest, GetMethod200) {
  857. auto res = cli_.Get("/hi");
  858. ASSERT_TRUE(res != nullptr);
  859. EXPECT_EQ("HTTP/1.1", res->version);
  860. EXPECT_EQ(200, res->status);
  861. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  862. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  863. EXPECT_EQ("Hello World!", res->body);
  864. }
  865. TEST_F(ServerTest, GetMethod302) {
  866. auto res = cli_.Get("/");
  867. ASSERT_TRUE(res != nullptr);
  868. EXPECT_EQ(302, res->status);
  869. EXPECT_EQ("/hi", res->get_header_value("Location"));
  870. }
  871. TEST_F(ServerTest, GetMethod404) {
  872. auto res = cli_.Get("/invalid");
  873. ASSERT_TRUE(res != nullptr);
  874. EXPECT_EQ(404, res->status);
  875. }
  876. TEST_F(ServerTest, HeadMethod200) {
  877. auto res = cli_.Head("/hi");
  878. ASSERT_TRUE(res != nullptr);
  879. EXPECT_EQ(200, res->status);
  880. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  881. EXPECT_EQ("", res->body);
  882. }
  883. TEST_F(ServerTest, HeadMethod404) {
  884. auto res = cli_.Head("/invalid");
  885. ASSERT_TRUE(res != nullptr);
  886. EXPECT_EQ(404, res->status);
  887. EXPECT_EQ("", res->body);
  888. }
  889. TEST_F(ServerTest, GetMethodPersonJohn) {
  890. auto res = cli_.Get("/person/john");
  891. ASSERT_TRUE(res != nullptr);
  892. EXPECT_EQ(200, res->status);
  893. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  894. EXPECT_EQ("programmer", res->body);
  895. }
  896. TEST_F(ServerTest, PostMethod1) {
  897. auto res = cli_.Get("/person/john1");
  898. ASSERT_TRUE(res != nullptr);
  899. ASSERT_EQ(404, res->status);
  900. res = cli_.Post("/person", "name=john1&note=coder",
  901. "application/x-www-form-urlencoded");
  902. ASSERT_TRUE(res != nullptr);
  903. ASSERT_EQ(200, res->status);
  904. res = cli_.Get("/person/john1");
  905. ASSERT_TRUE(res != nullptr);
  906. ASSERT_EQ(200, res->status);
  907. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  908. ASSERT_EQ("coder", res->body);
  909. }
  910. TEST_F(ServerTest, PostMethod2) {
  911. auto res = cli_.Get("/person/john2");
  912. ASSERT_TRUE(res != nullptr);
  913. ASSERT_EQ(404, res->status);
  914. Params params;
  915. params.emplace("name", "john2");
  916. params.emplace("note", "coder");
  917. res = cli_.Post("/person", params);
  918. ASSERT_TRUE(res != nullptr);
  919. ASSERT_EQ(200, res->status);
  920. res = cli_.Get("/person/john2");
  921. ASSERT_TRUE(res != nullptr);
  922. ASSERT_EQ(200, res->status);
  923. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  924. ASSERT_EQ("coder", res->body);
  925. }
  926. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  927. Params params;
  928. params.emplace("json", JSON_DATA);
  929. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  930. ASSERT_TRUE(res != nullptr);
  931. ASSERT_EQ(200, res->status);
  932. ASSERT_EQ(JSON_DATA, res->body);
  933. }
  934. TEST_F(ServerTest, PostEmptyContent) {
  935. auto res = cli_.Post("/empty", "", "text/plain");
  936. ASSERT_TRUE(res != nullptr);
  937. ASSERT_EQ(200, res->status);
  938. ASSERT_EQ("empty", res->body);
  939. }
  940. TEST_F(ServerTest, GetMethodDir) {
  941. auto res = cli_.Get("/dir/");
  942. ASSERT_TRUE(res != nullptr);
  943. EXPECT_EQ(200, res->status);
  944. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  945. auto body = R"(<html>
  946. <head>
  947. </head>
  948. <body>
  949. <a href="/dir/test.html">Test</a>
  950. <a href="/hi">hi</a>
  951. </body>
  952. </html>
  953. )";
  954. EXPECT_EQ(body, res->body);
  955. }
  956. TEST_F(ServerTest, GetMethodDirTest) {
  957. auto res = cli_.Get("/dir/test.html");
  958. ASSERT_TRUE(res != nullptr);
  959. EXPECT_EQ(200, res->status);
  960. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  961. EXPECT_EQ("test.html", res->body);
  962. }
  963. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  964. auto res = cli_.Get("/dir/../dir/test.html");
  965. ASSERT_TRUE(res != nullptr);
  966. EXPECT_EQ(200, res->status);
  967. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  968. EXPECT_EQ("test.html", res->body);
  969. }
  970. TEST_F(ServerTest, GetMethodInvalidPath) {
  971. auto res = cli_.Get("/dir/../test.html");
  972. ASSERT_TRUE(res != nullptr);
  973. EXPECT_EQ(404, res->status);
  974. }
  975. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  976. auto res = cli_.Get("/../www/dir/test.html");
  977. ASSERT_TRUE(res != nullptr);
  978. EXPECT_EQ(404, res->status);
  979. }
  980. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  981. auto res = cli_.Get("/dir/../../www/dir/test.html");
  982. ASSERT_TRUE(res != nullptr);
  983. EXPECT_EQ(404, res->status);
  984. }
  985. TEST_F(ServerTest, GetMethodDirMountTest) {
  986. auto res = cli_.Get("/mount/dir/test.html");
  987. ASSERT_TRUE(res != nullptr);
  988. EXPECT_EQ(200, res->status);
  989. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  990. EXPECT_EQ("test.html", res->body);
  991. }
  992. TEST_F(ServerTest, GetMethodDirMountTestWithDoubleDots) {
  993. auto res = cli_.Get("/mount/dir/../dir/test.html");
  994. ASSERT_TRUE(res != nullptr);
  995. EXPECT_EQ(200, res->status);
  996. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  997. EXPECT_EQ("test.html", res->body);
  998. }
  999. TEST_F(ServerTest, GetMethodInvalidMountPath) {
  1000. auto res = cli_.Get("/mount/dir/../test.html");
  1001. ASSERT_TRUE(res != nullptr);
  1002. EXPECT_EQ(404, res->status);
  1003. }
  1004. TEST_F(ServerTest, GetMethodOutOfBaseDirMount) {
  1005. auto res = cli_.Get("/mount/../www2/dir/test.html");
  1006. ASSERT_TRUE(res != nullptr);
  1007. EXPECT_EQ(404, res->status);
  1008. }
  1009. TEST_F(ServerTest, GetMethodOutOfBaseDirMount2) {
  1010. auto res = cli_.Get("/mount/dir/../../www2/dir/test.html");
  1011. ASSERT_TRUE(res != nullptr);
  1012. EXPECT_EQ(404, res->status);
  1013. }
  1014. TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
  1015. auto res = cli_.Get("/dir/test.abcde");
  1016. ASSERT_TRUE(res != nullptr);
  1017. EXPECT_EQ(200, res->status);
  1018. EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
  1019. EXPECT_EQ("abcde\n", res->body);
  1020. }
  1021. TEST_F(ServerTest, InvalidBaseDirMount) {
  1022. EXPECT_EQ(false, svr_.set_base_dir("./www3", "invalid_mount_point"));
  1023. }
  1024. TEST_F(ServerTest, EmptyRequest) {
  1025. auto res = cli_.Get("");
  1026. ASSERT_TRUE(res == nullptr);
  1027. }
  1028. TEST_F(ServerTest, LongRequest) {
  1029. std::string request;
  1030. for (size_t i = 0; i < 545; i++) {
  1031. request += "/TooLongRequest";
  1032. }
  1033. request += "OK";
  1034. auto res = cli_.Get(request.c_str());
  1035. ASSERT_TRUE(res != nullptr);
  1036. EXPECT_EQ(404, res->status);
  1037. }
  1038. TEST_F(ServerTest, TooLongRequest) {
  1039. std::string request;
  1040. for (size_t i = 0; i < 545; i++) {
  1041. request += "/TooLongRequest";
  1042. }
  1043. request += "_NG";
  1044. auto res = cli_.Get(request.c_str());
  1045. ASSERT_TRUE(res != nullptr);
  1046. EXPECT_EQ(414, res->status);
  1047. }
  1048. TEST_F(ServerTest, LongHeader) {
  1049. Request req;
  1050. req.method = "GET";
  1051. req.path = "/hi";
  1052. std::string host_and_port;
  1053. host_and_port += HOST;
  1054. host_and_port += ":";
  1055. host_and_port += std::to_string(PORT);
  1056. req.headers.emplace("Host", host_and_port.c_str());
  1057. req.headers.emplace("Accept", "*/*");
  1058. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1059. req.headers.emplace(
  1060. "Header-Name",
  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. "@@@@@@@@@@@@@@@@");
  1090. auto res = std::make_shared<Response>();
  1091. auto ret = cli_.send(req, *res);
  1092. ASSERT_TRUE(ret);
  1093. EXPECT_EQ(200, res->status);
  1094. }
  1095. TEST_F(ServerTest, LongQueryValue) {
  1096. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  1097. ASSERT_TRUE(res != nullptr);
  1098. EXPECT_EQ(414, res->status);
  1099. }
  1100. TEST_F(ServerTest, TooLongHeader) {
  1101. Request req;
  1102. req.method = "GET";
  1103. req.path = "/hi";
  1104. std::string host_and_port;
  1105. host_and_port += HOST;
  1106. host_and_port += ":";
  1107. host_and_port += std::to_string(PORT);
  1108. req.headers.emplace("Host", host_and_port.c_str());
  1109. req.headers.emplace("Accept", "*/*");
  1110. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1111. req.headers.emplace(
  1112. "Header-Name",
  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. "@@@@@@@@@@@@@@@@@");
  1142. auto res = std::make_shared<Response>();
  1143. auto ret = cli_.send(req, *res);
  1144. ASSERT_TRUE(ret);
  1145. EXPECT_EQ(200, res->status);
  1146. }
  1147. TEST_F(ServerTest, PercentEncoding) {
  1148. auto res = cli_.Get("/e%6edwith%");
  1149. ASSERT_TRUE(res != nullptr);
  1150. EXPECT_EQ(200, res->status);
  1151. }
  1152. TEST_F(ServerTest, PercentEncodingUnicode) {
  1153. auto res = cli_.Get("/e%u006edwith%");
  1154. ASSERT_TRUE(res != nullptr);
  1155. EXPECT_EQ(200, res->status);
  1156. }
  1157. TEST_F(ServerTest, InvalidPercentEncoding) {
  1158. auto res = cli_.Get("/%endwith%");
  1159. ASSERT_TRUE(res != nullptr);
  1160. EXPECT_EQ(404, res->status);
  1161. }
  1162. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  1163. auto res = cli_.Get("/%uendwith%");
  1164. ASSERT_TRUE(res != nullptr);
  1165. EXPECT_EQ(404, res->status);
  1166. }
  1167. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  1168. auto res = cli_.Get("/hello?aaa=bbb%");
  1169. ASSERT_TRUE(res != nullptr);
  1170. EXPECT_EQ(404, res->status);
  1171. }
  1172. TEST_F(ServerTest, MultipartFormData) {
  1173. MultipartFormDataItems items = {
  1174. {"text1", "text default", "", ""},
  1175. {"text2", "aωb", "", ""},
  1176. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1177. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1178. {"file3", "", "", "application/octet-stream"},
  1179. };
  1180. auto res = cli_.Post("/multipart", items);
  1181. ASSERT_TRUE(res != nullptr);
  1182. EXPECT_EQ(200, res->status);
  1183. }
  1184. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  1185. auto res = cli_.Get("/hi");
  1186. ASSERT_TRUE(res != nullptr);
  1187. EXPECT_EQ(200, res->status);
  1188. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  1189. EXPECT_EQ("Hello World!", res->body);
  1190. }
  1191. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  1192. Request req;
  1193. req.method = "POST";
  1194. req.path = "/chunked";
  1195. std::string host_and_port;
  1196. host_and_port += HOST;
  1197. host_and_port += ":";
  1198. host_and_port += std::to_string(PORT);
  1199. req.headers.emplace("Host", host_and_port.c_str());
  1200. req.headers.emplace("Accept", "*/*");
  1201. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1202. req.headers.emplace("Content-Type", "text/plain");
  1203. req.headers.emplace("Content-Length", "0");
  1204. req.headers.emplace(
  1205. "Transfer-Encoding",
  1206. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  1207. // Client does not chunk, so make a chunked body manually.
  1208. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  1209. auto res = std::make_shared<Response>();
  1210. auto ret = cli_.send(req, *res);
  1211. ASSERT_TRUE(ret);
  1212. EXPECT_EQ(200, res->status);
  1213. }
  1214. TEST_F(ServerTest, GetStreamed2) {
  1215. auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
  1216. ASSERT_TRUE(res != nullptr);
  1217. EXPECT_EQ(206, res->status);
  1218. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1219. EXPECT_EQ(std::string("ab"), res->body);
  1220. }
  1221. TEST_F(ServerTest, GetStreamed) {
  1222. auto res = cli_.Get("/streamed");
  1223. ASSERT_TRUE(res != nullptr);
  1224. EXPECT_EQ(200, res->status);
  1225. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1226. EXPECT_EQ(std::string("aaabbb"), res->body);
  1227. }
  1228. TEST_F(ServerTest, GetStreamedWithRange1) {
  1229. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
  1230. ASSERT_TRUE(res != nullptr);
  1231. EXPECT_EQ(206, res->status);
  1232. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1233. EXPECT_EQ(true, res->has_header("Content-Range"));
  1234. EXPECT_EQ(std::string("def"), res->body);
  1235. }
  1236. TEST_F(ServerTest, GetStreamedWithRange2) {
  1237. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
  1238. ASSERT_TRUE(res != nullptr);
  1239. EXPECT_EQ(206, res->status);
  1240. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1241. EXPECT_EQ(true, res->has_header("Content-Range"));
  1242. EXPECT_EQ(std::string("bcdefg"), res->body);
  1243. }
  1244. TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
  1245. auto res =
  1246. cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1247. ASSERT_TRUE(res != nullptr);
  1248. EXPECT_EQ(206, res->status);
  1249. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1250. EXPECT_EQ(false, res->has_header("Content-Range"));
  1251. EXPECT_EQ(269, res->body.size());
  1252. }
  1253. TEST_F(ServerTest, GetStreamedEndless) {
  1254. uint64_t offset = 0;
  1255. auto res = cli_.Get("/streamed-cancel",
  1256. [&](const char * /*data*/, uint64_t data_length) {
  1257. if (offset < 100) {
  1258. offset += data_length;
  1259. return true;
  1260. }
  1261. return false;
  1262. });
  1263. ASSERT_TRUE(res == nullptr);
  1264. }
  1265. TEST_F(ServerTest, GetWithRange1) {
  1266. auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
  1267. ASSERT_TRUE(res != nullptr);
  1268. EXPECT_EQ(206, res->status);
  1269. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1270. EXPECT_EQ(true, res->has_header("Content-Range"));
  1271. EXPECT_EQ(std::string("def"), res->body);
  1272. }
  1273. TEST_F(ServerTest, GetWithRange2) {
  1274. auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
  1275. ASSERT_TRUE(res != nullptr);
  1276. EXPECT_EQ(206, res->status);
  1277. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1278. EXPECT_EQ(true, res->has_header("Content-Range"));
  1279. EXPECT_EQ(std::string("bcdefg"), res->body);
  1280. }
  1281. TEST_F(ServerTest, GetWithRange3) {
  1282. auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
  1283. ASSERT_TRUE(res != nullptr);
  1284. EXPECT_EQ(206, res->status);
  1285. EXPECT_EQ("1", res->get_header_value("Content-Length"));
  1286. EXPECT_EQ(true, res->has_header("Content-Range"));
  1287. EXPECT_EQ(std::string("a"), res->body);
  1288. }
  1289. TEST_F(ServerTest, GetWithRange4) {
  1290. auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
  1291. ASSERT_TRUE(res != nullptr);
  1292. EXPECT_EQ(206, res->status);
  1293. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1294. EXPECT_EQ(true, res->has_header("Content-Range"));
  1295. EXPECT_EQ(std::string("fg"), res->body);
  1296. }
  1297. TEST_F(ServerTest, GetWithRangeMultipart) {
  1298. auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1299. ASSERT_TRUE(res != nullptr);
  1300. EXPECT_EQ(206, res->status);
  1301. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1302. EXPECT_EQ(false, res->has_header("Content-Range"));
  1303. EXPECT_EQ(269, res->body.size());
  1304. }
  1305. TEST_F(ServerTest, GetStreamedChunked) {
  1306. auto res = cli_.Get("/streamed-chunked");
  1307. ASSERT_TRUE(res != nullptr);
  1308. EXPECT_EQ(200, res->status);
  1309. EXPECT_EQ(std::string("123456789"), res->body);
  1310. }
  1311. TEST_F(ServerTest, LargeChunkedPost) {
  1312. Request req;
  1313. req.method = "POST";
  1314. req.path = "/large-chunked";
  1315. std::string host_and_port;
  1316. host_and_port += HOST;
  1317. host_and_port += ":";
  1318. host_and_port += std::to_string(PORT);
  1319. req.headers.emplace("Host", host_and_port.c_str());
  1320. req.headers.emplace("Accept", "*/*");
  1321. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1322. req.headers.emplace("Content-Type", "text/plain");
  1323. req.headers.emplace("Content-Length", "0");
  1324. req.headers.emplace("Transfer-Encoding", "chunked");
  1325. std::string long_string(30 * 1024u, 'a');
  1326. std::string chunk = "7800\r\n" + long_string + "\r\n";
  1327. // Attempt to make a large enough post to exceed OS buffers, to test that
  1328. // the server handles short reads if the full chunk data isn't available.
  1329. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  1330. auto res = std::make_shared<Response>();
  1331. auto ret = cli_.send(req, *res);
  1332. ASSERT_TRUE(ret);
  1333. EXPECT_EQ(200, res->status);
  1334. }
  1335. TEST_F(ServerTest, GetMethodRemoteAddr) {
  1336. auto res = cli_.Get("/remote_addr");
  1337. ASSERT_TRUE(res != nullptr);
  1338. EXPECT_EQ(200, res->status);
  1339. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1340. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  1341. }
  1342. TEST_F(ServerTest, SlowRequest) {
  1343. request_threads_.push_back(
  1344. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1345. request_threads_.push_back(
  1346. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1347. request_threads_.push_back(
  1348. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1349. msleep(100);
  1350. }
  1351. TEST_F(ServerTest, Put) {
  1352. auto res = cli_.Put("/put", "PUT", "text/plain");
  1353. ASSERT_TRUE(res != nullptr);
  1354. EXPECT_EQ(200, res->status);
  1355. EXPECT_EQ("PUT", res->body);
  1356. }
  1357. TEST_F(ServerTest, PutWithContentProvider) {
  1358. auto res = cli_.Put(
  1359. "/put", 3,
  1360. [](size_t /*offset*/, size_t /*length*/, DataSink sink) {
  1361. sink("PUT", 3);
  1362. },
  1363. "text/plain");
  1364. ASSERT_TRUE(res != nullptr);
  1365. EXPECT_EQ(200, res->status);
  1366. EXPECT_EQ("PUT", res->body);
  1367. }
  1368. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1369. TEST_F(ServerTest, PutWithContentProviderWithGzip) {
  1370. cli_.set_compress(true);
  1371. auto res = cli_.Put(
  1372. "/put", 3,
  1373. [](size_t /*offset*/, size_t /*length*/, DataSink sink) {
  1374. sink("PUT", 3);
  1375. },
  1376. "text/plain");
  1377. ASSERT_TRUE(res != nullptr);
  1378. EXPECT_EQ(200, res->status);
  1379. EXPECT_EQ("PUT", res->body);
  1380. }
  1381. TEST_F(ServerTest, PutLargeFileWithGzip) {
  1382. cli_.set_compress(true);
  1383. auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain");
  1384. ASSERT_TRUE(res != nullptr);
  1385. EXPECT_EQ(200, res->status);
  1386. EXPECT_EQ(LARGE_DATA, res->body);
  1387. }
  1388. #endif
  1389. TEST_F(ServerTest, Patch) {
  1390. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  1391. ASSERT_TRUE(res != nullptr);
  1392. EXPECT_EQ(200, res->status);
  1393. EXPECT_EQ("PATCH", res->body);
  1394. }
  1395. TEST_F(ServerTest, Delete) {
  1396. auto res = cli_.Delete("/delete");
  1397. ASSERT_TRUE(res != nullptr);
  1398. EXPECT_EQ(200, res->status);
  1399. EXPECT_EQ("DELETE", res->body);
  1400. }
  1401. TEST_F(ServerTest, Options) {
  1402. auto res = cli_.Options("*");
  1403. ASSERT_TRUE(res != nullptr);
  1404. EXPECT_EQ(200, res->status);
  1405. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  1406. EXPECT_TRUE(res->body.empty());
  1407. }
  1408. TEST_F(ServerTest, URL) {
  1409. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  1410. ASSERT_TRUE(res != nullptr);
  1411. EXPECT_EQ(200, res->status);
  1412. }
  1413. TEST_F(ServerTest, ArrayParam) {
  1414. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  1415. ASSERT_TRUE(res != nullptr);
  1416. EXPECT_EQ(200, res->status);
  1417. }
  1418. TEST_F(ServerTest, NoMultipleHeaders) {
  1419. Headers headers = {{"Content-Length", "5"}};
  1420. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1421. "text/plain");
  1422. ASSERT_TRUE(res != nullptr);
  1423. EXPECT_EQ(200, res->status);
  1424. }
  1425. TEST_F(ServerTest, PostContentReceiver) {
  1426. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1427. ASSERT_TRUE(res != nullptr);
  1428. ASSERT_EQ(200, res->status);
  1429. ASSERT_EQ("content", res->body);
  1430. }
  1431. TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
  1432. MultipartFormDataItems items = {
  1433. {"text1", "text default", "", ""},
  1434. {"text2", "aωb", "", ""},
  1435. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1436. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1437. {"file3", "", "", "application/octet-stream"},
  1438. };
  1439. auto res = cli_.Post("/content_receiver", items);
  1440. ASSERT_TRUE(res != nullptr);
  1441. EXPECT_EQ(200, res->status);
  1442. }
  1443. TEST_F(ServerTest, PostContentReceiverGzip) {
  1444. cli_.set_compress(true);
  1445. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1446. ASSERT_TRUE(res != nullptr);
  1447. ASSERT_EQ(200, res->status);
  1448. ASSERT_EQ("content", res->body);
  1449. }
  1450. TEST_F(ServerTest, PutContentReceiver) {
  1451. auto res = cli_.Put("/content_receiver", "content", "text/plain");
  1452. ASSERT_TRUE(res != nullptr);
  1453. ASSERT_EQ(200, res->status);
  1454. ASSERT_EQ("content", res->body);
  1455. }
  1456. TEST_F(ServerTest, PatchContentReceiver) {
  1457. auto res = cli_.Patch("/content_receiver", "content", "text/plain");
  1458. ASSERT_TRUE(res != nullptr);
  1459. ASSERT_EQ(200, res->status);
  1460. ASSERT_EQ("content", res->body);
  1461. }
  1462. TEST_F(ServerTest, PostQueryStringAndBody) {
  1463. auto res = cli_.Post("/query-string-and-body?key=value", "content", "text/plain");
  1464. ASSERT_TRUE(res != nullptr);
  1465. ASSERT_EQ(200, res->status);
  1466. }
  1467. TEST_F(ServerTest, HTTP2Magic) {
  1468. Request req;
  1469. req.method = "PRI";
  1470. req.path = "*";
  1471. req.body = "SM";
  1472. auto res = std::make_shared<Response>();
  1473. auto ret = cli_.send(req, *res);
  1474. ASSERT_TRUE(ret);
  1475. EXPECT_EQ(400, res->status);
  1476. }
  1477. TEST_F(ServerTest, KeepAlive) {
  1478. cli_.set_keep_alive_max_count(4);
  1479. std::vector<Request> requests;
  1480. Get(requests, "/hi");
  1481. Get(requests, "/hi");
  1482. Get(requests, "/hi");
  1483. Get(requests, "/not-exist");
  1484. Post(requests, "/empty", "", "text/plain");
  1485. std::vector<Response> responses;
  1486. auto ret = cli_.send(requests, responses);
  1487. ASSERT_TRUE(ret == true);
  1488. ASSERT_TRUE(requests.size() == responses.size());
  1489. for (int i = 0; i < 3; i++) {
  1490. auto &res = responses[i];
  1491. EXPECT_EQ(200, res.status);
  1492. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1493. EXPECT_EQ("Hello World!", res.body);
  1494. }
  1495. {
  1496. auto &res = responses[3];
  1497. EXPECT_EQ(404, res.status);
  1498. }
  1499. {
  1500. auto &res = responses[4];
  1501. EXPECT_EQ(200, res.status);
  1502. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1503. EXPECT_EQ("empty", res.body);
  1504. }
  1505. }
  1506. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1507. TEST_F(ServerTest, Gzip) {
  1508. Headers headers;
  1509. headers.emplace("Accept-Encoding", "gzip, deflate");
  1510. auto res = cli_.Get("/gzip", headers);
  1511. ASSERT_TRUE(res != nullptr);
  1512. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1513. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1514. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1515. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1516. "7890123456789012345678901234567890",
  1517. res->body);
  1518. EXPECT_EQ(200, res->status);
  1519. }
  1520. TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
  1521. Headers headers;
  1522. auto res = cli_.Get("/gzip", headers);
  1523. ASSERT_TRUE(res != nullptr);
  1524. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1525. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1526. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1527. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1528. "7890123456789012345678901234567890",
  1529. res->body);
  1530. EXPECT_EQ(200, res->status);
  1531. }
  1532. TEST_F(ServerTest, GzipWithContentReceiver) {
  1533. Headers headers;
  1534. headers.emplace("Accept-Encoding", "gzip, deflate");
  1535. std::string body;
  1536. auto res =
  1537. cli_.Get("/gzip", headers, [&](const char *data, uint64_t data_length) {
  1538. EXPECT_EQ(data_length, 100);
  1539. body.append(data, data_length);
  1540. return true;
  1541. });
  1542. ASSERT_TRUE(res != nullptr);
  1543. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1544. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1545. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1546. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1547. "7890123456789012345678901234567890",
  1548. body);
  1549. EXPECT_EQ(200, res->status);
  1550. }
  1551. TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
  1552. Headers headers;
  1553. std::string body;
  1554. auto res =
  1555. cli_.Get("/gzip", headers, [&](const char *data, uint64_t data_length) {
  1556. EXPECT_EQ(data_length, 100);
  1557. body.append(data, data_length);
  1558. return true;
  1559. });
  1560. ASSERT_TRUE(res != nullptr);
  1561. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1562. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1563. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1564. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1565. "7890123456789012345678901234567890",
  1566. body);
  1567. EXPECT_EQ(200, res->status);
  1568. }
  1569. TEST_F(ServerTest, NoGzip) {
  1570. Headers headers;
  1571. headers.emplace("Accept-Encoding", "gzip, deflate");
  1572. auto res = cli_.Get("/nogzip", headers);
  1573. ASSERT_TRUE(res != nullptr);
  1574. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1575. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1576. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1577. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1578. "7890123456789012345678901234567890",
  1579. res->body);
  1580. EXPECT_EQ(200, res->status);
  1581. }
  1582. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1583. Headers headers;
  1584. headers.emplace("Accept-Encoding", "gzip, deflate");
  1585. std::string body;
  1586. auto res =
  1587. cli_.Get("/nogzip", headers, [&](const char *data, uint64_t data_length) {
  1588. EXPECT_EQ(data_length, 100);
  1589. body.append(data, data_length);
  1590. return true;
  1591. });
  1592. ASSERT_TRUE(res != nullptr);
  1593. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1594. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1595. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1596. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1597. "7890123456789012345678901234567890",
  1598. body);
  1599. EXPECT_EQ(200, res->status);
  1600. }
  1601. TEST_F(ServerTest, MultipartFormDataGzip) {
  1602. MultipartFormDataItems items = {
  1603. {"key1", "test", "", ""},
  1604. {"key2", "--abcdefg123", "", ""},
  1605. };
  1606. cli_.set_compress(true);
  1607. auto res = cli_.Post("/gzipmultipart", items);
  1608. ASSERT_TRUE(res != nullptr);
  1609. EXPECT_EQ(200, res->status);
  1610. }
  1611. #endif
  1612. // Sends a raw request to a server listening at HOST:PORT.
  1613. static bool send_request(time_t read_timeout_sec, const std::string &req) {
  1614. auto client_sock = detail::create_client_socket(HOST, PORT, /*timeout_sec=*/5,
  1615. std::string());
  1616. if (client_sock == INVALID_SOCKET) { return false; }
  1617. return detail::process_and_close_socket(
  1618. true, client_sock, 1, read_timeout_sec, 0,
  1619. [&](Stream &strm, bool /*last_connection*/, bool &
  1620. /*connection_close*/) -> bool {
  1621. if (req.size() !=
  1622. static_cast<size_t>(strm.write(req.data(), req.size()))) {
  1623. return false;
  1624. }
  1625. char buf[512];
  1626. detail::stream_line_reader line_reader(strm, buf, sizeof(buf));
  1627. while (line_reader.getline()) {}
  1628. return true;
  1629. });
  1630. }
  1631. TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
  1632. Server svr;
  1633. std::string header_value;
  1634. svr.Get("/validate-ws-in-headers", [&](const Request &req, Response &res) {
  1635. header_value = req.get_header_value("foo");
  1636. res.set_content("ok", "text/plain");
  1637. });
  1638. thread t = thread([&] { svr.listen(HOST, PORT); });
  1639. while (!svr.is_running()) {
  1640. msleep(1);
  1641. }
  1642. // Only space and horizontal tab are whitespace. Make sure other whitespace-
  1643. // like characters are not treated the same - use vertical tab and escape.
  1644. const std::string req = "GET /validate-ws-in-headers HTTP/1.1\r\n"
  1645. "foo: \t \v bar \e\t \r\n"
  1646. "Connection: close\r\n"
  1647. "\r\n";
  1648. ASSERT_TRUE(send_request(5, req));
  1649. svr.stop();
  1650. t.join();
  1651. EXPECT_EQ(header_value, "\v bar \e");
  1652. }
  1653. TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
  1654. Server svr;
  1655. svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1656. res.set_content("ok", "text/plain");
  1657. });
  1658. // Server read timeout must be longer than the client read timeout for the
  1659. // bug to reproduce, probably to force the server to process a request
  1660. // without a trailing blank line.
  1661. const time_t client_read_timeout_sec = 1;
  1662. svr.set_read_timeout(client_read_timeout_sec + 1, 0);
  1663. bool listen_thread_ok = false;
  1664. thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
  1665. while (!svr.is_running()) {
  1666. msleep(1);
  1667. }
  1668. // A certain header line causes an exception if the header property is parsed
  1669. // naively with a single regex. This occurs with libc++ but not libstdc++.
  1670. const std::string req =
  1671. "GET /hi HTTP/1.1\r\n"
  1672. " : "
  1673. " ";
  1674. ASSERT_TRUE(send_request(client_read_timeout_sec, req));
  1675. svr.stop();
  1676. t.join();
  1677. EXPECT_TRUE(listen_thread_ok);
  1678. }
  1679. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1680. protected:
  1681. ServerTestWithAI_PASSIVE()
  1682. : cli_(HOST, PORT)
  1683. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1684. ,
  1685. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1686. #endif
  1687. {
  1688. }
  1689. virtual void SetUp() {
  1690. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1691. res.set_content("Hello World!", "text/plain");
  1692. });
  1693. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1694. while (!svr_.is_running()) {
  1695. msleep(1);
  1696. }
  1697. }
  1698. virtual void TearDown() {
  1699. svr_.stop();
  1700. t_.join();
  1701. }
  1702. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1703. SSLClient cli_;
  1704. SSLServer svr_;
  1705. #else
  1706. Client cli_;
  1707. Server svr_;
  1708. #endif
  1709. thread t_;
  1710. };
  1711. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1712. auto res = cli_.Get("/hi");
  1713. ASSERT_TRUE(res != nullptr);
  1714. EXPECT_EQ(200, res->status);
  1715. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1716. EXPECT_EQ("Hello World!", res->body);
  1717. }
  1718. class ServerUpDownTest : public ::testing::Test {
  1719. protected:
  1720. ServerUpDownTest() : cli_(HOST, PORT) {}
  1721. virtual void SetUp() {
  1722. t_ = thread([&]() {
  1723. svr_.bind_to_any_port(HOST);
  1724. msleep(500);
  1725. ASSERT_TRUE(svr_.listen_after_bind());
  1726. });
  1727. while (!svr_.is_running()) {
  1728. msleep(1);
  1729. }
  1730. }
  1731. virtual void TearDown() {
  1732. svr_.stop();
  1733. t_.join();
  1734. }
  1735. Client cli_;
  1736. Server svr_;
  1737. thread t_;
  1738. };
  1739. TEST_F(ServerUpDownTest, QuickStartStop) {
  1740. // Should not crash, especially when run with
  1741. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1742. }
  1743. class PayloadMaxLengthTest : public ::testing::Test {
  1744. protected:
  1745. PayloadMaxLengthTest()
  1746. : cli_(HOST, PORT)
  1747. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1748. ,
  1749. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1750. #endif
  1751. {
  1752. }
  1753. virtual void SetUp() {
  1754. svr_.set_payload_max_length(8);
  1755. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1756. res.set_content("test", "text/plain");
  1757. });
  1758. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1759. while (!svr_.is_running()) {
  1760. msleep(1);
  1761. }
  1762. }
  1763. virtual void TearDown() {
  1764. svr_.stop();
  1765. t_.join();
  1766. }
  1767. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1768. SSLClient cli_;
  1769. SSLServer svr_;
  1770. #else
  1771. Client cli_;
  1772. Server svr_;
  1773. #endif
  1774. thread t_;
  1775. };
  1776. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1777. auto res = cli_.Post("/test", "123456789", "text/plain");
  1778. ASSERT_TRUE(res != nullptr);
  1779. EXPECT_EQ(413, res->status);
  1780. res = cli_.Post("/test", "12345678", "text/plain");
  1781. ASSERT_TRUE(res != nullptr);
  1782. EXPECT_EQ(200, res->status);
  1783. }
  1784. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1785. TEST(SSLClientTest, ServerNameIndication) {
  1786. SSLClient cli("httpbin.org", 443);
  1787. auto res = cli.Get("/get");
  1788. ASSERT_TRUE(res != nullptr);
  1789. ASSERT_EQ(200, res->status);
  1790. }
  1791. TEST(SSLClientTest, ServerCertificateVerification) {
  1792. SSLClient cli("google.com");
  1793. auto res = cli.Get("/");
  1794. ASSERT_TRUE(res != nullptr);
  1795. ASSERT_EQ(301, res->status);
  1796. cli.enable_server_certificate_verification(true);
  1797. res = cli.Get("/");
  1798. ASSERT_TRUE(res == nullptr);
  1799. cli.set_ca_cert_path(CA_CERT_FILE);
  1800. res = cli.Get("/");
  1801. ASSERT_TRUE(res != nullptr);
  1802. ASSERT_EQ(301, res->status);
  1803. }
  1804. TEST(SSLClientTest, WildcardHostNameMatch) {
  1805. SSLClient cli("www.youtube.com");
  1806. cli.set_ca_cert_path(CA_CERT_FILE);
  1807. cli.enable_server_certificate_verification(true);
  1808. auto res = cli.Get("/");
  1809. ASSERT_TRUE(res != nullptr);
  1810. ASSERT_EQ(200, res->status);
  1811. }
  1812. TEST(SSLClientServerTest, ClientCertPresent) {
  1813. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1814. CLIENT_CA_CERT_DIR);
  1815. ASSERT_TRUE(svr.is_valid());
  1816. svr.Get("/test", [&](const Request &req, Response &res) {
  1817. res.set_content("test", "text/plain");
  1818. svr.stop();
  1819. ASSERT_TRUE(true);
  1820. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1821. ASSERT_TRUE(peer_cert != nullptr);
  1822. auto subject_name = X509_get_subject_name(peer_cert);
  1823. ASSERT_TRUE(subject_name != nullptr);
  1824. std::string common_name;
  1825. {
  1826. char name[BUFSIZ];
  1827. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1828. name, sizeof(name));
  1829. common_name.assign(name, name_len);
  1830. }
  1831. EXPECT_EQ("Common Name", common_name);
  1832. X509_free(peer_cert);
  1833. });
  1834. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1835. msleep(1);
  1836. httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE,
  1837. CLIENT_PRIVATE_KEY_FILE);
  1838. auto res = cli.Get("/test");
  1839. cli.set_timeout_sec(30);
  1840. ASSERT_TRUE(res != nullptr);
  1841. ASSERT_EQ(200, res->status);
  1842. t.join();
  1843. }
  1844. TEST(SSLClientServerTest, ClientCertMissing) {
  1845. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1846. CLIENT_CA_CERT_DIR);
  1847. ASSERT_TRUE(svr.is_valid());
  1848. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1849. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1850. msleep(1);
  1851. httplib::SSLClient cli(HOST, PORT);
  1852. auto res = cli.Get("/test");
  1853. cli.set_timeout_sec(30);
  1854. ASSERT_TRUE(res == nullptr);
  1855. svr.stop();
  1856. t.join();
  1857. }
  1858. TEST(SSLClientServerTest, TrustDirOptional) {
  1859. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1860. ASSERT_TRUE(svr.is_valid());
  1861. svr.Get("/test", [&](const Request &, Response &res) {
  1862. res.set_content("test", "text/plain");
  1863. svr.stop();
  1864. });
  1865. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1866. msleep(1);
  1867. httplib::SSLClient cli(HOST, PORT, CLIENT_CERT_FILE,
  1868. CLIENT_PRIVATE_KEY_FILE);
  1869. auto res = cli.Get("/test");
  1870. cli.set_timeout_sec(30);
  1871. ASSERT_TRUE(res != nullptr);
  1872. ASSERT_EQ(200, res->status);
  1873. t.join();
  1874. }
  1875. /* Cannot test this case as there is no external access to SSL object to check
  1876. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1877. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1878. nullptr, CLIENT_CA_CERT_DIR);
  1879. }
  1880. */
  1881. #endif
  1882. #ifdef _WIN32
  1883. TEST(CleanupTest, WSACleanup) {
  1884. int ret = WSACleanup();
  1885. ASSERT_EQ(0, ret);
  1886. }
  1887. #endif