test.cc 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. { "Content-Length", "5" }
  1008. };
  1009. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1010. "text/plain");
  1011. ASSERT_TRUE(res != nullptr);
  1012. EXPECT_EQ(200, res->status);
  1013. }
  1014. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1015. TEST_F(ServerTest, Gzip) {
  1016. Headers headers;
  1017. headers.emplace("Accept-Encoding", "gzip, deflate");
  1018. auto res = cli_.Get("/gzip", headers);
  1019. ASSERT_TRUE(res != nullptr);
  1020. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1021. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1022. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1023. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1024. "7890123456789012345678901234567890",
  1025. res->body);
  1026. EXPECT_EQ(200, res->status);
  1027. }
  1028. TEST_F(ServerTest, GzipWithContentReceiver) {
  1029. Headers headers;
  1030. headers.emplace("Accept-Encoding", "gzip, deflate");
  1031. std::string body;
  1032. auto res = cli_.Get("/gzip", headers, [&](const char *data, size_t len) {
  1033. body.append(data, len);
  1034. });
  1035. ASSERT_TRUE(res != nullptr);
  1036. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1037. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1038. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1039. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1040. "7890123456789012345678901234567890",
  1041. body);
  1042. EXPECT_EQ(200, res->status);
  1043. }
  1044. TEST_F(ServerTest, NoGzip) {
  1045. Headers headers;
  1046. headers.emplace("Accept-Encoding", "gzip, deflate");
  1047. auto res = cli_.Get("/nogzip", headers);
  1048. ASSERT_TRUE(res != nullptr);
  1049. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1050. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1051. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1052. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1053. "7890123456789012345678901234567890",
  1054. res->body);
  1055. EXPECT_EQ(200, res->status);
  1056. }
  1057. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1058. Headers headers;
  1059. headers.emplace("Accept-Encoding", "gzip, deflate");
  1060. std::string body;
  1061. auto res = cli_.Get("/nogzip", headers, [&](const char *data, size_t len) {
  1062. body.append(data, len);
  1063. });
  1064. ASSERT_TRUE(res != nullptr);
  1065. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1066. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1067. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1068. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1069. "7890123456789012345678901234567890",
  1070. body);
  1071. EXPECT_EQ(200, res->status);
  1072. }
  1073. TEST_F(ServerTest, MultipartFormDataGzip) {
  1074. Request req;
  1075. req.method = "POST";
  1076. req.path = "/gzipmultipart";
  1077. std::string host_and_port;
  1078. host_and_port += HOST;
  1079. host_and_port += ":";
  1080. host_and_port += std::to_string(PORT);
  1081. req.headers.emplace("Host", host_and_port.c_str());
  1082. req.headers.emplace("Accept", "*/*");
  1083. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1084. req.headers.emplace(
  1085. "Content-Type",
  1086. "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  1087. req.headers.emplace("Content-Encoding", "gzip");
  1088. // compressed_body generated by creating input.txt to this file:
  1089. /*
  1090. --------------------------fcba8368a9f48c0f
  1091. Content-Disposition: form-data; name="key1"
  1092. test
  1093. --------------------------fcba8368a9f48c0f
  1094. Content-Disposition: form-data; name="key2"
  1095. --abcdefg123
  1096. --------------------------fcba8368a9f48c0f--
  1097. */
  1098. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  1099. uint8_t compressed_body[] = {
  1100. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  1101. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  1102. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  1103. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  1104. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  1105. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  1106. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  1107. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  1108. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  1109. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  1110. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00};
  1111. req.body = std::string((char *)compressed_body,
  1112. sizeof(compressed_body) / sizeof(compressed_body[0]));
  1113. auto res = std::make_shared<Response>();
  1114. auto ret = cli_.send(req, *res);
  1115. ASSERT_TRUE(ret);
  1116. EXPECT_EQ(200, res->status);
  1117. }
  1118. #endif
  1119. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1120. protected:
  1121. ServerTestWithAI_PASSIVE()
  1122. : cli_(HOST, PORT)
  1123. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1124. ,
  1125. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1126. #endif
  1127. {
  1128. }
  1129. virtual void SetUp() {
  1130. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1131. res.set_content("Hello World!", "text/plain");
  1132. });
  1133. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1134. while (!svr_.is_running()) {
  1135. msleep(1);
  1136. }
  1137. }
  1138. virtual void TearDown() {
  1139. svr_.stop();
  1140. t_.join();
  1141. }
  1142. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1143. SSLClient cli_;
  1144. SSLServer svr_;
  1145. #else
  1146. Client cli_;
  1147. Server svr_;
  1148. #endif
  1149. thread t_;
  1150. };
  1151. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1152. auto res = cli_.Get("/hi");
  1153. ASSERT_TRUE(res != nullptr);
  1154. EXPECT_EQ(200, res->status);
  1155. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1156. EXPECT_EQ("Hello World!", res->body);
  1157. }
  1158. class ServerUpDownTest : public ::testing::Test {
  1159. protected:
  1160. ServerUpDownTest() : cli_(HOST, PORT) {}
  1161. virtual void SetUp() {
  1162. t_ = thread([&]() {
  1163. svr_.bind_to_any_port(HOST);
  1164. msleep(500);
  1165. ASSERT_TRUE(svr_.listen_after_bind());
  1166. });
  1167. while (!svr_.is_running()) {
  1168. msleep(1);
  1169. }
  1170. }
  1171. virtual void TearDown() {
  1172. svr_.stop();
  1173. t_.join();
  1174. }
  1175. Client cli_;
  1176. Server svr_;
  1177. thread t_;
  1178. };
  1179. TEST_F(ServerUpDownTest, QuickStartStop) {
  1180. // Should not crash, especially when run with
  1181. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1182. }
  1183. class PayloadMaxLengthTest : public ::testing::Test {
  1184. protected:
  1185. PayloadMaxLengthTest()
  1186. : cli_(HOST, PORT)
  1187. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1188. ,
  1189. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1190. #endif
  1191. {
  1192. }
  1193. virtual void SetUp() {
  1194. svr_.set_payload_max_length(8);
  1195. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1196. res.set_content("test", "text/plain");
  1197. });
  1198. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1199. while (!svr_.is_running()) {
  1200. msleep(1);
  1201. }
  1202. }
  1203. virtual void TearDown() {
  1204. svr_.stop();
  1205. t_.join();
  1206. }
  1207. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1208. SSLClient cli_;
  1209. SSLServer svr_;
  1210. #else
  1211. Client cli_;
  1212. Server svr_;
  1213. #endif
  1214. thread t_;
  1215. };
  1216. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1217. auto res = cli_.Post("/test", "123456789", "text/plain");
  1218. ASSERT_TRUE(res != nullptr);
  1219. EXPECT_EQ(413, res->status);
  1220. res = cli_.Post("/test", "12345678", "text/plain");
  1221. ASSERT_TRUE(res != nullptr);
  1222. EXPECT_EQ(200, res->status);
  1223. }
  1224. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1225. TEST(SSLClientTest, ServerNameIndication) {
  1226. SSLClient cli("httpbin.org", 443);
  1227. auto res = cli.Get("/get");
  1228. ASSERT_TRUE(res != nullptr);
  1229. ASSERT_EQ(200, res->status);
  1230. }
  1231. TEST(SSLClientTest, ServerCertificateVerification) {
  1232. SSLClient cli("google.com");
  1233. auto res = cli.Get("/");
  1234. ASSERT_TRUE(res != nullptr);
  1235. ASSERT_EQ(301, res->status);
  1236. cli.enable_server_certificate_verification(true);
  1237. res = cli.Get("/");
  1238. ASSERT_TRUE(res == nullptr);
  1239. cli.set_ca_cert_path(CA_CERT_FILE);
  1240. res = cli.Get("/");
  1241. ASSERT_TRUE(res != nullptr);
  1242. ASSERT_EQ(301, res->status);
  1243. }
  1244. TEST(SSLClientTest, WildcardHostNameMatch) {
  1245. SSLClient cli("www.youtube.com");
  1246. cli.set_ca_cert_path(CA_CERT_FILE);
  1247. cli.enable_server_certificate_verification(true);
  1248. auto res = cli.Get("/");
  1249. ASSERT_TRUE(res != nullptr);
  1250. ASSERT_EQ(200, res->status);
  1251. }
  1252. TEST(SSLClientServerTest, ClientCertPresent) {
  1253. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1254. CLIENT_CA_CERT_DIR);
  1255. ASSERT_TRUE(svr.is_valid());
  1256. svr.Get("/test", [&](const Request &req, Response &res) {
  1257. res.set_content("test", "text/plain");
  1258. svr.stop();
  1259. ASSERT_TRUE(true);
  1260. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1261. ASSERT_TRUE(peer_cert != nullptr);
  1262. auto subject_name = X509_get_subject_name(peer_cert);
  1263. ASSERT_TRUE(subject_name != nullptr);
  1264. std::string common_name;
  1265. {
  1266. char name[BUFSIZ];
  1267. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1268. name, sizeof(name));
  1269. common_name.assign(name, name_len);
  1270. }
  1271. EXPECT_EQ("Common Name", common_name);
  1272. X509_free(peer_cert);
  1273. });
  1274. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1275. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1276. CLIENT_PRIVATE_KEY_FILE);
  1277. auto res = cli.Get("/test");
  1278. ASSERT_TRUE(res != nullptr);
  1279. ASSERT_EQ(200, res->status);
  1280. t.join();
  1281. }
  1282. TEST(SSLClientServerTest, ClientCertMissing) {
  1283. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1284. CLIENT_CA_CERT_DIR);
  1285. ASSERT_TRUE(svr.is_valid());
  1286. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1287. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1288. httplib::SSLClient cli(HOST, PORT, 30);
  1289. auto res = cli.Get("/test");
  1290. ASSERT_TRUE(res == nullptr);
  1291. svr.stop();
  1292. t.join();
  1293. }
  1294. TEST(SSLClientServerTest, TrustDirOptional) {
  1295. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1296. ASSERT_TRUE(svr.is_valid());
  1297. svr.Get("/test", [&](const Request &, Response &res) {
  1298. res.set_content("test", "text/plain");
  1299. svr.stop();
  1300. });
  1301. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1302. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1303. CLIENT_PRIVATE_KEY_FILE);
  1304. auto res = cli.Get("/test");
  1305. ASSERT_TRUE(res != nullptr);
  1306. ASSERT_EQ(200, res->status);
  1307. t.join();
  1308. }
  1309. /* Cannot test this case as there is no external access to SSL object to check
  1310. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1311. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1312. nullptr, CLIENT_CA_CERT_DIR);
  1313. }
  1314. */
  1315. #endif
  1316. #ifdef _WIN32
  1317. TEST(CleanupTest, WSACleanup) {
  1318. int ret = WSACleanup();
  1319. ASSERT_EQ(0, ret);
  1320. }
  1321. #endif