test.cc 47 KB

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