test.cc 58 KB

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