test.cc 40 KB

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