test.cc 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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 =
  217. cli.Get("/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(Https2HttpRedirectTest, Redirect) {
  450. httplib::SSLClient cli("httpbin.org");
  451. cli.follow_location(true);
  452. auto res = cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
  453. ASSERT_TRUE(res != nullptr);
  454. }
  455. #endif
  456. TEST(Server, BindAndListenSeparately) {
  457. Server svr;
  458. int port = svr.bind_to_any_port("localhost");
  459. ASSERT_TRUE(port > 0);
  460. svr.stop();
  461. }
  462. class ServerTest : public ::testing::Test {
  463. protected:
  464. ServerTest()
  465. : cli_(HOST, PORT)
  466. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  467. ,
  468. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  469. #endif
  470. {
  471. }
  472. virtual void SetUp() {
  473. svr_.set_base_dir("./www");
  474. svr_.Get("/hi",
  475. [&](const Request & /*req*/, Response &res) {
  476. res.set_content("Hello World!", "text/plain");
  477. })
  478. .Get("/slow",
  479. [&](const Request & /*req*/, Response &res) {
  480. msleep(2000);
  481. res.set_content("slow", "text/plain");
  482. })
  483. .Get("/remote_addr",
  484. [&](const Request &req, Response &res) {
  485. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  486. res.set_content(remote_addr.c_str(), "text/plain");
  487. })
  488. .Get("/endwith%",
  489. [&](const Request & /*req*/, Response &res) {
  490. res.set_content("Hello World!", "text/plain");
  491. })
  492. .Get("/", [&](const Request & /*req*/,
  493. Response &res) { res.set_redirect("/hi"); })
  494. .Post("/person",
  495. [&](const Request &req, Response &res) {
  496. if (req.has_param("name") && req.has_param("note")) {
  497. persons_[req.get_param_value("name")] =
  498. req.get_param_value("note");
  499. } else {
  500. res.status = 400;
  501. }
  502. })
  503. .Get("/person/(.*)",
  504. [&](const Request &req, Response &res) {
  505. string name = req.matches[1];
  506. if (persons_.find(name) != persons_.end()) {
  507. auto note = persons_[name];
  508. res.set_content(note, "text/plain");
  509. } else {
  510. res.status = 404;
  511. }
  512. })
  513. .Post("/x-www-form-urlencoded-json",
  514. [&](const Request &req, Response &res) {
  515. auto json = req.get_param_value("json");
  516. ASSERT_EQ(JSON_DATA, json);
  517. res.set_content(json, "appliation/json");
  518. res.status = 200;
  519. })
  520. .Get("/streamed-chunked",
  521. [&](const Request & /*req*/, Response &res) {
  522. res.set_chunked_content_provider(
  523. [](uint64_t /*offset*/, DataSink sink, Done done) {
  524. sink("123", 3);
  525. sink("456", 3);
  526. sink("789", 3);
  527. done();
  528. });
  529. })
  530. .Get("/streamed",
  531. [&](const Request & /*req*/, Response &res) {
  532. res.set_content_provider(
  533. 6, [](uint64_t offset, uint64_t /*length*/, DataSink sink) {
  534. sink(offset < 3 ? "a" : "b", 1);
  535. });
  536. })
  537. .Get("/streamed-with-range",
  538. [&](const Request & /*req*/, Response &res) {
  539. auto data = new std::string("abcdefg");
  540. res.set_content_provider(
  541. data->size(),
  542. [data](uint64_t offset, uint64_t length, DataSink sink) {
  543. size_t DATA_CHUNK_SIZE = 4;
  544. const auto &d = *data;
  545. auto out_len = std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
  546. sink(&d[offset], out_len);
  547. },
  548. [data] { delete data; });
  549. })
  550. .Get("/streamed-cancel",
  551. [&](const Request & /*req*/, Response &res) {
  552. res.set_content_provider(
  553. size_t(-1),
  554. [](uint64_t /*offset*/, uint64_t /*length*/, DataSink sink) {
  555. std::string data = "data_chunk";
  556. sink(data.data(), data.size());
  557. });
  558. })
  559. .Get("/with-range",
  560. [&](const Request & /*req*/, Response &res) {
  561. res.set_content("abcdefg", "text/plain");
  562. })
  563. .Post("/chunked",
  564. [&](const Request &req, Response & /*res*/) {
  565. EXPECT_EQ(req.body, "dechunked post body");
  566. })
  567. .Post("/large-chunked",
  568. [&](const Request &req, Response & /*res*/) {
  569. std::string expected(6 * 30 * 1024u, 'a');
  570. EXPECT_EQ(req.body, expected);
  571. })
  572. .Post("/multipart",
  573. [&](const Request &req, Response & /*res*/) {
  574. EXPECT_EQ(5u, req.files.size());
  575. ASSERT_TRUE(!req.has_file("???"));
  576. {
  577. const auto &file = req.get_file_value("text1");
  578. EXPECT_EQ("", file.filename);
  579. EXPECT_EQ("text default",
  580. req.body.substr(file.offset, file.length));
  581. }
  582. {
  583. const auto &file = req.get_file_value("text2");
  584. EXPECT_EQ("", file.filename);
  585. EXPECT_EQ("aωb", req.body.substr(file.offset, file.length));
  586. }
  587. {
  588. const auto &file = req.get_file_value("file1");
  589. EXPECT_EQ("hello.txt", file.filename);
  590. EXPECT_EQ("text/plain", file.content_type);
  591. EXPECT_EQ("h\ne\n\nl\nl\no\n",
  592. req.body.substr(file.offset, file.length));
  593. }
  594. {
  595. const auto &file = req.get_file_value("file3");
  596. EXPECT_EQ("", file.filename);
  597. EXPECT_EQ("application/octet-stream", file.content_type);
  598. EXPECT_EQ(0u, file.length);
  599. }
  600. })
  601. .Post("/empty",
  602. [&](const Request &req, Response &res) {
  603. EXPECT_EQ(req.body, "");
  604. res.set_content("empty", "text/plain");
  605. })
  606. .Put("/put",
  607. [&](const Request &req, Response &res) {
  608. EXPECT_EQ(req.body, "PUT");
  609. res.set_content(req.body, "text/plain");
  610. })
  611. .Patch("/patch",
  612. [&](const Request &req, Response &res) {
  613. EXPECT_EQ(req.body, "PATCH");
  614. res.set_content(req.body, "text/plain");
  615. })
  616. .Delete("/delete",
  617. [&](const Request & /*req*/, Response &res) {
  618. res.set_content("DELETE", "text/plain");
  619. })
  620. .Options(R"(\*)",
  621. [&](const Request & /*req*/, Response &res) {
  622. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  623. })
  624. .Get("/request-target",
  625. [&](const Request &req, Response & /*res*/) {
  626. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  627. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  628. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  629. })
  630. .Get("/long-query-value",
  631. [&](const Request &req, Response & /*res*/) {
  632. EXPECT_EQ(LONG_QUERY_URL, req.target);
  633. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  634. })
  635. .Get("/array-param",
  636. [&](const Request &req, Response & /*res*/) {
  637. EXPECT_EQ(3u, req.get_param_value_count("array"));
  638. EXPECT_EQ("value1", req.get_param_value("array", 0));
  639. EXPECT_EQ("value2", req.get_param_value("array", 1));
  640. EXPECT_EQ("value3", req.get_param_value("array", 2));
  641. })
  642. .Post("/validate-no-multiple-headers",
  643. [&](const Request &req, Response & /*res*/) {
  644. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  645. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  646. })
  647. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  648. .Get("/gzip",
  649. [&](const Request & /*req*/, Response &res) {
  650. res.set_content(
  651. "12345678901234567890123456789012345678901234567890123456789"
  652. "01234567890123456789012345678901234567890",
  653. "text/plain");
  654. })
  655. .Get("/nogzip",
  656. [&](const Request & /*req*/, Response &res) {
  657. res.set_content(
  658. "12345678901234567890123456789012345678901234567890123456789"
  659. "01234567890123456789012345678901234567890",
  660. "application/octet-stream");
  661. })
  662. .Post("/gzipmultipart",
  663. [&](const Request &req, Response & /*res*/) {
  664. EXPECT_EQ(2u, req.files.size());
  665. ASSERT_TRUE(!req.has_file("???"));
  666. {
  667. const auto &file = req.get_file_value("key1");
  668. EXPECT_EQ("", file.filename);
  669. EXPECT_EQ("test", req.body.substr(file.offset, file.length));
  670. }
  671. {
  672. const auto &file = req.get_file_value("key2");
  673. EXPECT_EQ("", file.filename);
  674. EXPECT_EQ("--abcdefg123",
  675. req.body.substr(file.offset, file.length));
  676. }
  677. })
  678. #endif
  679. ;
  680. persons_["john"] = "programmer";
  681. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  682. while (!svr_.is_running()) {
  683. msleep(1);
  684. }
  685. }
  686. virtual void TearDown() {
  687. svr_.stop();
  688. for (auto &t : request_threads_) {
  689. t.join();
  690. }
  691. t_.join();
  692. }
  693. map<string, string> persons_;
  694. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  695. SSLClient cli_;
  696. SSLServer svr_;
  697. #else
  698. Client cli_;
  699. Server svr_;
  700. #endif
  701. thread t_;
  702. std::vector<thread> request_threads_;
  703. };
  704. TEST_F(ServerTest, GetMethod200) {
  705. auto res = cli_.Get("/hi");
  706. ASSERT_TRUE(res != nullptr);
  707. EXPECT_EQ("HTTP/1.1", res->version);
  708. EXPECT_EQ(200, res->status);
  709. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  710. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  711. EXPECT_EQ("Hello World!", res->body);
  712. }
  713. TEST_F(ServerTest, GetMethod302) {
  714. auto res = cli_.Get("/");
  715. ASSERT_TRUE(res != nullptr);
  716. EXPECT_EQ(302, res->status);
  717. EXPECT_EQ("/hi", res->get_header_value("Location"));
  718. }
  719. TEST_F(ServerTest, GetMethod404) {
  720. auto res = cli_.Get("/invalid");
  721. ASSERT_TRUE(res != nullptr);
  722. EXPECT_EQ(404, res->status);
  723. }
  724. TEST_F(ServerTest, HeadMethod200) {
  725. auto res = cli_.Head("/hi");
  726. ASSERT_TRUE(res != nullptr);
  727. EXPECT_EQ(200, res->status);
  728. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  729. EXPECT_EQ("", res->body);
  730. }
  731. TEST_F(ServerTest, HeadMethod404) {
  732. auto res = cli_.Head("/invalid");
  733. ASSERT_TRUE(res != nullptr);
  734. EXPECT_EQ(404, res->status);
  735. EXPECT_EQ("", res->body);
  736. }
  737. TEST_F(ServerTest, GetMethodPersonJohn) {
  738. auto res = cli_.Get("/person/john");
  739. ASSERT_TRUE(res != nullptr);
  740. EXPECT_EQ(200, res->status);
  741. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  742. EXPECT_EQ("programmer", res->body);
  743. }
  744. TEST_F(ServerTest, PostMethod1) {
  745. auto res = cli_.Get("/person/john1");
  746. ASSERT_TRUE(res != nullptr);
  747. ASSERT_EQ(404, res->status);
  748. res = cli_.Post("/person", "name=john1&note=coder",
  749. "application/x-www-form-urlencoded");
  750. ASSERT_TRUE(res != nullptr);
  751. ASSERT_EQ(200, res->status);
  752. res = cli_.Get("/person/john1");
  753. ASSERT_TRUE(res != nullptr);
  754. ASSERT_EQ(200, res->status);
  755. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  756. ASSERT_EQ("coder", res->body);
  757. }
  758. TEST_F(ServerTest, PostMethod2) {
  759. auto res = cli_.Get("/person/john2");
  760. ASSERT_TRUE(res != nullptr);
  761. ASSERT_EQ(404, res->status);
  762. Params params;
  763. params.emplace("name", "john2");
  764. params.emplace("note", "coder");
  765. res = cli_.Post("/person", params);
  766. ASSERT_TRUE(res != nullptr);
  767. ASSERT_EQ(200, res->status);
  768. res = cli_.Get("/person/john2");
  769. ASSERT_TRUE(res != nullptr);
  770. ASSERT_EQ(200, res->status);
  771. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  772. ASSERT_EQ("coder", res->body);
  773. }
  774. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  775. Params params;
  776. params.emplace("json", JSON_DATA);
  777. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  778. ASSERT_TRUE(res != nullptr);
  779. ASSERT_EQ(200, res->status);
  780. ASSERT_EQ(JSON_DATA, res->body);
  781. }
  782. TEST_F(ServerTest, PostEmptyContent) {
  783. auto res = cli_.Post("/empty", "", "text/plain");
  784. ASSERT_TRUE(res != nullptr);
  785. ASSERT_EQ(200, res->status);
  786. ASSERT_EQ("empty", res->body);
  787. }
  788. TEST_F(ServerTest, GetMethodDir) {
  789. auto res = cli_.Get("/dir/");
  790. ASSERT_TRUE(res != nullptr);
  791. EXPECT_EQ(200, res->status);
  792. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  793. auto body = R"(<html>
  794. <head>
  795. </head>
  796. <body>
  797. <a href="/dir/test.html">Test</a>
  798. <a href="/hi">hi</a>
  799. </body>
  800. </html>
  801. )";
  802. EXPECT_EQ(body, res->body);
  803. }
  804. TEST_F(ServerTest, GetMethodDirTest) {
  805. auto res = cli_.Get("/dir/test.html");
  806. ASSERT_TRUE(res != nullptr);
  807. EXPECT_EQ(200, res->status);
  808. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  809. EXPECT_EQ("test.html", res->body);
  810. }
  811. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  812. auto res = cli_.Get("/dir/../dir/test.html");
  813. ASSERT_TRUE(res != nullptr);
  814. EXPECT_EQ(200, res->status);
  815. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  816. EXPECT_EQ("test.html", res->body);
  817. }
  818. TEST_F(ServerTest, GetMethodInvalidPath) {
  819. auto res = cli_.Get("/dir/../test.html");
  820. ASSERT_TRUE(res != nullptr);
  821. EXPECT_EQ(404, res->status);
  822. }
  823. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  824. auto res = cli_.Get("/../www/dir/test.html");
  825. ASSERT_TRUE(res != nullptr);
  826. EXPECT_EQ(404, res->status);
  827. }
  828. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  829. auto res = cli_.Get("/dir/../../www/dir/test.html");
  830. ASSERT_TRUE(res != nullptr);
  831. EXPECT_EQ(404, res->status);
  832. }
  833. TEST_F(ServerTest, InvalidBaseDir) {
  834. EXPECT_EQ(false, svr_.set_base_dir("invalid_dir"));
  835. EXPECT_EQ(true, svr_.set_base_dir("."));
  836. }
  837. TEST_F(ServerTest, EmptyRequest) {
  838. auto res = cli_.Get("");
  839. ASSERT_TRUE(res == nullptr);
  840. }
  841. TEST_F(ServerTest, LongRequest) {
  842. std::string request;
  843. for (size_t i = 0; i < 545; i++) {
  844. request += "/TooLongRequest";
  845. }
  846. request += "OK";
  847. auto res = cli_.Get(request.c_str());
  848. ASSERT_TRUE(res != nullptr);
  849. EXPECT_EQ(404, res->status);
  850. }
  851. TEST_F(ServerTest, TooLongRequest) {
  852. std::string request;
  853. for (size_t i = 0; i < 545; i++) {
  854. request += "/TooLongRequest";
  855. }
  856. request += "_NG";
  857. auto res = cli_.Get(request.c_str());
  858. ASSERT_TRUE(res != nullptr);
  859. EXPECT_EQ(414, res->status);
  860. }
  861. TEST_F(ServerTest, LongHeader) {
  862. Request req;
  863. req.method = "GET";
  864. req.path = "/hi";
  865. std::string host_and_port;
  866. host_and_port += HOST;
  867. host_and_port += ":";
  868. host_and_port += std::to_string(PORT);
  869. req.headers.emplace("Host", host_and_port.c_str());
  870. req.headers.emplace("Accept", "*/*");
  871. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  872. req.headers.emplace(
  873. "Header-Name",
  874. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  875. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  876. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  877. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  878. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  879. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  880. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  881. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  882. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  883. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  884. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  885. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  886. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  887. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  888. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  889. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  890. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  891. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  892. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  893. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  894. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  895. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  896. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  897. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  898. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  899. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  900. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  901. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  902. "@@@@@@@@@@@@@@@@");
  903. auto res = std::make_shared<Response>();
  904. auto ret = cli_.send(req, *res);
  905. ASSERT_TRUE(ret);
  906. EXPECT_EQ(200, res->status);
  907. }
  908. TEST_F(ServerTest, LongQueryValue) {
  909. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  910. ASSERT_TRUE(res != nullptr);
  911. EXPECT_EQ(414, res->status);
  912. }
  913. TEST_F(ServerTest, TooLongHeader) {
  914. Request req;
  915. req.method = "GET";
  916. req.path = "/hi";
  917. std::string host_and_port;
  918. host_and_port += HOST;
  919. host_and_port += ":";
  920. host_and_port += std::to_string(PORT);
  921. req.headers.emplace("Host", host_and_port.c_str());
  922. req.headers.emplace("Accept", "*/*");
  923. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  924. req.headers.emplace(
  925. "Header-Name",
  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. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  952. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  953. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  954. "@@@@@@@@@@@@@@@@@");
  955. auto res = std::make_shared<Response>();
  956. auto ret = cli_.send(req, *res);
  957. ASSERT_TRUE(ret);
  958. EXPECT_EQ(200, res->status);
  959. }
  960. TEST_F(ServerTest, PercentEncoding) {
  961. auto res = cli_.Get("/e%6edwith%");
  962. ASSERT_TRUE(res != nullptr);
  963. EXPECT_EQ(200, res->status);
  964. }
  965. TEST_F(ServerTest, PercentEncodingUnicode) {
  966. auto res = cli_.Get("/e%u006edwith%");
  967. ASSERT_TRUE(res != nullptr);
  968. EXPECT_EQ(200, res->status);
  969. }
  970. TEST_F(ServerTest, InvalidPercentEncoding) {
  971. auto res = cli_.Get("/%endwith%");
  972. ASSERT_TRUE(res != nullptr);
  973. EXPECT_EQ(404, res->status);
  974. }
  975. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  976. auto res = cli_.Get("/%uendwith%");
  977. ASSERT_TRUE(res != nullptr);
  978. EXPECT_EQ(404, res->status);
  979. }
  980. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  981. auto res = cli_.Get("/hello?aaa=bbb%");
  982. ASSERT_TRUE(res != nullptr);
  983. EXPECT_EQ(404, res->status);
  984. }
  985. TEST_F(ServerTest, MultipartFormData) {
  986. MultipartFormDataItems items = {
  987. {"text1", "text default", "", ""},
  988. {"text2", "aωb", "", ""},
  989. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  990. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  991. {"file3", "", "", "application/octet-stream"},
  992. };
  993. auto res = cli_.Post("/multipart", items);
  994. ASSERT_TRUE(res != nullptr);
  995. EXPECT_EQ(200, res->status);
  996. }
  997. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  998. auto res = cli_.Get("/hi");
  999. ASSERT_TRUE(res != nullptr);
  1000. EXPECT_EQ(200, res->status);
  1001. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  1002. EXPECT_EQ("Hello World!", res->body);
  1003. }
  1004. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  1005. Request req;
  1006. req.method = "POST";
  1007. req.path = "/chunked";
  1008. std::string host_and_port;
  1009. host_and_port += HOST;
  1010. host_and_port += ":";
  1011. host_and_port += std::to_string(PORT);
  1012. req.headers.emplace("Host", host_and_port.c_str());
  1013. req.headers.emplace("Accept", "*/*");
  1014. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1015. req.headers.emplace("Content-Type", "text/plain");
  1016. req.headers.emplace("Content-Length", "0");
  1017. req.headers.emplace(
  1018. "Transfer-Encoding",
  1019. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  1020. // Client does not chunk, so make a chunked body manually.
  1021. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  1022. auto res = std::make_shared<Response>();
  1023. auto ret = cli_.send(req, *res);
  1024. ASSERT_TRUE(ret);
  1025. EXPECT_EQ(200, res->status);
  1026. }
  1027. TEST_F(ServerTest, GetStreamed2) {
  1028. auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
  1029. ASSERT_TRUE(res != nullptr);
  1030. EXPECT_EQ(206, res->status);
  1031. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1032. EXPECT_EQ(std::string("ab"), res->body);
  1033. }
  1034. TEST_F(ServerTest, GetStreamed) {
  1035. auto res = cli_.Get("/streamed");
  1036. ASSERT_TRUE(res != nullptr);
  1037. EXPECT_EQ(200, res->status);
  1038. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1039. EXPECT_EQ(std::string("aaabbb"), res->body);
  1040. }
  1041. TEST_F(ServerTest, GetStreamedWithRange1) {
  1042. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
  1043. ASSERT_TRUE(res != nullptr);
  1044. EXPECT_EQ(206, res->status);
  1045. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1046. EXPECT_EQ(true, res->has_header("Content-Range"));
  1047. EXPECT_EQ(std::string("def"), res->body);
  1048. }
  1049. TEST_F(ServerTest, GetStreamedWithRange2) {
  1050. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
  1051. ASSERT_TRUE(res != nullptr);
  1052. EXPECT_EQ(206, res->status);
  1053. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1054. EXPECT_EQ(true, res->has_header("Content-Range"));
  1055. EXPECT_EQ(std::string("bcdefg"), res->body);
  1056. }
  1057. TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
  1058. auto res =
  1059. cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1060. ASSERT_TRUE(res != nullptr);
  1061. EXPECT_EQ(206, res->status);
  1062. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1063. EXPECT_EQ(false, res->has_header("Content-Range"));
  1064. EXPECT_EQ(269, res->body.size());
  1065. }
  1066. TEST_F(ServerTest, GetStreamedEndless) {
  1067. auto res = cli_.Get("/streamed-cancel",
  1068. [](const char * /*data*/, uint64_t /*data_length*/,
  1069. uint64_t offset,
  1070. uint64_t /*content_length*/) { return offset < 100; });
  1071. ASSERT_TRUE(res == nullptr);
  1072. }
  1073. TEST_F(ServerTest, GetWithRange1) {
  1074. auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
  1075. ASSERT_TRUE(res != nullptr);
  1076. EXPECT_EQ(206, res->status);
  1077. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1078. EXPECT_EQ(true, res->has_header("Content-Range"));
  1079. EXPECT_EQ(std::string("def"), res->body);
  1080. }
  1081. TEST_F(ServerTest, GetWithRange2) {
  1082. auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
  1083. ASSERT_TRUE(res != nullptr);
  1084. EXPECT_EQ(206, res->status);
  1085. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1086. EXPECT_EQ(true, res->has_header("Content-Range"));
  1087. EXPECT_EQ(std::string("bcdefg"), res->body);
  1088. }
  1089. TEST_F(ServerTest, GetWithRange3) {
  1090. auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
  1091. ASSERT_TRUE(res != nullptr);
  1092. EXPECT_EQ(206, res->status);
  1093. EXPECT_EQ("1", res->get_header_value("Content-Length"));
  1094. EXPECT_EQ(true, res->has_header("Content-Range"));
  1095. EXPECT_EQ(std::string("a"), res->body);
  1096. }
  1097. TEST_F(ServerTest, GetWithRange4) {
  1098. auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
  1099. ASSERT_TRUE(res != nullptr);
  1100. EXPECT_EQ(206, res->status);
  1101. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1102. EXPECT_EQ(true, res->has_header("Content-Range"));
  1103. EXPECT_EQ(std::string("fg"), res->body);
  1104. }
  1105. TEST_F(ServerTest, GetWithRangeMultipart) {
  1106. auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1107. ASSERT_TRUE(res != nullptr);
  1108. EXPECT_EQ(206, res->status);
  1109. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1110. EXPECT_EQ(false, res->has_header("Content-Range"));
  1111. EXPECT_EQ(269, res->body.size());
  1112. }
  1113. TEST_F(ServerTest, GetStreamedChunked) {
  1114. auto res = cli_.Get("/streamed-chunked");
  1115. ASSERT_TRUE(res != nullptr);
  1116. EXPECT_EQ(200, res->status);
  1117. EXPECT_EQ(std::string("123456789"), res->body);
  1118. }
  1119. TEST_F(ServerTest, LargeChunkedPost) {
  1120. Request req;
  1121. req.method = "POST";
  1122. req.path = "/large-chunked";
  1123. std::string host_and_port;
  1124. host_and_port += HOST;
  1125. host_and_port += ":";
  1126. host_and_port += std::to_string(PORT);
  1127. req.headers.emplace("Host", host_and_port.c_str());
  1128. req.headers.emplace("Accept", "*/*");
  1129. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1130. req.headers.emplace("Content-Type", "text/plain");
  1131. req.headers.emplace("Content-Length", "0");
  1132. req.headers.emplace("Transfer-Encoding", "chunked");
  1133. std::string long_string(30 * 1024u, 'a');
  1134. std::string chunk = "7800\r\n" + long_string + "\r\n";
  1135. // Attempt to make a large enough post to exceed OS buffers, to test that
  1136. // the server handles short reads if the full chunk data isn't available.
  1137. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  1138. auto res = std::make_shared<Response>();
  1139. auto ret = cli_.send(req, *res);
  1140. ASSERT_TRUE(ret);
  1141. EXPECT_EQ(200, res->status);
  1142. }
  1143. TEST_F(ServerTest, GetMethodRemoteAddr) {
  1144. auto res = cli_.Get("/remote_addr");
  1145. ASSERT_TRUE(res != nullptr);
  1146. EXPECT_EQ(200, res->status);
  1147. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1148. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  1149. }
  1150. TEST_F(ServerTest, SlowRequest) {
  1151. request_threads_.push_back(
  1152. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1153. request_threads_.push_back(
  1154. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1155. request_threads_.push_back(
  1156. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1157. msleep(100);
  1158. }
  1159. TEST_F(ServerTest, Put) {
  1160. auto res = cli_.Put("/put", "PUT", "text/plain");
  1161. ASSERT_TRUE(res != nullptr);
  1162. EXPECT_EQ(200, res->status);
  1163. EXPECT_EQ("PUT", res->body);
  1164. }
  1165. TEST_F(ServerTest, Patch) {
  1166. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  1167. ASSERT_TRUE(res != nullptr);
  1168. EXPECT_EQ(200, res->status);
  1169. EXPECT_EQ("PATCH", res->body);
  1170. }
  1171. TEST_F(ServerTest, Delete) {
  1172. auto res = cli_.Delete("/delete");
  1173. ASSERT_TRUE(res != nullptr);
  1174. EXPECT_EQ(200, res->status);
  1175. EXPECT_EQ("DELETE", res->body);
  1176. }
  1177. TEST_F(ServerTest, Options) {
  1178. auto res = cli_.Options("*");
  1179. ASSERT_TRUE(res != nullptr);
  1180. EXPECT_EQ(200, res->status);
  1181. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  1182. EXPECT_TRUE(res->body.empty());
  1183. }
  1184. TEST_F(ServerTest, URL) {
  1185. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  1186. ASSERT_TRUE(res != nullptr);
  1187. EXPECT_EQ(200, res->status);
  1188. }
  1189. TEST_F(ServerTest, ArrayParam) {
  1190. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  1191. ASSERT_TRUE(res != nullptr);
  1192. EXPECT_EQ(200, res->status);
  1193. }
  1194. TEST_F(ServerTest, NoMultipleHeaders) {
  1195. Headers headers = {{"Content-Length", "5"}};
  1196. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1197. "text/plain");
  1198. ASSERT_TRUE(res != nullptr);
  1199. EXPECT_EQ(200, res->status);
  1200. }
  1201. TEST_F(ServerTest, KeepAlive) {
  1202. cli_.set_keep_alive_max_count(4);
  1203. std::vector<Request> requests;
  1204. Get(requests, "/hi");
  1205. Get(requests, "/hi");
  1206. Get(requests, "/hi");
  1207. Get(requests, "/not-exist");
  1208. Post(requests, "/empty", "", "text/plain");
  1209. std::vector<Response> responses;
  1210. auto ret = cli_.send(requests, responses);
  1211. ASSERT_TRUE(ret == true);
  1212. ASSERT_TRUE(requests.size() == responses.size());
  1213. for (int i = 0; i < 3; i++) {
  1214. auto& res = responses[i];
  1215. EXPECT_EQ(200, res.status);
  1216. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1217. EXPECT_EQ("Hello World!", res.body);
  1218. }
  1219. {
  1220. auto& res = responses[3];
  1221. EXPECT_EQ(404, res.status);
  1222. }
  1223. {
  1224. auto& res = responses[4];
  1225. EXPECT_EQ(200, res.status);
  1226. EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
  1227. EXPECT_EQ("empty", res.body);
  1228. }
  1229. }
  1230. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1231. TEST_F(ServerTest, Gzip) {
  1232. Headers headers;
  1233. headers.emplace("Accept-Encoding", "gzip, deflate");
  1234. auto res = cli_.Get("/gzip", headers);
  1235. ASSERT_TRUE(res != nullptr);
  1236. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1237. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1238. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1239. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1240. "7890123456789012345678901234567890",
  1241. res->body);
  1242. EXPECT_EQ(200, res->status);
  1243. }
  1244. TEST_F(ServerTest, GzipWithContentReceiver) {
  1245. Headers headers;
  1246. headers.emplace("Accept-Encoding", "gzip, deflate");
  1247. std::string body;
  1248. auto res = cli_.Get("/gzip", headers,
  1249. [&](const char *data, uint64_t data_length,
  1250. uint64_t /*offset*/, uint64_t /*content_length*/) {
  1251. body.append(data, data_length);
  1252. return true;
  1253. });
  1254. ASSERT_TRUE(res != nullptr);
  1255. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1256. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1257. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1258. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1259. "7890123456789012345678901234567890",
  1260. body);
  1261. EXPECT_EQ(200, res->status);
  1262. }
  1263. TEST_F(ServerTest, NoGzip) {
  1264. Headers headers;
  1265. headers.emplace("Accept-Encoding", "gzip, deflate");
  1266. auto res = cli_.Get("/nogzip", headers);
  1267. ASSERT_TRUE(res != nullptr);
  1268. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1269. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1270. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1271. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1272. "7890123456789012345678901234567890",
  1273. res->body);
  1274. EXPECT_EQ(200, res->status);
  1275. }
  1276. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  1277. Headers headers;
  1278. headers.emplace("Accept-Encoding", "gzip, deflate");
  1279. std::string body;
  1280. auto res = cli_.Get("/nogzip", headers,
  1281. [&](const char *data, uint64_t data_length,
  1282. uint64_t /*offset*/, uint64_t /*content_length*/) {
  1283. body.append(data, data_length);
  1284. return true;
  1285. });
  1286. ASSERT_TRUE(res != nullptr);
  1287. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  1288. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  1289. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  1290. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1291. "7890123456789012345678901234567890",
  1292. body);
  1293. EXPECT_EQ(200, res->status);
  1294. }
  1295. TEST_F(ServerTest, MultipartFormDataGzip) {
  1296. Request req;
  1297. req.method = "POST";
  1298. req.path = "/gzipmultipart";
  1299. std::string host_and_port;
  1300. host_and_port += HOST;
  1301. host_and_port += ":";
  1302. host_and_port += std::to_string(PORT);
  1303. req.headers.emplace("Host", host_and_port.c_str());
  1304. req.headers.emplace("Accept", "*/*");
  1305. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1306. req.headers.emplace(
  1307. "Content-Type",
  1308. "multipart/form-data; boundary=------------------------fcba8368a9f48c0f");
  1309. req.headers.emplace("Content-Encoding", "gzip");
  1310. // compressed_body generated by creating input.txt to this file:
  1311. /*
  1312. --------------------------fcba8368a9f48c0f
  1313. Content-Disposition: form-data; name="key1"
  1314. test
  1315. --------------------------fcba8368a9f48c0f
  1316. Content-Disposition: form-data; name="key2"
  1317. --abcdefg123
  1318. --------------------------fcba8368a9f48c0f--
  1319. */
  1320. // then running unix2dos input.txt; gzip -9 -c input.txt | xxd -i.
  1321. uint8_t compressed_body[] = {
  1322. 0x1f, 0x8b, 0x08, 0x08, 0x48, 0xf1, 0xd4, 0x5a, 0x02, 0x03, 0x69, 0x6e,
  1323. 0x70, 0x75, 0x74, 0x2e, 0x74, 0x78, 0x74, 0x00, 0xd3, 0xd5, 0xc5, 0x05,
  1324. 0xd2, 0x92, 0x93, 0x12, 0x2d, 0x8c, 0xcd, 0x2c, 0x12, 0x2d, 0xd3, 0x4c,
  1325. 0x2c, 0x92, 0x0d, 0xd2, 0x78, 0xb9, 0x9c, 0xf3, 0xf3, 0x4a, 0x52, 0xf3,
  1326. 0x4a, 0x74, 0x5d, 0x32, 0x8b, 0x0b, 0xf2, 0x8b, 0x33, 0x4b, 0x32, 0xf3,
  1327. 0xf3, 0xac, 0x14, 0xd2, 0xf2, 0x8b, 0x72, 0x75, 0x53, 0x12, 0x4b, 0x12,
  1328. 0xad, 0x15, 0xf2, 0x12, 0x73, 0x53, 0x6d, 0x95, 0xb2, 0x53, 0x2b, 0x0d,
  1329. 0x95, 0x78, 0xb9, 0x78, 0xb9, 0x4a, 0x52, 0x8b, 0x4b, 0x78, 0xb9, 0x74,
  1330. 0x69, 0x61, 0x81, 0x11, 0xd8, 0x02, 0x5d, 0xdd, 0xc4, 0xa4, 0xe4, 0x94,
  1331. 0xd4, 0xb4, 0x74, 0x43, 0x23, 0x63, 0x52, 0x2c, 0xd2, 0xd5, 0xe5, 0xe5,
  1332. 0x02, 0x00, 0xff, 0x0e, 0x72, 0xdf, 0xf8, 0x00, 0x00, 0x00};
  1333. req.body = std::string((char *)compressed_body,
  1334. sizeof(compressed_body) / sizeof(compressed_body[0]));
  1335. auto res = std::make_shared<Response>();
  1336. auto ret = cli_.send(req, *res);
  1337. ASSERT_TRUE(ret);
  1338. EXPECT_EQ(200, res->status);
  1339. }
  1340. #endif
  1341. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  1342. protected:
  1343. ServerTestWithAI_PASSIVE()
  1344. : cli_(HOST, PORT)
  1345. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1346. ,
  1347. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1348. #endif
  1349. {
  1350. }
  1351. virtual void SetUp() {
  1352. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  1353. res.set_content("Hello World!", "text/plain");
  1354. });
  1355. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  1356. while (!svr_.is_running()) {
  1357. msleep(1);
  1358. }
  1359. }
  1360. virtual void TearDown() {
  1361. svr_.stop();
  1362. t_.join();
  1363. }
  1364. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1365. SSLClient cli_;
  1366. SSLServer svr_;
  1367. #else
  1368. Client cli_;
  1369. Server svr_;
  1370. #endif
  1371. thread t_;
  1372. };
  1373. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  1374. auto res = cli_.Get("/hi");
  1375. ASSERT_TRUE(res != nullptr);
  1376. EXPECT_EQ(200, res->status);
  1377. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1378. EXPECT_EQ("Hello World!", res->body);
  1379. }
  1380. class ServerUpDownTest : public ::testing::Test {
  1381. protected:
  1382. ServerUpDownTest() : cli_(HOST, PORT) {}
  1383. virtual void SetUp() {
  1384. t_ = thread([&]() {
  1385. svr_.bind_to_any_port(HOST);
  1386. msleep(500);
  1387. ASSERT_TRUE(svr_.listen_after_bind());
  1388. });
  1389. while (!svr_.is_running()) {
  1390. msleep(1);
  1391. }
  1392. }
  1393. virtual void TearDown() {
  1394. svr_.stop();
  1395. t_.join();
  1396. }
  1397. Client cli_;
  1398. Server svr_;
  1399. thread t_;
  1400. };
  1401. TEST_F(ServerUpDownTest, QuickStartStop) {
  1402. // Should not crash, especially when run with
  1403. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  1404. }
  1405. class PayloadMaxLengthTest : public ::testing::Test {
  1406. protected:
  1407. PayloadMaxLengthTest()
  1408. : cli_(HOST, PORT)
  1409. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1410. ,
  1411. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  1412. #endif
  1413. {
  1414. }
  1415. virtual void SetUp() {
  1416. svr_.set_payload_max_length(8);
  1417. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  1418. res.set_content("test", "text/plain");
  1419. });
  1420. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1421. while (!svr_.is_running()) {
  1422. msleep(1);
  1423. }
  1424. }
  1425. virtual void TearDown() {
  1426. svr_.stop();
  1427. t_.join();
  1428. }
  1429. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1430. SSLClient cli_;
  1431. SSLServer svr_;
  1432. #else
  1433. Client cli_;
  1434. Server svr_;
  1435. #endif
  1436. thread t_;
  1437. };
  1438. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  1439. auto res = cli_.Post("/test", "123456789", "text/plain");
  1440. ASSERT_TRUE(res != nullptr);
  1441. EXPECT_EQ(413, res->status);
  1442. res = cli_.Post("/test", "12345678", "text/plain");
  1443. ASSERT_TRUE(res != nullptr);
  1444. EXPECT_EQ(200, res->status);
  1445. }
  1446. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1447. TEST(SSLClientTest, ServerNameIndication) {
  1448. SSLClient cli("httpbin.org", 443);
  1449. auto res = cli.Get("/get");
  1450. ASSERT_TRUE(res != nullptr);
  1451. ASSERT_EQ(200, res->status);
  1452. }
  1453. TEST(SSLClientTest, ServerCertificateVerification) {
  1454. SSLClient cli("google.com");
  1455. auto res = cli.Get("/");
  1456. ASSERT_TRUE(res != nullptr);
  1457. ASSERT_EQ(301, res->status);
  1458. cli.enable_server_certificate_verification(true);
  1459. res = cli.Get("/");
  1460. ASSERT_TRUE(res == nullptr);
  1461. cli.set_ca_cert_path(CA_CERT_FILE);
  1462. res = cli.Get("/");
  1463. ASSERT_TRUE(res != nullptr);
  1464. ASSERT_EQ(301, res->status);
  1465. }
  1466. TEST(SSLClientTest, WildcardHostNameMatch) {
  1467. SSLClient cli("www.youtube.com");
  1468. cli.set_ca_cert_path(CA_CERT_FILE);
  1469. cli.enable_server_certificate_verification(true);
  1470. auto res = cli.Get("/");
  1471. ASSERT_TRUE(res != nullptr);
  1472. ASSERT_EQ(200, res->status);
  1473. }
  1474. TEST(SSLClientServerTest, ClientCertPresent) {
  1475. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1476. CLIENT_CA_CERT_DIR);
  1477. ASSERT_TRUE(svr.is_valid());
  1478. svr.Get("/test", [&](const Request &req, Response &res) {
  1479. res.set_content("test", "text/plain");
  1480. svr.stop();
  1481. ASSERT_TRUE(true);
  1482. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  1483. ASSERT_TRUE(peer_cert != nullptr);
  1484. auto subject_name = X509_get_subject_name(peer_cert);
  1485. ASSERT_TRUE(subject_name != nullptr);
  1486. std::string common_name;
  1487. {
  1488. char name[BUFSIZ];
  1489. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  1490. name, sizeof(name));
  1491. common_name.assign(name, name_len);
  1492. }
  1493. EXPECT_EQ("Common Name", common_name);
  1494. X509_free(peer_cert);
  1495. });
  1496. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1497. msleep(1);
  1498. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1499. CLIENT_PRIVATE_KEY_FILE);
  1500. auto res = cli.Get("/test");
  1501. ASSERT_TRUE(res != nullptr);
  1502. ASSERT_EQ(200, res->status);
  1503. t.join();
  1504. }
  1505. TEST(SSLClientServerTest, ClientCertMissing) {
  1506. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  1507. CLIENT_CA_CERT_DIR);
  1508. ASSERT_TRUE(svr.is_valid());
  1509. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  1510. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1511. msleep(1);
  1512. httplib::SSLClient cli(HOST, PORT, 30);
  1513. auto res = cli.Get("/test");
  1514. ASSERT_TRUE(res == nullptr);
  1515. svr.stop();
  1516. t.join();
  1517. }
  1518. TEST(SSLClientServerTest, TrustDirOptional) {
  1519. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  1520. ASSERT_TRUE(svr.is_valid());
  1521. svr.Get("/test", [&](const Request &, Response &res) {
  1522. res.set_content("test", "text/plain");
  1523. svr.stop();
  1524. });
  1525. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  1526. msleep(1);
  1527. httplib::SSLClient cli(HOST, PORT, 30, CLIENT_CERT_FILE,
  1528. CLIENT_PRIVATE_KEY_FILE);
  1529. auto res = cli.Get("/test");
  1530. ASSERT_TRUE(res != nullptr);
  1531. ASSERT_EQ(200, res->status);
  1532. t.join();
  1533. }
  1534. /* Cannot test this case as there is no external access to SSL object to check
  1535. SSL_get_peer_certificate() == NULL TEST(SSLClientServerTest,
  1536. ClientCAPathRequired) { SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
  1537. nullptr, CLIENT_CA_CERT_DIR);
  1538. }
  1539. */
  1540. #endif
  1541. #ifdef _WIN32
  1542. TEST(CleanupTest, WSACleanup) {
  1543. int ret = WSACleanup();
  1544. ASSERT_EQ(0, ret);
  1545. }
  1546. #endif