test.cc 36 KB

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