test.cc 54 KB

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