test.cc 37 KB

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