test.cc 53 KB

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