test.cc 45 KB

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