test.cc 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. .Delete("/delete", [&](const Request& /*req*/, Response& res) {
  387. res.set_content("DELETE", "text/plain");
  388. })
  389. .Options(R"(\*)", [&](const Request& /*req*/, Response& res) {
  390. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  391. })
  392. .Get("/request-target", [&](const Request& req, Response& /*res*/) {
  393. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  394. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  395. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  396. })
  397. .Get("/long-query-value", [&](const Request& req, Response& /*res*/) {
  398. EXPECT_EQ(LONG_QUERY_URL, req.target);
  399. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  400. })
  401. .Get("/array-param", [&](const Request& req, Response& /*res*/) {
  402. EXPECT_EQ(3u, req.get_param_value_count("array"));
  403. EXPECT_EQ("value1", req.get_param_value("array", 0));
  404. EXPECT_EQ("value2", req.get_param_value("array", 1));
  405. EXPECT_EQ("value3", req.get_param_value("array", 2));
  406. })
  407. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  408. .Get("/gzip", [&](const Request& /*req*/, Response& res) {
  409. res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain");
  410. })
  411. .Get("/nogzip", [&](const Request& /*req*/, Response& res) {
  412. res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "application/octet-stream");
  413. })
  414. .Post("/gzipmultipart", [&](const Request& req, Response& /*res*/) {
  415. EXPECT_EQ(2u, req.files.size());
  416. ASSERT_TRUE(!req.has_file("???"));
  417. {
  418. const auto& file = req.get_file_value("key1");
  419. EXPECT_EQ("", file.filename);
  420. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  421. }
  422. {
  423. const auto& file = req.get_file_value("key2");
  424. EXPECT_EQ("", file.filename);
  425. EXPECT_EQ("--abcdefg123", req.body.substr(file.offset, file.length));
  426. }
  427. })
  428. #endif
  429. ;
  430. persons_["john"] = "programmer";
  431. t_ = thread([&](){
  432. ASSERT_TRUE(svr_.listen(HOST, PORT));
  433. });
  434. while (!svr_.is_running()) {
  435. msleep(1);
  436. }
  437. }
  438. virtual void TearDown() {
  439. svr_.stop();
  440. for (auto& t: request_threads_) {
  441. t.join();
  442. }
  443. t_.join();
  444. }
  445. map<string, string> persons_;
  446. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  447. SSLClient cli_;
  448. SSLServer svr_;
  449. #else
  450. Client cli_;
  451. Server svr_;
  452. #endif
  453. thread t_;
  454. std::vector<thread> request_threads_;
  455. };
  456. TEST_F(ServerTest, GetMethod200)
  457. {
  458. auto res = cli_.Get("/hi");
  459. ASSERT_TRUE(res != nullptr);
  460. EXPECT_EQ("HTTP/1.1", res->version);
  461. EXPECT_EQ(200, res->status);
  462. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  463. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  464. EXPECT_EQ("close", res->get_header_value("Connection"));
  465. EXPECT_EQ("Hello World!", res->body);
  466. }
  467. TEST_F(ServerTest, GetMethod302)
  468. {
  469. auto res = cli_.Get("/");
  470. ASSERT_TRUE(res != nullptr);
  471. EXPECT_EQ(302, res->status);
  472. EXPECT_EQ("/hi", res->get_header_value("Location"));
  473. }
  474. TEST_F(ServerTest, GetMethod404)
  475. {
  476. auto res = cli_.Get("/invalid");
  477. ASSERT_TRUE(res != nullptr);
  478. EXPECT_EQ(404, res->status);
  479. }
  480. TEST_F(ServerTest, HeadMethod200)
  481. {
  482. auto res = cli_.Head("/hi");
  483. ASSERT_TRUE(res != nullptr);
  484. EXPECT_EQ(200, res->status);
  485. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  486. EXPECT_EQ("", res->body);
  487. }
  488. TEST_F(ServerTest, HeadMethod404)
  489. {
  490. auto res = cli_.Head("/invalid");
  491. ASSERT_TRUE(res != nullptr);
  492. EXPECT_EQ(404, res->status);
  493. EXPECT_EQ("", res->body);
  494. }
  495. TEST_F(ServerTest, GetMethodPersonJohn)
  496. {
  497. auto res = cli_.Get("/person/john");
  498. ASSERT_TRUE(res != nullptr);
  499. EXPECT_EQ(200, res->status);
  500. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  501. EXPECT_EQ("programmer", res->body);
  502. }
  503. TEST_F(ServerTest, PostMethod1)
  504. {
  505. auto res = cli_.Get("/person/john1");
  506. ASSERT_TRUE(res != nullptr);
  507. ASSERT_EQ(404, res->status);
  508. res = cli_.Post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
  509. ASSERT_TRUE(res != nullptr);
  510. ASSERT_EQ(200, res->status);
  511. res = cli_.Get("/person/john1");
  512. ASSERT_TRUE(res != nullptr);
  513. ASSERT_EQ(200, res->status);
  514. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  515. ASSERT_EQ("coder", res->body);
  516. }
  517. TEST_F(ServerTest, PostMethod2)
  518. {
  519. auto res = cli_.Get("/person/john2");
  520. ASSERT_TRUE(res != nullptr);
  521. ASSERT_EQ(404, res->status);
  522. Params params;
  523. params.emplace("name", "john2");
  524. params.emplace("note", "coder");
  525. res = cli_.Post("/person", params);
  526. ASSERT_TRUE(res != nullptr);
  527. ASSERT_EQ(200, res->status);
  528. res = cli_.Get("/person/john2");
  529. ASSERT_TRUE(res != nullptr);
  530. ASSERT_EQ(200, res->status);
  531. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  532. ASSERT_EQ("coder", res->body);
  533. }
  534. TEST_F(ServerTest, PostWwwFormUrlEncodedJson)
  535. {
  536. Params params;
  537. params.emplace("json", JSON_DATA);
  538. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  539. ASSERT_TRUE(res != nullptr);
  540. ASSERT_EQ(200, res->status);
  541. ASSERT_EQ(JSON_DATA, res->body);
  542. }
  543. TEST_F(ServerTest, PostEmptyContent)
  544. {
  545. auto res = cli_.Post("/empty", "", "text/plain");
  546. ASSERT_TRUE(res != nullptr);
  547. ASSERT_EQ(200, res->status);
  548. ASSERT_EQ("empty", res->body);
  549. }
  550. TEST_F(ServerTest, GetMethodDir)
  551. {
  552. auto res = cli_.Get("/dir/");
  553. ASSERT_TRUE(res != nullptr);
  554. EXPECT_EQ(200, res->status);
  555. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  556. auto body = R"(<html>
  557. <head>
  558. </head>
  559. <body>
  560. <a href="/dir/test.html">Test</a>
  561. <a href="/hi">hi</a>
  562. </body>
  563. </html>
  564. )";
  565. EXPECT_EQ(body, res->body);
  566. }
  567. TEST_F(ServerTest, GetMethodDirTest)
  568. {
  569. auto res = cli_.Get("/dir/test.html");
  570. ASSERT_TRUE(res != nullptr);
  571. EXPECT_EQ(200, res->status);
  572. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  573. EXPECT_EQ("test.html", res->body);
  574. }
  575. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots)
  576. {
  577. auto res = cli_.Get("/dir/../dir/test.html");
  578. ASSERT_TRUE(res != nullptr);
  579. EXPECT_EQ(200, res->status);
  580. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  581. EXPECT_EQ("test.html", res->body);
  582. }
  583. TEST_F(ServerTest, GetMethodInvalidPath)
  584. {
  585. auto res = cli_.Get("/dir/../test.html");
  586. ASSERT_TRUE(res != nullptr);
  587. EXPECT_EQ(404, res->status);
  588. }
  589. TEST_F(ServerTest, GetMethodOutOfBaseDir)
  590. {
  591. auto res = cli_.Get("/../www/dir/test.html");
  592. ASSERT_TRUE(res != nullptr);
  593. EXPECT_EQ(404, res->status);
  594. }
  595. TEST_F(ServerTest, GetMethodOutOfBaseDir2)
  596. {
  597. auto res = cli_.Get("/dir/../../www/dir/test.html");
  598. ASSERT_TRUE(res != nullptr);
  599. EXPECT_EQ(404, res->status);
  600. }
  601. TEST_F(ServerTest, InvalidBaseDir)
  602. {
  603. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  604. EXPECT_EQ(true, svr_.set_base_dir("."));
  605. }
  606. TEST_F(ServerTest, EmptyRequest)
  607. {
  608. auto res = cli_.Get("");
  609. ASSERT_TRUE(res == nullptr);
  610. }
  611. TEST_F(ServerTest, LongRequest)
  612. {
  613. 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__");
  614. ASSERT_TRUE(res != nullptr);
  615. EXPECT_EQ(404, res->status);
  616. }
  617. TEST_F(ServerTest, TooLongRequest)
  618. {
  619. 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___");
  620. ASSERT_TRUE(res != nullptr);
  621. EXPECT_EQ(404, res->status);
  622. }
  623. TEST_F(ServerTest, LongHeader)
  624. {
  625. Request req;
  626. req.method = "GET";
  627. req.path = "/hi";
  628. std::string host_and_port;
  629. host_and_port += HOST;
  630. host_and_port += ":";
  631. host_and_port += std::to_string(PORT);
  632. req.headers.emplace("Host", host_and_port.c_str());
  633. req.headers.emplace("Accept", "*/*");
  634. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  635. req.headers.emplace("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  636. auto res = std::make_shared<Response>();
  637. auto ret = cli_.send(req, *res);
  638. ASSERT_TRUE(ret);
  639. EXPECT_EQ(200, res->status);
  640. }
  641. TEST_F(ServerTest, LongQueryValue)
  642. {
  643. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  644. ASSERT_TRUE(res != nullptr);
  645. EXPECT_EQ(200, res->status);
  646. }
  647. TEST_F(ServerTest, TooLongHeader)
  648. {
  649. Request req;
  650. req.method = "GET";
  651. req.path = "/hi";
  652. std::string host_and_port;
  653. host_and_port += HOST;
  654. host_and_port += ":";
  655. host_and_port += std::to_string(PORT);
  656. req.headers.emplace("Host", host_and_port.c_str());
  657. req.headers.emplace("Accept", "*/*");
  658. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  659. req.headers.emplace("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  660. auto res = std::make_shared<Response>();
  661. auto ret = cli_.send(req, *res);
  662. ASSERT_TRUE(ret);
  663. EXPECT_EQ(200, res->status);
  664. }
  665. TEST_F(ServerTest, PercentEncoding)
  666. {
  667. auto res = cli_.Get("/e%6edwith%");
  668. ASSERT_TRUE(res != nullptr);
  669. EXPECT_EQ(200, res->status);
  670. }
  671. TEST_F(ServerTest, PercentEncodingUnicode)
  672. {
  673. auto res = cli_.Get("/e%u006edwith%");
  674. ASSERT_TRUE(res != nullptr);
  675. EXPECT_EQ(200, res->status);
  676. }
  677. TEST_F(ServerTest, InvalidPercentEncoding)
  678. {
  679. auto res = cli_.Get("/%endwith%");
  680. ASSERT_TRUE(res != nullptr);
  681. EXPECT_EQ(404, res->status);
  682. }
  683. TEST_F(ServerTest, InvalidPercentEncodingUnicode)
  684. {
  685. auto res = cli_.Get("/%uendwith%");
  686. ASSERT_TRUE(res != nullptr);
  687. EXPECT_EQ(404, res->status);
  688. }
  689. TEST_F(ServerTest, EndWithPercentCharacterInQuery)
  690. {
  691. auto res = cli_.Get("/hello?aaa=bbb%");
  692. ASSERT_TRUE(res != nullptr);
  693. EXPECT_EQ(404, res->status);
  694. }
  695. TEST_F(ServerTest, MultipartFormData)
  696. {
  697. Request req;
  698. req.method = "POST";
  699. req.path = "/multipart";
  700. std::string host_and_port;
  701. host_and_port += HOST;
  702. host_and_port += ":";
  703. host_and_port += std::to_string(PORT);
  704. req.headers.emplace("Host", host_and_port.c_str());
  705. req.headers.emplace("Accept", "*/*");
  706. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  707. req.headers.emplace("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4");
  708. 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";
  709. auto res = std::make_shared<Response>();
  710. auto ret = cli_.send(req, *res);
  711. ASSERT_TRUE(ret);
  712. EXPECT_EQ(200, res->status);
  713. }
  714. TEST_F(ServerTest, CaseInsensitiveHeaderName)
  715. {
  716. auto res = cli_.Get("/hi");
  717. ASSERT_TRUE(res != nullptr);
  718. EXPECT_EQ(200, res->status);
  719. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  720. EXPECT_EQ("Hello World!", res->body);
  721. }
  722. TEST_F(ServerTest, CaseInsensitiveTransferEncoding)
  723. {
  724. Request req;
  725. req.method = "POST";
  726. req.path = "/chunked";
  727. std::string host_and_port;
  728. host_and_port += HOST;
  729. host_and_port += ":";
  730. host_and_port += std::to_string(PORT);
  731. req.headers.emplace("Host", host_and_port.c_str());
  732. req.headers.emplace("Accept", "*/*");
  733. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  734. req.headers.emplace("Content-Type", "text/plain");
  735. req.headers.emplace("Content-Length", "0");
  736. req.headers.emplace("Transfer-Encoding", "Chunked"); // Note, "Chunked" rather than typical "chunked".
  737. // Client does not chunk, so make a chunked body manually.
  738. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  739. auto res = std::make_shared<Response>();
  740. auto ret = cli_.send(req, *res);
  741. ASSERT_TRUE(ret);
  742. EXPECT_EQ(200, res->status);
  743. }
  744. TEST_F(ServerTest, GetStreamed)
  745. {
  746. auto res = cli_.Get("/streamed");
  747. ASSERT_TRUE(res != nullptr);
  748. EXPECT_EQ(200, res->status);
  749. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  750. EXPECT_TRUE(res->body == "aaabbb");
  751. }
  752. TEST_F(ServerTest, GetStreamedChunked)
  753. {
  754. auto res = cli_.Get("/streamedchunked");
  755. ASSERT_TRUE(res != nullptr);
  756. EXPECT_EQ(200, res->status);
  757. EXPECT_TRUE(res->body == "aaabbb");
  758. }
  759. TEST_F(ServerTest, LargeChunkedPost) {
  760. Request req;
  761. req.method = "POST";
  762. req.path = "/largechunked";
  763. std::string host_and_port;
  764. host_and_port += HOST;
  765. host_and_port += ":";
  766. host_and_port += std::to_string(PORT);
  767. req.headers.emplace("Host", host_and_port.c_str());
  768. req.headers.emplace("Accept", "*/*");
  769. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  770. req.headers.emplace("Content-Type", "text/plain");
  771. req.headers.emplace("Content-Length", "0");
  772. req.headers.emplace("Transfer-Encoding", "chunked");
  773. std::string long_string(30 * 1024u, 'a');
  774. std::string chunk = "7800\r\n" + long_string + "\r\n";
  775. // Attempt to make a large enough post to exceed OS buffers, to test that
  776. // the server handles short reads if the full chunk data isn't available.
  777. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  778. auto res = std::make_shared<Response>();
  779. auto ret = cli_.send(req, *res);
  780. ASSERT_TRUE(ret);
  781. EXPECT_EQ(200, res->status);
  782. }
  783. TEST_F(ServerTest, GetMethodRemoteAddr)
  784. {
  785. auto res = cli_.Get("/remote_addr");
  786. ASSERT_TRUE(res != nullptr);
  787. EXPECT_EQ(200, res->status);
  788. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  789. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  790. }
  791. TEST_F(ServerTest, SlowRequest)
  792. {
  793. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  794. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  795. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  796. msleep(100);
  797. }
  798. TEST_F(ServerTest, Put)
  799. {
  800. auto res = cli_.Put("/put", "PUT", "text/plain");
  801. ASSERT_TRUE(res != nullptr);
  802. EXPECT_EQ(200, res->status);
  803. EXPECT_EQ("PUT", res->body);
  804. }
  805. TEST_F(ServerTest, Delete)
  806. {
  807. auto res = cli_.Delete("/delete");
  808. ASSERT_TRUE(res != nullptr);
  809. EXPECT_EQ(200, res->status);
  810. EXPECT_EQ("DELETE", res->body);
  811. }
  812. TEST_F(ServerTest, Options)
  813. {
  814. auto res = cli_.Options("*");
  815. ASSERT_TRUE(res != nullptr);
  816. EXPECT_EQ(200, res->status);
  817. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  818. EXPECT_TRUE(res->body.empty());
  819. }
  820. TEST_F(ServerTest, URL)
  821. {
  822. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  823. ASSERT_TRUE(res != nullptr);
  824. EXPECT_EQ(200, res->status);
  825. }
  826. TEST_F(ServerTest, ArrayParam)
  827. {
  828. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  829. ASSERT_TRUE(res != nullptr);
  830. EXPECT_EQ(200, res->status);
  831. }
  832. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  833. TEST_F(ServerTest, Gzip)
  834. {
  835. Headers headers;
  836. headers.emplace("Accept-Encoding", "gzip, deflate");
  837. auto res = cli_.Get("/gzip", headers);
  838. ASSERT_TRUE(res != nullptr);
  839. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  840. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  841. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  842. EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body);
  843. EXPECT_EQ(200, res->status);
  844. }
  845. TEST_F(ServerTest, NoGzip)
  846. {
  847. Headers headers;
  848. headers.emplace("Accept-Encoding", "gzip, deflate");
  849. auto res = cli_.Get("/nogzip", headers);
  850. ASSERT_TRUE(res != nullptr);
  851. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  852. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  853. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  854. EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body);
  855. EXPECT_EQ(200, res->status);
  856. }
  857. TEST_F(ServerTest, MultipartFormDataGzip)
  858. {
  859. Request req;
  860. req.method = "POST";
  861. req.path = "/gzipmultipart";
  862. std::string host_and_port;
  863. host_and_port += HOST;
  864. host_and_port += ":";
  865. host_and_port += std::to_string(PORT);
  866. req.headers.emplace("Host", host_and_port.c_str());
  867. req.headers.emplace("Accept", "*/*");
  868. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  869. req.headers.emplace("Content-Type", "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  870. req.headers.emplace("Content-Encoding", "gzip");
  871. // compressed_body generated by creating input.txt to this file:
  872. /*
  873. --------------------------fcba8368a9f48c0f
  874. Content-Disposition: form-data; name="key1"
  875. test
  876. --------------------------fcba8368a9f48c0f
  877. Content-Disposition: form-data; name="key2"
  878. --abcdefg123
  879. --------------------------fcba8368a9f48c0f--
  880. */
  881. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  882. uint8_t compressed_body[] = {
  883. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  884. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  885. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  886. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  887. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  888. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  889. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  890. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  891. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  892. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  893. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00
  894. };
  895. req.body = std::string((char*)compressed_body, sizeof(compressed_body) / sizeof(compressed_body[0]));
  896. auto res = std::make_shared<Response>();
  897. auto ret = cli_.send(req, *res);
  898. ASSERT_TRUE(ret);
  899. EXPECT_EQ(200, res->status);
  900. }
  901. #endif
  902. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  903. protected:
  904. ServerTestWithAI_PASSIVE()
  905. : cli_(HOST, PORT)
  906. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  907. , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  908. #endif
  909. {}
  910. virtual void SetUp() {
  911. svr_.Get("/hi", [&](const Request& /*req*/, Response& res) {
  912. res.set_content("Hello World!", "text/plain");
  913. });
  914. t_ = thread([&]() {
  915. ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE));
  916. });
  917. while (!svr_.is_running()) {
  918. msleep(1);
  919. }
  920. }
  921. virtual void TearDown() {
  922. svr_.stop();
  923. t_.join();
  924. }
  925. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  926. SSLClient cli_;
  927. SSLServer svr_;
  928. #else
  929. Client cli_;
  930. Server svr_;
  931. #endif
  932. thread t_;
  933. };
  934. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200)
  935. {
  936. auto res = cli_.Get("/hi");
  937. ASSERT_TRUE(res != nullptr);
  938. EXPECT_EQ(200, res->status);
  939. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  940. EXPECT_EQ("Hello World!", res->body);
  941. }
  942. class ServerUpDownTest : public ::testing::Test {
  943. protected:
  944. ServerUpDownTest()
  945. : cli_(HOST, PORT)
  946. {}
  947. virtual void SetUp() {
  948. t_ = thread([&](){
  949. svr_.bind_to_any_port(HOST);
  950. msleep(500);
  951. ASSERT_TRUE(svr_.listen_after_bind());
  952. });
  953. while (!svr_.is_running()) {
  954. msleep(1);
  955. }
  956. }
  957. virtual void TearDown() {
  958. svr_.stop();
  959. t_.join();
  960. }
  961. Client cli_;
  962. Server svr_;
  963. thread t_;
  964. };
  965. TEST_F(ServerUpDownTest, QuickStartStop)
  966. {
  967. // Should not crash, especially when run with
  968. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  969. }
  970. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  971. TEST(SSLClientTest, ServerNameIndication)
  972. {
  973. SSLClient cli("httpbin.org", 443);
  974. auto res = cli.Get("/get");
  975. ASSERT_TRUE(res != nullptr);
  976. ASSERT_EQ(200, res->status);
  977. }
  978. #endif
  979. #ifdef _WIN32
  980. TEST(CleanupTest, WSACleanup)
  981. {
  982. int ret = WSACleanup();
  983. ASSERT_EQ(0, ret);
  984. }
  985. #endif
  986. // vim: et ts=4 sw=4 cin cino={1s ff=unix