test.cc 41 KB

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