test.cc 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  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\nContent-Disposition: "
  859. "form-data; name=\"text1\"\r\n\r\ntext "
  860. "default\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-"
  861. "Disposition: form-data; "
  862. "name=\"text2\"\r\n\r\naωb\r\n------"
  863. "WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; "
  864. "name=\"file1\"; filename=\"hello.txt\"\r\nContent-Type: "
  865. "text/"
  866. "plain\r\n\r\nh\ne\n\nl\nl\no\n\r\n------"
  867. "WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; "
  868. "name=\"file2\"; filename=\"world.json\"\r\nContent-Type: "
  869. "application/json\r\n\r\n{\n \"world\", "
  870. "true\n}\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\ncontent-"
  871. "disposition: form-data; name=\"file3\"; filename=\"\"\r\ncontent-type: "
  872. "application/"
  873. "octet-stream\r\n\r\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4--\r\n";
  874. auto res = std::make_shared<Response>();
  875. auto ret = cli_.send(req, *res);
  876. ASSERT_TRUE(ret);
  877. EXPECT_EQ(200, res->status);
  878. }
  879. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  880. auto res = cli_.Get("/hi");
  881. ASSERT_TRUE(res != nullptr);
  882. EXPECT_EQ(200, res->status);
  883. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  884. EXPECT_EQ("Hello World!", res->body);
  885. }
  886. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  887. Request req;
  888. req.method = "POST";
  889. req.path = "/chunked";
  890. std::string host_and_port;
  891. host_and_port += HOST;
  892. host_and_port += ":";
  893. host_and_port += std::to_string(PORT);
  894. req.headers.emplace("Host", host_and_port.c_str());
  895. req.headers.emplace("Accept", "*/*");
  896. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  897. req.headers.emplace("Content-Type", "text/plain");
  898. req.headers.emplace("Content-Length", "0");
  899. req.headers.emplace(
  900. "Transfer-Encoding",
  901. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  902. // Client does not chunk, so make a chunked body manually.
  903. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  904. auto res = std::make_shared<Response>();
  905. auto ret = cli_.send(req, *res);
  906. ASSERT_TRUE(ret);
  907. EXPECT_EQ(200, res->status);
  908. }
  909. TEST_F(ServerTest, GetStreamed) {
  910. auto res = cli_.Get("/streamed");
  911. ASSERT_TRUE(res != nullptr);
  912. EXPECT_EQ(200, res->status);
  913. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  914. EXPECT_TRUE(res->body == "aaabbb");
  915. }
  916. TEST_F(ServerTest, GetStreamedChunked) {
  917. auto res = cli_.Get("/streamedchunked");
  918. ASSERT_TRUE(res != nullptr);
  919. EXPECT_EQ(200, res->status);
  920. EXPECT_TRUE(res->body == "aaabbb");
  921. }
  922. TEST_F(ServerTest, LargeChunkedPost) {
  923. Request req;
  924. req.method = "POST";
  925. req.path = "/largechunked";
  926. std::string host_and_port;
  927. host_and_port += HOST;
  928. host_and_port += ":";
  929. host_and_port += std::to_string(PORT);
  930. req.headers.emplace("Host", host_and_port.c_str());
  931. req.headers.emplace("Accept", "*/*");
  932. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  933. req.headers.emplace("Content-Type", "text/plain");
  934. req.headers.emplace("Content-Length", "0");
  935. req.headers.emplace("Transfer-Encoding", "chunked");
  936. std::string long_string(30 * 1024u, 'a');
  937. std::string chunk = "7800\r\n" + long_string + "\r\n";
  938. // Attempt to make a large enough post to exceed OS buffers, to test that
  939. // the server handles short reads if the full chunk data isn't available.
  940. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  941. auto res = std::make_shared<Response>();
  942. auto ret = cli_.send(req, *res);
  943. ASSERT_TRUE(ret);
  944. EXPECT_EQ(200, res->status);
  945. }
  946. TEST_F(ServerTest, GetMethodRemoteAddr) {
  947. auto res = cli_.Get("/remote_addr");
  948. ASSERT_TRUE(res != nullptr);
  949. EXPECT_EQ(200, res->status);
  950. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  951. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  952. }
  953. TEST_F(ServerTest, SlowRequest) {
  954. request_threads_.push_back(
  955. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  956. request_threads_.push_back(
  957. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  958. request_threads_.push_back(
  959. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  960. msleep(100);
  961. }
  962. TEST_F(ServerTest, Put) {
  963. auto res = cli_.Put("/put", "PUT", "text/plain");
  964. ASSERT_TRUE(res != nullptr);
  965. EXPECT_EQ(200, res->status);
  966. EXPECT_EQ("PUT", res->body);
  967. }
  968. TEST_F(ServerTest, Patch) {
  969. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  970. ASSERT_TRUE(res != nullptr);
  971. EXPECT_EQ(200, res->status);
  972. EXPECT_EQ("PATCH", res->body);
  973. }
  974. TEST_F(ServerTest, Delete) {
  975. auto res = cli_.Delete("/delete");
  976. ASSERT_TRUE(res != nullptr);
  977. EXPECT_EQ(200, res->status);
  978. EXPECT_EQ("DELETE", res->body);
  979. }
  980. TEST_F(ServerTest, Options) {
  981. auto res = cli_.Options("*");
  982. ASSERT_TRUE(res != nullptr);
  983. EXPECT_EQ(200, res->status);
  984. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  985. EXPECT_TRUE(res->body.empty());
  986. }
  987. TEST_F(ServerTest, URL) {
  988. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  989. ASSERT_TRUE(res != nullptr);
  990. EXPECT_EQ(200, res->status);
  991. }
  992. TEST_F(ServerTest, ArrayParam) {
  993. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  994. ASSERT_TRUE(res != nullptr);
  995. EXPECT_EQ(200, res->status);
  996. }
  997. TEST_F(ServerTest, NoMultipleHeaders) {
  998. Headers headers;
  999. headers.emplace("Content-Length", "5");
  1000. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1001. "text/plain");
  1002. ASSERT_TRUE(res != nullptr);
  1003. EXPECT_EQ(200, res->status);
  1004. }
  1005. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1006. TEST_F(ServerTest, Gzip) {
  1007. Headers headers;
  1008. headers.emplace("Accept-Encoding", "gzip, deflate");
  1009. auto res = cli_.Get("/gzip", headers);
  1010. ASSERT_TRUE(res != nullptr);
  1011. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1012. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1013. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1014. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1015. "7890123456789012345678901234567890",
  1016. res->body);
  1017. EXPECT_EQ(200, res->status);
  1018. }
  1019. TEST_F(ServerTest, GzipWithContentReceiver) {
  1020. Headers headers;
  1021. headers.emplace("Accept-Encoding", "gzip, deflate");
  1022. std::string body;
  1023. auto res = cli_.Get("/gzip", headers, [&](const char *data, size_t len) {
  1024. body.append(data, len);
  1025. });
  1026. ASSERT_TRUE(res != nullptr);
  1027. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1028. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1029. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1030. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1031. "7890123456789012345678901234567890",
  1032. body);
  1033. EXPECT_EQ(200, res->status);
  1034. }
  1035. TEST_F(ServerTest, NoGzip) {
  1036. Headers headers;
  1037. headers.emplace("Accept-Encoding", "gzip, deflate");
  1038. auto res = cli_.Get("/nogzip", headers);
  1039. ASSERT_TRUE(res != nullptr);
  1040. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1041. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1042. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1043. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1044. "7890123456789012345678901234567890",
  1045. res->body);
  1046. EXPECT_EQ(200, res->status);
  1047. }
  1048. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1049. Headers headers;
  1050. headers.emplace("Accept-Encoding", "gzip, deflate");
  1051. std::string body;
  1052. auto res = cli_.Get("/nogzip", headers, [&](const char *data, size_t len) {
  1053. body.append(data, len);
  1054. });
  1055. ASSERT_TRUE(res != nullptr);
  1056. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1057. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1058. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1059. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1060. "7890123456789012345678901234567890",
  1061. body);
  1062. EXPECT_EQ(200, res->status);
  1063. }
  1064. TEST_F(ServerTest, MultipartFormDataGzip) {
  1065. Request req;
  1066. req.method = "POST";
  1067. req.path = "/gzipmultipart";
  1068. std::string host_and_port;
  1069. host_and_port += HOST;
  1070. host_and_port += ":";
  1071. host_and_port += std::to_string(PORT);
  1072. req.headers.emplace("Host", host_and_port.c_str());
  1073. req.headers.emplace("Accept", "*/*");
  1074. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1075. req.headers.emplace(
  1076. "Content-Type",
  1077. "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  1078. req.headers.emplace("Content-Encoding", "gzip");
  1079. // compressed_body generated by creating input.txt to this file:
  1080. /*
  1081. --------------------------fcba8368a9f48c0f
  1082. Content-Disposition: form-data; name="key1"
  1083. test
  1084. --------------------------fcba8368a9f48c0f
  1085. Content-Disposition: form-data; name="key2"
  1086. --abcdefg123
  1087. --------------------------fcba8368a9f48c0f--
  1088. */
  1089. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  1090. uint8_t compressed_body[] = {
  1091. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  1092. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  1093. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  1094. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  1095. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  1096. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  1097. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  1098. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  1099. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  1100. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  1101. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00};
  1102. req.body = std::string((char *)compressed_body,
  1103. sizeof(compressed_body) / sizeof(compressed_body[0]));
  1104. auto res = std::make_shared<Response>();
  1105. auto ret = cli_.send(req, *res);
  1106. ASSERT_TRUE(ret);
  1107. EXPECT_EQ(200, res->status);
  1108. }
  1109. #endif
  1110. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1111. protected:
  1112. ServerTestWithAI_PASSIVE()
  1113. : cli_(HOST, PORT)
  1114. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1115. ,
  1116. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1117. #endif
  1118. {
  1119. }
  1120. virtual void SetUp() {
  1121. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1122. res.set_content("Hello World!", "text/plain");
  1123. });
  1124. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1125. while (!svr_.is_running()) {
  1126. msleep(1);
  1127. }
  1128. }
  1129. virtual void TearDown() {
  1130. svr_.stop();
  1131. t_.join();
  1132. }
  1133. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1134. SSLClient cli_;
  1135. SSLServer svr_;
  1136. #else
  1137. Client cli_;
  1138. Server svr_;
  1139. #endif
  1140. thread t_;
  1141. };
  1142. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1143. auto res = cli_.Get("/hi");
  1144. ASSERT_TRUE(res != nullptr);
  1145. EXPECT_EQ(200, res->status);
  1146. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1147. EXPECT_EQ("Hello World!", res->body);
  1148. }
  1149. class ServerUpDownTest : public ::testing::Test {
  1150. protected:
  1151. ServerUpDownTest() : cli_(HOST, PORT) {}
  1152. virtual void SetUp() {
  1153. t_ = thread([&]() {
  1154. svr_.bind_to_any_port(HOST);
  1155. msleep(500);
  1156. ASSERT_TRUE(svr_.listen_after_bind());
  1157. });
  1158. while (!svr_.is_running()) {
  1159. msleep(1);
  1160. }
  1161. }
  1162. virtual void TearDown() {
  1163. svr_.stop();
  1164. t_.join();
  1165. }
  1166. Client cli_;
  1167. Server svr_;
  1168. thread t_;
  1169. };
  1170. TEST_F(ServerUpDownTest, QuickStartStop) {
  1171. // Should not crash, especially when run with
  1172. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1173. }
  1174. class PayloadMaxLengthTest : public ::testing::Test {
  1175. protected:
  1176. PayloadMaxLengthTest()
  1177. : cli_(HOST, PORT)
  1178. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1179. ,
  1180. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1181. #endif
  1182. {
  1183. }
  1184. virtual void SetUp() {
  1185. svr_.set_payload_max_length(8);
  1186. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1187. res.set_content("test", "text/plain");
  1188. });
  1189. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1190. while (!svr_.is_running()) {
  1191. msleep(1);
  1192. }
  1193. }
  1194. virtual void TearDown() {
  1195. svr_.stop();
  1196. t_.join();
  1197. }
  1198. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1199. SSLClient cli_;
  1200. SSLServer svr_;
  1201. #else
  1202. Client cli_;
  1203. Server svr_;
  1204. #endif
  1205. thread t_;
  1206. };
  1207. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1208. auto res = cli_.Post("/test", "123456789", "text/plain");
  1209. ASSERT_TRUE(res != nullptr);
  1210. EXPECT_EQ(413, res->status);
  1211. res = cli_.Post("/test", "12345678", "text/plain");
  1212. ASSERT_TRUE(res != nullptr);
  1213. EXPECT_EQ(200, res->status);
  1214. }
  1215. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1216. TEST(SSLClientTest, ServerNameIndication) {
  1217. SSLClient cli("httpbin.org", 443);
  1218. auto res = cli.Get("/get");
  1219. ASSERT_TRUE(res != nullptr);
  1220. ASSERT_EQ(200, res->status);
  1221. }
  1222. TEST(SSLClientTest, ServerCertificateVerification) {
  1223. SSLClient cli("google.com");
  1224. auto res = cli.Get("/");
  1225. ASSERT_TRUE(res != nullptr);
  1226. ASSERT_EQ(301, res->status);
  1227. cli.enable_server_certificate_verification(true);
  1228. res = cli.Get("/");
  1229. ASSERT_TRUE(res == nullptr);
  1230. cli.set_ca_cert_path(CA_CERT_FILE);
  1231. res = cli.Get("/");
  1232. ASSERT_TRUE(res != nullptr);
  1233. ASSERT_EQ(301, res->status);
  1234. }
  1235. TEST(SSLClientTest, WildcardHostNameMatch) {
  1236. SSLClient cli("www.youtube.com");
  1237. cli.set_ca_cert_path(CA_CERT_FILE);
  1238. cli.enable_server_certificate_verification(true);
  1239. auto res = cli.Get("/");
  1240. ASSERT_TRUE(res != nullptr);
  1241. ASSERT_EQ(200, res->status);
  1242. }
  1243. TEST(SSLClientServerTest, ClientCertPresent) {
  1244. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1245. CLIENT_CA_CERT_DIR);
  1246. ASSERT_TRUE(svr.is_valid());
  1247. svr.Get("/test", [&](const Request &req, Response &res) {
  1248. res.set_content("test", "text/plain");
  1249. svr.stop();
  1250. ASSERT_TRUE(true);
  1251. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1252. ASSERT_TRUE(peer_cert != nullptr);
  1253. auto subject_name = X509_get_subject_name(peer_cert);
  1254. ASSERT_TRUE(subject_name != nullptr);
  1255. std::string common_name;
  1256. {
  1257. char name[BUFSIZ];
  1258. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1259. name, sizeof(name));
  1260. common_name.assign(name, name_len);
  1261. }
  1262. EXPECT_EQ("Common Name", common_name);
  1263. X509_free(peer_cert);
  1264. });
  1265. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1266. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1267. CLIENT_PRIVATE_KEY_FILE);
  1268. auto res = cli.Get("/test");
  1269. ASSERT_TRUE(res != nullptr);
  1270. ASSERT_EQ(200, res->status);
  1271. t.join();
  1272. }
  1273. TEST(SSLClientServerTest, ClientCertMissing) {
  1274. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1275. CLIENT_CA_CERT_DIR);
  1276. ASSERT_TRUE(svr.is_valid());
  1277. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1278. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1279. httplib::SSLClient cli(HOST, PORT, 30);
  1280. auto res = cli.Get("/test");
  1281. ASSERT_TRUE(res == nullptr);
  1282. svr.stop();
  1283. t.join();
  1284. }
  1285. TEST(SSLClientServerTest, TrustDirOptional) {
  1286. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1287. ASSERT_TRUE(svr.is_valid());
  1288. svr.Get("/test", [&](const Request &, Response &res) {
  1289. res.set_content("test", "text/plain");
  1290. svr.stop();
  1291. });
  1292. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1293. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1294. CLIENT_PRIVATE_KEY_FILE);
  1295. auto res = cli.Get("/test");
  1296. ASSERT_TRUE(res != nullptr);
  1297. ASSERT_EQ(200, res->status);
  1298. t.join();
  1299. }
  1300. /* Cannot test this case as there is no external access to SSL object to check
  1301. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1302. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1303. nullptr, CLIENT_CA_CERT_DIR);
  1304. }
  1305. */
  1306. #endif
  1307. #ifdef _WIN32
  1308. TEST(CleanupTest, WSACleanup) {
  1309. int ret = WSACleanup();
  1310. ASSERT_EQ(0, ret);
  1311. }
  1312. #endif