test.cc 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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)};
  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, 10)};
  86. auto val = detail::get_header_value(headers, "Range", 0, 0);
  87. EXPECT_STREQ("bytes=1-10", val);
  88. }
  89. {
  90. Headers headers = {make_range_header(1, 10, 100)};
  91. auto val = detail::get_header_value(headers, "Range", 0, 0);
  92. EXPECT_STREQ("bytes=1-10, 100-", val);
  93. }
  94. {
  95. Headers headers = {make_range_header(1, 10, 100, 200)};
  96. auto val = detail::get_header_value(headers, "Range", 0, 0);
  97. EXPECT_STREQ("bytes=1-10, 100-200", val);
  98. }
  99. }
  100. TEST(ChunkedEncodingTest, FromHTTPWatch) {
  101. auto host = "www.httpwatch.com";
  102. auto sec = 2;
  103. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  104. auto port = 443;
  105. httplib::SSLClient cli(host, port, sec);
  106. #else
  107. auto port = 80;
  108. httplib::Client cli(host, port, sec);
  109. #endif
  110. auto res =
  111. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
  112. ASSERT_TRUE(res != nullptr);
  113. std::string out;
  114. httplib::detail::read_file("./image.jpg", out);
  115. EXPECT_EQ(200, res->status);
  116. EXPECT_EQ(out, res->body);
  117. }
  118. TEST(RangeTest, FromHTTPBin) {
  119. auto host = "httpbin.org";
  120. auto sec = 5;
  121. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  122. auto port = 443;
  123. httplib::SSLClient cli(host, port, sec);
  124. #else
  125. auto port = 80;
  126. httplib::Client cli(host, port, sec);
  127. #endif
  128. {
  129. httplib::Headers headers;
  130. auto res = cli.Get("/range/32", headers);
  131. ASSERT_TRUE(res != nullptr);
  132. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  133. EXPECT_EQ(200, res->status);
  134. }
  135. {
  136. httplib::Headers headers = {httplib::make_range_header(1)};
  137. auto res = cli.Get("/range/32", headers);
  138. ASSERT_TRUE(res != nullptr);
  139. EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef");
  140. EXPECT_EQ(206, res->status);
  141. }
  142. {
  143. httplib::Headers headers = {httplib::make_range_header(1, 10)};
  144. auto res = cli.Get("/range/32", headers);
  145. ASSERT_TRUE(res != nullptr);
  146. EXPECT_EQ(res->body, "bcdefghijk");
  147. EXPECT_EQ(206, res->status);
  148. }
  149. }
  150. TEST(ConnectionErrorTest, InvalidHost) {
  151. auto host = "abcde.com";
  152. auto sec = 2;
  153. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  154. auto port = 443;
  155. httplib::SSLClient cli(host, port, sec);
  156. #else
  157. auto port = 80;
  158. httplib::Client cli(host, port, sec);
  159. #endif
  160. auto res = cli.Get("/");
  161. ASSERT_TRUE(res == nullptr);
  162. }
  163. TEST(ConnectionErrorTest, InvalidPort) {
  164. auto host = "localhost";
  165. auto sec = 2;
  166. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  167. auto port = 44380;
  168. httplib::SSLClient cli(host, port, sec);
  169. #else
  170. auto port = 8080;
  171. httplib::Client cli(host, port, sec);
  172. #endif
  173. auto res = cli.Get("/");
  174. ASSERT_TRUE(res == nullptr);
  175. }
  176. TEST(ConnectionErrorTest, Timeout) {
  177. auto host = "google.com";
  178. auto sec = 2;
  179. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  180. auto port = 44380;
  181. httplib::SSLClient cli(host, port, sec);
  182. #else
  183. auto port = 8080;
  184. httplib::Client cli(host, port, sec);
  185. #endif
  186. auto res = cli.Get("/");
  187. ASSERT_TRUE(res == nullptr);
  188. }
  189. TEST(CancelTest, NoCancel) {
  190. auto host = "httpbin.org";
  191. auto sec = 5;
  192. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  193. auto port = 443;
  194. httplib::SSLClient cli(host, port, sec);
  195. #else
  196. auto port = 80;
  197. httplib::Client cli(host, port, sec);
  198. #endif
  199. httplib::Headers headers;
  200. auto res =
  201. cli.Get("/range/32", headers, [](uint64_t, uint64_t) { return true; });
  202. ASSERT_TRUE(res != nullptr);
  203. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  204. EXPECT_EQ(200, res->status);
  205. }
  206. TEST(CancelTest, WithCancelSmallPayload) {
  207. auto host = "httpbin.org";
  208. auto sec = 5;
  209. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  210. auto port = 443;
  211. httplib::SSLClient cli(host, port, sec);
  212. #else
  213. auto port = 80;
  214. httplib::Client cli(host, port, sec);
  215. #endif
  216. httplib::Headers headers;
  217. auto res =
  218. cli.Get("/range/32", headers, [](uint64_t, uint64_t) { return false; });
  219. ASSERT_TRUE(res == nullptr);
  220. }
  221. TEST(CancelTest, WithCancelLargePayload) {
  222. auto host = "httpbin.org";
  223. auto sec = 5;
  224. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  225. auto port = 443;
  226. httplib::SSLClient cli(host, port, sec);
  227. #else
  228. auto port = 80;
  229. httplib::Client cli(host, port, sec);
  230. #endif
  231. uint32_t count = 0;
  232. httplib::Headers headers;
  233. auto res = cli.Get("/range/65536", headers,
  234. [&count](uint64_t, uint64_t) { return (count++ == 0); });
  235. ASSERT_TRUE(res == nullptr);
  236. }
  237. TEST(BaseAuthTest, FromHTTPWatch) {
  238. auto host = "httpbin.org";
  239. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  240. auto port = 443;
  241. httplib::SSLClient cli(host, port);
  242. #else
  243. auto port = 80;
  244. httplib::Client cli(host, port);
  245. #endif
  246. {
  247. auto res = cli.Get("/basic-auth/hello/world");
  248. ASSERT_TRUE(res != nullptr);
  249. EXPECT_EQ(401, res->status);
  250. }
  251. {
  252. httplib::Headers headers = {{"Authorization", "Basic aGVsbG86d29ybGQ="}};
  253. auto res = cli.Get("/basic-auth/hello/world", headers);
  254. ASSERT_TRUE(res != nullptr);
  255. EXPECT_EQ(res->body,
  256. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  257. EXPECT_EQ(200, res->status);
  258. }
  259. }
  260. TEST(Server, BindAndListenSeparately) {
  261. Server svr;
  262. int port = svr.bind_to_any_port("localhost");
  263. ASSERT_TRUE(port > 0);
  264. svr.stop();
  265. }
  266. class ServerTest : public ::testing::Test {
  267. protected:
  268. ServerTest()
  269. : cli_(HOST, PORT)
  270. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  271. ,
  272. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  273. #endif
  274. {
  275. }
  276. virtual void SetUp() {
  277. svr_.set_base_dir("./www");
  278. svr_.Get("/hi",
  279. [&](const Request & /*req*/, Response &res) {
  280. res.set_content("Hello World!", "text/plain");
  281. })
  282. .Get("/slow",
  283. [&](const Request & /*req*/, Response &res) {
  284. msleep(2000);
  285. res.set_content("slow", "text/plain");
  286. })
  287. .Get("/remote_addr",
  288. [&](const Request &req, Response &res) {
  289. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  290. res.set_content(remote_addr.c_str(), "text/plain");
  291. })
  292. .Get("/endwith%",
  293. [&](const Request & /*req*/, Response &res) {
  294. res.set_content("Hello World!", "text/plain");
  295. })
  296. .Get("/", [&](const Request & /*req*/,
  297. Response &res) { res.set_redirect("/hi"); })
  298. .Post("/person",
  299. [&](const Request &req, Response &res) {
  300. if (req.has_param("name") && req.has_param("note")) {
  301. persons_[req.get_param_value("name")] =
  302. req.get_param_value("note");
  303. } else {
  304. res.status = 400;
  305. }
  306. })
  307. .Get("/person/(.*)",
  308. [&](const Request &req, Response &res) {
  309. string name = req.matches[1];
  310. if (persons_.find(name) != persons_.end()) {
  311. auto note = persons_[name];
  312. res.set_content(note, "text/plain");
  313. } else {
  314. res.status = 404;
  315. }
  316. })
  317. .Post("/x-www-form-urlencoded-json",
  318. [&](const Request &req, Response &res) {
  319. auto json = req.get_param_value("json");
  320. ASSERT_EQ(JSON_DATA, json);
  321. res.set_content(json, "appliation/json");
  322. res.status = 200;
  323. })
  324. .Get("/streamedchunked",
  325. [&](const Request & /*req*/, Response &res) {
  326. res.streamcb = [](uint64_t offset) {
  327. if (offset < 3) return "a";
  328. if (offset < 6) return "b";
  329. return "";
  330. };
  331. })
  332. .Get("/streamed",
  333. [&](const Request & /*req*/, Response &res) {
  334. res.set_header("Content-Length", "6");
  335. res.streamcb = [](uint64_t offset) {
  336. if (offset < 3) return "a";
  337. if (offset < 6) return "b";
  338. return "";
  339. };
  340. })
  341. .Post("/chunked",
  342. [&](const Request &req, Response & /*res*/) {
  343. EXPECT_EQ(req.body, "dechunked post body");
  344. })
  345. .Post("/largechunked",
  346. [&](const Request &req, Response & /*res*/) {
  347. std::string expected(6 * 30 * 1024u, 'a');
  348. EXPECT_EQ(req.body, expected);
  349. })
  350. .Post("/multipart",
  351. [&](const Request &req, Response & /*res*/) {
  352. EXPECT_EQ(5u, req.files.size());
  353. ASSERT_TRUE(!req.has_file("???"));
  354. {
  355. const auto &file = req.get_file_value("text1");
  356. EXPECT_EQ("", file.filename);
  357. EXPECT_EQ("text default",
  358. req.body.substr(file.offset, file.length));
  359. }
  360. {
  361. const auto &file = req.get_file_value("text2");
  362. EXPECT_EQ("", file.filename);
  363. EXPECT_EQ("aωb", req.body.substr(file.offset, file.length));
  364. }
  365. {
  366. const auto &file = req.get_file_value("file1");
  367. EXPECT_EQ("hello.txt", file.filename);
  368. EXPECT_EQ("text/plain", file.content_type);
  369. EXPECT_EQ("h\ne\n\nl\nl\no\n",
  370. req.body.substr(file.offset, file.length));
  371. }
  372. {
  373. const auto &file = req.get_file_value("file3");
  374. EXPECT_EQ("", file.filename);
  375. EXPECT_EQ("application/octet-stream", file.content_type);
  376. EXPECT_EQ(0u, file.length);
  377. }
  378. })
  379. .Post("/empty",
  380. [&](const Request &req, Response &res) {
  381. EXPECT_EQ(req.body, "");
  382. res.set_content("empty", "text/plain");
  383. })
  384. .Put("/put",
  385. [&](const Request &req, Response &res) {
  386. EXPECT_EQ(req.body, "PUT");
  387. res.set_content(req.body, "text/plain");
  388. })
  389. .Patch("/patch",
  390. [&](const Request &req, Response &res) {
  391. EXPECT_EQ(req.body, "PATCH");
  392. res.set_content(req.body, "text/plain");
  393. })
  394. .Delete("/delete",
  395. [&](const Request & /*req*/, Response &res) {
  396. res.set_content("DELETE", "text/plain");
  397. })
  398. .Options(R"(\*)",
  399. [&](const Request & /*req*/, Response &res) {
  400. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  401. })
  402. .Get("/request-target",
  403. [&](const Request &req, Response & /*res*/) {
  404. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  405. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  406. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  407. })
  408. .Get("/long-query-value",
  409. [&](const Request &req, Response & /*res*/) {
  410. EXPECT_EQ(LONG_QUERY_URL, req.target);
  411. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  412. })
  413. .Get("/array-param",
  414. [&](const Request &req, Response & /*res*/) {
  415. EXPECT_EQ(3u, req.get_param_value_count("array"));
  416. EXPECT_EQ("value1", req.get_param_value("array", 0));
  417. EXPECT_EQ("value2", req.get_param_value("array", 1));
  418. EXPECT_EQ("value3", req.get_param_value("array", 2));
  419. })
  420. .Post("/validate-no-multiple-headers",
  421. [&](const Request &req, Response & /*res*/) {
  422. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  423. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  424. })
  425. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  426. .Get("/gzip",
  427. [&](const Request & /*req*/, Response &res) {
  428. res.set_content(
  429. "12345678901234567890123456789012345678901234567890123456789"
  430. "01234567890123456789012345678901234567890",
  431. "text/plain");
  432. })
  433. .Get("/nogzip",
  434. [&](const Request & /*req*/, Response &res) {
  435. res.set_content(
  436. "12345678901234567890123456789012345678901234567890123456789"
  437. "01234567890123456789012345678901234567890",
  438. "application/octet-stream");
  439. })
  440. .Post("/gzipmultipart",
  441. [&](const Request &req, Response & /*res*/) {
  442. EXPECT_EQ(2u, req.files.size());
  443. ASSERT_TRUE(!req.has_file("???"));
  444. {
  445. const auto &file = req.get_file_value("key1");
  446. EXPECT_EQ("", file.filename);
  447. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  448. }
  449. {
  450. const auto &file = req.get_file_value("key2");
  451. EXPECT_EQ("", file.filename);
  452. EXPECT_EQ("--abcdefg123",
  453. req.body.substr(file.offset, file.length));
  454. }
  455. })
  456. #endif
  457. ;
  458. persons_["john"] = "programmer";
  459. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  460. while (!svr_.is_running()) {
  461. msleep(1);
  462. }
  463. }
  464. virtual void TearDown() {
  465. svr_.stop();
  466. for (auto &t : request_threads_) {
  467. t.join();
  468. }
  469. t_.join();
  470. }
  471. map<string, string> persons_;
  472. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  473. SSLClient cli_;
  474. SSLServer svr_;
  475. #else
  476. Client cli_;
  477. Server svr_;
  478. #endif
  479. thread t_;
  480. std::vector<thread> request_threads_;
  481. };
  482. TEST_F(ServerTest, GetMethod200) {
  483. auto res = cli_.Get("/hi");
  484. ASSERT_TRUE(res != nullptr);
  485. EXPECT_EQ("HTTP/1.1", res->version);
  486. EXPECT_EQ(200, res->status);
  487. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  488. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  489. EXPECT_EQ("close", res->get_header_value("Connection"));
  490. EXPECT_EQ("Hello World!", res->body);
  491. }
  492. TEST_F(ServerTest, GetMethod302) {
  493. auto res = cli_.Get("/");
  494. ASSERT_TRUE(res != nullptr);
  495. EXPECT_EQ(302, res->status);
  496. EXPECT_EQ("/hi", res->get_header_value("Location"));
  497. }
  498. TEST_F(ServerTest, GetMethod404) {
  499. auto res = cli_.Get("/invalid");
  500. ASSERT_TRUE(res != nullptr);
  501. EXPECT_EQ(404, res->status);
  502. }
  503. TEST_F(ServerTest, HeadMethod200) {
  504. auto res = cli_.Head("/hi");
  505. ASSERT_TRUE(res != nullptr);
  506. EXPECT_EQ(200, res->status);
  507. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  508. EXPECT_EQ("", res->body);
  509. }
  510. TEST_F(ServerTest, HeadMethod404) {
  511. auto res = cli_.Head("/invalid");
  512. ASSERT_TRUE(res != nullptr);
  513. EXPECT_EQ(404, res->status);
  514. EXPECT_EQ("", res->body);
  515. }
  516. TEST_F(ServerTest, GetMethodPersonJohn) {
  517. auto res = cli_.Get("/person/john");
  518. ASSERT_TRUE(res != nullptr);
  519. EXPECT_EQ(200, res->status);
  520. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  521. EXPECT_EQ("programmer", res->body);
  522. }
  523. TEST_F(ServerTest, PostMethod1) {
  524. auto res = cli_.Get("/person/john1");
  525. ASSERT_TRUE(res != nullptr);
  526. ASSERT_EQ(404, res->status);
  527. res = cli_.Post("/person", "name=john1&note=coder",
  528. "application/x-www-form-urlencoded");
  529. ASSERT_TRUE(res != nullptr);
  530. ASSERT_EQ(200, res->status);
  531. res = cli_.Get("/person/john1");
  532. ASSERT_TRUE(res != nullptr);
  533. ASSERT_EQ(200, res->status);
  534. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  535. ASSERT_EQ("coder", res->body);
  536. }
  537. TEST_F(ServerTest, PostMethod2) {
  538. auto res = cli_.Get("/person/john2");
  539. ASSERT_TRUE(res != nullptr);
  540. ASSERT_EQ(404, res->status);
  541. Params params;
  542. params.emplace("name", "john2");
  543. params.emplace("note", "coder");
  544. res = cli_.Post("/person", params);
  545. ASSERT_TRUE(res != nullptr);
  546. ASSERT_EQ(200, res->status);
  547. res = cli_.Get("/person/john2");
  548. ASSERT_TRUE(res != nullptr);
  549. ASSERT_EQ(200, res->status);
  550. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  551. ASSERT_EQ("coder", res->body);
  552. }
  553. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  554. Params params;
  555. params.emplace("json", JSON_DATA);
  556. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  557. ASSERT_TRUE(res != nullptr);
  558. ASSERT_EQ(200, res->status);
  559. ASSERT_EQ(JSON_DATA, res->body);
  560. }
  561. TEST_F(ServerTest, PostEmptyContent) {
  562. auto res = cli_.Post("/empty", "", "text/plain");
  563. ASSERT_TRUE(res != nullptr);
  564. ASSERT_EQ(200, res->status);
  565. ASSERT_EQ("empty", res->body);
  566. }
  567. TEST_F(ServerTest, GetMethodDir) {
  568. auto res = cli_.Get("/dir/");
  569. ASSERT_TRUE(res != nullptr);
  570. EXPECT_EQ(200, res->status);
  571. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  572. auto body = R"(<html>
  573. <head>
  574. </head>
  575. <body>
  576. <a href="/dir/test.html">Test</a>
  577. <a href="/hi">hi</a>
  578. </body>
  579. </html>
  580. )";
  581. EXPECT_EQ(body, res->body);
  582. }
  583. TEST_F(ServerTest, GetMethodDirTest) {
  584. auto res = cli_.Get("/dir/test.html");
  585. ASSERT_TRUE(res != nullptr);
  586. EXPECT_EQ(200, res->status);
  587. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  588. EXPECT_EQ("test.html", res->body);
  589. }
  590. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  591. auto res = cli_.Get("/dir/../dir/test.html");
  592. ASSERT_TRUE(res != nullptr);
  593. EXPECT_EQ(200, res->status);
  594. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  595. EXPECT_EQ("test.html", res->body);
  596. }
  597. TEST_F(ServerTest, GetMethodInvalidPath) {
  598. auto res = cli_.Get("/dir/../test.html");
  599. ASSERT_TRUE(res != nullptr);
  600. EXPECT_EQ(404, res->status);
  601. }
  602. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  603. auto res = cli_.Get("/../www/dir/test.html");
  604. ASSERT_TRUE(res != nullptr);
  605. EXPECT_EQ(404, res->status);
  606. }
  607. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  608. auto res = cli_.Get("/dir/../../www/dir/test.html");
  609. ASSERT_TRUE(res != nullptr);
  610. EXPECT_EQ(404, res->status);
  611. }
  612. TEST_F(ServerTest, InvalidBaseDir) {
  613. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  614. EXPECT_EQ(true, svr_.set_base_dir("."));
  615. }
  616. TEST_F(ServerTest, EmptyRequest) {
  617. auto res = cli_.Get("");
  618. ASSERT_TRUE(res == nullptr);
  619. }
  620. TEST_F(ServerTest, LongRequest) {
  621. auto res =
  622. cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  623. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  624. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  625. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  626. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  627. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  628. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  629. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  630. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  631. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  632. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  633. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  634. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  635. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  636. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  637. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  638. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  639. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  640. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  641. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  642. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  643. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  644. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  645. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  646. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  647. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  648. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  649. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  650. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  651. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  652. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  653. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  654. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  655. "TooLongRequest/TooLongRequest/TooLongRequest/__ok__");
  656. ASSERT_TRUE(res != nullptr);
  657. EXPECT_EQ(404, res->status);
  658. }
  659. TEST_F(ServerTest, TooLongRequest) {
  660. auto res =
  661. cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  662. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  663. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  664. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  665. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  666. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  667. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  668. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  669. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  670. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  671. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  672. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  673. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  674. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  675. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  676. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  677. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  678. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  679. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  680. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  681. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  682. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  683. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  684. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  685. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  686. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  687. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  688. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  689. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  690. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  691. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  692. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  693. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  694. "TooLongRequest/TooLongRequest/TooLongRequest/__ng___");
  695. ASSERT_TRUE(res != nullptr);
  696. EXPECT_EQ(404, res->status);
  697. }
  698. TEST_F(ServerTest, LongHeader) {
  699. Request req;
  700. req.method = "GET";
  701. req.path = "/hi";
  702. std::string host_and_port;
  703. host_and_port += HOST;
  704. host_and_port += ":";
  705. host_and_port += std::to_string(PORT);
  706. req.headers.emplace("Host", host_and_port.c_str());
  707. req.headers.emplace("Accept", "*/*");
  708. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  709. req.headers.emplace(
  710. "Header-Name",
  711. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  712. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  713. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  714. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  715. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  716. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  717. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  718. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  719. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  720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@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  734. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  735. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  736. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  737. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  738. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  739. "@@@@@@@@@@@@@@@@");
  740. auto res = std::make_shared<Response>();
  741. auto ret = cli_.send(req, *res);
  742. ASSERT_TRUE(ret);
  743. EXPECT_EQ(200, res->status);
  744. }
  745. TEST_F(ServerTest, LongQueryValue) {
  746. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  747. ASSERT_TRUE(res != nullptr);
  748. EXPECT_EQ(414, res->status);
  749. }
  750. TEST_F(ServerTest, TooLongHeader) {
  751. Request req;
  752. req.method = "GET";
  753. req.path = "/hi";
  754. std::string host_and_port;
  755. host_and_port += HOST;
  756. host_and_port += ":";
  757. host_and_port += std::to_string(PORT);
  758. req.headers.emplace("Host", host_and_port.c_str());
  759. req.headers.emplace("Accept", "*/*");
  760. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  761. req.headers.emplace(
  762. "Header-Name",
  763. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  764. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@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"@@@@@@@@@@@@@@@@@");
  792. auto res = std::make_shared<Response>();
  793. auto ret = cli_.send(req, *res);
  794. ASSERT_TRUE(ret);
  795. EXPECT_EQ(200, res->status);
  796. }
  797. TEST_F(ServerTest, PercentEncoding) {
  798. auto res = cli_.Get("/e%6edwith%");
  799. ASSERT_TRUE(res != nullptr);
  800. EXPECT_EQ(200, res->status);
  801. }
  802. TEST_F(ServerTest, PercentEncodingUnicode) {
  803. auto res = cli_.Get("/e%u006edwith%");
  804. ASSERT_TRUE(res != nullptr);
  805. EXPECT_EQ(200, res->status);
  806. }
  807. TEST_F(ServerTest, InvalidPercentEncoding) {
  808. auto res = cli_.Get("/%endwith%");
  809. ASSERT_TRUE(res != nullptr);
  810. EXPECT_EQ(404, res->status);
  811. }
  812. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  813. auto res = cli_.Get("/%uendwith%");
  814. ASSERT_TRUE(res != nullptr);
  815. EXPECT_EQ(404, res->status);
  816. }
  817. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  818. auto res = cli_.Get("/hello?aaa=bbb%");
  819. ASSERT_TRUE(res != nullptr);
  820. EXPECT_EQ(404, res->status);
  821. }
  822. TEST_F(ServerTest, MultipartFormData) {
  823. Request req;
  824. req.method = "POST";
  825. req.path = "/multipart";
  826. std::string host_and_port;
  827. host_and_port += HOST;
  828. host_and_port += ":";
  829. host_and_port += std::to_string(PORT);
  830. req.headers.emplace("Host", host_and_port.c_str());
  831. req.headers.emplace("Accept", "*/*");
  832. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  833. req.headers.emplace(
  834. "Content-Type",
  835. "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4");
  836. req.body =
  837. "------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: "
  838. "form-data; name=\"text1\"\r\n\r\ntext "
  839. "default\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-"
  840. "Disposition: form-data; "
  841. "name=\"text2\"\r\n\r\naωb\r\n------"
  842. "WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; "
  843. "name=\"file1\"; filename=\"hello.txt\"\r\nContent-Type: "
  844. "text/"
  845. "plain\r\n\r\nh\ne\n\nl\nl\no\n\r\n------"
  846. "WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; "
  847. "name=\"file2\"; filename=\"world.json\"\r\nContent-Type: "
  848. "application/json\r\n\r\n{\n \"world\", "
  849. "true\n}\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\ncontent-"
  850. "disposition: form-data; name=\"file3\"; filename=\"\"\r\ncontent-type: "
  851. "application/"
  852. "octet-stream\r\n\r\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4--\r\n";
  853. auto res = std::make_shared<Response>();
  854. auto ret = cli_.send(req, *res);
  855. ASSERT_TRUE(ret);
  856. EXPECT_EQ(200, res->status);
  857. }
  858. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  859. auto res = cli_.Get("/hi");
  860. ASSERT_TRUE(res != nullptr);
  861. EXPECT_EQ(200, res->status);
  862. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  863. EXPECT_EQ("Hello World!", res->body);
  864. }
  865. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  866. Request req;
  867. req.method = "POST";
  868. req.path = "/chunked";
  869. std::string host_and_port;
  870. host_and_port += HOST;
  871. host_and_port += ":";
  872. host_and_port += std::to_string(PORT);
  873. req.headers.emplace("Host", host_and_port.c_str());
  874. req.headers.emplace("Accept", "*/*");
  875. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  876. req.headers.emplace("Content-Type", "text/plain");
  877. req.headers.emplace("Content-Length", "0");
  878. req.headers.emplace(
  879. "Transfer-Encoding",
  880. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  881. // Client does not chunk, so make a chunked body manually.
  882. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  883. auto res = std::make_shared<Response>();
  884. auto ret = cli_.send(req, *res);
  885. ASSERT_TRUE(ret);
  886. EXPECT_EQ(200, res->status);
  887. }
  888. TEST_F(ServerTest, GetStreamed) {
  889. auto res = cli_.Get("/streamed");
  890. ASSERT_TRUE(res != nullptr);
  891. EXPECT_EQ(200, res->status);
  892. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  893. EXPECT_TRUE(res->body == "aaabbb");
  894. }
  895. TEST_F(ServerTest, GetStreamedChunked) {
  896. auto res = cli_.Get("/streamedchunked");
  897. ASSERT_TRUE(res != nullptr);
  898. EXPECT_EQ(200, res->status);
  899. EXPECT_TRUE(res->body == "aaabbb");
  900. }
  901. TEST_F(ServerTest, LargeChunkedPost) {
  902. Request req;
  903. req.method = "POST";
  904. req.path = "/largechunked";
  905. std::string host_and_port;
  906. host_and_port += HOST;
  907. host_and_port += ":";
  908. host_and_port += std::to_string(PORT);
  909. req.headers.emplace("Host", host_and_port.c_str());
  910. req.headers.emplace("Accept", "*/*");
  911. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  912. req.headers.emplace("Content-Type", "text/plain");
  913. req.headers.emplace("Content-Length", "0");
  914. req.headers.emplace("Transfer-Encoding", "chunked");
  915. std::string long_string(30 * 1024u, 'a');
  916. std::string chunk = "7800\r\n" + long_string + "\r\n";
  917. // Attempt to make a large enough post to exceed OS buffers, to test that
  918. // the server handles short reads if the full chunk data isn't available.
  919. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  920. auto res = std::make_shared<Response>();
  921. auto ret = cli_.send(req, *res);
  922. ASSERT_TRUE(ret);
  923. EXPECT_EQ(200, res->status);
  924. }
  925. TEST_F(ServerTest, GetMethodRemoteAddr) {
  926. auto res = cli_.Get("/remote_addr");
  927. ASSERT_TRUE(res != nullptr);
  928. EXPECT_EQ(200, res->status);
  929. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  930. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  931. }
  932. TEST_F(ServerTest, SlowRequest) {
  933. request_threads_.push_back(
  934. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  935. request_threads_.push_back(
  936. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  937. request_threads_.push_back(
  938. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  939. msleep(100);
  940. }
  941. TEST_F(ServerTest, Put) {
  942. auto res = cli_.Put("/put", "PUT", "text/plain");
  943. ASSERT_TRUE(res != nullptr);
  944. EXPECT_EQ(200, res->status);
  945. EXPECT_EQ("PUT", res->body);
  946. }
  947. TEST_F(ServerTest, Patch) {
  948. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  949. ASSERT_TRUE(res != nullptr);
  950. EXPECT_EQ(200, res->status);
  951. EXPECT_EQ("PATCH", res->body);
  952. }
  953. TEST_F(ServerTest, Delete) {
  954. auto res = cli_.Delete("/delete");
  955. ASSERT_TRUE(res != nullptr);
  956. EXPECT_EQ(200, res->status);
  957. EXPECT_EQ("DELETE", res->body);
  958. }
  959. TEST_F(ServerTest, Options) {
  960. auto res = cli_.Options("*");
  961. ASSERT_TRUE(res != nullptr);
  962. EXPECT_EQ(200, res->status);
  963. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  964. EXPECT_TRUE(res->body.empty());
  965. }
  966. TEST_F(ServerTest, URL) {
  967. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  968. ASSERT_TRUE(res != nullptr);
  969. EXPECT_EQ(200, res->status);
  970. }
  971. TEST_F(ServerTest, ArrayParam) {
  972. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  973. ASSERT_TRUE(res != nullptr);
  974. EXPECT_EQ(200, res->status);
  975. }
  976. TEST_F(ServerTest, NoMultipleHeaders) {
  977. Headers headers;
  978. headers.emplace("Content-Length", "5");
  979. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  980. "text/plain");
  981. ASSERT_TRUE(res != nullptr);
  982. EXPECT_EQ(200, res->status);
  983. }
  984. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  985. TEST_F(ServerTest, Gzip) {
  986. Headers headers;
  987. headers.emplace("Accept-Encoding", "gzip, deflate");
  988. auto res = cli_.Get("/gzip", headers);
  989. ASSERT_TRUE(res != nullptr);
  990. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  991. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  992. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  993. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  994. "7890123456789012345678901234567890",
  995. res->body);
  996. EXPECT_EQ(200, res->status);
  997. }
  998. TEST_F(ServerTest, NoGzip) {
  999. Headers headers;
  1000. headers.emplace("Accept-Encoding", "gzip, deflate");
  1001. auto res = cli_.Get("/nogzip", headers);
  1002. ASSERT_TRUE(res != nullptr);
  1003. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1004. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1005. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1006. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1007. "7890123456789012345678901234567890",
  1008. res->body);
  1009. EXPECT_EQ(200, res->status);
  1010. }
  1011. TEST_F(ServerTest, MultipartFormDataGzip) {
  1012. Request req;
  1013. req.method = "POST";
  1014. req.path = "/gzipmultipart";
  1015. std::string host_and_port;
  1016. host_and_port += HOST;
  1017. host_and_port += ":";
  1018. host_and_port += std::to_string(PORT);
  1019. req.headers.emplace("Host", host_and_port.c_str());
  1020. req.headers.emplace("Accept", "*/*");
  1021. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1022. req.headers.emplace(
  1023. "Content-Type",
  1024. "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  1025. req.headers.emplace("Content-Encoding", "gzip");
  1026. // compressed_body generated by creating input.txt to this file:
  1027. /*
  1028. --------------------------fcba8368a9f48c0f
  1029. Content-Disposition: form-data; name="key1"
  1030. test
  1031. --------------------------fcba8368a9f48c0f
  1032. Content-Disposition: form-data; name="key2"
  1033. --abcdefg123
  1034. --------------------------fcba8368a9f48c0f--
  1035. */
  1036. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  1037. uint8_t compressed_body[] = {
  1038. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  1039. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  1040. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  1041. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  1042. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  1043. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  1044. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  1045. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  1046. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  1047. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  1048. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00};
  1049. req.body = std::string((char *)compressed_body,
  1050. sizeof(compressed_body) / sizeof(compressed_body[0]));
  1051. auto res = std::make_shared<Response>();
  1052. auto ret = cli_.send(req, *res);
  1053. ASSERT_TRUE(ret);
  1054. EXPECT_EQ(200, res->status);
  1055. }
  1056. #endif
  1057. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1058. protected:
  1059. ServerTestWithAI_PASSIVE()
  1060. : cli_(HOST, PORT)
  1061. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1062. ,
  1063. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1064. #endif
  1065. {
  1066. }
  1067. virtual void SetUp() {
  1068. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1069. res.set_content("Hello World!", "text/plain");
  1070. });
  1071. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1072. while (!svr_.is_running()) {
  1073. msleep(1);
  1074. }
  1075. }
  1076. virtual void TearDown() {
  1077. svr_.stop();
  1078. t_.join();
  1079. }
  1080. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1081. SSLClient cli_;
  1082. SSLServer svr_;
  1083. #else
  1084. Client cli_;
  1085. Server svr_;
  1086. #endif
  1087. thread t_;
  1088. };
  1089. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1090. auto res = cli_.Get("/hi");
  1091. ASSERT_TRUE(res != nullptr);
  1092. EXPECT_EQ(200, res->status);
  1093. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1094. EXPECT_EQ("Hello World!", res->body);
  1095. }
  1096. class ServerUpDownTest : public ::testing::Test {
  1097. protected:
  1098. ServerUpDownTest() : cli_(HOST, PORT) {}
  1099. virtual void SetUp() {
  1100. t_ = thread([&]() {
  1101. svr_.bind_to_any_port(HOST);
  1102. msleep(500);
  1103. ASSERT_TRUE(svr_.listen_after_bind());
  1104. });
  1105. while (!svr_.is_running()) {
  1106. msleep(1);
  1107. }
  1108. }
  1109. virtual void TearDown() {
  1110. svr_.stop();
  1111. t_.join();
  1112. }
  1113. Client cli_;
  1114. Server svr_;
  1115. thread t_;
  1116. };
  1117. TEST_F(ServerUpDownTest, QuickStartStop) {
  1118. // Should not crash, especially when run with
  1119. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1120. }
  1121. class PayloadMaxLengthTest : public ::testing::Test {
  1122. protected:
  1123. PayloadMaxLengthTest()
  1124. : cli_(HOST, PORT)
  1125. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1126. ,
  1127. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1128. #endif
  1129. {
  1130. }
  1131. virtual void SetUp() {
  1132. svr_.set_payload_max_length(8);
  1133. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1134. res.set_content("test", "text/plain");
  1135. });
  1136. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1137. while (!svr_.is_running()) {
  1138. msleep(1);
  1139. }
  1140. }
  1141. virtual void TearDown() {
  1142. svr_.stop();
  1143. t_.join();
  1144. }
  1145. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1146. SSLClient cli_;
  1147. SSLServer svr_;
  1148. #else
  1149. Client cli_;
  1150. Server svr_;
  1151. #endif
  1152. thread t_;
  1153. };
  1154. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1155. auto res = cli_.Post("/test", "123456789", "text/plain");
  1156. ASSERT_TRUE(res != nullptr);
  1157. EXPECT_EQ(413, res->status);
  1158. res = cli_.Post("/test", "12345678", "text/plain");
  1159. ASSERT_TRUE(res != nullptr);
  1160. EXPECT_EQ(200, res->status);
  1161. }
  1162. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1163. TEST(SSLClientTest, ServerNameIndication) {
  1164. SSLClient cli("httpbin.org", 443);
  1165. auto res = cli.Get("/get");
  1166. ASSERT_TRUE(res != nullptr);
  1167. ASSERT_EQ(200, res->status);
  1168. }
  1169. TEST(SSLClientTest, ServerCertificateVerification) {
  1170. SSLClient cli("google.com");
  1171. auto res = cli.Get("/");
  1172. ASSERT_TRUE(res != nullptr);
  1173. ASSERT_EQ(301, res->status);
  1174. cli.enable_server_certificate_verification(true);
  1175. res = cli.Get("/");
  1176. ASSERT_TRUE(res == nullptr);
  1177. cli.set_ca_cert_path(CA_CERT_FILE);
  1178. res = cli.Get("/");
  1179. ASSERT_TRUE(res != nullptr);
  1180. ASSERT_EQ(301, res->status);
  1181. }
  1182. TEST(SSLClientTest, WildcardHostNameMatch) {
  1183. SSLClient cli("www.youtube.com");
  1184. cli.set_ca_cert_path(CA_CERT_FILE);
  1185. cli.enable_server_certificate_verification(true);
  1186. auto res = cli.Get("/");
  1187. ASSERT_TRUE(res != nullptr);
  1188. ASSERT_EQ(200, res->status);
  1189. }
  1190. TEST(SSLClientServerTest, ClientCertPresent) {
  1191. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1192. CLIENT_CA_CERT_DIR);
  1193. ASSERT_TRUE(svr.is_valid());
  1194. svr.Get("/test", [&](const Request &req, Response &res) {
  1195. res.set_content("test", "text/plain");
  1196. svr.stop();
  1197. ASSERT_TRUE(true);
  1198. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1199. ASSERT_TRUE(peer_cert != nullptr);
  1200. auto subject_name = X509_get_subject_name(peer_cert);
  1201. ASSERT_TRUE(subject_name != nullptr);
  1202. std::string common_name;
  1203. {
  1204. char name[BUFSIZ];
  1205. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1206. name, sizeof(name));
  1207. common_name.assign(name, name_len);
  1208. }
  1209. EXPECT_EQ("Common Name", common_name);
  1210. X509_free(peer_cert);
  1211. });
  1212. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1213. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1214. CLIENT_PRIVATE_KEY_FILE);
  1215. auto res = cli.Get("/test");
  1216. ASSERT_TRUE(res != nullptr);
  1217. ASSERT_EQ(200, res->status);
  1218. t.join();
  1219. }
  1220. TEST(SSLClientServerTest, ClientCertMissing) {
  1221. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1222. CLIENT_CA_CERT_DIR);
  1223. ASSERT_TRUE(svr.is_valid());
  1224. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1225. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1226. httplib::SSLClient cli(HOST, PORT, 30);
  1227. auto res = cli.Get("/test");
  1228. ASSERT_TRUE(res == nullptr);
  1229. svr.stop();
  1230. t.join();
  1231. }
  1232. TEST(SSLClientServerTest, TrustDirOptional) {
  1233. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1234. ASSERT_TRUE(svr.is_valid());
  1235. svr.Get("/test", [&](const Request &, Response &res) {
  1236. res.set_content("test", "text/plain");
  1237. svr.stop();
  1238. });
  1239. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1240. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1241. CLIENT_PRIVATE_KEY_FILE);
  1242. auto res = cli.Get("/test");
  1243. ASSERT_TRUE(res != nullptr);
  1244. ASSERT_EQ(200, res->status);
  1245. t.join();
  1246. }
  1247. /* Cannot test this case as there is no external access to SSL object to check
  1248. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1249. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1250. nullptr, CLIENT_CA_CERT_DIR);
  1251. }
  1252. */
  1253. #endif
  1254. #ifdef _WIN32
  1255. TEST(CleanupTest, WSACleanup) {
  1256. int ret = WSACleanup();
  1257. ASSERT_EQ(0, ret);
  1258. }
  1259. #endif