test.cc 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  1. #include <future>
  2. #include <gtest/gtest.h>
  3. #include <httplib.h>
  4. #define SERVER_CERT_FILE "./cert.pem"
  5. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  6. #define CA_CERT_FILE "./ca-bundle.crt"
  7. #define CLIENT_CA_CERT_FILE "./rootCA.cert.pem"
  8. #define CLIENT_CA_CERT_DIR "."
  9. #define CLIENT_CERT_FILE "./client.cert.pem"
  10. #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem"
  11. #ifdef _WIN32
  12. #include <process.h>
  13. #define msleep(n) ::Sleep(n)
  14. #else
  15. #define msleep(n) ::usleep(n * 1000)
  16. #endif
  17. using namespace std;
  18. using namespace httplib;
  19. const char *HOST = "localhost";
  20. const int PORT = 1234;
  21. const string LONG_QUERY_VALUE = string(25000, '@');
  22. const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
  23. const std::string JSON_DATA = "{\"hello\":\"world\"}";
  24. #ifdef _WIN32
  25. TEST(StartupTest, WSAStartup) {
  26. WSADATA wsaData;
  27. int ret = WSAStartup(0x0002, &wsaData);
  28. ASSERT_EQ(0, ret);
  29. }
  30. #endif
  31. TEST(SplitTest, ParseQueryString) {
  32. string s = "key1=val1&key2=val2&key3=val3";
  33. Params dic;
  34. detail::split(s.c_str(), s.c_str() + s.size(), '&',
  35. [&](const char *b, const char *e) {
  36. string key, val;
  37. detail::split(b, e, '=', [&](const char *b, const char *e) {
  38. if (key.empty()) {
  39. key.assign(b, e);
  40. } else {
  41. val.assign(b, e);
  42. }
  43. });
  44. dic.emplace(key, val);
  45. });
  46. EXPECT_EQ("val1", dic.find("key1")->second);
  47. EXPECT_EQ("val2", dic.find("key2")->second);
  48. EXPECT_EQ("val3", dic.find("key3")->second);
  49. }
  50. TEST(ParseQueryTest, ParseQueryString) {
  51. string s = "key1=val1&key2=val2&key3=val3";
  52. Params dic;
  53. detail::parse_query_text(s, dic);
  54. EXPECT_EQ("val1", dic.find("key1")->second);
  55. EXPECT_EQ("val2", dic.find("key2")->second);
  56. EXPECT_EQ("val3", dic.find("key3")->second);
  57. }
  58. TEST(GetHeaderValueTest, DefaultValue) {
  59. Headers headers = {{"Dummy", "Dummy"}};
  60. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  61. EXPECT_STREQ("text/plain", val);
  62. }
  63. TEST(GetHeaderValueTest, DefaultValueInt) {
  64. Headers headers = {{"Dummy", "Dummy"}};
  65. auto val = detail::get_header_value_uint64(headers, "Content-Length", 100);
  66. EXPECT_EQ(100ull, val);
  67. }
  68. TEST(GetHeaderValueTest, RegularValue) {
  69. Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
  70. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  71. EXPECT_STREQ("text/html", val);
  72. }
  73. TEST(GetHeaderValueTest, RegularValueInt) {
  74. Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
  75. auto val = detail::get_header_value_uint64(headers, "Content-Length", 0);
  76. EXPECT_EQ(100ull, val);
  77. }
  78. TEST(GetHeaderValueTest, Range) {
  79. {
  80. Headers headers = {make_range_header({{1, -1}})};
  81. auto val = detail::get_header_value(headers, "Range", 0, 0);
  82. EXPECT_STREQ("bytes=1-", val);
  83. }
  84. {
  85. Headers headers = {make_range_header({{-1, 1}})};
  86. auto val = detail::get_header_value(headers, "Range", 0, 0);
  87. EXPECT_STREQ("bytes=-1", val);
  88. }
  89. {
  90. Headers headers = {make_range_header({{1, 10}})};
  91. auto val = detail::get_header_value(headers, "Range", 0, 0);
  92. EXPECT_STREQ("bytes=1-10", val);
  93. }
  94. {
  95. Headers headers = {make_range_header({{1, 10}, {100, -1}})};
  96. auto val = detail::get_header_value(headers, "Range", 0, 0);
  97. EXPECT_STREQ("bytes=1-10, 100-", val);
  98. }
  99. {
  100. Headers headers = {make_range_header({{1, 10}, {100, 200}})};
  101. auto val = detail::get_header_value(headers, "Range", 0, 0);
  102. EXPECT_STREQ("bytes=1-10, 100-200", val);
  103. }
  104. {
  105. Headers headers = {make_range_header({{0, 0}, {-1, 1}})};
  106. auto val = detail::get_header_value(headers, "Range", 0, 0);
  107. EXPECT_STREQ("bytes=0-0, -1", val);
  108. }
  109. }
  110. TEST(ParseHeaderValueTest, Range) {
  111. {
  112. Ranges ranges;
  113. auto ret = detail::parse_range_header("bytes=1-", ranges);
  114. EXPECT_TRUE(ret);
  115. EXPECT_EQ(1u, ranges.size());
  116. EXPECT_EQ(1u, ranges[0].first);
  117. EXPECT_EQ(-1, ranges[0].second);
  118. }
  119. {
  120. Ranges ranges;
  121. auto ret = detail::parse_range_header("bytes=-1", ranges);
  122. EXPECT_TRUE(ret);
  123. EXPECT_EQ(1u, ranges.size());
  124. EXPECT_EQ(-1, ranges[0].first);
  125. EXPECT_EQ(1u, ranges[0].second);
  126. }
  127. {
  128. Ranges ranges;
  129. auto ret = detail::parse_range_header("bytes=1-10", ranges);
  130. EXPECT_TRUE(ret);
  131. EXPECT_EQ(1u, ranges.size());
  132. EXPECT_EQ(1u, ranges[0].first);
  133. EXPECT_EQ(10u, ranges[0].second);
  134. }
  135. {
  136. Ranges ranges;
  137. auto ret = detail::parse_range_header("bytes=10-1", ranges);
  138. EXPECT_FALSE(ret);
  139. }
  140. {
  141. Ranges ranges;
  142. auto ret = detail::parse_range_header("bytes=1-10, 100-", ranges);
  143. EXPECT_TRUE(ret);
  144. EXPECT_EQ(2u, ranges.size());
  145. EXPECT_EQ(1u, ranges[0].first);
  146. EXPECT_EQ(10u, ranges[0].second);
  147. EXPECT_EQ(100u, ranges[1].first);
  148. EXPECT_EQ(-1, ranges[1].second);
  149. }
  150. {
  151. Ranges ranges;
  152. auto ret =
  153. detail::parse_range_header("bytes=1-10, 100-200, 300-400", ranges);
  154. EXPECT_TRUE(ret);
  155. EXPECT_EQ(3u, ranges.size());
  156. EXPECT_EQ(1u, ranges[0].first);
  157. EXPECT_EQ(10u, ranges[0].second);
  158. EXPECT_EQ(100u, ranges[1].first);
  159. EXPECT_EQ(200u, ranges[1].second);
  160. EXPECT_EQ(300u, ranges[2].first);
  161. EXPECT_EQ(400u, ranges[2].second);
  162. }
  163. }
  164. TEST(ChunkedEncodingTest, FromHTTPWatch) {
  165. auto host = "www.httpwatch.com";
  166. auto sec = 2;
  167. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  168. auto port = 443;
  169. httplib::SSLClient cli(host, port, sec);
  170. #else
  171. auto port = 80;
  172. httplib::Client cli(host, port, sec);
  173. #endif
  174. auto res =
  175. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
  176. ASSERT_TRUE(res != nullptr);
  177. std::string out;
  178. httplib::detail::read_file("./image.jpg", out);
  179. EXPECT_EQ(200, res->status);
  180. EXPECT_EQ(out, res->body);
  181. }
  182. TEST(ChunkedEncodingTest, WithContentReceiver) {
  183. auto host = "www.httpwatch.com";
  184. auto sec = 2;
  185. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  186. auto port = 443;
  187. httplib::SSLClient cli(host, port, sec);
  188. #else
  189. auto port = 80;
  190. httplib::Client cli(host, port, sec);
  191. #endif
  192. std::string body;
  193. auto res =
  194. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
  195. [&](const char *data, size_t data_length, uint64_t, uint64_t) {
  196. body.append(data, data_length);
  197. return true;
  198. });
  199. ASSERT_TRUE(res != nullptr);
  200. std::string out;
  201. httplib::detail::read_file("./image.jpg", out);
  202. EXPECT_EQ(200, res->status);
  203. EXPECT_EQ(out, body);
  204. }
  205. TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
  206. auto host = "www.httpwatch.com";
  207. auto sec = 2;
  208. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  209. auto port = 443;
  210. httplib::SSLClient cli(host, port, sec);
  211. #else
  212. auto port = 80;
  213. httplib::Client cli(host, port, sec);
  214. #endif
  215. std::string body;
  216. auto res = cli.Get(
  217. "/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", Headers(),
  218. [&](const Response &response) {
  219. EXPECT_EQ(200, response.status);
  220. return true;
  221. },
  222. [&](const char *data, size_t data_length, uint64_t, uint64_t) {
  223. body.append(data, data_length);
  224. return true;
  225. });
  226. ASSERT_TRUE(res != nullptr);
  227. std::string out;
  228. httplib::detail::read_file("./image.jpg", out);
  229. EXPECT_EQ(200, res->status);
  230. EXPECT_EQ(out, body);
  231. }
  232. TEST(RangeTest, FromHTTPBin) {
  233. auto host = "httpbin.org";
  234. auto sec = 5;
  235. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  236. auto port = 443;
  237. httplib::SSLClient cli(host, port, sec);
  238. #else
  239. auto port = 80;
  240. httplib::Client cli(host, port, sec);
  241. #endif
  242. {
  243. httplib::Headers headers;
  244. auto res = cli.Get("/range/32", headers);
  245. ASSERT_TRUE(res != nullptr);
  246. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  247. EXPECT_EQ(200, res->status);
  248. }
  249. {
  250. httplib::Headers headers = {httplib::make_range_header({{1, -1}})};
  251. auto res = cli.Get("/range/32", headers);
  252. ASSERT_TRUE(res != nullptr);
  253. EXPECT_EQ(res->body, "bcdefghijklmnopqrstuvwxyzabcdef");
  254. EXPECT_EQ(206, res->status);
  255. }
  256. {
  257. httplib::Headers headers = {httplib::make_range_header({{1, 10}})};
  258. auto res = cli.Get("/range/32", headers);
  259. ASSERT_TRUE(res != nullptr);
  260. EXPECT_EQ(res->body, "bcdefghijk");
  261. EXPECT_EQ(206, res->status);
  262. }
  263. {
  264. httplib::Headers headers = {httplib::make_range_header({{0, 31}})};
  265. auto res = cli.Get("/range/32", headers);
  266. ASSERT_TRUE(res != nullptr);
  267. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  268. EXPECT_EQ(200, res->status);
  269. }
  270. {
  271. httplib::Headers headers = {httplib::make_range_header({{0, -1}})};
  272. auto res = cli.Get("/range/32", headers);
  273. ASSERT_TRUE(res != nullptr);
  274. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  275. EXPECT_EQ(200, res->status);
  276. }
  277. {
  278. httplib::Headers headers = {httplib::make_range_header({{0, 32}})};
  279. auto res = cli.Get("/range/32", headers);
  280. ASSERT_TRUE(res != nullptr);
  281. EXPECT_EQ(416, res->status);
  282. }
  283. }
  284. TEST(ConnectionErrorTest, InvalidHost) {
  285. auto host = "-abcde.com";
  286. auto sec = 2;
  287. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  288. auto port = 443;
  289. httplib::SSLClient cli(host, port, sec);
  290. #else
  291. auto port = 80;
  292. httplib::Client cli(host, port, sec);
  293. #endif
  294. auto res = cli.Get("/");
  295. ASSERT_TRUE(res == nullptr);
  296. }
  297. TEST(ConnectionErrorTest, InvalidPort) {
  298. auto host = "localhost";
  299. auto sec = 2;
  300. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  301. auto port = 44380;
  302. httplib::SSLClient cli(host, port, sec);
  303. #else
  304. auto port = 8080;
  305. httplib::Client cli(host, port, sec);
  306. #endif
  307. auto res = cli.Get("/");
  308. ASSERT_TRUE(res == nullptr);
  309. }
  310. TEST(ConnectionErrorTest, Timeout) {
  311. auto host = "google.com";
  312. auto sec = 2;
  313. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  314. auto port = 44380;
  315. httplib::SSLClient cli(host, port, sec);
  316. #else
  317. auto port = 8080;
  318. httplib::Client cli(host, port, sec);
  319. #endif
  320. auto res = cli.Get("/");
  321. ASSERT_TRUE(res == nullptr);
  322. }
  323. TEST(CancelTest, NoCancel) {
  324. auto host = "httpbin.org";
  325. auto sec = 5;
  326. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  327. auto port = 443;
  328. httplib::SSLClient cli(host, port, sec);
  329. #else
  330. auto port = 80;
  331. httplib::Client cli(host, port, sec);
  332. #endif
  333. auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; });
  334. ASSERT_TRUE(res != nullptr);
  335. EXPECT_EQ(res->body, "abcdefghijklmnopqrstuvwxyzabcdef");
  336. EXPECT_EQ(200, res->status);
  337. }
  338. TEST(CancelTest, WithCancelSmallPayload) {
  339. auto host = "httpbin.org";
  340. auto sec = 5;
  341. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  342. auto port = 443;
  343. httplib::SSLClient cli(host, port, sec);
  344. #else
  345. auto port = 80;
  346. httplib::Client cli(host, port, sec);
  347. #endif
  348. auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return false; });
  349. ASSERT_TRUE(res == nullptr);
  350. }
  351. TEST(CancelTest, WithCancelLargePayload) {
  352. auto host = "httpbin.org";
  353. auto sec = 5;
  354. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  355. auto port = 443;
  356. httplib::SSLClient cli(host, port, sec);
  357. #else
  358. auto port = 80;
  359. httplib::Client cli(host, port, sec);
  360. #endif
  361. uint32_t count = 0;
  362. httplib::Headers headers;
  363. auto res = cli.Get("/range/65536", headers,
  364. [&count](uint64_t, uint64_t) { return (count++ == 0); });
  365. ASSERT_TRUE(res == nullptr);
  366. }
  367. TEST(BaseAuthTest, FromHTTPWatch) {
  368. auto host = "httpbin.org";
  369. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  370. auto port = 443;
  371. httplib::SSLClient cli(host, port);
  372. #else
  373. auto port = 80;
  374. httplib::Client cli(host, port);
  375. #endif
  376. {
  377. auto res = cli.Get("/basic-auth/hello/world");
  378. ASSERT_TRUE(res != nullptr);
  379. EXPECT_EQ(401, res->status);
  380. }
  381. {
  382. auto res =
  383. cli.Get("/basic-auth/hello/world",
  384. {httplib::make_basic_authentication_header("hello", "world")});
  385. ASSERT_TRUE(res != nullptr);
  386. EXPECT_EQ(res->body,
  387. "{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n");
  388. EXPECT_EQ(200, res->status);
  389. }
  390. }
  391. TEST(AbsoluteRedirectTest, Redirect) {
  392. auto host = "httpbin.org";
  393. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  394. httplib::SSLClient cli(host);
  395. #else
  396. httplib::Client cli(host);
  397. #endif
  398. cli.follow_location(true);
  399. auto res = cli.Get("/absolute-redirect/3");
  400. ASSERT_TRUE(res != nullptr);
  401. EXPECT_EQ(200, res->status);
  402. }
  403. TEST(RedirectTest, Redirect) {
  404. auto host = "httpbin.org";
  405. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  406. httplib::SSLClient cli(host);
  407. #else
  408. httplib::Client cli(host);
  409. #endif
  410. cli.follow_location(true);
  411. auto res = cli.Get("/redirect/3");
  412. ASSERT_TRUE(res != nullptr);
  413. EXPECT_EQ(200, res->status);
  414. }
  415. TEST(RelativeRedirectTest, Redirect) {
  416. auto host = "httpbin.org";
  417. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  418. httplib::SSLClient cli(host);
  419. #else
  420. httplib::Client cli(host);
  421. #endif
  422. cli.follow_location(true);
  423. auto res = cli.Get("/relative-redirect/3");
  424. ASSERT_TRUE(res != nullptr);
  425. EXPECT_EQ(200, res->status);
  426. }
  427. TEST(TooManyRedirectTest, Redirect) {
  428. auto host = "httpbin.org";
  429. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  430. httplib::SSLClient cli(host);
  431. #else
  432. httplib::Client cli(host);
  433. #endif
  434. cli.follow_location(true);
  435. auto res = cli.Get("/redirect/21");
  436. ASSERT_TRUE(res == nullptr);
  437. }
  438. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  439. TEST(YahooRedirectTest, Redirect) {
  440. httplib::Client cli("yahoo.com");
  441. auto res = cli.Get("/");
  442. ASSERT_TRUE(res != nullptr);
  443. EXPECT_EQ(301, res->status);
  444. cli.follow_location(true);
  445. res = cli.Get("/");
  446. ASSERT_TRUE(res != nullptr);
  447. EXPECT_EQ(200, res->status);
  448. }
  449. TEST(HttpsToHttpRedirectTest, Redirect) {
  450. httplib::SSLClient cli("httpbin.org");
  451. cli.follow_location(true);
  452. auto res =
  453. cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
  454. ASSERT_TRUE(res != nullptr);
  455. }
  456. #endif
  457. TEST(Server, BindAndListenSeparately) {
  458. Server svr;
  459. int port = svr.bind_to_any_port("localhost");
  460. ASSERT_TRUE(port > 0);
  461. svr.stop();
  462. }
  463. class ServerTest : public ::testing::Test {
  464. protected:
  465. ServerTest()
  466. : cli_(HOST, PORT)
  467. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  468. ,
  469. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  470. #endif
  471. {
  472. }
  473. virtual void SetUp() {
  474. svr_.set_base_dir("./www");
  475. svr_.Get("/hi",
  476. [&](const Request & /*req*/, Response &res) {
  477. res.set_content("Hello World!", "text/plain");
  478. })
  479. .Get("/slow",
  480. [&](const Request & /*req*/, Response &res) {
  481. msleep(2000);
  482. res.set_content("slow", "text/plain");
  483. })
  484. .Get("/remote_addr",
  485. [&](const Request &req, Response &res) {
  486. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  487. res.set_content(remote_addr.c_str(), "text/plain");
  488. })
  489. .Get("/endwith%",
  490. [&](const Request & /*req*/, Response &res) {
  491. res.set_content("Hello World!", "text/plain");
  492. })
  493. .Get("/", [&](const Request & /*req*/,
  494. Response &res) { res.set_redirect("/hi"); })
  495. .Post("/person",
  496. [&](const Request &req, Response &res) {
  497. if (req.has_param("name") && req.has_param("note")) {
  498. persons_[req.get_param_value("name")] =
  499. req.get_param_value("note");
  500. } else {
  501. res.status = 400;
  502. }
  503. })
  504. .Get("/person/(.*)",
  505. [&](const Request &req, Response &res) {
  506. string name = req.matches[1];
  507. if (persons_.find(name) != persons_.end()) {
  508. auto note = persons_[name];
  509. res.set_content(note, "text/plain");
  510. } else {
  511. res.status = 404;
  512. }
  513. })
  514. .Post("/x-www-form-urlencoded-json",
  515. [&](const Request &req, Response &res) {
  516. auto json = req.get_param_value("json");
  517. ASSERT_EQ(JSON_DATA, json);
  518. res.set_content(json, "appliation/json");
  519. res.status = 200;
  520. })
  521. .Get("/streamed-chunked",
  522. [&](const Request & /*req*/, Response &res) {
  523. res.set_chunked_content_provider(
  524. [](uint64_t /*offset*/, DataSink sink, Done done) {
  525. sink("123", 3);
  526. sink("456", 3);
  527. sink("789", 3);
  528. done();
  529. });
  530. })
  531. .Get("/streamed",
  532. [&](const Request & /*req*/, Response &res) {
  533. res.set_content_provider(
  534. 6, [](uint64_t offset, uint64_t /*length*/, DataSink sink) {
  535. sink(offset < 3 ? "a" : "b", 1);
  536. });
  537. })
  538. .Get("/streamed-with-range",
  539. [&](const Request & /*req*/, Response &res) {
  540. auto data = new std::string("abcdefg");
  541. res.set_content_provider(
  542. data->size(),
  543. [data](uint64_t offset, uint64_t length, DataSink sink) {
  544. size_t DATA_CHUNK_SIZE = 4;
  545. const auto &d = *data;
  546. auto out_len =
  547. std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
  548. sink(&d[static_cast<size_t>(offset)], out_len);
  549. },
  550. [data] { delete data; });
  551. })
  552. .Get("/streamed-cancel",
  553. [&](const Request & /*req*/, Response &res) {
  554. res.set_content_provider(
  555. size_t(-1),
  556. [](uint64_t /*offset*/, uint64_t /*length*/, DataSink sink) {
  557. std::string data = "data_chunk";
  558. sink(data.data(), data.size());
  559. });
  560. })
  561. .Get("/with-range",
  562. [&](const Request & /*req*/, Response &res) {
  563. res.set_content("abcdefg", "text/plain");
  564. })
  565. .Post("/chunked",
  566. [&](const Request &req, Response & /*res*/) {
  567. EXPECT_EQ(req.body, "dechunked post body");
  568. })
  569. .Post("/large-chunked",
  570. [&](const Request &req, Response & /*res*/) {
  571. std::string expected(6 * 30 * 1024u, 'a');
  572. EXPECT_EQ(req.body, expected);
  573. })
  574. .Post("/multipart",
  575. [&](const Request &req, Response & /*res*/) {
  576. EXPECT_EQ(5u, req.files.size());
  577. ASSERT_TRUE(!req.has_file("???"));
  578. {
  579. const auto &file = req.get_file_value("text1");
  580. EXPECT_EQ("", file.filename);
  581. EXPECT_EQ("text default",
  582. req.body.substr(file.offset, file.length));
  583. }
  584. {
  585. const auto &file = req.get_file_value("text2");
  586. EXPECT_EQ("", file.filename);
  587. EXPECT_EQ("aωb", req.body.substr(file.offset, file.length));
  588. }
  589. {
  590. const auto &file = req.get_file_value("file1");
  591. EXPECT_EQ("hello.txt", file.filename);
  592. EXPECT_EQ("text/plain", file.content_type);
  593. EXPECT_EQ("h\ne\n\nl\nl\no\n",
  594. req.body.substr(file.offset, file.length));
  595. }
  596. {
  597. const auto &file = req.get_file_value("file3");
  598. EXPECT_EQ("", file.filename);
  599. EXPECT_EQ("application/octet-stream", file.content_type);
  600. EXPECT_EQ(0u, file.length);
  601. }
  602. })
  603. .Post("/empty",
  604. [&](const Request &req, Response &res) {
  605. EXPECT_EQ(req.body, "");
  606. res.set_content("empty", "text/plain");
  607. })
  608. .Put("/put",
  609. [&](const Request &req, Response &res) {
  610. EXPECT_EQ(req.body, "PUT");
  611. res.set_content(req.body, "text/plain");
  612. })
  613. .Patch("/patch",
  614. [&](const Request &req, Response &res) {
  615. EXPECT_EQ(req.body, "PATCH");
  616. res.set_content(req.body, "text/plain");
  617. })
  618. .Delete("/delete",
  619. [&](const Request & /*req*/, Response &res) {
  620. res.set_content("DELETE", "text/plain");
  621. })
  622. .Options(R"(\*)",
  623. [&](const Request & /*req*/, Response &res) {
  624. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  625. })
  626. .Get("/request-target",
  627. [&](const Request &req, Response & /*res*/) {
  628. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  629. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  630. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  631. })
  632. .Get("/long-query-value",
  633. [&](const Request &req, Response & /*res*/) {
  634. EXPECT_EQ(LONG_QUERY_URL, req.target);
  635. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  636. })
  637. .Get("/array-param",
  638. [&](const Request &req, Response & /*res*/) {
  639. EXPECT_EQ(3u, req.get_param_value_count("array"));
  640. EXPECT_EQ("value1", req.get_param_value("array", 0));
  641. EXPECT_EQ("value2", req.get_param_value("array", 1));
  642. EXPECT_EQ("value3", req.get_param_value("array", 2));
  643. })
  644. .Post("/validate-no-multiple-headers",
  645. [&](const Request &req, Response & /*res*/) {
  646. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  647. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  648. })
  649. .Post("/content_receiver",
  650. [&](const Request & req, Response &res,
  651. const ContentReader &content_reader) {
  652. std::string body;
  653. content_reader([&](const char *data, size_t data_length,
  654. size_t offset,
  655. uint64_t content_length) {
  656. EXPECT_EQ(offset, 0);
  657. if (req.get_header_value("Content-Encoding") == "gzip") {
  658. EXPECT_EQ(content_length, 0);
  659. } else {
  660. EXPECT_EQ(content_length, 7);
  661. }
  662. EXPECT_EQ(data_length, 7);
  663. body.append(data, data_length);
  664. return true;
  665. });
  666. EXPECT_EQ(body, "content");
  667. res.set_content(body, "text/plain");
  668. })
  669. .Put("/content_receiver",
  670. [&](const Request & /*req*/, Response &res,
  671. const ContentReader &content_reader) {
  672. std::string body;
  673. content_reader([&](const char *data, size_t data_length,
  674. size_t /*offset*/,
  675. uint64_t /*content_length*/) {
  676. body.append(data, data_length);
  677. return true;
  678. });
  679. EXPECT_EQ(body, "content");
  680. res.set_content(body, "text/plain");
  681. })
  682. .Patch("/content_receiver",
  683. [&](const Request & /*req*/, Response &res,
  684. const ContentReader &content_reader) {
  685. std::string body;
  686. content_reader([&](const char *data, size_t data_length,
  687. size_t /*offset*/,
  688. uint64_t /*content_length*/) {
  689. body.append(data, data_length);
  690. return true;
  691. });
  692. EXPECT_EQ(body, "content");
  693. res.set_content(body, "text/plain");
  694. })
  695. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  696. .Get("/gzip",
  697. [&](const Request & /*req*/, Response &res) {
  698. res.set_content(
  699. "12345678901234567890123456789012345678901234567890123456789"
  700. "01234567890123456789012345678901234567890",
  701. "text/plain");
  702. })
  703. .Get("/nogzip",
  704. [&](const Request & /*req*/, Response &res) {
  705. res.set_content(
  706. "12345678901234567890123456789012345678901234567890123456789"
  707. "01234567890123456789012345678901234567890",
  708. "application/octet-stream");
  709. })
  710. .Post("/gzipmultipart",
  711. [&](const Request &req, Response & /*res*/) {
  712. EXPECT_EQ(2u, req.files.size());
  713. ASSERT_TRUE(!req.has_file("???"));
  714. {
  715. const auto &file = req.get_file_value("key1");
  716. EXPECT_EQ("", file.filename);
  717. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  718. }
  719. {
  720. const auto &file = req.get_file_value("key2");
  721. EXPECT_EQ("", file.filename);
  722. EXPECT_EQ("--abcdefg123",
  723. req.body.substr(file.offset, file.length));
  724. }
  725. })
  726. #endif
  727. ;
  728. persons_["john"] = "programmer";
  729. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  730. while (!svr_.is_running()) {
  731. msleep(1);
  732. }
  733. }
  734. virtual void TearDown() {
  735. svr_.stop();
  736. for (auto &t : request_threads_) {
  737. t.join();
  738. }
  739. t_.join();
  740. }
  741. map<string, string> persons_;
  742. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  743. SSLClient cli_;
  744. SSLServer svr_;
  745. #else
  746. Client cli_;
  747. Server svr_;
  748. #endif
  749. thread t_;
  750. std::vector<thread> request_threads_;
  751. };
  752. TEST_F(ServerTest, GetMethod200) {
  753. auto res = cli_.Get("/hi");
  754. ASSERT_TRUE(res != nullptr);
  755. EXPECT_EQ("HTTP/1.1", res->version);
  756. EXPECT_EQ(200, res->status);
  757. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  758. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  759. EXPECT_EQ("Hello World!", res->body);
  760. }
  761. TEST_F(ServerTest, GetMethod302) {
  762. auto res = cli_.Get("/");
  763. ASSERT_TRUE(res != nullptr);
  764. EXPECT_EQ(302, res->status);
  765. EXPECT_EQ("/hi", res->get_header_value("Location"));
  766. }
  767. TEST_F(ServerTest, GetMethod404) {
  768. auto res = cli_.Get("/invalid");
  769. ASSERT_TRUE(res != nullptr);
  770. EXPECT_EQ(404, res->status);
  771. }
  772. TEST_F(ServerTest, HeadMethod200) {
  773. auto res = cli_.Head("/hi");
  774. ASSERT_TRUE(res != nullptr);
  775. EXPECT_EQ(200, res->status);
  776. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  777. EXPECT_EQ("", res->body);
  778. }
  779. TEST_F(ServerTest, HeadMethod404) {
  780. auto res = cli_.Head("/invalid");
  781. ASSERT_TRUE(res != nullptr);
  782. EXPECT_EQ(404, res->status);
  783. EXPECT_EQ("", res->body);
  784. }
  785. TEST_F(ServerTest, GetMethodPersonJohn) {
  786. auto res = cli_.Get("/person/john");
  787. ASSERT_TRUE(res != nullptr);
  788. EXPECT_EQ(200, res->status);
  789. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  790. EXPECT_EQ("programmer", res->body);
  791. }
  792. TEST_F(ServerTest, PostMethod1) {
  793. auto res = cli_.Get("/person/john1");
  794. ASSERT_TRUE(res != nullptr);
  795. ASSERT_EQ(404, res->status);
  796. res = cli_.Post("/person", "name=john1&note=coder",
  797. "application/x-www-form-urlencoded");
  798. ASSERT_TRUE(res != nullptr);
  799. ASSERT_EQ(200, res->status);
  800. res = cli_.Get("/person/john1");
  801. ASSERT_TRUE(res != nullptr);
  802. ASSERT_EQ(200, res->status);
  803. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  804. ASSERT_EQ("coder", res->body);
  805. }
  806. TEST_F(ServerTest, PostMethod2) {
  807. auto res = cli_.Get("/person/john2");
  808. ASSERT_TRUE(res != nullptr);
  809. ASSERT_EQ(404, res->status);
  810. Params params;
  811. params.emplace("name", "john2");
  812. params.emplace("note", "coder");
  813. res = cli_.Post("/person", params);
  814. ASSERT_TRUE(res != nullptr);
  815. ASSERT_EQ(200, res->status);
  816. res = cli_.Get("/person/john2");
  817. ASSERT_TRUE(res != nullptr);
  818. ASSERT_EQ(200, res->status);
  819. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  820. ASSERT_EQ("coder", res->body);
  821. }
  822. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  823. Params params;
  824. params.emplace("json", JSON_DATA);
  825. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  826. ASSERT_TRUE(res != nullptr);
  827. ASSERT_EQ(200, res->status);
  828. ASSERT_EQ(JSON_DATA, res->body);
  829. }
  830. TEST_F(ServerTest, PostEmptyContent) {
  831. auto res = cli_.Post("/empty", "", "text/plain");
  832. ASSERT_TRUE(res != nullptr);
  833. ASSERT_EQ(200, res->status);
  834. ASSERT_EQ("empty", res->body);
  835. }
  836. TEST_F(ServerTest, GetMethodDir) {
  837. auto res = cli_.Get("/dir/");
  838. ASSERT_TRUE(res != nullptr);
  839. EXPECT_EQ(200, res->status);
  840. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  841. auto body = R"(<html>
  842. <head>
  843. </head>
  844. <body>
  845. <a href="/dir/test.html">Test</a>
  846. <a href="/hi">hi</a>
  847. </body>
  848. </html>
  849. )";
  850. EXPECT_EQ(body, res->body);
  851. }
  852. TEST_F(ServerTest, GetMethodDirTest) {
  853. auto res = cli_.Get("/dir/test.html");
  854. ASSERT_TRUE(res != nullptr);
  855. EXPECT_EQ(200, res->status);
  856. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  857. EXPECT_EQ("test.html", res->body);
  858. }
  859. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  860. auto res = cli_.Get("/dir/../dir/test.html");
  861. ASSERT_TRUE(res != nullptr);
  862. EXPECT_EQ(200, res->status);
  863. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  864. EXPECT_EQ("test.html", res->body);
  865. }
  866. TEST_F(ServerTest, GetMethodInvalidPath) {
  867. auto res = cli_.Get("/dir/../test.html");
  868. ASSERT_TRUE(res != nullptr);
  869. EXPECT_EQ(404, res->status);
  870. }
  871. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  872. auto res = cli_.Get("/../www/dir/test.html");
  873. ASSERT_TRUE(res != nullptr);
  874. EXPECT_EQ(404, res->status);
  875. }
  876. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  877. auto res = cli_.Get("/dir/../../www/dir/test.html");
  878. ASSERT_TRUE(res != nullptr);
  879. EXPECT_EQ(404, res->status);
  880. }
  881. TEST_F(ServerTest, InvalidBaseDir) {
  882. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  883. EXPECT_EQ(true, svr_.set_base_dir("."));
  884. }
  885. TEST_F(ServerTest, EmptyRequest) {
  886. auto res = cli_.Get("");
  887. ASSERT_TRUE(res == nullptr);
  888. }
  889. TEST_F(ServerTest, LongRequest) {
  890. std::string request;
  891. for (size_t i = 0; i < 545; i++) {
  892. request += "/TooLongRequest";
  893. }
  894. request += "OK";
  895. auto res = cli_.Get(request.c_str());
  896. ASSERT_TRUE(res != nullptr);
  897. EXPECT_EQ(404, res->status);
  898. }
  899. TEST_F(ServerTest, TooLongRequest) {
  900. std::string request;
  901. for (size_t i = 0; i < 545; i++) {
  902. request += "/TooLongRequest";
  903. }
  904. request += "_NG";
  905. auto res = cli_.Get(request.c_str());
  906. ASSERT_TRUE(res != nullptr);
  907. EXPECT_EQ(414, res->status);
  908. }
  909. TEST_F(ServerTest, LongHeader) {
  910. Request req;
  911. req.method = "GET";
  912. req.path = "/hi";
  913. std::string host_and_port;
  914. host_and_port += HOST;
  915. host_and_port += ":";
  916. host_and_port += std::to_string(PORT);
  917. req.headers.emplace("Host", host_and_port.c_str());
  918. req.headers.emplace("Accept", "*/*");
  919. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  920. req.headers.emplace(
  921. "Header-Name",
  922. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  923. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  924. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  925. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  926. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  927. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  928. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  929. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  930. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  931. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  932. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  933. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  934. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  935. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  936. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  937. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  938. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  939. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  940. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  941. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  942. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  943. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  944. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  945. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  946. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  947. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  948. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  949. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  950. "@@@@@@@@@@@@@@@@");
  951. auto res = std::make_shared<Response>();
  952. auto ret = cli_.send(req, *res);
  953. ASSERT_TRUE(ret);
  954. EXPECT_EQ(200, res->status);
  955. }
  956. TEST_F(ServerTest, LongQueryValue) {
  957. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  958. ASSERT_TRUE(res != nullptr);
  959. EXPECT_EQ(414, res->status);
  960. }
  961. TEST_F(ServerTest, TooLongHeader) {
  962. Request req;
  963. req.method = "GET";
  964. req.path = "/hi";
  965. std::string host_and_port;
  966. host_and_port += HOST;
  967. host_and_port += ":";
  968. host_and_port += std::to_string(PORT);
  969. req.headers.emplace("Host", host_and_port.c_str());
  970. req.headers.emplace("Accept", "*/*");
  971. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  972. req.headers.emplace(
  973. "Header-Name",
  974. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  975. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  976. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  977. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  978. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  979. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  980. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  981. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  982. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  983. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  984. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  985. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  986. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  987. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  988. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  989. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  990. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  991. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  992. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  993. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  994. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  995. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  996. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  997. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  998. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  999. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1000. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1001. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1002. "@@@@@@@@@@@@@@@@@");
  1003. auto res = std::make_shared<Response>();
  1004. auto ret = cli_.send(req, *res);
  1005. ASSERT_TRUE(ret);
  1006. EXPECT_EQ(200, res->status);
  1007. }
  1008. TEST_F(ServerTest, PercentEncoding) {
  1009. auto res = cli_.Get("/e%6edwith%");
  1010. ASSERT_TRUE(res != nullptr);
  1011. EXPECT_EQ(200, res->status);
  1012. }
  1013. TEST_F(ServerTest, PercentEncodingUnicode) {
  1014. auto res = cli_.Get("/e%u006edwith%");
  1015. ASSERT_TRUE(res != nullptr);
  1016. EXPECT_EQ(200, res->status);
  1017. }
  1018. TEST_F(ServerTest, InvalidPercentEncoding) {
  1019. auto res = cli_.Get("/%endwith%");
  1020. ASSERT_TRUE(res != nullptr);
  1021. EXPECT_EQ(404, res->status);
  1022. }
  1023. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  1024. auto res = cli_.Get("/%uendwith%");
  1025. ASSERT_TRUE(res != nullptr);
  1026. EXPECT_EQ(404, res->status);
  1027. }
  1028. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  1029. auto res = cli_.Get("/hello?aaa=bbb%");
  1030. ASSERT_TRUE(res != nullptr);
  1031. EXPECT_EQ(404, res->status);
  1032. }
  1033. TEST_F(ServerTest, MultipartFormData) {
  1034. MultipartFormDataItems items = {
  1035. {"text1", "text default", "", ""},
  1036. {"text2", "aωb", "", ""},
  1037. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1038. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1039. {"file3", "", "", "application/octet-stream"},
  1040. };
  1041. auto res = cli_.Post("/multipart", items);
  1042. ASSERT_TRUE(res != nullptr);
  1043. EXPECT_EQ(200, res->status);
  1044. }
  1045. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  1046. auto res = cli_.Get("/hi");
  1047. ASSERT_TRUE(res != nullptr);
  1048. EXPECT_EQ(200, res->status);
  1049. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  1050. EXPECT_EQ("Hello World!", res->body);
  1051. }
  1052. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  1053. Request req;
  1054. req.method = "POST";
  1055. req.path = "/chunked";
  1056. std::string host_and_port;
  1057. host_and_port += HOST;
  1058. host_and_port += ":";
  1059. host_and_port += std::to_string(PORT);
  1060. req.headers.emplace("Host", host_and_port.c_str());
  1061. req.headers.emplace("Accept", "*/*");
  1062. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1063. req.headers.emplace("Content-Type", "text/plain");
  1064. req.headers.emplace("Content-Length", "0");
  1065. req.headers.emplace(
  1066. "Transfer-Encoding",
  1067. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  1068. // Client does not chunk, so make a chunked body manually.
  1069. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  1070. auto res = std::make_shared<Response>();
  1071. auto ret = cli_.send(req, *res);
  1072. ASSERT_TRUE(ret);
  1073. EXPECT_EQ(200, res->status);
  1074. }
  1075. TEST_F(ServerTest, GetStreamed2) {
  1076. auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
  1077. ASSERT_TRUE(res != nullptr);
  1078. EXPECT_EQ(206, res->status);
  1079. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1080. EXPECT_EQ(std::string("ab"), res->body);
  1081. }
  1082. TEST_F(ServerTest, GetStreamed) {
  1083. auto res = cli_.Get("/streamed");
  1084. ASSERT_TRUE(res != nullptr);
  1085. EXPECT_EQ(200, res->status);
  1086. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1087. EXPECT_EQ(std::string("aaabbb"), res->body);
  1088. }
  1089. TEST_F(ServerTest, GetStreamedWithRange1) {
  1090. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
  1091. ASSERT_TRUE(res != nullptr);
  1092. EXPECT_EQ(206, res->status);
  1093. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1094. EXPECT_EQ(true, res->has_header("Content-Range"));
  1095. EXPECT_EQ(std::string("def"), res->body);
  1096. }
  1097. TEST_F(ServerTest, GetStreamedWithRange2) {
  1098. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
  1099. ASSERT_TRUE(res != nullptr);
  1100. EXPECT_EQ(206, res->status);
  1101. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1102. EXPECT_EQ(true, res->has_header("Content-Range"));
  1103. EXPECT_EQ(std::string("bcdefg"), res->body);
  1104. }
  1105. TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
  1106. auto res =
  1107. cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1108. ASSERT_TRUE(res != nullptr);
  1109. EXPECT_EQ(206, res->status);
  1110. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1111. EXPECT_EQ(false, res->has_header("Content-Range"));
  1112. EXPECT_EQ(269, res->body.size());
  1113. }
  1114. TEST_F(ServerTest, GetStreamedEndless) {
  1115. auto res = cli_.Get("/streamed-cancel",
  1116. [](const char * /*data*/, uint64_t /*data_length*/,
  1117. uint64_t offset,
  1118. uint64_t /*content_length*/) { return offset < 100; });
  1119. ASSERT_TRUE(res == nullptr);
  1120. }
  1121. TEST_F(ServerTest, GetWithRange1) {
  1122. auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
  1123. ASSERT_TRUE(res != nullptr);
  1124. EXPECT_EQ(206, res->status);
  1125. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1126. EXPECT_EQ(true, res->has_header("Content-Range"));
  1127. EXPECT_EQ(std::string("def"), res->body);
  1128. }
  1129. TEST_F(ServerTest, GetWithRange2) {
  1130. auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
  1131. ASSERT_TRUE(res != nullptr);
  1132. EXPECT_EQ(206, res->status);
  1133. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1134. EXPECT_EQ(true, res->has_header("Content-Range"));
  1135. EXPECT_EQ(std::string("bcdefg"), res->body);
  1136. }
  1137. TEST_F(ServerTest, GetWithRange3) {
  1138. auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
  1139. ASSERT_TRUE(res != nullptr);
  1140. EXPECT_EQ(206, res->status);
  1141. EXPECT_EQ("1", res->get_header_value("Content-Length"));
  1142. EXPECT_EQ(true, res->has_header("Content-Range"));
  1143. EXPECT_EQ(std::string("a"), res->body);
  1144. }
  1145. TEST_F(ServerTest, GetWithRange4) {
  1146. auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
  1147. ASSERT_TRUE(res != nullptr);
  1148. EXPECT_EQ(206, res->status);
  1149. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1150. EXPECT_EQ(true, res->has_header("Content-Range"));
  1151. EXPECT_EQ(std::string("fg"), res->body);
  1152. }
  1153. TEST_F(ServerTest, GetWithRangeMultipart) {
  1154. auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1155. ASSERT_TRUE(res != nullptr);
  1156. EXPECT_EQ(206, res->status);
  1157. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1158. EXPECT_EQ(false, res->has_header("Content-Range"));
  1159. EXPECT_EQ(269, res->body.size());
  1160. }
  1161. TEST_F(ServerTest, GetStreamedChunked) {
  1162. auto res = cli_.Get("/streamed-chunked");
  1163. ASSERT_TRUE(res != nullptr);
  1164. EXPECT_EQ(200, res->status);
  1165. EXPECT_EQ(std::string("123456789"), res->body);
  1166. }
  1167. TEST_F(ServerTest, LargeChunkedPost) {
  1168. Request req;
  1169. req.method = "POST";
  1170. req.path = "/large-chunked";
  1171. std::string host_and_port;
  1172. host_and_port += HOST;
  1173. host_and_port += ":";
  1174. host_and_port += std::to_string(PORT);
  1175. req.headers.emplace("Host", host_and_port.c_str());
  1176. req.headers.emplace("Accept", "*/*");
  1177. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1178. req.headers.emplace("Content-Type", "text/plain");
  1179. req.headers.emplace("Content-Length", "0");
  1180. req.headers.emplace("Transfer-Encoding", "chunked");
  1181. std::string long_string(30 * 1024u, 'a');
  1182. std::string chunk = "7800\r\n" + long_string + "\r\n";
  1183. // Attempt to make a large enough post to exceed OS buffers, to test that
  1184. // the server handles short reads if the full chunk data isn't available.
  1185. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  1186. auto res = std::make_shared<Response>();
  1187. auto ret = cli_.send(req, *res);
  1188. ASSERT_TRUE(ret);
  1189. EXPECT_EQ(200, res->status);
  1190. }
  1191. TEST_F(ServerTest, GetMethodRemoteAddr) {
  1192. auto res = cli_.Get("/remote_addr");
  1193. ASSERT_TRUE(res != nullptr);
  1194. EXPECT_EQ(200, res->status);
  1195. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1196. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  1197. }
  1198. TEST_F(ServerTest, SlowRequest) {
  1199. request_threads_.push_back(
  1200. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1201. request_threads_.push_back(
  1202. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1203. request_threads_.push_back(
  1204. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1205. msleep(100);
  1206. }
  1207. TEST_F(ServerTest, Put) {
  1208. auto res = cli_.Put("/put", "PUT", "text/plain");
  1209. ASSERT_TRUE(res != nullptr);
  1210. EXPECT_EQ(200, res->status);
  1211. EXPECT_EQ("PUT", res->body);
  1212. }
  1213. TEST_F(ServerTest, PutWithContentProvider) {
  1214. auto res = cli_.Put(
  1215. "/put", 3,
  1216. [](size_t /*offset*/, size_t /*length*/, DataSink sink) {
  1217. sink("PUT", 3);
  1218. },
  1219. "text/plain");
  1220. ASSERT_TRUE(res != nullptr);
  1221. EXPECT_EQ(200, res->status);
  1222. EXPECT_EQ("PUT", res->body);
  1223. }
  1224. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1225. TEST_F(ServerTest, PutWithContentProviderWithGzip) {
  1226. auto res = cli_.Put(
  1227. "/put", 3,
  1228. [](size_t /*offset*/, size_t /*length*/, DataSink sink) {
  1229. sink("PUT", 3);
  1230. },
  1231. "text/plain", true);
  1232. ASSERT_TRUE(res != nullptr);
  1233. EXPECT_EQ(200, res->status);
  1234. EXPECT_EQ("PUT", res->body);
  1235. }
  1236. #endif
  1237. TEST_F(ServerTest, Patch) {
  1238. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  1239. ASSERT_TRUE(res != nullptr);
  1240. EXPECT_EQ(200, res->status);
  1241. EXPECT_EQ("PATCH", res->body);
  1242. }
  1243. TEST_F(ServerTest, Delete) {
  1244. auto res = cli_.Delete("/delete");
  1245. ASSERT_TRUE(res != nullptr);
  1246. EXPECT_EQ(200, res->status);
  1247. EXPECT_EQ("DELETE", res->body);
  1248. }
  1249. TEST_F(ServerTest, Options) {
  1250. auto res = cli_.Options("*");
  1251. ASSERT_TRUE(res != nullptr);
  1252. EXPECT_EQ(200, res->status);
  1253. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  1254. EXPECT_TRUE(res->body.empty());
  1255. }
  1256. TEST_F(ServerTest, URL) {
  1257. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  1258. ASSERT_TRUE(res != nullptr);
  1259. EXPECT_EQ(200, res->status);
  1260. }
  1261. TEST_F(ServerTest, ArrayParam) {
  1262. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  1263. ASSERT_TRUE(res != nullptr);
  1264. EXPECT_EQ(200, res->status);
  1265. }
  1266. TEST_F(ServerTest, NoMultipleHeaders) {
  1267. Headers headers = {{"Content-Length", "5"}};
  1268. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1269. "text/plain");
  1270. ASSERT_TRUE(res != nullptr);
  1271. EXPECT_EQ(200, res->status);
  1272. }
  1273. TEST_F(ServerTest, PostContentReceiver) {
  1274. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1275. ASSERT_TRUE(res != nullptr);
  1276. ASSERT_EQ(200, res->status);
  1277. ASSERT_EQ("content", res->body);
  1278. }
  1279. TEST_F(ServerTest, PostContentReceiverGzip) {
  1280. auto res = cli_.Post("/content_receiver", "content", "text/plain", true);
  1281. ASSERT_TRUE(res != nullptr);
  1282. ASSERT_EQ(200, res->status);
  1283. ASSERT_EQ("content", res->body);
  1284. }
  1285. TEST_F(ServerTest, PutContentReceiver) {
  1286. auto res = cli_.Put("/content_receiver", "content", "text/plain");
  1287. ASSERT_TRUE(res != nullptr);
  1288. ASSERT_EQ(200, res->status);
  1289. ASSERT_EQ("content", res->body);
  1290. }
  1291. TEST_F(ServerTest, PatchContentReceiver) {
  1292. auto res = cli_.Patch("/content_receiver", "content", "text/plain");
  1293. ASSERT_TRUE(res != nullptr);
  1294. ASSERT_EQ(200, res->status);
  1295. ASSERT_EQ("content", res->body);
  1296. }
  1297. TEST_F(ServerTest, HTTP2Magic) {
  1298. Request req;
  1299. req.method = "PRI";
  1300. req.path = "*";
  1301. req.body = "SM";
  1302. auto res = std::make_shared<Response>();
  1303. auto ret = cli_.send(req, *res);
  1304. ASSERT_TRUE(ret);
  1305. EXPECT_EQ(400, res->status);
  1306. }
  1307. TEST_F(ServerTest, KeepAlive) {
  1308. cli_.set_keep_alive_max_count(4);
  1309. std::vector<Request> requests;
  1310. Get(requests, "/hi");
  1311. Get(requests, "/hi");
  1312. Get(requests, "/hi");
  1313. Get(requests, "/not-exist");
  1314. Post(requests, "/empty", "", "text/plain");
  1315. std::vector<Response> responses;
  1316. auto ret = cli_.send(requests, responses);
  1317. ASSERT_TRUE(ret == true);
  1318. ASSERT_TRUE(requests.size() == responses.size());
  1319. for (int i = 0; i < 3; i++) {
  1320. auto &res = responses[i];
  1321. EXPECT_EQ(200, res.status);
  1322. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1323. EXPECT_EQ("Hello World!", res.body);
  1324. }
  1325. {
  1326. auto &res = responses[3];
  1327. EXPECT_EQ(404, res.status);
  1328. }
  1329. {
  1330. auto &res = responses[4];
  1331. EXPECT_EQ(200, res.status);
  1332. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1333. EXPECT_EQ("empty", res.body);
  1334. }
  1335. }
  1336. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1337. TEST_F(ServerTest, Gzip) {
  1338. Headers headers;
  1339. headers.emplace("Accept-Encoding", "gzip, deflate");
  1340. auto res = cli_.Get("/gzip", headers);
  1341. ASSERT_TRUE(res != nullptr);
  1342. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1343. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1344. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1345. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1346. "7890123456789012345678901234567890",
  1347. res->body);
  1348. EXPECT_EQ(200, res->status);
  1349. }
  1350. TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
  1351. Headers headers;
  1352. auto res = cli_.Get("/gzip", headers);
  1353. ASSERT_TRUE(res != nullptr);
  1354. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1355. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1356. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1357. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1358. "7890123456789012345678901234567890",
  1359. res->body);
  1360. EXPECT_EQ(200, res->status);
  1361. }
  1362. TEST_F(ServerTest, GzipWithContentReceiver) {
  1363. Headers headers;
  1364. headers.emplace("Accept-Encoding", "gzip, deflate");
  1365. std::string body;
  1366. auto res = cli_.Get("/gzip", headers,
  1367. [&](const char *data, uint64_t data_length,
  1368. uint64_t offset, uint64_t content_length) {
  1369. EXPECT_EQ(data_length, 100);
  1370. EXPECT_EQ(offset, 0);
  1371. EXPECT_EQ(content_length, 0);
  1372. body.append(data, data_length);
  1373. return true;
  1374. });
  1375. ASSERT_TRUE(res != nullptr);
  1376. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1377. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1378. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1379. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1380. "7890123456789012345678901234567890",
  1381. body);
  1382. EXPECT_EQ(200, res->status);
  1383. }
  1384. TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
  1385. Headers headers;
  1386. std::string body;
  1387. auto res = cli_.Get("/gzip", headers,
  1388. [&](const char *data, uint64_t data_length,
  1389. uint64_t offset, uint64_t content_length) {
  1390. EXPECT_EQ(data_length, 100);
  1391. EXPECT_EQ(offset, 0);
  1392. EXPECT_EQ(content_length, 100);
  1393. body.append(data, data_length);
  1394. return true;
  1395. });
  1396. ASSERT_TRUE(res != nullptr);
  1397. EXPECT_EQ("", res->get_header_value("Content-Encoding"));
  1398. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1399. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1400. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1401. "7890123456789012345678901234567890",
  1402. body);
  1403. EXPECT_EQ(200, res->status);
  1404. }
  1405. TEST_F(ServerTest, NoGzip) {
  1406. Headers headers;
  1407. headers.emplace("Accept-Encoding", "gzip, deflate");
  1408. auto res = cli_.Get("/nogzip", headers);
  1409. ASSERT_TRUE(res != nullptr);
  1410. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1411. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1412. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1413. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1414. "7890123456789012345678901234567890",
  1415. res->body);
  1416. EXPECT_EQ(200, res->status);
  1417. }
  1418. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1419. Headers headers;
  1420. headers.emplace("Accept-Encoding", "gzip, deflate");
  1421. std::string body;
  1422. auto res = cli_.Get("/nogzip", headers,
  1423. [&](const char *data, uint64_t data_length,
  1424. uint64_t offset, uint64_t content_length) {
  1425. EXPECT_EQ(data_length, 100);
  1426. EXPECT_EQ(offset, 0);
  1427. EXPECT_EQ(content_length, 100);
  1428. body.append(data, data_length);
  1429. return true;
  1430. });
  1431. ASSERT_TRUE(res != nullptr);
  1432. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1433. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1434. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1435. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1436. "7890123456789012345678901234567890",
  1437. body);
  1438. EXPECT_EQ(200, res->status);
  1439. }
  1440. TEST_F(ServerTest, MultipartFormDataGzip) {
  1441. MultipartFormDataItems items = {
  1442. {"key1", "test", "", ""},
  1443. {"key2", "--abcdefg123", "", ""},
  1444. };
  1445. auto res = cli_.Post("/gzipmultipart", items, true);
  1446. ASSERT_TRUE(res != nullptr);
  1447. EXPECT_EQ(200, res->status);
  1448. }
  1449. #endif
  1450. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1451. protected:
  1452. ServerTestWithAI_PASSIVE()
  1453. : cli_(HOST, PORT)
  1454. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1455. ,
  1456. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1457. #endif
  1458. {
  1459. }
  1460. virtual void SetUp() {
  1461. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1462. res.set_content("Hello World!", "text/plain");
  1463. });
  1464. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1465. while (!svr_.is_running()) {
  1466. msleep(1);
  1467. }
  1468. }
  1469. virtual void TearDown() {
  1470. svr_.stop();
  1471. t_.join();
  1472. }
  1473. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1474. SSLClient cli_;
  1475. SSLServer svr_;
  1476. #else
  1477. Client cli_;
  1478. Server svr_;
  1479. #endif
  1480. thread t_;
  1481. };
  1482. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1483. auto res = cli_.Get("/hi");
  1484. ASSERT_TRUE(res != nullptr);
  1485. EXPECT_EQ(200, res->status);
  1486. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1487. EXPECT_EQ("Hello World!", res->body);
  1488. }
  1489. class ServerUpDownTest : public ::testing::Test {
  1490. protected:
  1491. ServerUpDownTest() : cli_(HOST, PORT) {}
  1492. virtual void SetUp() {
  1493. t_ = thread([&]() {
  1494. svr_.bind_to_any_port(HOST);
  1495. msleep(500);
  1496. ASSERT_TRUE(svr_.listen_after_bind());
  1497. });
  1498. while (!svr_.is_running()) {
  1499. msleep(1);
  1500. }
  1501. }
  1502. virtual void TearDown() {
  1503. svr_.stop();
  1504. t_.join();
  1505. }
  1506. Client cli_;
  1507. Server svr_;
  1508. thread t_;
  1509. };
  1510. TEST_F(ServerUpDownTest, QuickStartStop) {
  1511. // Should not crash, especially when run with
  1512. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1513. }
  1514. class PayloadMaxLengthTest : public ::testing::Test {
  1515. protected:
  1516. PayloadMaxLengthTest()
  1517. : cli_(HOST, PORT)
  1518. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1519. ,
  1520. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1521. #endif
  1522. {
  1523. }
  1524. virtual void SetUp() {
  1525. svr_.set_payload_max_length(8);
  1526. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1527. res.set_content("test", "text/plain");
  1528. });
  1529. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1530. while (!svr_.is_running()) {
  1531. msleep(1);
  1532. }
  1533. }
  1534. virtual void TearDown() {
  1535. svr_.stop();
  1536. t_.join();
  1537. }
  1538. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1539. SSLClient cli_;
  1540. SSLServer svr_;
  1541. #else
  1542. Client cli_;
  1543. Server svr_;
  1544. #endif
  1545. thread t_;
  1546. };
  1547. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1548. auto res = cli_.Post("/test", "123456789", "text/plain");
  1549. ASSERT_TRUE(res != nullptr);
  1550. EXPECT_EQ(413, res->status);
  1551. res = cli_.Post("/test", "12345678", "text/plain");
  1552. ASSERT_TRUE(res != nullptr);
  1553. EXPECT_EQ(200, res->status);
  1554. }
  1555. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1556. TEST(SSLClientTest, ServerNameIndication) {
  1557. SSLClient cli("httpbin.org", 443);
  1558. auto res = cli.Get("/get");
  1559. ASSERT_TRUE(res != nullptr);
  1560. ASSERT_EQ(200, res->status);
  1561. }
  1562. TEST(SSLClientTest, ServerCertificateVerification) {
  1563. SSLClient cli("google.com");
  1564. auto res = cli.Get("/");
  1565. ASSERT_TRUE(res != nullptr);
  1566. ASSERT_EQ(301, res->status);
  1567. cli.enable_server_certificate_verification(true);
  1568. res = cli.Get("/");
  1569. ASSERT_TRUE(res == nullptr);
  1570. cli.set_ca_cert_path(CA_CERT_FILE);
  1571. res = cli.Get("/");
  1572. ASSERT_TRUE(res != nullptr);
  1573. ASSERT_EQ(301, res->status);
  1574. }
  1575. TEST(SSLClientTest, WildcardHostNameMatch) {
  1576. SSLClient cli("www.youtube.com");
  1577. cli.set_ca_cert_path(CA_CERT_FILE);
  1578. cli.enable_server_certificate_verification(true);
  1579. auto res = cli.Get("/");
  1580. ASSERT_TRUE(res != nullptr);
  1581. ASSERT_EQ(200, res->status);
  1582. }
  1583. TEST(SSLClientServerTest, ClientCertPresent) {
  1584. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1585. CLIENT_CA_CERT_DIR);
  1586. ASSERT_TRUE(svr.is_valid());
  1587. svr.Get("/test", [&](const Request &req, Response &res) {
  1588. res.set_content("test", "text/plain");
  1589. svr.stop();
  1590. ASSERT_TRUE(true);
  1591. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1592. ASSERT_TRUE(peer_cert != nullptr);
  1593. auto subject_name = X509_get_subject_name(peer_cert);
  1594. ASSERT_TRUE(subject_name != nullptr);
  1595. std::string common_name;
  1596. {
  1597. char name[BUFSIZ];
  1598. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1599. name, sizeof(name));
  1600. common_name.assign(name, name_len);
  1601. }
  1602. EXPECT_EQ("Common Name", common_name);
  1603. X509_free(peer_cert);
  1604. });
  1605. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1606. msleep(1);
  1607. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1608. CLIENT_PRIVATE_KEY_FILE);
  1609. auto res = cli.Get("/test");
  1610. ASSERT_TRUE(res != nullptr);
  1611. ASSERT_EQ(200, res->status);
  1612. t.join();
  1613. }
  1614. TEST(SSLClientServerTest, ClientCertMissing) {
  1615. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1616. CLIENT_CA_CERT_DIR);
  1617. ASSERT_TRUE(svr.is_valid());
  1618. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1619. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1620. msleep(1);
  1621. httplib::SSLClient cli(HOST, PORT, 30);
  1622. auto res = cli.Get("/test");
  1623. ASSERT_TRUE(res == nullptr);
  1624. svr.stop();
  1625. t.join();
  1626. }
  1627. TEST(SSLClientServerTest, TrustDirOptional) {
  1628. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1629. ASSERT_TRUE(svr.is_valid());
  1630. svr.Get("/test", [&](const Request &, Response &res) {
  1631. res.set_content("test", "text/plain");
  1632. svr.stop();
  1633. });
  1634. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1635. msleep(1);
  1636. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1637. CLIENT_PRIVATE_KEY_FILE);
  1638. auto res = cli.Get("/test");
  1639. ASSERT_TRUE(res != nullptr);
  1640. ASSERT_EQ(200, res->status);
  1641. t.join();
  1642. }
  1643. /* Cannot test this case as there is no external access to SSL object to check
  1644. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1645. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1646. nullptr, CLIENT_CA_CERT_DIR);
  1647. }
  1648. */
  1649. #endif
  1650. #ifdef _WIN32
  1651. TEST(CleanupTest, WSACleanup) {
  1652. int ret = WSACleanup();
  1653. ASSERT_EQ(0, ret);
  1654. }
  1655. #endif