test.cc 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. #include <future>
  2. #include <gtest/gtest.h>
  3. #include <httplib.h>
  4. #define SERVER_CERT_FILE "./cert.pem"
  5. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  6. #define CA_CERT_FILE "./ca-bundle.crt"
  7. #define CLIENT_CA_CERT_FILE "./rootCA.cert.pem"
  8. #define CLIENT_CA_CERT_DIR "."
  9. #define CLIENT_CERT_FILE "./client.cert.pem"
  10. #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem"
  11. #ifdef _WIN32
  12. #include <process.h>
  13. #define msleep(n) ::Sleep(n)
  14. #else
  15. #define msleep(n) ::usleep(n * 1000)
  16. #endif
  17. using namespace std;
  18. using namespace httplib;
  19. const char *HOST = "localhost";
  20. const int PORT = 1234;
  21. const string LONG_QUERY_VALUE = string(25000, '@');
  22. const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
  23. const std::string JSON_DATA = "{\"hello\":\"world\"}";
  24. #ifdef _WIN32
  25. TEST(StartupTest, WSAStartup) {
  26. WSADATA wsaData;
  27. int ret = WSAStartup(0x0002, &wsaData);
  28. ASSERT_EQ(0, ret);
  29. }
  30. #endif
  31. TEST(SplitTest, ParseQueryString) {
  32. string s = "key1=val1&key2=val2&key3=val3";
  33. Params dic;
  34. detail::split(s.c_str(), s.c_str() + s.size(), '&',
  35. [&](const char *b, const char *e) {
  36. string key, val;
  37. detail::split(b, e, '=', [&](const char *b, const char *e) {
  38. if (key.empty()) {
  39. key.assign(b, e);
  40. } else {
  41. val.assign(b, e);
  42. }
  43. });
  44. dic.emplace(key, val);
  45. });
  46. EXPECT_EQ("val1", dic.find("key1")->second);
  47. EXPECT_EQ("val2", dic.find("key2")->second);
  48. EXPECT_EQ("val3", dic.find("key3")->second);
  49. }
  50. TEST(ParseQueryTest, ParseQueryString) {
  51. string s = "key1=val1&key2=val2&key3=val3";
  52. Params dic;
  53. detail::parse_query_text(s, dic);
  54. EXPECT_EQ("val1", dic.find("key1")->second);
  55. EXPECT_EQ("val2", dic.find("key2")->second);
  56. EXPECT_EQ("val3", dic.find("key3")->second);
  57. }
  58. TEST(GetHeaderValueTest, DefaultValue) {
  59. Headers headers = {{"Dummy", "Dummy"}};
  60. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  61. EXPECT_STREQ("text/plain", val);
  62. }
  63. TEST(GetHeaderValueTest, DefaultValueInt) {
  64. Headers headers = {{"Dummy", "Dummy"}};
  65. auto val = detail::get_header_value_uint64(headers, "Content-Length", 100);
  66. EXPECT_EQ(100ull, val);
  67. }
  68. TEST(GetHeaderValueTest, RegularValue) {
  69. Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
  70. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  71. EXPECT_STREQ("text/html", val);
  72. }
  73. TEST(GetHeaderValueTest, RegularValueInt) {
  74. Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
  75. auto val = detail::get_header_value_uint64(headers, "Content-Length", 0);
  76. EXPECT_EQ(100ull, val);
  77. }
  78. TEST(GetHeaderValueTest, Range) {
  79. {
  80. Headers headers = {make_range_header(1)};
  81. auto val = detail::get_header_value(headers, "Range", 0, 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, 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, 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, 0);
  97. EXPECT_STREQ("bytes=1-10, 100-200", val);
  98. }
  99. }
  100. TEST(ChunkedEncodingTest, FromHTTPWatch) {
  101. auto host = "www.httpwatch.com";
  102. auto sec = 2;
  103. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  104. auto port = 443;
  105. httplib::SSLClient cli(host, port, sec);
  106. #else
  107. auto port = 80;
  108. httplib::Client cli(host, port, sec);
  109. #endif
  110. auto res =
  111. 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(ChunkedEncodingTest, WithContentReceiver) {
  119. auto host = "www.httpwatch.com";
  120. auto sec = 2;
  121. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  122. auto port = 443;
  123. httplib::SSLClient cli(host, port, sec);
  124. #else
  125. auto port = 80;
  126. httplib::Client cli(host, port, sec);
  127. #endif
  128. std::string body;
  129. auto res =
  130. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
  131. [&](const char *data, size_t len) { body.append(data, len); });
  132. ASSERT_TRUE(res != nullptr);
  133. std::string out;
  134. httplib::detail::read_file("./image.jpg", out);
  135. EXPECT_EQ(200, res->status);
  136. EXPECT_EQ(out, body);
  137. }
  138. TEST(RangeTest, FromHTTPBin) {
  139. auto host = "httpbin.org";
  140. auto sec = 5;
  141. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  142. auto port = 443;
  143. httplib::SSLClient cli(host, port, sec);
  144. #else
  145. auto port = 80;
  146. httplib::Client cli(host, port, sec);
  147. #endif
  148. {
  149. httplib::Headers headers;
  150. auto res = cli.Get("/range/32", headers);
  151. ASSERT_TRUE(res != nullptr);
  152. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  153. EXPECT_EQ(200, res->status);
  154. }
  155. {
  156. httplib::Headers headers = {httplib::make_range_header(1)};
  157. auto res = cli.Get("/range/32", headers);
  158. ASSERT_TRUE(res != nullptr);
  159. EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef");
  160. EXPECT_EQ(206, res->status);
  161. }
  162. {
  163. httplib::Headers headers = {httplib::make_range_header(1, 10)};
  164. auto res = cli.Get("/range/32", headers);
  165. ASSERT_TRUE(res != nullptr);
  166. EXPECT_EQ(res->body, "bcdefghijk");
  167. EXPECT_EQ(206, res->status);
  168. }
  169. }
  170. TEST(ConnectionErrorTest, InvalidHost) {
  171. auto host = "abcde.com";
  172. auto sec = 2;
  173. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  174. auto port = 443;
  175. httplib::SSLClient cli(host, port, sec);
  176. #else
  177. auto port = 80;
  178. httplib::Client cli(host, port, sec);
  179. #endif
  180. auto res = cli.Get("/");
  181. ASSERT_TRUE(res == nullptr);
  182. }
  183. TEST(ConnectionErrorTest, InvalidPort) {
  184. auto host = "localhost";
  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(ConnectionErrorTest, Timeout) {
  197. auto host = "google.com";
  198. auto sec = 2;
  199. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  200. auto port = 44380;
  201. httplib::SSLClient cli(host, port, sec);
  202. #else
  203. auto port = 8080;
  204. httplib::Client cli(host, port, sec);
  205. #endif
  206. auto res = cli.Get("/");
  207. ASSERT_TRUE(res == nullptr);
  208. }
  209. TEST(CancelTest, NoCancel) {
  210. auto host = "httpbin.org";
  211. auto sec = 5;
  212. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  213. auto port = 443;
  214. httplib::SSLClient cli(host, port, sec);
  215. #else
  216. auto port = 80;
  217. httplib::Client cli(host, port, sec);
  218. #endif
  219. httplib::Headers headers;
  220. auto res =
  221. cli.Get("/range/32", headers, [](uint64_t, uint64_t) { return true; });
  222. ASSERT_TRUE(res != nullptr);
  223. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  224. EXPECT_EQ(200, res->status);
  225. }
  226. TEST(CancelTest, WithCancelSmallPayload) {
  227. auto host = "httpbin.org";
  228. auto sec = 5;
  229. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  230. auto port = 443;
  231. httplib::SSLClient cli(host, port, sec);
  232. #else
  233. auto port = 80;
  234. httplib::Client cli(host, port, sec);
  235. #endif
  236. httplib::Headers headers;
  237. auto res =
  238. cli.Get("/range/32", headers, [](uint64_t, uint64_t) { return false; });
  239. ASSERT_TRUE(res == nullptr);
  240. }
  241. TEST(CancelTest, WithCancelLargePayload) {
  242. auto host = "httpbin.org";
  243. auto sec = 5;
  244. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  245. auto port = 443;
  246. httplib::SSLClient cli(host, port, sec);
  247. #else
  248. auto port = 80;
  249. httplib::Client cli(host, port, sec);
  250. #endif
  251. uint32_t count = 0;
  252. httplib::Headers headers;
  253. auto res = cli.Get("/range/65536", headers,
  254. [&count](uint64_t, uint64_t) { return (count++ == 0); });
  255. ASSERT_TRUE(res == nullptr);
  256. }
  257. TEST(BaseAuthTest, FromHTTPWatch) {
  258. auto host = "httpbin.org";
  259. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  260. auto port = 443;
  261. httplib::SSLClient cli(host, port);
  262. #else
  263. auto port = 80;
  264. httplib::Client cli(host, port);
  265. #endif
  266. {
  267. auto res = cli.Get("/basic-auth/hello/world");
  268. ASSERT_TRUE(res != nullptr);
  269. EXPECT_EQ(401, res->status);
  270. }
  271. {
  272. auto res = cli.Get("/basic-auth/hello/world", {
  273. httplib::make_basic_authentication_header("hello", "world")
  274. });
  275. ASSERT_TRUE(res != nullptr);
  276. EXPECT_EQ(res->body,
  277. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  278. EXPECT_EQ(200, res->status);
  279. }
  280. }
  281. TEST(Server, BindAndListenSeparately) {
  282. Server svr;
  283. int port = svr.bind_to_any_port("localhost");
  284. ASSERT_TRUE(port > 0);
  285. svr.stop();
  286. }
  287. class ServerTest : public ::testing::Test {
  288. protected:
  289. ServerTest()
  290. : cli_(HOST, PORT)
  291. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  292. ,
  293. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  294. #endif
  295. {
  296. }
  297. virtual void SetUp() {
  298. svr_.set_base_dir("./www");
  299. svr_.Get("/hi",
  300. [&](const Request & /*req*/, Response &res) {
  301. res.set_content("Hello World!", "text/plain");
  302. })
  303. .Get("/slow",
  304. [&](const Request & /*req*/, Response &res) {
  305. msleep(2000);
  306. res.set_content("slow", "text/plain");
  307. })
  308. .Get("/remote_addr",
  309. [&](const Request &req, Response &res) {
  310. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  311. res.set_content(remote_addr.c_str(), "text/plain");
  312. })
  313. .Get("/endwith%",
  314. [&](const Request & /*req*/, Response &res) {
  315. res.set_content("Hello World!", "text/plain");
  316. })
  317. .Get("/", [&](const Request & /*req*/,
  318. Response &res) { res.set_redirect("/hi"); })
  319. .Post("/person",
  320. [&](const Request &req, Response &res) {
  321. if (req.has_param("name") && req.has_param("note")) {
  322. persons_[req.get_param_value("name")] =
  323. req.get_param_value("note");
  324. } else {
  325. res.status = 400;
  326. }
  327. })
  328. .Get("/person/(.*)",
  329. [&](const Request &req, Response &res) {
  330. string name = req.matches[1];
  331. if (persons_.find(name) != persons_.end()) {
  332. auto note = persons_[name];
  333. res.set_content(note, "text/plain");
  334. } else {
  335. res.status = 404;
  336. }
  337. })
  338. .Post("/x-www-form-urlencoded-json",
  339. [&](const Request &req, Response &res) {
  340. auto json = req.get_param_value("json");
  341. ASSERT_EQ(JSON_DATA, json);
  342. res.set_content(json, "appliation/json");
  343. res.status = 200;
  344. })
  345. .Get("/streamedchunked",
  346. [&](const Request & /*req*/, Response &res) {
  347. res.content_producer = [](uint64_t offset) {
  348. if (offset < 3) return "a";
  349. if (offset < 6) return "b";
  350. return "";
  351. };
  352. })
  353. .Get("/streamed",
  354. [&](const Request & /*req*/, Response &res) {
  355. res.set_header("Content-Length", "6");
  356. res.content_producer = [](uint64_t offset) {
  357. if (offset < 3) return "a";
  358. if (offset < 6) return "b";
  359. return "";
  360. };
  361. })
  362. .Post("/chunked",
  363. [&](const Request &req, Response & /*res*/) {
  364. EXPECT_EQ(req.body, "dechunked post body");
  365. })
  366. .Post("/largechunked",
  367. [&](const Request &req, Response & /*res*/) {
  368. std::string expected(6 * 30 * 1024u, 'a');
  369. EXPECT_EQ(req.body, expected);
  370. })
  371. .Post("/multipart",
  372. [&](const Request &req, Response & /*res*/) {
  373. EXPECT_EQ(5u, req.files.size());
  374. ASSERT_TRUE(!req.has_file("???"));
  375. {
  376. const auto &file = req.get_file_value("text1");
  377. EXPECT_EQ("", file.filename);
  378. EXPECT_EQ("text default",
  379. req.body.substr(file.offset, file.length));
  380. }
  381. {
  382. const auto &file = req.get_file_value("text2");
  383. EXPECT_EQ("", file.filename);
  384. EXPECT_EQ("aωb", req.body.substr(file.offset, file.length));
  385. }
  386. {
  387. const auto &file = req.get_file_value("file1");
  388. EXPECT_EQ("hello.txt", file.filename);
  389. EXPECT_EQ("text/plain", file.content_type);
  390. EXPECT_EQ("h\ne\n\nl\nl\no\n",
  391. req.body.substr(file.offset, file.length));
  392. }
  393. {
  394. const auto &file = req.get_file_value("file3");
  395. EXPECT_EQ("", file.filename);
  396. EXPECT_EQ("application/octet-stream", file.content_type);
  397. EXPECT_EQ(0u, file.length);
  398. }
  399. })
  400. .Post("/empty",
  401. [&](const Request &req, Response &res) {
  402. EXPECT_EQ(req.body, "");
  403. res.set_content("empty", "text/plain");
  404. })
  405. .Put("/put",
  406. [&](const Request &req, Response &res) {
  407. EXPECT_EQ(req.body, "PUT");
  408. res.set_content(req.body, "text/plain");
  409. })
  410. .Patch("/patch",
  411. [&](const Request &req, Response &res) {
  412. EXPECT_EQ(req.body, "PATCH");
  413. res.set_content(req.body, "text/plain");
  414. })
  415. .Delete("/delete",
  416. [&](const Request & /*req*/, Response &res) {
  417. res.set_content("DELETE", "text/plain");
  418. })
  419. .Options(R"(\*)",
  420. [&](const Request & /*req*/, Response &res) {
  421. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  422. })
  423. .Get("/request-target",
  424. [&](const Request &req, Response & /*res*/) {
  425. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  426. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  427. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  428. })
  429. .Get("/long-query-value",
  430. [&](const Request &req, Response & /*res*/) {
  431. EXPECT_EQ(LONG_QUERY_URL, req.target);
  432. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  433. })
  434. .Get("/array-param",
  435. [&](const Request &req, Response & /*res*/) {
  436. EXPECT_EQ(3u, req.get_param_value_count("array"));
  437. EXPECT_EQ("value1", req.get_param_value("array", 0));
  438. EXPECT_EQ("value2", req.get_param_value("array", 1));
  439. EXPECT_EQ("value3", req.get_param_value("array", 2));
  440. })
  441. .Post("/validate-no-multiple-headers",
  442. [&](const Request &req, Response & /*res*/) {
  443. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  444. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  445. })
  446. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  447. .Get("/gzip",
  448. [&](const Request & /*req*/, Response &res) {
  449. res.set_content(
  450. "12345678901234567890123456789012345678901234567890123456789"
  451. "01234567890123456789012345678901234567890",
  452. "text/plain");
  453. })
  454. .Get("/nogzip",
  455. [&](const Request & /*req*/, Response &res) {
  456. res.set_content(
  457. "12345678901234567890123456789012345678901234567890123456789"
  458. "01234567890123456789012345678901234567890",
  459. "application/octet-stream");
  460. })
  461. .Post("/gzipmultipart",
  462. [&](const Request &req, Response & /*res*/) {
  463. EXPECT_EQ(2u, req.files.size());
  464. ASSERT_TRUE(!req.has_file("???"));
  465. {
  466. const auto &file = req.get_file_value("key1");
  467. EXPECT_EQ("", file.filename);
  468. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  469. }
  470. {
  471. const auto &file = req.get_file_value("key2");
  472. EXPECT_EQ("", file.filename);
  473. EXPECT_EQ("--abcdefg123",
  474. req.body.substr(file.offset, file.length));
  475. }
  476. })
  477. #endif
  478. ;
  479. persons_["john"] = "programmer";
  480. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  481. while (!svr_.is_running()) {
  482. msleep(1);
  483. }
  484. }
  485. virtual void TearDown() {
  486. svr_.stop();
  487. for (auto &t : request_threads_) {
  488. t.join();
  489. }
  490. t_.join();
  491. }
  492. map<string, string> persons_;
  493. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  494. SSLClient cli_;
  495. SSLServer svr_;
  496. #else
  497. Client cli_;
  498. Server svr_;
  499. #endif
  500. thread t_;
  501. std::vector<thread> request_threads_;
  502. };
  503. TEST_F(ServerTest, GetMethod200) {
  504. auto res = cli_.Get("/hi");
  505. ASSERT_TRUE(res != nullptr);
  506. EXPECT_EQ("HTTP/1.1", res->version);
  507. EXPECT_EQ(200, res->status);
  508. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  509. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  510. EXPECT_EQ("close", res->get_header_value("Connection"));
  511. EXPECT_EQ("Hello World!", res->body);
  512. }
  513. TEST_F(ServerTest, GetMethod302) {
  514. auto res = cli_.Get("/");
  515. ASSERT_TRUE(res != nullptr);
  516. EXPECT_EQ(302, res->status);
  517. EXPECT_EQ("/hi", res->get_header_value("Location"));
  518. }
  519. TEST_F(ServerTest, GetMethod404) {
  520. auto res = cli_.Get("/invalid");
  521. ASSERT_TRUE(res != nullptr);
  522. EXPECT_EQ(404, res->status);
  523. }
  524. TEST_F(ServerTest, HeadMethod200) {
  525. auto res = cli_.Head("/hi");
  526. ASSERT_TRUE(res != nullptr);
  527. EXPECT_EQ(200, res->status);
  528. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  529. EXPECT_EQ("", res->body);
  530. }
  531. TEST_F(ServerTest, HeadMethod404) {
  532. auto res = cli_.Head("/invalid");
  533. ASSERT_TRUE(res != nullptr);
  534. EXPECT_EQ(404, res->status);
  535. EXPECT_EQ("", res->body);
  536. }
  537. TEST_F(ServerTest, GetMethodPersonJohn) {
  538. auto res = cli_.Get("/person/john");
  539. ASSERT_TRUE(res != nullptr);
  540. EXPECT_EQ(200, res->status);
  541. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  542. EXPECT_EQ("programmer", res->body);
  543. }
  544. TEST_F(ServerTest, PostMethod1) {
  545. auto res = cli_.Get("/person/john1");
  546. ASSERT_TRUE(res != nullptr);
  547. ASSERT_EQ(404, res->status);
  548. res = cli_.Post("/person", "name=john1&note=coder",
  549. "application/x-www-form-urlencoded");
  550. ASSERT_TRUE(res != nullptr);
  551. ASSERT_EQ(200, res->status);
  552. res = cli_.Get("/person/john1");
  553. ASSERT_TRUE(res != nullptr);
  554. ASSERT_EQ(200, res->status);
  555. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  556. ASSERT_EQ("coder", res->body);
  557. }
  558. TEST_F(ServerTest, PostMethod2) {
  559. auto res = cli_.Get("/person/john2");
  560. ASSERT_TRUE(res != nullptr);
  561. ASSERT_EQ(404, res->status);
  562. Params params;
  563. params.emplace("name", "john2");
  564. params.emplace("note", "coder");
  565. res = cli_.Post("/person", params);
  566. ASSERT_TRUE(res != nullptr);
  567. ASSERT_EQ(200, res->status);
  568. res = cli_.Get("/person/john2");
  569. ASSERT_TRUE(res != nullptr);
  570. ASSERT_EQ(200, res->status);
  571. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  572. ASSERT_EQ("coder", res->body);
  573. }
  574. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  575. Params params;
  576. params.emplace("json", JSON_DATA);
  577. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  578. ASSERT_TRUE(res != nullptr);
  579. ASSERT_EQ(200, res->status);
  580. ASSERT_EQ(JSON_DATA, res->body);
  581. }
  582. TEST_F(ServerTest, PostEmptyContent) {
  583. auto res = cli_.Post("/empty", "", "text/plain");
  584. ASSERT_TRUE(res != nullptr);
  585. ASSERT_EQ(200, res->status);
  586. ASSERT_EQ("empty", res->body);
  587. }
  588. TEST_F(ServerTest, GetMethodDir) {
  589. auto res = cli_.Get("/dir/");
  590. ASSERT_TRUE(res != nullptr);
  591. EXPECT_EQ(200, res->status);
  592. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  593. auto body = R"(<html>
  594. <head>
  595. </head>
  596. <body>
  597. <a href="/dir/test.html">Test</a>
  598. <a href="/hi">hi</a>
  599. </body>
  600. </html>
  601. )";
  602. EXPECT_EQ(body, res->body);
  603. }
  604. TEST_F(ServerTest, GetMethodDirTest) {
  605. auto res = cli_.Get("/dir/test.html");
  606. ASSERT_TRUE(res != nullptr);
  607. EXPECT_EQ(200, res->status);
  608. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  609. EXPECT_EQ("test.html", res->body);
  610. }
  611. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  612. auto res = cli_.Get("/dir/../dir/test.html");
  613. ASSERT_TRUE(res != nullptr);
  614. EXPECT_EQ(200, res->status);
  615. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  616. EXPECT_EQ("test.html", res->body);
  617. }
  618. TEST_F(ServerTest, GetMethodInvalidPath) {
  619. auto res = cli_.Get("/dir/../test.html");
  620. ASSERT_TRUE(res != nullptr);
  621. EXPECT_EQ(404, res->status);
  622. }
  623. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  624. auto res = cli_.Get("/../www/dir/test.html");
  625. ASSERT_TRUE(res != nullptr);
  626. EXPECT_EQ(404, res->status);
  627. }
  628. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  629. auto res = cli_.Get("/dir/../../www/dir/test.html");
  630. ASSERT_TRUE(res != nullptr);
  631. EXPECT_EQ(404, res->status);
  632. }
  633. TEST_F(ServerTest, InvalidBaseDir) {
  634. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  635. EXPECT_EQ(true, svr_.set_base_dir("."));
  636. }
  637. TEST_F(ServerTest, EmptyRequest) {
  638. auto res = cli_.Get("");
  639. ASSERT_TRUE(res == nullptr);
  640. }
  641. TEST_F(ServerTest, LongRequest) {
  642. auto res =
  643. cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  644. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  645. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  646. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  647. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  648. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  649. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  650. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  651. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  652. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  653. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  654. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  655. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  656. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  657. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  658. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  659. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  660. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  661. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  662. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  663. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  664. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  665. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  666. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  667. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  668. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  669. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  670. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  671. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  672. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  673. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  674. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  675. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  676. "TooLongRequest/TooLongRequest/TooLongRequest/__ok__");
  677. ASSERT_TRUE(res != nullptr);
  678. EXPECT_EQ(404, res->status);
  679. }
  680. TEST_F(ServerTest, TooLongRequest) {
  681. auto res =
  682. cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  683. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  684. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  685. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  686. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  687. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  688. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  689. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  690. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  691. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  692. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  693. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  694. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  695. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  696. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  697. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  698. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  699. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  700. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  701. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  702. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  703. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  704. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  705. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  706. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  707. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  708. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  709. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  710. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  711. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  712. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  713. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  714. "TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/"
  715. "TooLongRequest/TooLongRequest/TooLongRequest/__ng___");
  716. ASSERT_TRUE(res != nullptr);
  717. EXPECT_EQ(404, res->status);
  718. }
  719. TEST_F(ServerTest, LongHeader) {
  720. Request req;
  721. req.method = "GET";
  722. req.path = "/hi";
  723. std::string host_and_port;
  724. host_and_port += HOST;
  725. host_and_port += ":";
  726. host_and_port += std::to_string(PORT);
  727. req.headers.emplace("Host", host_and_port.c_str());
  728. req.headers.emplace("Accept", "*/*");
  729. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  730. req.headers.emplace(
  731. "Header-Name",
  732. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  733. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  734. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  735. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  736. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  737. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  738. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  739. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  740. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  741. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  742. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  743. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  744. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  745. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  746. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  747. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  748. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  749. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  750. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  751. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  752. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  753. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  754. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  755. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  756. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  757. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  758. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  759. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  760. "@@@@@@@@@@@@@@@@");
  761. auto res = std::make_shared<Response>();
  762. auto ret = cli_.send(req, *res);
  763. ASSERT_TRUE(ret);
  764. EXPECT_EQ(200, res->status);
  765. }
  766. TEST_F(ServerTest, LongQueryValue) {
  767. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  768. ASSERT_TRUE(res != nullptr);
  769. EXPECT_EQ(414, res->status);
  770. }
  771. TEST_F(ServerTest, TooLongHeader) {
  772. Request req;
  773. req.method = "GET";
  774. req.path = "/hi";
  775. std::string host_and_port;
  776. host_and_port += HOST;
  777. host_and_port += ":";
  778. host_and_port += std::to_string(PORT);
  779. req.headers.emplace("Host", host_and_port.c_str());
  780. req.headers.emplace("Accept", "*/*");
  781. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  782. req.headers.emplace(
  783. "Header-Name",
  784. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  785. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  786. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  787. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  788. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  789. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  790. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  791. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  792. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  793. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  794. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  795. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  796. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  797. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  798. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  799. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  800. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  801. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  802. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  803. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  804. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  805. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  806. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  807. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  808. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  809. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  810. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  811. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  812. "@@@@@@@@@@@@@@@@@");
  813. auto res = std::make_shared<Response>();
  814. auto ret = cli_.send(req, *res);
  815. ASSERT_TRUE(ret);
  816. EXPECT_EQ(200, res->status);
  817. }
  818. TEST_F(ServerTest, PercentEncoding) {
  819. auto res = cli_.Get("/e%6edwith%");
  820. ASSERT_TRUE(res != nullptr);
  821. EXPECT_EQ(200, res->status);
  822. }
  823. TEST_F(ServerTest, PercentEncodingUnicode) {
  824. auto res = cli_.Get("/e%u006edwith%");
  825. ASSERT_TRUE(res != nullptr);
  826. EXPECT_EQ(200, res->status);
  827. }
  828. TEST_F(ServerTest, InvalidPercentEncoding) {
  829. auto res = cli_.Get("/%endwith%");
  830. ASSERT_TRUE(res != nullptr);
  831. EXPECT_EQ(404, res->status);
  832. }
  833. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  834. auto res = cli_.Get("/%uendwith%");
  835. ASSERT_TRUE(res != nullptr);
  836. EXPECT_EQ(404, res->status);
  837. }
  838. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  839. auto res = cli_.Get("/hello?aaa=bbb%");
  840. ASSERT_TRUE(res != nullptr);
  841. EXPECT_EQ(404, res->status);
  842. }
  843. TEST_F(ServerTest, MultipartFormData) {
  844. Request req;
  845. req.method = "POST";
  846. req.path = "/multipart";
  847. std::string host_and_port;
  848. host_and_port += HOST;
  849. host_and_port += ":";
  850. host_and_port += std::to_string(PORT);
  851. req.headers.emplace("Host", host_and_port.c_str());
  852. req.headers.emplace("Accept", "*/*");
  853. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  854. req.headers.emplace(
  855. "Content-Type",
  856. "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4");
  857. req.body =
  858. "------WebKitFormBoundarysBREP3G013oUrLB4\r\n"
  859. "Content-Disposition: form-data; name=\"text1\"\r\n"
  860. "\r\n"
  861. "text default\r\n"
  862. "------WebKitFormBoundarysBREP3G013oUrLB4\r\n"
  863. "Content-Disposition: form-data; name=\"text2\"\r\n"
  864. "\r\n"
  865. "aωb\r\n"
  866. "------WebKitFormBoundarysBREP3G013oUrLB4\r\n"
  867. "Content-Disposition: form-data; name=\"file1\"; filename=\"hello.txt\"\r\n"
  868. "Content-Type: text/plain\r\n"
  869. "\r\n"
  870. "h\ne\n\nl\nl\no\n\r\n"
  871. "------WebKitFormBoundarysBREP3G013oUrLB4\r\n"
  872. "Content-Disposition: form-data; name=\"file2\"; filename=\"world.json\"\r\n"
  873. "Content-Type: application/json\r\n"
  874. "\r\n"
  875. "{\n \"world\", true\n}\n\r\n"
  876. "------WebKitFormBoundarysBREP3G013oUrLB4\r\n"
  877. "content-disposition: form-data; name=\"file3\"; filename=\"\"\r\n"
  878. "content-type: application/octet-stream\r\n"
  879. "\r\n"
  880. "\r\n"
  881. "------WebKitFormBoundarysBREP3G013oUrLB4--\r\n";
  882. auto res = std::make_shared<Response>();
  883. auto ret = cli_.send(req, *res);
  884. ASSERT_TRUE(ret);
  885. EXPECT_EQ(200, res->status);
  886. }
  887. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  888. auto res = cli_.Get("/hi");
  889. ASSERT_TRUE(res != nullptr);
  890. EXPECT_EQ(200, res->status);
  891. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  892. EXPECT_EQ("Hello World!", res->body);
  893. }
  894. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  895. Request req;
  896. req.method = "POST";
  897. req.path = "/chunked";
  898. std::string host_and_port;
  899. host_and_port += HOST;
  900. host_and_port += ":";
  901. host_and_port += std::to_string(PORT);
  902. req.headers.emplace("Host", host_and_port.c_str());
  903. req.headers.emplace("Accept", "*/*");
  904. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  905. req.headers.emplace("Content-Type", "text/plain");
  906. req.headers.emplace("Content-Length", "0");
  907. req.headers.emplace(
  908. "Transfer-Encoding",
  909. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  910. // Client does not chunk, so make a chunked body manually.
  911. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  912. auto res = std::make_shared<Response>();
  913. auto ret = cli_.send(req, *res);
  914. ASSERT_TRUE(ret);
  915. EXPECT_EQ(200, res->status);
  916. }
  917. TEST_F(ServerTest, GetStreamed) {
  918. auto res = cli_.Get("/streamed");
  919. ASSERT_TRUE(res != nullptr);
  920. EXPECT_EQ(200, res->status);
  921. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  922. EXPECT_TRUE(res->body == "aaabbb");
  923. }
  924. TEST_F(ServerTest, GetStreamedChunked) {
  925. auto res = cli_.Get("/streamedchunked");
  926. ASSERT_TRUE(res != nullptr);
  927. EXPECT_EQ(200, res->status);
  928. EXPECT_TRUE(res->body == "aaabbb");
  929. }
  930. TEST_F(ServerTest, LargeChunkedPost) {
  931. Request req;
  932. req.method = "POST";
  933. req.path = "/largechunked";
  934. std::string host_and_port;
  935. host_and_port += HOST;
  936. host_and_port += ":";
  937. host_and_port += std::to_string(PORT);
  938. req.headers.emplace("Host", host_and_port.c_str());
  939. req.headers.emplace("Accept", "*/*");
  940. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  941. req.headers.emplace("Content-Type", "text/plain");
  942. req.headers.emplace("Content-Length", "0");
  943. req.headers.emplace("Transfer-Encoding", "chunked");
  944. std::string long_string(30 * 1024u, 'a');
  945. std::string chunk = "7800\r\n" + long_string + "\r\n";
  946. // Attempt to make a large enough post to exceed OS buffers, to test that
  947. // the server handles short reads if the full chunk data isn't available.
  948. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  949. auto res = std::make_shared<Response>();
  950. auto ret = cli_.send(req, *res);
  951. ASSERT_TRUE(ret);
  952. EXPECT_EQ(200, res->status);
  953. }
  954. TEST_F(ServerTest, GetMethodRemoteAddr) {
  955. auto res = cli_.Get("/remote_addr");
  956. ASSERT_TRUE(res != nullptr);
  957. EXPECT_EQ(200, res->status);
  958. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  959. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  960. }
  961. TEST_F(ServerTest, SlowRequest) {
  962. request_threads_.push_back(
  963. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  964. request_threads_.push_back(
  965. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  966. request_threads_.push_back(
  967. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  968. msleep(100);
  969. }
  970. TEST_F(ServerTest, Put) {
  971. auto res = cli_.Put("/put", "PUT", "text/plain");
  972. ASSERT_TRUE(res != nullptr);
  973. EXPECT_EQ(200, res->status);
  974. EXPECT_EQ("PUT", res->body);
  975. }
  976. TEST_F(ServerTest, Patch) {
  977. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  978. ASSERT_TRUE(res != nullptr);
  979. EXPECT_EQ(200, res->status);
  980. EXPECT_EQ("PATCH", res->body);
  981. }
  982. TEST_F(ServerTest, Delete) {
  983. auto res = cli_.Delete("/delete");
  984. ASSERT_TRUE(res != nullptr);
  985. EXPECT_EQ(200, res->status);
  986. EXPECT_EQ("DELETE", res->body);
  987. }
  988. TEST_F(ServerTest, Options) {
  989. auto res = cli_.Options("*");
  990. ASSERT_TRUE(res != nullptr);
  991. EXPECT_EQ(200, res->status);
  992. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  993. EXPECT_TRUE(res->body.empty());
  994. }
  995. TEST_F(ServerTest, URL) {
  996. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  997. ASSERT_TRUE(res != nullptr);
  998. EXPECT_EQ(200, res->status);
  999. }
  1000. TEST_F(ServerTest, ArrayParam) {
  1001. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  1002. ASSERT_TRUE(res != nullptr);
  1003. EXPECT_EQ(200, res->status);
  1004. }
  1005. TEST_F(ServerTest, NoMultipleHeaders) {
  1006. Headers headers;
  1007. headers.emplace("Content-Length", "5");
  1008. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1009. "text/plain");
  1010. ASSERT_TRUE(res != nullptr);
  1011. EXPECT_EQ(200, res->status);
  1012. }
  1013. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1014. TEST_F(ServerTest, Gzip) {
  1015. Headers headers;
  1016. headers.emplace("Accept-Encoding", "gzip, deflate");
  1017. auto res = cli_.Get("/gzip", headers);
  1018. ASSERT_TRUE(res != nullptr);
  1019. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1020. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1021. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1022. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1023. "7890123456789012345678901234567890",
  1024. res->body);
  1025. EXPECT_EQ(200, res->status);
  1026. }
  1027. TEST_F(ServerTest, GzipWithContentReceiver) {
  1028. Headers headers;
  1029. headers.emplace("Accept-Encoding", "gzip, deflate");
  1030. std::string body;
  1031. auto res = cli_.Get("/gzip", headers, [&](const char *data, size_t len) {
  1032. body.append(data, len);
  1033. });
  1034. ASSERT_TRUE(res != nullptr);
  1035. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1036. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1037. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1038. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1039. "7890123456789012345678901234567890",
  1040. body);
  1041. EXPECT_EQ(200, res->status);
  1042. }
  1043. TEST_F(ServerTest, NoGzip) {
  1044. Headers headers;
  1045. headers.emplace("Accept-Encoding", "gzip, deflate");
  1046. auto res = cli_.Get("/nogzip", headers);
  1047. ASSERT_TRUE(res != nullptr);
  1048. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1049. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1050. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1051. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1052. "7890123456789012345678901234567890",
  1053. res->body);
  1054. EXPECT_EQ(200, res->status);
  1055. }
  1056. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1057. Headers headers;
  1058. headers.emplace("Accept-Encoding", "gzip, deflate");
  1059. std::string body;
  1060. auto res = cli_.Get("/nogzip", headers, [&](const char *data, size_t len) {
  1061. body.append(data, len);
  1062. });
  1063. ASSERT_TRUE(res != nullptr);
  1064. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1065. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1066. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1067. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1068. "7890123456789012345678901234567890",
  1069. body);
  1070. EXPECT_EQ(200, res->status);
  1071. }
  1072. TEST_F(ServerTest, MultipartFormDataGzip) {
  1073. Request req;
  1074. req.method = "POST";
  1075. req.path = "/gzipmultipart";
  1076. std::string host_and_port;
  1077. host_and_port += HOST;
  1078. host_and_port += ":";
  1079. host_and_port += std::to_string(PORT);
  1080. req.headers.emplace("Host", host_and_port.c_str());
  1081. req.headers.emplace("Accept", "*/*");
  1082. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1083. req.headers.emplace(
  1084. "Content-Type",
  1085. "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  1086. req.headers.emplace("Content-Encoding", "gzip");
  1087. // compressed_body generated by creating input.txt to this file:
  1088. /*
  1089. --------------------------fcba8368a9f48c0f
  1090. Content-Disposition: form-data; name="key1"
  1091. test
  1092. --------------------------fcba8368a9f48c0f
  1093. Content-Disposition: form-data; name="key2"
  1094. --abcdefg123
  1095. --------------------------fcba8368a9f48c0f--
  1096. */
  1097. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  1098. uint8_t compressed_body[] = {
  1099. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  1100. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  1101. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  1102. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  1103. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  1104. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  1105. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  1106. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  1107. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  1108. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  1109. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00};
  1110. req.body = std::string((char *)compressed_body,
  1111. sizeof(compressed_body) / sizeof(compressed_body[0]));
  1112. auto res = std::make_shared<Response>();
  1113. auto ret = cli_.send(req, *res);
  1114. ASSERT_TRUE(ret);
  1115. EXPECT_EQ(200, res->status);
  1116. }
  1117. #endif
  1118. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1119. protected:
  1120. ServerTestWithAI_PASSIVE()
  1121. : cli_(HOST, PORT)
  1122. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1123. ,
  1124. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1125. #endif
  1126. {
  1127. }
  1128. virtual void SetUp() {
  1129. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1130. res.set_content("Hello World!", "text/plain");
  1131. });
  1132. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1133. while (!svr_.is_running()) {
  1134. msleep(1);
  1135. }
  1136. }
  1137. virtual void TearDown() {
  1138. svr_.stop();
  1139. t_.join();
  1140. }
  1141. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1142. SSLClient cli_;
  1143. SSLServer svr_;
  1144. #else
  1145. Client cli_;
  1146. Server svr_;
  1147. #endif
  1148. thread t_;
  1149. };
  1150. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1151. auto res = cli_.Get("/hi");
  1152. ASSERT_TRUE(res != nullptr);
  1153. EXPECT_EQ(200, res->status);
  1154. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1155. EXPECT_EQ("Hello World!", res->body);
  1156. }
  1157. class ServerUpDownTest : public ::testing::Test {
  1158. protected:
  1159. ServerUpDownTest() : cli_(HOST, PORT) {}
  1160. virtual void SetUp() {
  1161. t_ = thread([&]() {
  1162. svr_.bind_to_any_port(HOST);
  1163. msleep(500);
  1164. ASSERT_TRUE(svr_.listen_after_bind());
  1165. });
  1166. while (!svr_.is_running()) {
  1167. msleep(1);
  1168. }
  1169. }
  1170. virtual void TearDown() {
  1171. svr_.stop();
  1172. t_.join();
  1173. }
  1174. Client cli_;
  1175. Server svr_;
  1176. thread t_;
  1177. };
  1178. TEST_F(ServerUpDownTest, QuickStartStop) {
  1179. // Should not crash, especially when run with
  1180. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1181. }
  1182. class PayloadMaxLengthTest : public ::testing::Test {
  1183. protected:
  1184. PayloadMaxLengthTest()
  1185. : cli_(HOST, PORT)
  1186. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1187. ,
  1188. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1189. #endif
  1190. {
  1191. }
  1192. virtual void SetUp() {
  1193. svr_.set_payload_max_length(8);
  1194. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1195. res.set_content("test", "text/plain");
  1196. });
  1197. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1198. while (!svr_.is_running()) {
  1199. msleep(1);
  1200. }
  1201. }
  1202. virtual void TearDown() {
  1203. svr_.stop();
  1204. t_.join();
  1205. }
  1206. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1207. SSLClient cli_;
  1208. SSLServer svr_;
  1209. #else
  1210. Client cli_;
  1211. Server svr_;
  1212. #endif
  1213. thread t_;
  1214. };
  1215. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1216. auto res = cli_.Post("/test", "123456789", "text/plain");
  1217. ASSERT_TRUE(res != nullptr);
  1218. EXPECT_EQ(413, res->status);
  1219. res = cli_.Post("/test", "12345678", "text/plain");
  1220. ASSERT_TRUE(res != nullptr);
  1221. EXPECT_EQ(200, res->status);
  1222. }
  1223. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1224. TEST(SSLClientTest, ServerNameIndication) {
  1225. SSLClient cli("httpbin.org", 443);
  1226. auto res = cli.Get("/get");
  1227. ASSERT_TRUE(res != nullptr);
  1228. ASSERT_EQ(200, res->status);
  1229. }
  1230. TEST(SSLClientTest, ServerCertificateVerification) {
  1231. SSLClient cli("google.com");
  1232. auto res = cli.Get("/");
  1233. ASSERT_TRUE(res != nullptr);
  1234. ASSERT_EQ(301, res->status);
  1235. cli.enable_server_certificate_verification(true);
  1236. res = cli.Get("/");
  1237. ASSERT_TRUE(res == nullptr);
  1238. cli.set_ca_cert_path(CA_CERT_FILE);
  1239. res = cli.Get("/");
  1240. ASSERT_TRUE(res != nullptr);
  1241. ASSERT_EQ(301, res->status);
  1242. }
  1243. TEST(SSLClientTest, WildcardHostNameMatch) {
  1244. SSLClient cli("www.youtube.com");
  1245. cli.set_ca_cert_path(CA_CERT_FILE);
  1246. cli.enable_server_certificate_verification(true);
  1247. auto res = cli.Get("/");
  1248. ASSERT_TRUE(res != nullptr);
  1249. ASSERT_EQ(200, res->status);
  1250. }
  1251. TEST(SSLClientServerTest, ClientCertPresent) {
  1252. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1253. CLIENT_CA_CERT_DIR);
  1254. ASSERT_TRUE(svr.is_valid());
  1255. svr.Get("/test", [&](const Request &req, Response &res) {
  1256. res.set_content("test", "text/plain");
  1257. svr.stop();
  1258. ASSERT_TRUE(true);
  1259. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1260. ASSERT_TRUE(peer_cert != nullptr);
  1261. auto subject_name = X509_get_subject_name(peer_cert);
  1262. ASSERT_TRUE(subject_name != nullptr);
  1263. std::string common_name;
  1264. {
  1265. char name[BUFSIZ];
  1266. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1267. name, sizeof(name));
  1268. common_name.assign(name, name_len);
  1269. }
  1270. EXPECT_EQ("Common Name", common_name);
  1271. X509_free(peer_cert);
  1272. });
  1273. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1274. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1275. CLIENT_PRIVATE_KEY_FILE);
  1276. auto res = cli.Get("/test");
  1277. ASSERT_TRUE(res != nullptr);
  1278. ASSERT_EQ(200, res->status);
  1279. t.join();
  1280. }
  1281. TEST(SSLClientServerTest, ClientCertMissing) {
  1282. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1283. CLIENT_CA_CERT_DIR);
  1284. ASSERT_TRUE(svr.is_valid());
  1285. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1286. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1287. httplib::SSLClient cli(HOST, PORT, 30);
  1288. auto res = cli.Get("/test");
  1289. ASSERT_TRUE(res == nullptr);
  1290. svr.stop();
  1291. t.join();
  1292. }
  1293. TEST(SSLClientServerTest, TrustDirOptional) {
  1294. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1295. ASSERT_TRUE(svr.is_valid());
  1296. svr.Get("/test", [&](const Request &, Response &res) {
  1297. res.set_content("test", "text/plain");
  1298. svr.stop();
  1299. });
  1300. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1301. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1302. CLIENT_PRIVATE_KEY_FILE);
  1303. auto res = cli.Get("/test");
  1304. ASSERT_TRUE(res != nullptr);
  1305. ASSERT_EQ(200, res->status);
  1306. t.join();
  1307. }
  1308. /* Cannot test this case as there is no external access to SSL object to check
  1309. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1310. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1311. nullptr, CLIENT_CA_CERT_DIR);
  1312. }
  1313. */
  1314. #endif
  1315. #ifdef _WIN32
  1316. TEST(CleanupTest, WSACleanup) {
  1317. int ret = WSACleanup();
  1318. ASSERT_EQ(0, ret);
  1319. }
  1320. #endif