test.cc 39 KB

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