test.cc 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. #include <gtest/gtest.h>
  2. #include <httplib.h>
  3. #include <future>
  4. #define SERVER_CERT_FILE "./cert.pem"
  5. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  6. #ifdef _WIN32
  7. #include <process.h>
  8. #define msleep(n) ::Sleep(n)
  9. #else
  10. #define msleep(n) ::usleep(n * 1000)
  11. #endif
  12. using namespace std;
  13. using namespace httplib;
  14. const char* HOST = "localhost";
  15. const int PORT = 1234;
  16. const string LONG_QUERY_VALUE = string(25000, '@');
  17. const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
  18. const std::string JSON_DATA = "{\"hello\":\"world\"}";
  19. #ifdef _WIN32
  20. TEST(StartupTest, WSAStartup)
  21. {
  22. WSADATA wsaData;
  23. int ret = WSAStartup(0x0002, &wsaData);
  24. ASSERT_EQ(0, ret);
  25. }
  26. #endif
  27. TEST(SplitTest, ParseQueryString)
  28. {
  29. string s = "key1=val1&key2=val2&key3=val3";
  30. Params dic;
  31. detail::split(s.c_str(), s.c_str() + s.size(), '&', [&](const char* b, const char* e) {
  32. string key, val;
  33. detail::split(b, e, '=', [&](const char* b, const char* e) {
  34. if (key.empty()) {
  35. key.assign(b, e);
  36. } else {
  37. val.assign(b, e);
  38. }
  39. });
  40. dic.emplace(key, val);
  41. });
  42. EXPECT_EQ("val1", dic.find("key1")->second);
  43. EXPECT_EQ("val2", dic.find("key2")->second);
  44. EXPECT_EQ("val3", dic.find("key3")->second);
  45. }
  46. TEST(ParseQueryTest, ParseQueryString)
  47. {
  48. string s = "key1=val1&key2=val2&key3=val3";
  49. Params dic;
  50. detail::parse_query_text(s, dic);
  51. EXPECT_EQ("val1", dic.find("key1")->second);
  52. EXPECT_EQ("val2", dic.find("key2")->second);
  53. EXPECT_EQ("val3", dic.find("key3")->second);
  54. }
  55. TEST(GetHeaderValueTest, DefaultValue)
  56. {
  57. Headers headers = {{"Dummy","Dummy"}};
  58. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  59. EXPECT_STREQ("text/plain", val);
  60. }
  61. TEST(GetHeaderValueTest, DefaultValueInt)
  62. {
  63. Headers headers = {{"Dummy","Dummy"}};
  64. auto val = detail::get_header_value_int(headers, "Content-Length", 100);
  65. EXPECT_EQ(100, val);
  66. }
  67. TEST(GetHeaderValueTest, RegularValue)
  68. {
  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. {
  75. Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
  76. auto val = detail::get_header_value_int(headers, "Content-Length", 0);
  77. EXPECT_EQ(100, val);
  78. }
  79. TEST(GetHeaderValueTest, Range)
  80. {
  81. {
  82. Headers headers = { make_range_header(1) };
  83. auto val = detail::get_header_value(headers, "Range", 0, 0);
  84. EXPECT_STREQ("bytes=1-", val);
  85. }
  86. {
  87. Headers headers = { make_range_header(1, 10) };
  88. auto val = detail::get_header_value(headers, "Range", 0, 0);
  89. EXPECT_STREQ("bytes=1-10", val);
  90. }
  91. {
  92. Headers headers = { make_range_header(1, 10, 100) };
  93. auto val = detail::get_header_value(headers, "Range", 0, 0);
  94. EXPECT_STREQ("bytes=1-10, 100-", val);
  95. }
  96. {
  97. Headers headers = { make_range_header(1, 10, 100, 200) };
  98. auto val = detail::get_header_value(headers, "Range", 0, 0);
  99. EXPECT_STREQ("bytes=1-10, 100-200", val);
  100. }
  101. }
  102. TEST(ChunkedEncodingTest, FromHTTPWatch)
  103. {
  104. auto host = "www.httpwatch.com";
  105. auto sec = 2;
  106. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  107. auto port = 443;
  108. httplib::SSLClient cli(host, port, sec);
  109. #else
  110. auto port = 80;
  111. httplib::Client cli(host, port, sec);
  112. #endif
  113. auto res = cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
  114. ASSERT_TRUE(res != nullptr);
  115. std::string out;
  116. httplib::detail::read_file("./image.jpg", out);
  117. EXPECT_EQ(200, res->status);
  118. EXPECT_EQ(out, res->body);
  119. }
  120. TEST(RangeTest, FromHTTPBin)
  121. {
  122. auto host = "httpbin.org";
  123. auto sec = 5;
  124. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  125. auto port = 443;
  126. httplib::SSLClient cli(host, port, sec);
  127. #else
  128. auto port = 80;
  129. httplib::Client cli(host, port, sec);
  130. #endif
  131. {
  132. httplib::Headers headers;
  133. auto res = cli.Get("/range/32", headers);
  134. ASSERT_TRUE(res != nullptr);
  135. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  136. EXPECT_EQ(200, res->status);
  137. }
  138. {
  139. httplib::Headers headers = { httplib::make_range_header(1) };
  140. auto res = cli.Get("/range/32", headers);
  141. ASSERT_TRUE(res != nullptr);
  142. EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef");
  143. EXPECT_EQ(206, res->status);
  144. }
  145. {
  146. httplib::Headers headers = { httplib::make_range_header(1, 10) };
  147. auto res = cli.Get("/range/32", headers);
  148. ASSERT_TRUE(res != nullptr);
  149. EXPECT_EQ(res->body, "bcdefghijk");
  150. EXPECT_EQ(206, res->status);
  151. }
  152. }
  153. TEST(ConnectionErrorTest, InvalidHost)
  154. {
  155. auto host = "abcde.com";
  156. auto sec = 2;
  157. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  158. auto port = 443;
  159. httplib::SSLClient cli(host, port, sec);
  160. #else
  161. auto port = 80;
  162. httplib::Client cli(host, port, sec);
  163. #endif
  164. auto res = cli.Get("/");
  165. ASSERT_TRUE(res == nullptr);
  166. }
  167. TEST(ConnectionErrorTest, InvalidPort)
  168. {
  169. auto host = "localhost";
  170. auto sec = 2;
  171. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  172. auto port = 44380;
  173. httplib::SSLClient cli(host, port, sec);
  174. #else
  175. auto port = 8080;
  176. httplib::Client cli(host, port, sec);
  177. #endif
  178. auto res = cli.Get("/");
  179. ASSERT_TRUE(res == nullptr);
  180. }
  181. TEST(ConnectionErrorTest, Timeout)
  182. {
  183. auto host = "google.com";
  184. auto sec = 2;
  185. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  186. auto port = 44380;
  187. httplib::SSLClient cli(host, port, sec);
  188. #else
  189. auto port = 8080;
  190. httplib::Client cli(host, port, sec);
  191. #endif
  192. auto res = cli.Get("/");
  193. ASSERT_TRUE(res == nullptr);
  194. }
  195. TEST(CancelTest, NoCancel) {
  196. auto host = "httpbin.org";
  197. auto sec = 5;
  198. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  199. auto port = 443;
  200. httplib::SSLClient cli(host, port, sec);
  201. #else
  202. auto port = 80;
  203. httplib::Client cli(host, port, sec);
  204. #endif
  205. httplib::Headers headers;
  206. auto res = cli.Get("/range/32", headers, [](uint64_t, uint64_t) {
  207. return true;
  208. });
  209. ASSERT_TRUE(res != nullptr);
  210. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  211. EXPECT_EQ(200, res->status);
  212. }
  213. TEST(CancelTest, WithCancelSmallPayload) {
  214. auto host = "httpbin.org";
  215. auto sec = 5;
  216. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  217. auto port = 443;
  218. httplib::SSLClient cli(host, port, sec);
  219. #else
  220. auto port = 80;
  221. httplib::Client cli(host, port, sec);
  222. #endif
  223. httplib::Headers headers;
  224. auto res = cli.Get("/range/32", headers, [](uint64_t, uint64_t) {
  225. return false;
  226. });
  227. ASSERT_TRUE(res == nullptr);
  228. }
  229. TEST(CancelTest, WithCancelLargePayload) {
  230. auto host = "httpbin.org";
  231. auto sec = 5;
  232. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  233. auto port = 443;
  234. httplib::SSLClient cli(host, port, sec);
  235. #else
  236. auto port = 80;
  237. httplib::Client cli(host, port, sec);
  238. #endif
  239. uint32_t count = 0;
  240. httplib::Headers headers;
  241. auto res = cli.Get("/range/65536", headers, [&count](uint64_t, uint64_t) {
  242. return (count++ == 0);
  243. });
  244. ASSERT_TRUE(res == nullptr);
  245. }
  246. TEST(Server, BindAndListenSeparately) {
  247. Server svr;
  248. int port = svr.bind_to_any_port("localhost");
  249. ASSERT_TRUE(port > 0);
  250. svr.stop();
  251. }
  252. class ServerTest : public ::testing::Test {
  253. protected:
  254. ServerTest()
  255. : cli_(HOST, PORT)
  256. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  257. , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  258. #endif
  259. {}
  260. virtual void SetUp() {
  261. svr_.set_base_dir("./www");
  262. svr_.Get("/hi", [&](const Request& /*req*/, Response& res) {
  263. res.set_content("Hello World!", "text/plain");
  264. })
  265. .Get("/slow", [&](const Request& /*req*/, Response& res) {
  266. msleep(2000);
  267. res.set_content("slow", "text/plain");
  268. })
  269. .Get("/remote_addr", [&](const Request& req, Response& res) {
  270. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  271. res.set_content(remote_addr.c_str(), "text/plain");
  272. })
  273. .Get("/endwith%", [&](const Request& /*req*/, Response& res) {
  274. res.set_content("Hello World!", "text/plain");
  275. })
  276. .Get("/", [&](const Request& /*req*/, Response& res) {
  277. res.set_redirect("/hi");
  278. })
  279. .Post("/person", [&](const Request& req, Response& res) {
  280. if (req.has_param("name") && req.has_param("note")) {
  281. persons_[req.get_param_value("name")] = req.get_param_value("note");
  282. } else {
  283. res.status = 400;
  284. }
  285. })
  286. .Get("/person/(.*)", [&](const Request& req, Response& res) {
  287. string name = req.matches[1];
  288. if (persons_.find(name) != persons_.end()) {
  289. auto note = persons_[name];
  290. res.set_content(note, "text/plain");
  291. } else {
  292. res.status = 404;
  293. }
  294. })
  295. .Post("/x-www-form-urlencoded-json", [&](const Request& req, Response& res) {
  296. auto json = req.get_param_value("json");
  297. ASSERT_EQ(JSON_DATA, json);
  298. res.set_content(json, "appliation/json");
  299. res.status = 200;
  300. })
  301. .Get("/streamedchunked", [&](const Request& /*req*/, Response& res) {
  302. res.streamcb = [] (uint64_t offset) {
  303. if (offset < 3)
  304. return "a";
  305. if (offset < 6)
  306. return "b";
  307. return "";
  308. };
  309. })
  310. .Get("/streamed", [&](const Request& /*req*/, Response& res) {
  311. res.set_header("Content-Length", "6");
  312. res.streamcb = [] (uint64_t offset) {
  313. if (offset < 3)
  314. return "a";
  315. if (offset < 6)
  316. return "b";
  317. return "";
  318. };
  319. })
  320. .Post("/chunked", [&](const Request& req, Response& /*res*/) {
  321. EXPECT_EQ(req.body, "dechunked post body");
  322. })
  323. .Post("/largechunked", [&](const Request& req, Response& /*res*/) {
  324. std::string expected(6 * 30 * 1024u, 'a');
  325. EXPECT_EQ(req.body, expected);
  326. })
  327. .Post("/multipart", [&](const Request& req, Response& /*res*/) {
  328. EXPECT_EQ(5u, req.files.size());
  329. ASSERT_TRUE(!req.has_file("???"));
  330. {
  331. const auto& file = req.get_file_value("text1");
  332. EXPECT_EQ("", file.filename);
  333. EXPECT_EQ("text default", req.body.substr(file.offset, file.length));
  334. }
  335. {
  336. const auto& file = req.get_file_value("text2");
  337. EXPECT_EQ("", file.filename);
  338. EXPECT_EQ("aωb", req.body.substr(file.offset, file.length));
  339. }
  340. {
  341. const auto& file = req.get_file_value("file1");
  342. EXPECT_EQ("hello.txt", file.filename);
  343. EXPECT_EQ("text/plain", file.content_type);
  344. EXPECT_EQ("h\ne\n\nl\nl\no\n", req.body.substr(file.offset, file.length));
  345. }
  346. {
  347. const auto& file = req.get_file_value("file3");
  348. EXPECT_EQ("", file.filename);
  349. EXPECT_EQ("application/octet-stream", file.content_type);
  350. EXPECT_EQ(0u, file.length);
  351. }
  352. })
  353. .Post("/empty", [&](const Request& req, Response& res) {
  354. EXPECT_EQ(req.body, "");
  355. res.set_content("empty", "text/plain");
  356. })
  357. .Put("/put", [&](const Request& req, Response& res) {
  358. EXPECT_EQ(req.body, "PUT");
  359. res.set_content(req.body, "text/plain");
  360. })
  361. .Delete("/delete", [&](const Request& /*req*/, Response& res) {
  362. res.set_content("DELETE", "text/plain");
  363. })
  364. .Options(R"(\*)", [&](const Request& /*req*/, Response& res) {
  365. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  366. })
  367. .Get("/request-target", [&](const Request& req, Response& /*res*/) {
  368. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  369. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  370. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  371. })
  372. .Get("/long-query-value", [&](const Request& req, Response& /*res*/) {
  373. EXPECT_EQ(LONG_QUERY_URL, req.target);
  374. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  375. })
  376. .Get("/array-param", [&](const Request& req, Response& /*res*/) {
  377. EXPECT_EQ(3u, req.get_param_value_count("array"));
  378. EXPECT_EQ("value1", req.get_param_value("array", 0));
  379. EXPECT_EQ("value2", req.get_param_value("array", 1));
  380. EXPECT_EQ("value3", req.get_param_value("array", 2));
  381. })
  382. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  383. .Get("/gzip", [&](const Request& /*req*/, Response& res) {
  384. res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain");
  385. })
  386. .Get("/nogzip", [&](const Request& /*req*/, Response& res) {
  387. res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "application/octet-stream");
  388. })
  389. .Post("/gzipmultipart", [&](const Request& req, Response& /*res*/) {
  390. EXPECT_EQ(2u, req.files.size());
  391. ASSERT_TRUE(!req.has_file("???"));
  392. {
  393. const auto& file = req.get_file_value("key1");
  394. EXPECT_EQ("", file.filename);
  395. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  396. }
  397. {
  398. const auto& file = req.get_file_value("key2");
  399. EXPECT_EQ("", file.filename);
  400. EXPECT_EQ("--abcdefg123", req.body.substr(file.offset, file.length));
  401. }
  402. })
  403. #endif
  404. ;
  405. persons_["john"] = "programmer";
  406. t_ = thread([&](){
  407. ASSERT_TRUE(svr_.listen(HOST, PORT));
  408. });
  409. while (!svr_.is_running()) {
  410. msleep(1);
  411. }
  412. }
  413. virtual void TearDown() {
  414. svr_.stop();
  415. for (auto& t: request_threads_) {
  416. t.join();
  417. }
  418. t_.join();
  419. }
  420. map<string, string> persons_;
  421. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  422. SSLClient cli_;
  423. SSLServer svr_;
  424. #else
  425. Client cli_;
  426. Server svr_;
  427. #endif
  428. thread t_;
  429. std::vector<thread> request_threads_;
  430. };
  431. TEST_F(ServerTest, GetMethod200)
  432. {
  433. auto res = cli_.Get("/hi");
  434. ASSERT_TRUE(res != nullptr);
  435. EXPECT_EQ("HTTP/1.1", res->version);
  436. EXPECT_EQ(200, res->status);
  437. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  438. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  439. EXPECT_EQ("close", res->get_header_value("Connection"));
  440. EXPECT_EQ("Hello World!", res->body);
  441. }
  442. TEST_F(ServerTest, GetMethod302)
  443. {
  444. auto res = cli_.Get("/");
  445. ASSERT_TRUE(res != nullptr);
  446. EXPECT_EQ(302, res->status);
  447. EXPECT_EQ("/hi", res->get_header_value("Location"));
  448. }
  449. TEST_F(ServerTest, GetMethod404)
  450. {
  451. auto res = cli_.Get("/invalid");
  452. ASSERT_TRUE(res != nullptr);
  453. EXPECT_EQ(404, res->status);
  454. }
  455. TEST_F(ServerTest, HeadMethod200)
  456. {
  457. auto res = cli_.Head("/hi");
  458. ASSERT_TRUE(res != nullptr);
  459. EXPECT_EQ(200, res->status);
  460. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  461. EXPECT_EQ("", res->body);
  462. }
  463. TEST_F(ServerTest, HeadMethod404)
  464. {
  465. auto res = cli_.Head("/invalid");
  466. ASSERT_TRUE(res != nullptr);
  467. EXPECT_EQ(404, res->status);
  468. EXPECT_EQ("", res->body);
  469. }
  470. TEST_F(ServerTest, GetMethodPersonJohn)
  471. {
  472. auto res = cli_.Get("/person/john");
  473. ASSERT_TRUE(res != nullptr);
  474. EXPECT_EQ(200, res->status);
  475. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  476. EXPECT_EQ("programmer", res->body);
  477. }
  478. TEST_F(ServerTest, PostMethod1)
  479. {
  480. auto res = cli_.Get("/person/john1");
  481. ASSERT_TRUE(res != nullptr);
  482. ASSERT_EQ(404, res->status);
  483. res = cli_.Post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
  484. ASSERT_TRUE(res != nullptr);
  485. ASSERT_EQ(200, res->status);
  486. res = cli_.Get("/person/john1");
  487. ASSERT_TRUE(res != nullptr);
  488. ASSERT_EQ(200, res->status);
  489. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  490. ASSERT_EQ("coder", res->body);
  491. }
  492. TEST_F(ServerTest, PostMethod2)
  493. {
  494. auto res = cli_.Get("/person/john2");
  495. ASSERT_TRUE(res != nullptr);
  496. ASSERT_EQ(404, res->status);
  497. Params params;
  498. params.emplace("name", "john2");
  499. params.emplace("note", "coder");
  500. res = cli_.Post("/person", params);
  501. ASSERT_TRUE(res != nullptr);
  502. ASSERT_EQ(200, res->status);
  503. res = cli_.Get("/person/john2");
  504. ASSERT_TRUE(res != nullptr);
  505. ASSERT_EQ(200, res->status);
  506. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  507. ASSERT_EQ("coder", res->body);
  508. }
  509. TEST_F(ServerTest, PostWwwFormUrlEncodedJson)
  510. {
  511. Params params;
  512. params.emplace("json", JSON_DATA);
  513. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  514. ASSERT_TRUE(res != nullptr);
  515. ASSERT_EQ(200, res->status);
  516. ASSERT_EQ(JSON_DATA, res->body);
  517. }
  518. TEST_F(ServerTest, PostEmptyContent)
  519. {
  520. auto res = cli_.Post("/empty", "", "text/plain");
  521. ASSERT_TRUE(res != nullptr);
  522. ASSERT_EQ(200, res->status);
  523. ASSERT_EQ("empty", res->body);
  524. }
  525. TEST_F(ServerTest, GetMethodDir)
  526. {
  527. auto res = cli_.Get("/dir/");
  528. ASSERT_TRUE(res != nullptr);
  529. EXPECT_EQ(200, res->status);
  530. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  531. auto body = R"(<html>
  532. <head>
  533. </head>
  534. <body>
  535. <a href="/dir/test.html">Test</a>
  536. <a href="/hi">hi</a>
  537. </body>
  538. </html>
  539. )";
  540. EXPECT_EQ(body, res->body);
  541. }
  542. TEST_F(ServerTest, GetMethodDirTest)
  543. {
  544. auto res = cli_.Get("/dir/test.html");
  545. ASSERT_TRUE(res != nullptr);
  546. EXPECT_EQ(200, res->status);
  547. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  548. EXPECT_EQ("test.html", res->body);
  549. }
  550. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots)
  551. {
  552. auto res = cli_.Get("/dir/../dir/test.html");
  553. ASSERT_TRUE(res != nullptr);
  554. EXPECT_EQ(200, res->status);
  555. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  556. EXPECT_EQ("test.html", res->body);
  557. }
  558. TEST_F(ServerTest, GetMethodInvalidPath)
  559. {
  560. auto res = cli_.Get("/dir/../test.html");
  561. ASSERT_TRUE(res != nullptr);
  562. EXPECT_EQ(404, res->status);
  563. }
  564. TEST_F(ServerTest, GetMethodOutOfBaseDir)
  565. {
  566. auto res = cli_.Get("/../www/dir/test.html");
  567. ASSERT_TRUE(res != nullptr);
  568. EXPECT_EQ(404, res->status);
  569. }
  570. TEST_F(ServerTest, GetMethodOutOfBaseDir2)
  571. {
  572. auto res = cli_.Get("/dir/../../www/dir/test.html");
  573. ASSERT_TRUE(res != nullptr);
  574. EXPECT_EQ(404, res->status);
  575. }
  576. TEST_F(ServerTest, InvalidBaseDir)
  577. {
  578. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  579. EXPECT_EQ(true, svr_.set_base_dir("."));
  580. }
  581. TEST_F(ServerTest, EmptyRequest)
  582. {
  583. auto res = cli_.Get("");
  584. ASSERT_TRUE(res == nullptr);
  585. }
  586. TEST_F(ServerTest, LongRequest)
  587. {
  588. auto res = cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ok__");
  589. ASSERT_TRUE(res != nullptr);
  590. EXPECT_EQ(404, res->status);
  591. }
  592. TEST_F(ServerTest, TooLongRequest)
  593. {
  594. auto res = cli_.Get("/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/TooLongRequest/__ng___");
  595. ASSERT_TRUE(res != nullptr);
  596. EXPECT_EQ(404, res->status);
  597. }
  598. TEST_F(ServerTest, LongHeader)
  599. {
  600. Request req;
  601. req.method = "GET";
  602. req.path = "/hi";
  603. std::string host_and_port;
  604. host_and_port += HOST;
  605. host_and_port += ":";
  606. host_and_port += std::to_string(PORT);
  607. req.headers.emplace("Host", host_and_port.c_str());
  608. req.headers.emplace("Accept", "*/*");
  609. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  610. req.headers.emplace("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  611. auto res = std::make_shared<Response>();
  612. auto ret = cli_.send(req, *res);
  613. ASSERT_TRUE(ret);
  614. EXPECT_EQ(200, res->status);
  615. }
  616. TEST_F(ServerTest, LongQueryValue)
  617. {
  618. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  619. ASSERT_TRUE(res != nullptr);
  620. EXPECT_EQ(200, res->status);
  621. }
  622. TEST_F(ServerTest, TooLongHeader)
  623. {
  624. Request req;
  625. req.method = "GET";
  626. req.path = "/hi";
  627. std::string host_and_port;
  628. host_and_port += HOST;
  629. host_and_port += ":";
  630. host_and_port += std::to_string(PORT);
  631. req.headers.emplace("Host", host_and_port.c_str());
  632. req.headers.emplace("Accept", "*/*");
  633. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  634. req.headers.emplace("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  635. auto res = std::make_shared<Response>();
  636. auto ret = cli_.send(req, *res);
  637. ASSERT_TRUE(ret);
  638. EXPECT_EQ(200, res->status);
  639. }
  640. TEST_F(ServerTest, PercentEncoding)
  641. {
  642. auto res = cli_.Get("/e%6edwith%");
  643. ASSERT_TRUE(res != nullptr);
  644. EXPECT_EQ(200, res->status);
  645. }
  646. TEST_F(ServerTest, PercentEncodingUnicode)
  647. {
  648. auto res = cli_.Get("/e%u006edwith%");
  649. ASSERT_TRUE(res != nullptr);
  650. EXPECT_EQ(200, res->status);
  651. }
  652. TEST_F(ServerTest, InvalidPercentEncoding)
  653. {
  654. auto res = cli_.Get("/%endwith%");
  655. ASSERT_TRUE(res != nullptr);
  656. EXPECT_EQ(404, res->status);
  657. }
  658. TEST_F(ServerTest, InvalidPercentEncodingUnicode)
  659. {
  660. auto res = cli_.Get("/%uendwith%");
  661. ASSERT_TRUE(res != nullptr);
  662. EXPECT_EQ(404, res->status);
  663. }
  664. TEST_F(ServerTest, EndWithPercentCharacterInQuery)
  665. {
  666. auto res = cli_.Get("/hello?aaa=bbb%");
  667. ASSERT_TRUE(res != nullptr);
  668. EXPECT_EQ(404, res->status);
  669. }
  670. TEST_F(ServerTest, MultipartFormData)
  671. {
  672. Request req;
  673. req.method = "POST";
  674. req.path = "/multipart";
  675. std::string host_and_port;
  676. host_and_port += HOST;
  677. host_and_port += ":";
  678. host_and_port += std::to_string(PORT);
  679. req.headers.emplace("Host", host_and_port.c_str());
  680. req.headers.emplace("Accept", "*/*");
  681. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  682. req.headers.emplace("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4");
  683. req.body = "------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text1\"\r\n\r\ntext default\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"text2\"\r\n\r\naωb\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"hello.txt\"\r\nContent-Type: text/plain\r\n\r\nh\ne\n\nl\nl\no\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\nContent-Disposition: form-data; name=\"file2\"; filename=\"world.json\"\r\nContent-Type: application/json\r\n\r\n{\n \"world\", true\n}\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4\r\ncontent-disposition: form-data; name=\"file3\"; filename=\"\"\r\ncontent-type: application/octet-stream\r\n\r\n\r\n------WebKitFormBoundarysBREP3G013oUrLB4--\r\n";
  684. auto res = std::make_shared<Response>();
  685. auto ret = cli_.send(req, *res);
  686. ASSERT_TRUE(ret);
  687. EXPECT_EQ(200, res->status);
  688. }
  689. TEST_F(ServerTest, CaseInsensitiveHeaderName)
  690. {
  691. auto res = cli_.Get("/hi");
  692. ASSERT_TRUE(res != nullptr);
  693. EXPECT_EQ(200, res->status);
  694. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  695. EXPECT_EQ("Hello World!", res->body);
  696. }
  697. TEST_F(ServerTest, CaseInsensitiveTransferEncoding)
  698. {
  699. Request req;
  700. req.method = "POST";
  701. req.path = "/chunked";
  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("Content-Type", "text/plain");
  710. req.headers.emplace("Content-Length", "0");
  711. req.headers.emplace("Transfer-Encoding", "Chunked"); // Note, "Chunked" rather than typical "chunked".
  712. // Client does not chunk, so make a chunked body manually.
  713. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  714. auto res = std::make_shared<Response>();
  715. auto ret = cli_.send(req, *res);
  716. ASSERT_TRUE(ret);
  717. EXPECT_EQ(200, res->status);
  718. }
  719. TEST_F(ServerTest, GetStreamed)
  720. {
  721. auto res = cli_.Get("/streamed");
  722. ASSERT_TRUE(res != nullptr);
  723. EXPECT_EQ(200, res->status);
  724. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  725. EXPECT_TRUE(res->body == "aaabbb");
  726. }
  727. TEST_F(ServerTest, GetStreamedChunked)
  728. {
  729. auto res = cli_.Get("/streamedchunked");
  730. ASSERT_TRUE(res != nullptr);
  731. EXPECT_EQ(200, res->status);
  732. EXPECT_TRUE(res->body == "aaabbb");
  733. }
  734. TEST_F(ServerTest, LargeChunkedPost) {
  735. Request req;
  736. req.method = "POST";
  737. req.path = "/largechunked";
  738. std::string host_and_port;
  739. host_and_port += HOST;
  740. host_and_port += ":";
  741. host_and_port += std::to_string(PORT);
  742. req.headers.emplace("Host", host_and_port.c_str());
  743. req.headers.emplace("Accept", "*/*");
  744. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  745. req.headers.emplace("Content-Type", "text/plain");
  746. req.headers.emplace("Content-Length", "0");
  747. req.headers.emplace("Transfer-Encoding", "chunked");
  748. std::string long_string(30 * 1024u, 'a');
  749. std::string chunk = "7800\r\n" + long_string + "\r\n";
  750. // Attempt to make a large enough post to exceed OS buffers, to test that
  751. // the server handles short reads if the full chunk data isn't available.
  752. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  753. auto res = std::make_shared<Response>();
  754. auto ret = cli_.send(req, *res);
  755. ASSERT_TRUE(ret);
  756. EXPECT_EQ(200, res->status);
  757. }
  758. TEST_F(ServerTest, GetMethodRemoteAddr)
  759. {
  760. auto res = cli_.Get("/remote_addr");
  761. ASSERT_TRUE(res != nullptr);
  762. EXPECT_EQ(200, res->status);
  763. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  764. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  765. }
  766. TEST_F(ServerTest, SlowRequest)
  767. {
  768. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  769. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  770. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  771. msleep(100);
  772. }
  773. TEST_F(ServerTest, Put)
  774. {
  775. auto res = cli_.Put("/put", "PUT", "text/plain");
  776. ASSERT_TRUE(res != nullptr);
  777. EXPECT_EQ(200, res->status);
  778. EXPECT_EQ("PUT", res->body);
  779. }
  780. TEST_F(ServerTest, Delete)
  781. {
  782. auto res = cli_.Delete("/delete");
  783. ASSERT_TRUE(res != nullptr);
  784. EXPECT_EQ(200, res->status);
  785. EXPECT_EQ("DELETE", res->body);
  786. }
  787. TEST_F(ServerTest, Options)
  788. {
  789. auto res = cli_.Options("*");
  790. ASSERT_TRUE(res != nullptr);
  791. EXPECT_EQ(200, res->status);
  792. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  793. EXPECT_TRUE(res->body.empty());
  794. }
  795. TEST_F(ServerTest, URL)
  796. {
  797. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  798. ASSERT_TRUE(res != nullptr);
  799. EXPECT_EQ(200, res->status);
  800. }
  801. TEST_F(ServerTest, ArrayParam)
  802. {
  803. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  804. ASSERT_TRUE(res != nullptr);
  805. EXPECT_EQ(200, res->status);
  806. }
  807. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  808. TEST_F(ServerTest, Gzip)
  809. {
  810. Headers headers;
  811. headers.emplace("Accept-Encoding", "gzip, deflate");
  812. auto res = cli_.Get("/gzip", headers);
  813. ASSERT_TRUE(res != nullptr);
  814. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  815. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  816. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  817. EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body);
  818. EXPECT_EQ(200, res->status);
  819. }
  820. TEST_F(ServerTest, NoGzip)
  821. {
  822. Headers headers;
  823. headers.emplace("Accept-Encoding", "gzip, deflate");
  824. auto res = cli_.Get("/nogzip", headers);
  825. ASSERT_TRUE(res != nullptr);
  826. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  827. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  828. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  829. EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body);
  830. EXPECT_EQ(200, res->status);
  831. }
  832. TEST_F(ServerTest, MultipartFormDataGzip)
  833. {
  834. Request req;
  835. req.method = "POST";
  836. req.path = "/gzipmultipart";
  837. std::string host_and_port;
  838. host_and_port += HOST;
  839. host_and_port += ":";
  840. host_and_port += std::to_string(PORT);
  841. req.headers.emplace("Host", host_and_port.c_str());
  842. req.headers.emplace("Accept", "*/*");
  843. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  844. req.headers.emplace("Content-Type", "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  845. req.headers.emplace("Content-Encoding", "gzip");
  846. // compressed_body generated by creating input.txt to this file:
  847. /*
  848. --------------------------fcba8368a9f48c0f
  849. Content-Disposition: form-data; name="key1"
  850. test
  851. --------------------------fcba8368a9f48c0f
  852. Content-Disposition: form-data; name="key2"
  853. --abcdefg123
  854. --------------------------fcba8368a9f48c0f--
  855. */
  856. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  857. uint8_t compressed_body[] = {
  858. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  859. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  860. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  861. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  862. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  863. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  864. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  865. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  866. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  867. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  868. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00
  869. };
  870. req.body = std::string((char*)compressed_body, sizeof(compressed_body) / sizeof(compressed_body[0]));
  871. auto res = std::make_shared<Response>();
  872. auto ret = cli_.send(req, *res);
  873. ASSERT_TRUE(ret);
  874. EXPECT_EQ(200, res->status);
  875. }
  876. #endif
  877. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  878. protected:
  879. ServerTestWithAI_PASSIVE()
  880. : cli_(HOST, PORT)
  881. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  882. , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  883. #endif
  884. {}
  885. virtual void SetUp() {
  886. svr_.Get("/hi", [&](const Request& /*req*/, Response& res) {
  887. res.set_content("Hello World!", "text/plain");
  888. });
  889. t_ = thread([&]() {
  890. ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE));
  891. });
  892. while (!svr_.is_running()) {
  893. msleep(1);
  894. }
  895. }
  896. virtual void TearDown() {
  897. svr_.stop();
  898. t_.join();
  899. }
  900. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  901. SSLClient cli_;
  902. SSLServer svr_;
  903. #else
  904. Client cli_;
  905. Server svr_;
  906. #endif
  907. thread t_;
  908. };
  909. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200)
  910. {
  911. auto res = cli_.Get("/hi");
  912. ASSERT_TRUE(res != nullptr);
  913. EXPECT_EQ(200, res->status);
  914. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  915. EXPECT_EQ("Hello World!", res->body);
  916. }
  917. class ServerUpDownTest : public ::testing::Test {
  918. protected:
  919. ServerUpDownTest()
  920. : cli_(HOST, PORT)
  921. {}
  922. virtual void SetUp() {
  923. t_ = thread([&](){
  924. svr_.bind_to_any_port(HOST);
  925. msleep(500);
  926. ASSERT_TRUE(svr_.listen_after_bind());
  927. });
  928. while (!svr_.is_running()) {
  929. msleep(1);
  930. }
  931. }
  932. virtual void TearDown() {
  933. svr_.stop();
  934. t_.join();
  935. }
  936. Client cli_;
  937. Server svr_;
  938. thread t_;
  939. };
  940. TEST_F(ServerUpDownTest, QuickStartStop)
  941. {
  942. // Should not crash, especially when run with
  943. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  944. }
  945. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  946. TEST(SSLClientTest, ServerNameIndication)
  947. {
  948. SSLClient cli("httpbin.org", 443);
  949. auto res = cli.Get("/get");
  950. ASSERT_TRUE(res != nullptr);
  951. ASSERT_EQ(200, res->status);
  952. }
  953. #endif
  954. #ifdef _WIN32
  955. TEST(CleanupTest, WSACleanup)
  956. {
  957. int ret = WSACleanup();
  958. ASSERT_EQ(0, ret);
  959. }
  960. #endif
  961. // vim: et ts=4 sw=4 cin cino={1s ff=unix