test.cc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. #include <gtest/gtest.h>
  2. #include <httplib.h>
  3. #include <future>
  4. #include <iostream>
  5. #define SERVER_CERT_FILE "./cert.pem"
  6. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  7. #ifdef _WIN32
  8. #include <process.h>
  9. #define msleep(n) ::Sleep(n)
  10. #else
  11. #define msleep(n) ::usleep(n * 1000)
  12. #endif
  13. using namespace std;
  14. using namespace httplib;
  15. const char* HOST = "localhost";
  16. const int PORT = 1234;
  17. const string LONG_QUERY_VALUE = string(25000, '@');
  18. const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
  19. #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", "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", "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);
  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);
  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);
  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);
  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(Server, BindAndListenSeparately) {
  247. Server svr;
  248. int port = svr.bind_to_any_port("localhost");
  249. ASSERT_TRUE(port > 0);
  250. svr.stop();
  251. }
  252. class ServerTest : public ::testing::Test {
  253. protected:
  254. ServerTest()
  255. : cli_(HOST, PORT)
  256. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  257. , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  258. #endif
  259. {}
  260. virtual void SetUp() {
  261. svr_.set_base_dir("./www");
  262. svr_.Get("/hi", [&](const Request& /*req*/, Response& res) {
  263. res.set_content("Hello World!", "text/plain");
  264. })
  265. .Get("/slow", [&](const Request& /*req*/, Response& res) {
  266. msleep(2000);
  267. res.set_content("slow", "text/plain");
  268. })
  269. .Get("/remote_addr", [&](const Request& req, Response& res) {
  270. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  271. res.set_content(remote_addr.c_str(), "text/plain");
  272. })
  273. .Get("/endwith%", [&](const Request& /*req*/, Response& res) {
  274. res.set_content("Hello World!", "text/plain");
  275. })
  276. .Get("/", [&](const Request& /*req*/, Response& res) {
  277. res.set_redirect("/hi");
  278. })
  279. .Post("/person", [&](const Request& req, Response& res) {
  280. if (req.has_param("name") && req.has_param("note")) {
  281. persons_[req.get_param_value("name")] = req.get_param_value("note");
  282. } else {
  283. res.status = 400;
  284. }
  285. })
  286. .Get("/person/(.*)", [&](const Request& req, Response& res) {
  287. string name = req.matches[1];
  288. if (persons_.find(name) != persons_.end()) {
  289. auto note = persons_[name];
  290. res.set_content(note, "text/plain");
  291. } else {
  292. res.status = 404;
  293. }
  294. })
  295. .Get("/streamedchunked", [&](const Request& /*req*/, Response& res) {
  296. res.streamcb = [] (uint64_t offset) {
  297. if (offset < 3)
  298. return "a";
  299. if (offset < 6)
  300. return "b";
  301. return "";
  302. };
  303. })
  304. .Get("/streamed", [&](const Request& /*req*/, Response& res) {
  305. res.set_header("Content-Length", "6");
  306. res.streamcb = [] (uint64_t offset) {
  307. if (offset < 3)
  308. return "a";
  309. if (offset < 6)
  310. return "b";
  311. return "";
  312. };
  313. })
  314. .Post("/chunked", [&](const Request& req, Response& /*res*/) {
  315. EXPECT_EQ(req.body, "dechunked post body");
  316. })
  317. .Post("/largechunked", [&](const Request& req, Response& /*res*/) {
  318. std::string expected(6 * 30 * 1024u, 'a');
  319. EXPECT_EQ(req.body, expected);
  320. })
  321. .Post("/multipart", [&](const Request& req, Response& /*res*/) {
  322. EXPECT_EQ(5u, req.files.size());
  323. ASSERT_TRUE(!req.has_file("???"));
  324. {
  325. const auto& file = req.get_file_value("text1");
  326. EXPECT_EQ("", file.filename);
  327. EXPECT_EQ("text default", req.body.substr(file.offset, file.length));
  328. }
  329. {
  330. const auto& file = req.get_file_value("text2");
  331. EXPECT_EQ("", file.filename);
  332. EXPECT_EQ("aωb", req.body.substr(file.offset, file.length));
  333. }
  334. {
  335. const auto& file = req.get_file_value("file1");
  336. EXPECT_EQ("hello.txt", file.filename);
  337. EXPECT_EQ("text/plain", file.content_type);
  338. EXPECT_EQ("h\ne\n\nl\nl\no\n", req.body.substr(file.offset, file.length));
  339. }
  340. {
  341. const auto& file = req.get_file_value("file3");
  342. EXPECT_EQ("", file.filename);
  343. EXPECT_EQ("application/octet-stream", file.content_type);
  344. EXPECT_EQ(0u, file.length);
  345. }
  346. })
  347. .Post("/empty", [&](const Request& req, Response& res) {
  348. EXPECT_EQ(req.body, "");
  349. res.set_content("empty", "text/plain");
  350. })
  351. .Put("/put", [&](const Request& req, Response& res) {
  352. EXPECT_EQ(req.body, "PUT");
  353. res.set_content(req.body, "text/plain");
  354. })
  355. .Delete("/delete", [&](const Request& /*req*/, Response& res) {
  356. res.set_content("DELETE", "text/plain");
  357. })
  358. .Options(R"(\*)", [&](const Request& /*req*/, Response& res) {
  359. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  360. })
  361. .Get("/request-target", [&](const Request& req, Response& /*res*/) {
  362. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  363. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  364. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  365. })
  366. .Get("/long-query-value", [&](const Request& req, Response& /*res*/) {
  367. EXPECT_EQ(LONG_QUERY_URL, req.target);
  368. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  369. })
  370. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  371. .Get("/gzip", [&](const Request& /*req*/, Response& res) {
  372. res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain");
  373. })
  374. .Get("/nogzip", [&](const Request& /*req*/, Response& res) {
  375. res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "application/octet-stream");
  376. })
  377. .Post("/gzipmultipart", [&](const Request& req, Response& /*res*/) {
  378. EXPECT_EQ(2u, req.files.size());
  379. ASSERT_TRUE(!req.has_file("???"));
  380. {
  381. const auto& file = req.get_file_value("key1");
  382. EXPECT_EQ("", file.filename);
  383. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  384. }
  385. {
  386. const auto& file = req.get_file_value("key2");
  387. EXPECT_EQ("", file.filename);
  388. EXPECT_EQ("--abcdefg123", req.body.substr(file.offset, file.length));
  389. }
  390. })
  391. #endif
  392. ;
  393. persons_["john"] = "programmer";
  394. t_ = thread([&](){
  395. ASSERT_TRUE(svr_.listen(HOST, PORT));
  396. });
  397. while (!svr_.is_running()) {
  398. msleep(1);
  399. }
  400. }
  401. virtual void TearDown() {
  402. svr_.stop();
  403. for (auto& t: request_threads_) {
  404. t.join();
  405. }
  406. t_.join();
  407. }
  408. map<string, string> persons_;
  409. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  410. SSLClient cli_;
  411. SSLServer svr_;
  412. #else
  413. Client cli_;
  414. Server svr_;
  415. #endif
  416. thread t_;
  417. std::vector<thread> request_threads_;
  418. };
  419. TEST_F(ServerTest, GetMethod200)
  420. {
  421. auto res = cli_.Get("/hi");
  422. ASSERT_TRUE(res != nullptr);
  423. EXPECT_EQ("HTTP/1.1", res->version);
  424. EXPECT_EQ(200, res->status);
  425. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  426. EXPECT_EQ("close", res->get_header_value("Connection"));
  427. EXPECT_EQ("Hello World!", res->body);
  428. }
  429. TEST_F(ServerTest, GetMethod302)
  430. {
  431. auto res = cli_.Get("/");
  432. ASSERT_TRUE(res != nullptr);
  433. EXPECT_EQ(302, res->status);
  434. EXPECT_EQ("/hi", res->get_header_value("Location"));
  435. }
  436. TEST_F(ServerTest, GetMethod404)
  437. {
  438. auto res = cli_.Get("/invalid");
  439. ASSERT_TRUE(res != nullptr);
  440. EXPECT_EQ(404, res->status);
  441. }
  442. TEST_F(ServerTest, HeadMethod200)
  443. {
  444. auto res = cli_.Head("/hi");
  445. ASSERT_TRUE(res != nullptr);
  446. EXPECT_EQ(200, res->status);
  447. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  448. EXPECT_EQ("", res->body);
  449. }
  450. TEST_F(ServerTest, HeadMethod404)
  451. {
  452. auto res = cli_.Head("/invalid");
  453. ASSERT_TRUE(res != nullptr);
  454. EXPECT_EQ(404, res->status);
  455. EXPECT_EQ("", res->body);
  456. }
  457. TEST_F(ServerTest, GetMethodPersonJohn)
  458. {
  459. auto res = cli_.Get("/person/john");
  460. ASSERT_TRUE(res != nullptr);
  461. EXPECT_EQ(200, res->status);
  462. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  463. EXPECT_EQ("programmer", res->body);
  464. }
  465. TEST_F(ServerTest, PostMethod1)
  466. {
  467. auto res = cli_.Get("/person/john1");
  468. ASSERT_TRUE(res != nullptr);
  469. ASSERT_EQ(404, res->status);
  470. res = cli_.Post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
  471. ASSERT_TRUE(res != nullptr);
  472. ASSERT_EQ(200, res->status);
  473. res = cli_.Get("/person/john1");
  474. ASSERT_TRUE(res != nullptr);
  475. ASSERT_EQ(200, res->status);
  476. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  477. ASSERT_EQ("coder", res->body);
  478. }
  479. TEST_F(ServerTest, PostMethod2)
  480. {
  481. auto res = cli_.Get("/person/john2");
  482. ASSERT_TRUE(res != nullptr);
  483. ASSERT_EQ(404, res->status);
  484. Params params;
  485. params.emplace("name", "john2");
  486. params.emplace("note", "coder");
  487. res = cli_.Post("/person", params);
  488. ASSERT_TRUE(res != nullptr);
  489. ASSERT_EQ(200, res->status);
  490. res = cli_.Get("/person/john2");
  491. ASSERT_TRUE(res != nullptr);
  492. ASSERT_EQ(200, res->status);
  493. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  494. ASSERT_EQ("coder", res->body);
  495. }
  496. TEST_F(ServerTest, PostEmptyContent)
  497. {
  498. auto res = cli_.Post("/empty", "", "text/plain");
  499. ASSERT_TRUE(res != nullptr);
  500. ASSERT_EQ(200, res->status);
  501. ASSERT_EQ("empty", res->body);
  502. }
  503. TEST_F(ServerTest, GetMethodDir)
  504. {
  505. auto res = cli_.Get("/dir/");
  506. ASSERT_TRUE(res != nullptr);
  507. EXPECT_EQ(200, res->status);
  508. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  509. auto body = R"(<html>
  510. <head>
  511. </head>
  512. <body>
  513. <a href="/dir/test.html">Test</a>
  514. <a href="/hi">hi</a>
  515. </body>
  516. </html>
  517. )";
  518. EXPECT_EQ(body, res->body);
  519. }
  520. TEST_F(ServerTest, GetMethodDirTest)
  521. {
  522. auto res = cli_.Get("/dir/test.html");
  523. ASSERT_TRUE(res != nullptr);
  524. EXPECT_EQ(200, res->status);
  525. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  526. EXPECT_EQ("test.html", res->body);
  527. }
  528. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots)
  529. {
  530. auto res = cli_.Get("/dir/../dir/test.html");
  531. ASSERT_TRUE(res != nullptr);
  532. EXPECT_EQ(200, res->status);
  533. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  534. EXPECT_EQ("test.html", res->body);
  535. }
  536. TEST_F(ServerTest, GetMethodInvalidPath)
  537. {
  538. auto res = cli_.Get("/dir/../test.html");
  539. ASSERT_TRUE(res != nullptr);
  540. EXPECT_EQ(404, res->status);
  541. }
  542. TEST_F(ServerTest, GetMethodOutOfBaseDir)
  543. {
  544. auto res = cli_.Get("/../www/dir/test.html");
  545. ASSERT_TRUE(res != nullptr);
  546. EXPECT_EQ(404, res->status);
  547. }
  548. TEST_F(ServerTest, GetMethodOutOfBaseDir2)
  549. {
  550. auto res = cli_.Get("/dir/../../www/dir/test.html");
  551. ASSERT_TRUE(res != nullptr);
  552. EXPECT_EQ(404, res->status);
  553. }
  554. TEST_F(ServerTest, InvalidBaseDir)
  555. {
  556. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  557. EXPECT_EQ(true, svr_.set_base_dir("."));
  558. }
  559. TEST_F(ServerTest, EmptyRequest)
  560. {
  561. auto res = cli_.Get("");
  562. ASSERT_TRUE(res == nullptr);
  563. }
  564. TEST_F(ServerTest, LongRequest)
  565. {
  566. 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__");
  567. ASSERT_TRUE(res != nullptr);
  568. EXPECT_EQ(404, res->status);
  569. }
  570. TEST_F(ServerTest, TooLongRequest)
  571. {
  572. 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___");
  573. ASSERT_TRUE(res != nullptr);
  574. EXPECT_EQ(404, res->status);
  575. }
  576. TEST_F(ServerTest, LongHeader)
  577. {
  578. Request req;
  579. req.method = "GET";
  580. req.path = "/hi";
  581. std::string host_and_port;
  582. host_and_port += HOST;
  583. host_and_port += ":";
  584. host_and_port += std::to_string(PORT);
  585. req.headers.emplace("Host", host_and_port.c_str());
  586. req.headers.emplace("Accept", "*/*");
  587. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  588. req.headers.emplace("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  589. auto res = std::make_shared<Response>();
  590. auto ret = cli_.send(req, *res);
  591. ASSERT_TRUE(ret);
  592. EXPECT_EQ(200, res->status);
  593. }
  594. TEST_F(ServerTest, LongQueryValue)
  595. {
  596. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  597. ASSERT_TRUE(res != nullptr);
  598. EXPECT_EQ(200, res->status);
  599. }
  600. TEST_F(ServerTest, TooLongHeader)
  601. {
  602. Request req;
  603. req.method = "GET";
  604. req.path = "/hi";
  605. std::string host_and_port;
  606. host_and_port += HOST;
  607. host_and_port += ":";
  608. host_and_port += std::to_string(PORT);
  609. req.headers.emplace("Host", host_and_port.c_str());
  610. req.headers.emplace("Accept", "*/*");
  611. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  612. req.headers.emplace("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  613. auto res = std::make_shared<Response>();
  614. auto ret = cli_.send(req, *res);
  615. ASSERT_TRUE(ret);
  616. EXPECT_EQ(200, res->status);
  617. }
  618. TEST_F(ServerTest, PercentEncoding)
  619. {
  620. auto res = cli_.Get("/e%6edwith%");
  621. ASSERT_TRUE(res != nullptr);
  622. EXPECT_EQ(200, res->status);
  623. }
  624. TEST_F(ServerTest, PercentEncodingUnicode)
  625. {
  626. auto res = cli_.Get("/e%u006edwith%");
  627. ASSERT_TRUE(res != nullptr);
  628. EXPECT_EQ(200, res->status);
  629. }
  630. TEST_F(ServerTest, InvalidPercentEncoding)
  631. {
  632. auto res = cli_.Get("/%endwith%");
  633. ASSERT_TRUE(res != nullptr);
  634. EXPECT_EQ(404, res->status);
  635. }
  636. TEST_F(ServerTest, InvalidPercentEncodingUnicode)
  637. {
  638. auto res = cli_.Get("/%uendwith%");
  639. ASSERT_TRUE(res != nullptr);
  640. EXPECT_EQ(404, res->status);
  641. }
  642. TEST_F(ServerTest, EndWithPercentCharacterInQuery)
  643. {
  644. auto res = cli_.Get("/hello?aaa=bbb%");
  645. ASSERT_TRUE(res != nullptr);
  646. EXPECT_EQ(404, res->status);
  647. }
  648. TEST_F(ServerTest, MultipartFormData)
  649. {
  650. Request req;
  651. req.method = "POST";
  652. req.path = "/multipart";
  653. std::string host_and_port;
  654. host_and_port += HOST;
  655. host_and_port += ":";
  656. host_and_port += std::to_string(PORT);
  657. req.headers.emplace("Host", host_and_port.c_str());
  658. req.headers.emplace("Accept", "*/*");
  659. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  660. req.headers.emplace("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4");
  661. 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";
  662. auto res = std::make_shared<Response>();
  663. auto ret = cli_.send(req, *res);
  664. ASSERT_TRUE(ret);
  665. EXPECT_EQ(200, res->status);
  666. }
  667. TEST_F(ServerTest, CaseInsensitiveHeaderName)
  668. {
  669. auto res = cli_.Get("/hi");
  670. ASSERT_TRUE(res != nullptr);
  671. EXPECT_EQ(200, res->status);
  672. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  673. EXPECT_EQ("Hello World!", res->body);
  674. }
  675. TEST_F(ServerTest, CaseInsensitiveTransferEncoding)
  676. {
  677. Request req;
  678. req.method = "POST";
  679. req.path = "/chunked";
  680. std::string host_and_port;
  681. host_and_port += HOST;
  682. host_and_port += ":";
  683. host_and_port += std::to_string(PORT);
  684. req.headers.emplace("Host", host_and_port.c_str());
  685. req.headers.emplace("Accept", "*/*");
  686. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  687. req.headers.emplace("Content-Type", "text/plain");
  688. req.headers.emplace("Content-Length", "0");
  689. req.headers.emplace("Transfer-Encoding", "Chunked"); // Note, "Chunked" rather than typical "chunked".
  690. // Client does not chunk, so make a chunked body manually.
  691. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  692. auto res = std::make_shared<Response>();
  693. auto ret = cli_.send(req, *res);
  694. ASSERT_TRUE(ret);
  695. EXPECT_EQ(200, res->status);
  696. }
  697. TEST_F(ServerTest, GetStreamed)
  698. {
  699. auto res = cli_.Get("/streamed");
  700. ASSERT_TRUE(res != nullptr);
  701. EXPECT_EQ(200, res->status);
  702. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  703. EXPECT_TRUE(res->body == "aaabbb");
  704. }
  705. TEST_F(ServerTest, GetStreamedChunked)
  706. {
  707. auto res = cli_.Get("/streamedchunked");
  708. ASSERT_TRUE(res != nullptr);
  709. EXPECT_EQ(200, res->status);
  710. EXPECT_TRUE(res->body == "aaabbb");
  711. }
  712. TEST_F(ServerTest, LargeChunkedPost) {
  713. Request req;
  714. req.method = "POST";
  715. req.path = "/largechunked";
  716. std::string host_and_port;
  717. host_and_port += HOST;
  718. host_and_port += ":";
  719. host_and_port += std::to_string(PORT);
  720. req.headers.emplace("Host", host_and_port.c_str());
  721. req.headers.emplace("Accept", "*/*");
  722. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  723. req.headers.emplace("Content-Type", "text/plain");
  724. req.headers.emplace("Content-Length", "0");
  725. req.headers.emplace("Transfer-Encoding", "chunked");
  726. std::string long_string(30 * 1024u, 'a');
  727. std::string chunk = "7800\r\n" + long_string + "\r\n";
  728. // Attempt to make a large enough post to exceed OS buffers, to test that
  729. // the server handles short reads if the full chunk data isn't available.
  730. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  731. auto res = std::make_shared<Response>();
  732. auto ret = cli_.send(req, *res);
  733. ASSERT_TRUE(ret);
  734. EXPECT_EQ(200, res->status);
  735. }
  736. TEST_F(ServerTest, GetMethodRemoteAddr)
  737. {
  738. auto res = cli_.Get("/remote_addr");
  739. ASSERT_TRUE(res != nullptr);
  740. EXPECT_EQ(200, res->status);
  741. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  742. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  743. }
  744. TEST_F(ServerTest, SlowRequest)
  745. {
  746. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  747. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  748. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  749. msleep(100);
  750. }
  751. TEST_F(ServerTest, Put)
  752. {
  753. auto res = cli_.Put("/put", "PUT", "text/plain");
  754. ASSERT_TRUE(res != nullptr);
  755. EXPECT_EQ(200, res->status);
  756. EXPECT_EQ("PUT", res->body);
  757. }
  758. TEST_F(ServerTest, Delete)
  759. {
  760. auto res = cli_.Delete("/delete");
  761. ASSERT_TRUE(res != nullptr);
  762. EXPECT_EQ(200, res->status);
  763. EXPECT_EQ("DELETE", res->body);
  764. }
  765. TEST_F(ServerTest, Options)
  766. {
  767. auto res = cli_.Options("*");
  768. ASSERT_TRUE(res != nullptr);
  769. EXPECT_EQ(200, res->status);
  770. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  771. EXPECT_TRUE(res->body.empty());
  772. }
  773. TEST_F(ServerTest, URL)
  774. {
  775. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  776. ASSERT_TRUE(res != nullptr);
  777. EXPECT_EQ(200, res->status);
  778. }
  779. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  780. TEST_F(ServerTest, Gzip)
  781. {
  782. Headers headers;
  783. headers.emplace("Accept-Encoding", "gzip, deflate");
  784. auto res = cli_.Get("/gzip", headers);
  785. ASSERT_TRUE(res != nullptr);
  786. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  787. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  788. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  789. EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body);
  790. EXPECT_EQ(200, res->status);
  791. }
  792. TEST_F(ServerTest, NoGzip)
  793. {
  794. Headers headers;
  795. headers.emplace("Accept-Encoding", "gzip, deflate");
  796. auto res = cli_.Get("/nogzip", headers);
  797. ASSERT_TRUE(res != nullptr);
  798. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  799. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  800. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  801. EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body);
  802. EXPECT_EQ(200, res->status);
  803. }
  804. TEST_F(ServerTest, MultipartFormDataGzip)
  805. {
  806. Request req;
  807. req.method = "POST";
  808. req.path = "/gzipmultipart";
  809. std::string host_and_port;
  810. host_and_port += HOST;
  811. host_and_port += ":";
  812. host_and_port += std::to_string(PORT);
  813. req.headers.emplace("Host", host_and_port.c_str());
  814. req.headers.emplace("Accept", "*/*");
  815. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  816. req.headers.emplace("Content-Type", "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  817. req.headers.emplace("Content-Encoding", "gzip");
  818. // compressed_body generated by creating input.txt to this file:
  819. /*
  820. --------------------------fcba8368a9f48c0f
  821. Content-Disposition: form-data; name="key1"
  822. test
  823. --------------------------fcba8368a9f48c0f
  824. Content-Disposition: form-data; name="key2"
  825. --abcdefg123
  826. --------------------------fcba8368a9f48c0f--
  827. */
  828. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  829. uint8_t compressed_body[] = {
  830. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  831. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  832. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  833. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  834. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  835. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  836. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  837. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  838. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  839. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  840. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00
  841. };
  842. req.body = std::string((char*)compressed_body, sizeof(compressed_body) / sizeof(compressed_body[0]));
  843. auto res = std::make_shared<Response>();
  844. auto ret = cli_.send(req, *res);
  845. ASSERT_TRUE(ret);
  846. EXPECT_EQ(200, res->status);
  847. }
  848. #endif
  849. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  850. protected:
  851. ServerTestWithAI_PASSIVE()
  852. : cli_(HOST, PORT)
  853. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  854. , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  855. #endif
  856. {}
  857. virtual void SetUp() {
  858. svr_.Get("/hi", [&](const Request& /*req*/, Response& res) {
  859. res.set_content("Hello World!", "text/plain");
  860. });
  861. t_ = thread([&]() {
  862. ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE));
  863. });
  864. while (!svr_.is_running()) {
  865. msleep(1);
  866. }
  867. }
  868. virtual void TearDown() {
  869. svr_.stop();
  870. t_.join();
  871. }
  872. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  873. SSLClient cli_;
  874. SSLServer svr_;
  875. #else
  876. Client cli_;
  877. Server svr_;
  878. #endif
  879. thread t_;
  880. };
  881. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200)
  882. {
  883. auto res = cli_.Get("/hi");
  884. ASSERT_TRUE(res != nullptr);
  885. EXPECT_EQ(200, res->status);
  886. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  887. EXPECT_EQ("Hello World!", res->body);
  888. }
  889. class ServerUpDownTest : public ::testing::Test {
  890. protected:
  891. ServerUpDownTest()
  892. : cli_(HOST, PORT)
  893. {}
  894. virtual void SetUp() {
  895. t_ = thread([&](){
  896. svr_.bind_to_any_port(HOST);
  897. msleep(500);
  898. ASSERT_TRUE(svr_.listen_after_bind());
  899. });
  900. while (!svr_.is_running()) {
  901. msleep(1);
  902. }
  903. }
  904. virtual void TearDown() {
  905. svr_.stop();
  906. t_.join();
  907. }
  908. Client cli_;
  909. Server svr_;
  910. thread t_;
  911. };
  912. TEST_F(ServerUpDownTest, QuickStartStop)
  913. {
  914. // Should not crash, especially when run with
  915. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  916. }
  917. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  918. TEST(SSLClientTest, ServerNameIndication)
  919. {
  920. SSLClient cli("httpbin.org", 443);
  921. auto res = cli.Get("/get");
  922. ASSERT_TRUE(res != nullptr);
  923. ASSERT_EQ(200, res->status);
  924. }
  925. #endif
  926. #ifdef _WIN32
  927. TEST(CleanupTest, WSACleanup)
  928. {
  929. int ret = WSACleanup();
  930. ASSERT_EQ(0, ret);
  931. }
  932. #endif
  933. // vim: et ts=4 sw=4 cin cino={1s ff=unix