test.cc 54 KB

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