test.cc 53 KB

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