test.cc 35 KB

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