test.cc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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(BaseAuthTest, FromHTTPWatch)
  247. {
  248. auto host = "httpbin.org";
  249. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  250. auto port = 443;
  251. httplib::SSLClient cli(host, port);
  252. #else
  253. auto port = 80;
  254. httplib::Client cli(host, port);
  255. #endif
  256. {
  257. auto res = cli.Get("/basic-auth/hello/world");
  258. ASSERT_TRUE(res != nullptr);
  259. EXPECT_EQ(401, res->status);
  260. }
  261. {
  262. httplib::Headers headers = {
  263. { "Authorization", "Basic aGVsbG86d29ybGQ=" }
  264. };
  265. auto res = cli.Get("/basic-auth/hello/world", headers);
  266. ASSERT_TRUE(res != nullptr);
  267. EXPECT_EQ(res->body, "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  268. EXPECT_EQ(200, res->status);
  269. }
  270. }
  271. TEST(Server, BindAndListenSeparately) {
  272. Server svr;
  273. int port = svr.bind_to_any_port("localhost");
  274. ASSERT_TRUE(port > 0);
  275. svr.stop();
  276. }
  277. class ServerTest : public ::testing::Test {
  278. protected:
  279. ServerTest()
  280. : cli_(HOST, PORT)
  281. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  282. , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  283. #endif
  284. {}
  285. virtual void SetUp() {
  286. svr_.set_base_dir("./www");
  287. svr_.Get("/hi", [&](const Request& /*req*/, Response& res) {
  288. res.set_content("Hello World!", "text/plain");
  289. })
  290. .Get("/slow", [&](const Request& /*req*/, Response& res) {
  291. msleep(2000);
  292. res.set_content("slow", "text/plain");
  293. })
  294. .Get("/remote_addr", [&](const Request& req, Response& res) {
  295. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  296. res.set_content(remote_addr.c_str(), "text/plain");
  297. })
  298. .Get("/endwith%", [&](const Request& /*req*/, Response& res) {
  299. res.set_content("Hello World!", "text/plain");
  300. })
  301. .Get("/", [&](const Request& /*req*/, Response& res) {
  302. res.set_redirect("/hi");
  303. })
  304. .Post("/person", [&](const Request& req, Response& res) {
  305. if (req.has_param("name") && req.has_param("note")) {
  306. persons_[req.get_param_value("name")] = req.get_param_value("note");
  307. } else {
  308. res.status = 400;
  309. }
  310. })
  311. .Get("/person/(.*)", [&](const Request& req, Response& res) {
  312. string name = req.matches[1];
  313. if (persons_.find(name) != persons_.end()) {
  314. auto note = persons_[name];
  315. res.set_content(note, "text/plain");
  316. } else {
  317. res.status = 404;
  318. }
  319. })
  320. .Post("/x-www-form-urlencoded-json", [&](const Request& req, Response& res) {
  321. auto json = req.get_param_value("json");
  322. ASSERT_EQ(JSON_DATA, json);
  323. res.set_content(json, "appliation/json");
  324. res.status = 200;
  325. })
  326. .Get("/streamedchunked", [&](const Request& /*req*/, Response& res) {
  327. res.streamcb = [] (uint64_t offset) {
  328. if (offset < 3)
  329. return "a";
  330. if (offset < 6)
  331. return "b";
  332. return "";
  333. };
  334. })
  335. .Get("/streamed", [&](const Request& /*req*/, Response& res) {
  336. res.set_header("Content-Length", "6");
  337. res.streamcb = [] (uint64_t offset) {
  338. if (offset < 3)
  339. return "a";
  340. if (offset < 6)
  341. return "b";
  342. return "";
  343. };
  344. })
  345. .Post("/chunked", [&](const Request& req, Response& /*res*/) {
  346. EXPECT_EQ(req.body, "dechunked post body");
  347. })
  348. .Post("/largechunked", [&](const Request& req, Response& /*res*/) {
  349. std::string expected(6 * 30 * 1024u, 'a');
  350. EXPECT_EQ(req.body, expected);
  351. })
  352. .Post("/multipart", [&](const Request& req, Response& /*res*/) {
  353. EXPECT_EQ(5u, req.files.size());
  354. ASSERT_TRUE(!req.has_file("???"));
  355. {
  356. const auto& file = req.get_file_value("text1");
  357. EXPECT_EQ("", file.filename);
  358. EXPECT_EQ("text default", 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", req.body.substr(file.offset, file.length));
  370. }
  371. {
  372. const auto& file = req.get_file_value("file3");
  373. EXPECT_EQ("", file.filename);
  374. EXPECT_EQ("application/octet-stream", file.content_type);
  375. EXPECT_EQ(0u, file.length);
  376. }
  377. })
  378. .Post("/empty", [&](const Request& req, Response& res) {
  379. EXPECT_EQ(req.body, "");
  380. res.set_content("empty", "text/plain");
  381. })
  382. .Put("/put", [&](const Request& req, Response& res) {
  383. EXPECT_EQ(req.body, "PUT");
  384. res.set_content(req.body, "text/plain");
  385. })
  386. .Patch("/patch", [&](const Request& req, Response& res) {
  387. EXPECT_EQ(req.body, "PATCH");
  388. res.set_content(req.body, "text/plain");
  389. })
  390. .Delete("/delete", [&](const Request& /*req*/, Response& res) {
  391. res.set_content("DELETE", "text/plain");
  392. })
  393. .Options(R"(\*)", [&](const Request& /*req*/, Response& res) {
  394. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  395. })
  396. .Get("/request-target", [&](const Request& req, Response& /*res*/) {
  397. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  398. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  399. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  400. })
  401. .Get("/long-query-value", [&](const Request& req, Response& /*res*/) {
  402. EXPECT_EQ(LONG_QUERY_URL, req.target);
  403. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  404. })
  405. .Get("/array-param", [&](const Request& req, Response& /*res*/) {
  406. EXPECT_EQ(3u, req.get_param_value_count("array"));
  407. EXPECT_EQ("value1", req.get_param_value("array", 0));
  408. EXPECT_EQ("value2", req.get_param_value("array", 1));
  409. EXPECT_EQ("value3", req.get_param_value("array", 2));
  410. })
  411. .Post("/validate-no-multiple-headers", [&](const Request& req, Response& res) {
  412. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  413. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  414. })
  415. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  416. .Get("/gzip", [&](const Request& /*req*/, Response& res) {
  417. res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "text/plain");
  418. })
  419. .Get("/nogzip", [&](const Request& /*req*/, Response& res) {
  420. res.set_content("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", "application/octet-stream");
  421. })
  422. .Post("/gzipmultipart", [&](const Request& req, Response& /*res*/) {
  423. EXPECT_EQ(2u, req.files.size());
  424. ASSERT_TRUE(!req.has_file("???"));
  425. {
  426. const auto& file = req.get_file_value("key1");
  427. EXPECT_EQ("", file.filename);
  428. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  429. }
  430. {
  431. const auto& file = req.get_file_value("key2");
  432. EXPECT_EQ("", file.filename);
  433. EXPECT_EQ("--abcdefg123", req.body.substr(file.offset, file.length));
  434. }
  435. })
  436. #endif
  437. ;
  438. persons_["john"] = "programmer";
  439. t_ = thread([&](){
  440. ASSERT_TRUE(svr_.listen(HOST, PORT));
  441. });
  442. while (!svr_.is_running()) {
  443. msleep(1);
  444. }
  445. }
  446. virtual void TearDown() {
  447. svr_.stop();
  448. for (auto& t: request_threads_) {
  449. t.join();
  450. }
  451. t_.join();
  452. }
  453. map<string, string> persons_;
  454. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  455. SSLClient cli_;
  456. SSLServer svr_;
  457. #else
  458. Client cli_;
  459. Server svr_;
  460. #endif
  461. thread t_;
  462. std::vector<thread> request_threads_;
  463. };
  464. TEST_F(ServerTest, GetMethod200)
  465. {
  466. auto res = cli_.Get("/hi");
  467. ASSERT_TRUE(res != nullptr);
  468. EXPECT_EQ("HTTP/1.1", res->version);
  469. EXPECT_EQ(200, res->status);
  470. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  471. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  472. EXPECT_EQ("close", res->get_header_value("Connection"));
  473. EXPECT_EQ("Hello World!", res->body);
  474. }
  475. TEST_F(ServerTest, GetMethod302)
  476. {
  477. auto res = cli_.Get("/");
  478. ASSERT_TRUE(res != nullptr);
  479. EXPECT_EQ(302, res->status);
  480. EXPECT_EQ("/hi", res->get_header_value("Location"));
  481. }
  482. TEST_F(ServerTest, GetMethod404)
  483. {
  484. auto res = cli_.Get("/invalid");
  485. ASSERT_TRUE(res != nullptr);
  486. EXPECT_EQ(404, res->status);
  487. }
  488. TEST_F(ServerTest, HeadMethod200)
  489. {
  490. auto res = cli_.Head("/hi");
  491. ASSERT_TRUE(res != nullptr);
  492. EXPECT_EQ(200, res->status);
  493. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  494. EXPECT_EQ("", res->body);
  495. }
  496. TEST_F(ServerTest, HeadMethod404)
  497. {
  498. auto res = cli_.Head("/invalid");
  499. ASSERT_TRUE(res != nullptr);
  500. EXPECT_EQ(404, res->status);
  501. EXPECT_EQ("", res->body);
  502. }
  503. TEST_F(ServerTest, GetMethodPersonJohn)
  504. {
  505. auto res = cli_.Get("/person/john");
  506. ASSERT_TRUE(res != nullptr);
  507. EXPECT_EQ(200, res->status);
  508. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  509. EXPECT_EQ("programmer", res->body);
  510. }
  511. TEST_F(ServerTest, PostMethod1)
  512. {
  513. auto res = cli_.Get("/person/john1");
  514. ASSERT_TRUE(res != nullptr);
  515. ASSERT_EQ(404, res->status);
  516. res = cli_.Post("/person", "name=john1&note=coder", "application/x-www-form-urlencoded");
  517. ASSERT_TRUE(res != nullptr);
  518. ASSERT_EQ(200, res->status);
  519. res = cli_.Get("/person/john1");
  520. ASSERT_TRUE(res != nullptr);
  521. ASSERT_EQ(200, res->status);
  522. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  523. ASSERT_EQ("coder", res->body);
  524. }
  525. TEST_F(ServerTest, PostMethod2)
  526. {
  527. auto res = cli_.Get("/person/john2");
  528. ASSERT_TRUE(res != nullptr);
  529. ASSERT_EQ(404, res->status);
  530. Params params;
  531. params.emplace("name", "john2");
  532. params.emplace("note", "coder");
  533. res = cli_.Post("/person", params);
  534. ASSERT_TRUE(res != nullptr);
  535. ASSERT_EQ(200, res->status);
  536. res = cli_.Get("/person/john2");
  537. ASSERT_TRUE(res != nullptr);
  538. ASSERT_EQ(200, res->status);
  539. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  540. ASSERT_EQ("coder", res->body);
  541. }
  542. TEST_F(ServerTest, PostWwwFormUrlEncodedJson)
  543. {
  544. Params params;
  545. params.emplace("json", JSON_DATA);
  546. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  547. ASSERT_TRUE(res != nullptr);
  548. ASSERT_EQ(200, res->status);
  549. ASSERT_EQ(JSON_DATA, res->body);
  550. }
  551. TEST_F(ServerTest, PostEmptyContent)
  552. {
  553. auto res = cli_.Post("/empty", "", "text/plain");
  554. ASSERT_TRUE(res != nullptr);
  555. ASSERT_EQ(200, res->status);
  556. ASSERT_EQ("empty", res->body);
  557. }
  558. TEST_F(ServerTest, GetMethodDir)
  559. {
  560. auto res = cli_.Get("/dir/");
  561. ASSERT_TRUE(res != nullptr);
  562. EXPECT_EQ(200, res->status);
  563. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  564. auto body = R"(<html>
  565. <head>
  566. </head>
  567. <body>
  568. <a href="/dir/test.html">Test</a>
  569. <a href="/hi">hi</a>
  570. </body>
  571. </html>
  572. )";
  573. EXPECT_EQ(body, res->body);
  574. }
  575. TEST_F(ServerTest, GetMethodDirTest)
  576. {
  577. auto res = cli_.Get("/dir/test.html");
  578. ASSERT_TRUE(res != nullptr);
  579. EXPECT_EQ(200, res->status);
  580. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  581. EXPECT_EQ("test.html", res->body);
  582. }
  583. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots)
  584. {
  585. auto res = cli_.Get("/dir/../dir/test.html");
  586. ASSERT_TRUE(res != nullptr);
  587. EXPECT_EQ(200, res->status);
  588. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  589. EXPECT_EQ("test.html", res->body);
  590. }
  591. TEST_F(ServerTest, GetMethodInvalidPath)
  592. {
  593. auto res = cli_.Get("/dir/../test.html");
  594. ASSERT_TRUE(res != nullptr);
  595. EXPECT_EQ(404, res->status);
  596. }
  597. TEST_F(ServerTest, GetMethodOutOfBaseDir)
  598. {
  599. auto res = cli_.Get("/../www/dir/test.html");
  600. ASSERT_TRUE(res != nullptr);
  601. EXPECT_EQ(404, res->status);
  602. }
  603. TEST_F(ServerTest, GetMethodOutOfBaseDir2)
  604. {
  605. auto res = cli_.Get("/dir/../../www/dir/test.html");
  606. ASSERT_TRUE(res != nullptr);
  607. EXPECT_EQ(404, res->status);
  608. }
  609. TEST_F(ServerTest, InvalidBaseDir)
  610. {
  611. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  612. EXPECT_EQ(true, svr_.set_base_dir("."));
  613. }
  614. TEST_F(ServerTest, EmptyRequest)
  615. {
  616. auto res = cli_.Get("");
  617. ASSERT_TRUE(res == nullptr);
  618. }
  619. TEST_F(ServerTest, LongRequest)
  620. {
  621. 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__");
  622. ASSERT_TRUE(res != nullptr);
  623. EXPECT_EQ(404, res->status);
  624. }
  625. TEST_F(ServerTest, TooLongRequest)
  626. {
  627. 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___");
  628. ASSERT_TRUE(res != nullptr);
  629. EXPECT_EQ(404, res->status);
  630. }
  631. TEST_F(ServerTest, LongHeader)
  632. {
  633. Request req;
  634. req.method = "GET";
  635. req.path = "/hi";
  636. std::string host_and_port;
  637. host_and_port += HOST;
  638. host_and_port += ":";
  639. host_and_port += std::to_string(PORT);
  640. req.headers.emplace("Host", host_and_port.c_str());
  641. req.headers.emplace("Accept", "*/*");
  642. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  643. req.headers.emplace("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  644. auto res = std::make_shared<Response>();
  645. auto ret = cli_.send(req, *res);
  646. ASSERT_TRUE(ret);
  647. EXPECT_EQ(200, res->status);
  648. }
  649. TEST_F(ServerTest, LongQueryValue)
  650. {
  651. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  652. ASSERT_TRUE(res != nullptr);
  653. EXPECT_EQ(200, res->status);
  654. }
  655. TEST_F(ServerTest, TooLongHeader)
  656. {
  657. Request req;
  658. req.method = "GET";
  659. req.path = "/hi";
  660. std::string host_and_port;
  661. host_and_port += HOST;
  662. host_and_port += ":";
  663. host_and_port += std::to_string(PORT);
  664. req.headers.emplace("Host", host_and_port.c_str());
  665. req.headers.emplace("Accept", "*/*");
  666. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  667. req.headers.emplace("Header-Name", "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  668. auto res = std::make_shared<Response>();
  669. auto ret = cli_.send(req, *res);
  670. ASSERT_TRUE(ret);
  671. EXPECT_EQ(200, res->status);
  672. }
  673. TEST_F(ServerTest, PercentEncoding)
  674. {
  675. auto res = cli_.Get("/e%6edwith%");
  676. ASSERT_TRUE(res != nullptr);
  677. EXPECT_EQ(200, res->status);
  678. }
  679. TEST_F(ServerTest, PercentEncodingUnicode)
  680. {
  681. auto res = cli_.Get("/e%u006edwith%");
  682. ASSERT_TRUE(res != nullptr);
  683. EXPECT_EQ(200, res->status);
  684. }
  685. TEST_F(ServerTest, InvalidPercentEncoding)
  686. {
  687. auto res = cli_.Get("/%endwith%");
  688. ASSERT_TRUE(res != nullptr);
  689. EXPECT_EQ(404, res->status);
  690. }
  691. TEST_F(ServerTest, InvalidPercentEncodingUnicode)
  692. {
  693. auto res = cli_.Get("/%uendwith%");
  694. ASSERT_TRUE(res != nullptr);
  695. EXPECT_EQ(404, res->status);
  696. }
  697. TEST_F(ServerTest, EndWithPercentCharacterInQuery)
  698. {
  699. auto res = cli_.Get("/hello?aaa=bbb%");
  700. ASSERT_TRUE(res != nullptr);
  701. EXPECT_EQ(404, res->status);
  702. }
  703. TEST_F(ServerTest, MultipartFormData)
  704. {
  705. Request req;
  706. req.method = "POST";
  707. req.path = "/multipart";
  708. std::string host_and_port;
  709. host_and_port += HOST;
  710. host_and_port += ":";
  711. host_and_port += std::to_string(PORT);
  712. req.headers.emplace("Host", host_and_port.c_str());
  713. req.headers.emplace("Accept", "*/*");
  714. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  715. req.headers.emplace("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarysBREP3G013oUrLB4");
  716. 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";
  717. auto res = std::make_shared<Response>();
  718. auto ret = cli_.send(req, *res);
  719. ASSERT_TRUE(ret);
  720. EXPECT_EQ(200, res->status);
  721. }
  722. TEST_F(ServerTest, CaseInsensitiveHeaderName)
  723. {
  724. auto res = cli_.Get("/hi");
  725. ASSERT_TRUE(res != nullptr);
  726. EXPECT_EQ(200, res->status);
  727. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  728. EXPECT_EQ("Hello World!", res->body);
  729. }
  730. TEST_F(ServerTest, CaseInsensitiveTransferEncoding)
  731. {
  732. Request req;
  733. req.method = "POST";
  734. req.path = "/chunked";
  735. std::string host_and_port;
  736. host_and_port += HOST;
  737. host_and_port += ":";
  738. host_and_port += std::to_string(PORT);
  739. req.headers.emplace("Host", host_and_port.c_str());
  740. req.headers.emplace("Accept", "*/*");
  741. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  742. req.headers.emplace("Content-Type", "text/plain");
  743. req.headers.emplace("Content-Length", "0");
  744. req.headers.emplace("Transfer-Encoding", "Chunked"); // Note, "Chunked" rather than typical "chunked".
  745. // Client does not chunk, so make a chunked body manually.
  746. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  747. auto res = std::make_shared<Response>();
  748. auto ret = cli_.send(req, *res);
  749. ASSERT_TRUE(ret);
  750. EXPECT_EQ(200, res->status);
  751. }
  752. TEST_F(ServerTest, GetStreamed)
  753. {
  754. auto res = cli_.Get("/streamed");
  755. ASSERT_TRUE(res != nullptr);
  756. EXPECT_EQ(200, res->status);
  757. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  758. EXPECT_TRUE(res->body == "aaabbb");
  759. }
  760. TEST_F(ServerTest, GetStreamedChunked)
  761. {
  762. auto res = cli_.Get("/streamedchunked");
  763. ASSERT_TRUE(res != nullptr);
  764. EXPECT_EQ(200, res->status);
  765. EXPECT_TRUE(res->body == "aaabbb");
  766. }
  767. TEST_F(ServerTest, LargeChunkedPost) {
  768. Request req;
  769. req.method = "POST";
  770. req.path = "/largechunked";
  771. std::string host_and_port;
  772. host_and_port += HOST;
  773. host_and_port += ":";
  774. host_and_port += std::to_string(PORT);
  775. req.headers.emplace("Host", host_and_port.c_str());
  776. req.headers.emplace("Accept", "*/*");
  777. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  778. req.headers.emplace("Content-Type", "text/plain");
  779. req.headers.emplace("Content-Length", "0");
  780. req.headers.emplace("Transfer-Encoding", "chunked");
  781. std::string long_string(30 * 1024u, 'a');
  782. std::string chunk = "7800\r\n" + long_string + "\r\n";
  783. // Attempt to make a large enough post to exceed OS buffers, to test that
  784. // the server handles short reads if the full chunk data isn't available.
  785. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  786. auto res = std::make_shared<Response>();
  787. auto ret = cli_.send(req, *res);
  788. ASSERT_TRUE(ret);
  789. EXPECT_EQ(200, res->status);
  790. }
  791. TEST_F(ServerTest, GetMethodRemoteAddr)
  792. {
  793. auto res = cli_.Get("/remote_addr");
  794. ASSERT_TRUE(res != nullptr);
  795. EXPECT_EQ(200, res->status);
  796. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  797. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  798. }
  799. TEST_F(ServerTest, SlowRequest)
  800. {
  801. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  802. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  803. request_threads_.push_back(std::thread([=]() { auto res = cli_.Get("/slow"); }));
  804. msleep(100);
  805. }
  806. TEST_F(ServerTest, Put)
  807. {
  808. auto res = cli_.Put("/put", "PUT", "text/plain");
  809. ASSERT_TRUE(res != nullptr);
  810. EXPECT_EQ(200, res->status);
  811. EXPECT_EQ("PUT", res->body);
  812. }
  813. TEST_F(ServerTest, Patch)
  814. {
  815. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  816. ASSERT_TRUE(res != nullptr);
  817. EXPECT_EQ(200, res->status);
  818. EXPECT_EQ("PATCH", res->body);
  819. }
  820. TEST_F(ServerTest, Delete)
  821. {
  822. auto res = cli_.Delete("/delete");
  823. ASSERT_TRUE(res != nullptr);
  824. EXPECT_EQ(200, res->status);
  825. EXPECT_EQ("DELETE", res->body);
  826. }
  827. TEST_F(ServerTest, Options)
  828. {
  829. auto res = cli_.Options("*");
  830. ASSERT_TRUE(res != nullptr);
  831. EXPECT_EQ(200, res->status);
  832. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  833. EXPECT_TRUE(res->body.empty());
  834. }
  835. TEST_F(ServerTest, URL)
  836. {
  837. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  838. ASSERT_TRUE(res != nullptr);
  839. EXPECT_EQ(200, res->status);
  840. }
  841. TEST_F(ServerTest, ArrayParam)
  842. {
  843. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  844. ASSERT_TRUE(res != nullptr);
  845. EXPECT_EQ(200, res->status);
  846. }
  847. TEST_F(ServerTest, NoMultipleHeaders)
  848. {
  849. Headers headers;
  850. headers.emplace("Content-Length", "5");
  851. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello", "text/plain");
  852. ASSERT_TRUE(res != nullptr);
  853. EXPECT_EQ(200, res->status);
  854. }
  855. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  856. TEST_F(ServerTest, Gzip)
  857. {
  858. Headers headers;
  859. headers.emplace("Accept-Encoding", "gzip, deflate");
  860. auto res = cli_.Get("/gzip", headers);
  861. ASSERT_TRUE(res != nullptr);
  862. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  863. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  864. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  865. EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body);
  866. EXPECT_EQ(200, res->status);
  867. }
  868. TEST_F(ServerTest, NoGzip)
  869. {
  870. Headers headers;
  871. headers.emplace("Accept-Encoding", "gzip, deflate");
  872. auto res = cli_.Get("/nogzip", headers);
  873. ASSERT_TRUE(res != nullptr);
  874. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  875. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  876. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  877. EXPECT_EQ("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890", res->body);
  878. EXPECT_EQ(200, res->status);
  879. }
  880. TEST_F(ServerTest, MultipartFormDataGzip)
  881. {
  882. Request req;
  883. req.method = "POST";
  884. req.path = "/gzipmultipart";
  885. std::string host_and_port;
  886. host_and_port += HOST;
  887. host_and_port += ":";
  888. host_and_port += std::to_string(PORT);
  889. req.headers.emplace("Host", host_and_port.c_str());
  890. req.headers.emplace("Accept", "*/*");
  891. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  892. req.headers.emplace("Content-Type", "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  893. req.headers.emplace("Content-Encoding", "gzip");
  894. // compressed_body generated by creating input.txt to this file:
  895. /*
  896. --------------------------fcba8368a9f48c0f
  897. Content-Disposition: form-data; name="key1"
  898. test
  899. --------------------------fcba8368a9f48c0f
  900. Content-Disposition: form-data; name="key2"
  901. --abcdefg123
  902. --------------------------fcba8368a9f48c0f--
  903. */
  904. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  905. uint8_t compressed_body[] = {
  906. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  907. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  908. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  909. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  910. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  911. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  912. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  913. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  914. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  915. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  916. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00
  917. };
  918. req.body = std::string((char*)compressed_body, sizeof(compressed_body) / sizeof(compressed_body[0]));
  919. auto res = std::make_shared<Response>();
  920. auto ret = cli_.send(req, *res);
  921. ASSERT_TRUE(ret);
  922. EXPECT_EQ(200, res->status);
  923. }
  924. #endif
  925. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  926. protected:
  927. ServerTestWithAI_PASSIVE()
  928. : cli_(HOST, PORT)
  929. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  930. , svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  931. #endif
  932. {}
  933. virtual void SetUp() {
  934. svr_.Get("/hi", [&](const Request& /*req*/, Response& res) {
  935. res.set_content("Hello World!", "text/plain");
  936. });
  937. t_ = thread([&]() {
  938. ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE));
  939. });
  940. while (!svr_.is_running()) {
  941. msleep(1);
  942. }
  943. }
  944. virtual void TearDown() {
  945. svr_.stop();
  946. t_.join();
  947. }
  948. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  949. SSLClient cli_;
  950. SSLServer svr_;
  951. #else
  952. Client cli_;
  953. Server svr_;
  954. #endif
  955. thread t_;
  956. };
  957. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200)
  958. {
  959. auto res = cli_.Get("/hi");
  960. ASSERT_TRUE(res != nullptr);
  961. EXPECT_EQ(200, res->status);
  962. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  963. EXPECT_EQ("Hello World!", res->body);
  964. }
  965. class ServerUpDownTest : public ::testing::Test {
  966. protected:
  967. ServerUpDownTest()
  968. : cli_(HOST, PORT)
  969. {}
  970. virtual void SetUp() {
  971. t_ = thread([&](){
  972. svr_.bind_to_any_port(HOST);
  973. msleep(500);
  974. ASSERT_TRUE(svr_.listen_after_bind());
  975. });
  976. while (!svr_.is_running()) {
  977. msleep(1);
  978. }
  979. }
  980. virtual void TearDown() {
  981. svr_.stop();
  982. t_.join();
  983. }
  984. Client cli_;
  985. Server svr_;
  986. thread t_;
  987. };
  988. TEST_F(ServerUpDownTest, QuickStartStop)
  989. {
  990. // Should not crash, especially when run with
  991. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  992. }
  993. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  994. TEST(SSLClientTest, ServerNameIndication)
  995. {
  996. SSLClient cli("httpbin.org", 443);
  997. auto res = cli.Get("/get");
  998. ASSERT_TRUE(res != nullptr);
  999. ASSERT_EQ(200, res->status);
  1000. }
  1001. #endif
  1002. #ifdef _WIN32
  1003. TEST(CleanupTest, WSACleanup)
  1004. {
  1005. int ret = WSACleanup();
  1006. ASSERT_EQ(0, ret);
  1007. }
  1008. #endif
  1009. // vim: et ts=4 sw=4 cin cino={1s ff=unix