test.cc 40 KB

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