test.cc 38 KB

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