test.cc 44 KB

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