test.cc 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228
  1. #include <httplib.h>
  2. #include <gtest/gtest.h>
  3. #include <chrono>
  4. #include <future>
  5. #include <thread>
  6. #define SERVER_CERT_FILE "./cert.pem"
  7. #define SERVER_PRIVATE_KEY_FILE "./key.pem"
  8. #define CA_CERT_FILE "./ca-bundle.crt"
  9. #define CLIENT_CA_CERT_FILE "./rootCA.cert.pem"
  10. #define CLIENT_CA_CERT_DIR "."
  11. #define CLIENT_CERT_FILE "./client.cert.pem"
  12. #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem"
  13. using namespace std;
  14. using namespace httplib;
  15. const char *HOST = "localhost";
  16. const int PORT = 1234;
  17. const string LONG_QUERY_VALUE = string(25000, '@');
  18. const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
  19. const std::string JSON_DATA = "{\"hello\":\"world\"}";
  20. const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB
  21. MultipartFormData &get_file_value(MultipartFormDataItems &files,
  22. const char *key) {
  23. auto it = std::find_if(
  24. files.begin(), files.end(),
  25. [&](const MultipartFormData &file) { return file.name == key; });
  26. if (it != files.end()) { return *it; }
  27. throw std::runtime_error("invalid mulitpart form data name error");
  28. }
  29. #ifdef _WIN32
  30. TEST(StartupTest, WSAStartup) {
  31. WSADATA wsaData;
  32. int ret = WSAStartup(0x0002, &wsaData);
  33. ASSERT_EQ(0, ret);
  34. }
  35. #endif
  36. TEST(SplitTest, ParseQueryString) {
  37. string s = "key1=val1&key2=val2&key3=val3";
  38. Params dic;
  39. detail::split(s.c_str(), s.c_str() + s.size(), '&',
  40. [&](const char *b, const char *e) {
  41. string key, val;
  42. detail::split(b, e, '=', [&](const char *b, const char *e) {
  43. if (key.empty()) {
  44. key.assign(b, e);
  45. } else {
  46. val.assign(b, e);
  47. }
  48. });
  49. dic.emplace(key, val);
  50. });
  51. EXPECT_EQ("val1", dic.find("key1")->second);
  52. EXPECT_EQ("val2", dic.find("key2")->second);
  53. EXPECT_EQ("val3", dic.find("key3")->second);
  54. }
  55. TEST(ParseQueryTest, ParseQueryString) {
  56. string s = "key1=val1&key2=val2&key3=val3";
  57. Params dic;
  58. detail::parse_query_text(s, dic);
  59. EXPECT_EQ("val1", dic.find("key1")->second);
  60. EXPECT_EQ("val2", dic.find("key2")->second);
  61. EXPECT_EQ("val3", dic.find("key3")->second);
  62. }
  63. TEST(ParamsToQueryTest, ConvertParamsToQuery) {
  64. Params dic;
  65. EXPECT_EQ(detail::params_to_query_str(dic), "");
  66. dic.emplace("key1", "val1");
  67. EXPECT_EQ(detail::params_to_query_str(dic), "key1=val1");
  68. dic.emplace("key2", "val2");
  69. dic.emplace("key3", "val3");
  70. EXPECT_EQ(detail::params_to_query_str(dic), "key1=val1&key2=val2&key3=val3");
  71. }
  72. TEST(GetHeaderValueTest, DefaultValue) {
  73. Headers headers = {{"Dummy", "Dummy"}};
  74. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  75. EXPECT_STREQ("text/plain", val);
  76. }
  77. TEST(GetHeaderValueTest, DefaultValueInt) {
  78. Headers headers = {{"Dummy", "Dummy"}};
  79. auto val =
  80. detail::get_header_value<uint64_t>(headers, "Content-Length", 0, 100);
  81. EXPECT_EQ(100ull, val);
  82. }
  83. TEST(GetHeaderValueTest, RegularValue) {
  84. Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
  85. auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
  86. EXPECT_STREQ("text/html", val);
  87. }
  88. TEST(GetHeaderValueTest, RegularValueInt) {
  89. Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
  90. auto val =
  91. detail::get_header_value<uint64_t>(headers, "Content-Length", 0, 0);
  92. EXPECT_EQ(100ull, val);
  93. }
  94. TEST(GetHeaderValueTest, Range) {
  95. {
  96. Headers headers = {make_range_header({{1, -1}})};
  97. auto val = detail::get_header_value(headers, "Range", 0, 0);
  98. EXPECT_STREQ("bytes=1-", val);
  99. }
  100. {
  101. Headers headers = {make_range_header({{-1, 1}})};
  102. auto val = detail::get_header_value(headers, "Range", 0, 0);
  103. EXPECT_STREQ("bytes=-1", val);
  104. }
  105. {
  106. Headers headers = {make_range_header({{1, 10}})};
  107. auto val = detail::get_header_value(headers, "Range", 0, 0);
  108. EXPECT_STREQ("bytes=1-10", val);
  109. }
  110. {
  111. Headers headers = {make_range_header({{1, 10}, {100, -1}})};
  112. auto val = detail::get_header_value(headers, "Range", 0, 0);
  113. EXPECT_STREQ("bytes=1-10, 100-", val);
  114. }
  115. {
  116. Headers headers = {make_range_header({{1, 10}, {100, 200}})};
  117. auto val = detail::get_header_value(headers, "Range", 0, 0);
  118. EXPECT_STREQ("bytes=1-10, 100-200", val);
  119. }
  120. {
  121. Headers headers = {make_range_header({{0, 0}, {-1, 1}})};
  122. auto val = detail::get_header_value(headers, "Range", 0, 0);
  123. EXPECT_STREQ("bytes=0-0, -1", val);
  124. }
  125. }
  126. TEST(ParseHeaderValueTest, Range) {
  127. {
  128. Ranges ranges;
  129. auto ret = detail::parse_range_header("bytes=1-", ranges);
  130. EXPECT_TRUE(ret);
  131. EXPECT_EQ(1u, ranges.size());
  132. EXPECT_EQ(1u, ranges[0].first);
  133. EXPECT_EQ(-1, ranges[0].second);
  134. }
  135. {
  136. Ranges ranges;
  137. auto ret = detail::parse_range_header("bytes=-1", ranges);
  138. EXPECT_TRUE(ret);
  139. EXPECT_EQ(1u, ranges.size());
  140. EXPECT_EQ(-1, ranges[0].first);
  141. EXPECT_EQ(1u, ranges[0].second);
  142. }
  143. {
  144. Ranges ranges;
  145. auto ret = detail::parse_range_header("bytes=1-10", ranges);
  146. EXPECT_TRUE(ret);
  147. EXPECT_EQ(1u, ranges.size());
  148. EXPECT_EQ(1u, ranges[0].first);
  149. EXPECT_EQ(10u, ranges[0].second);
  150. }
  151. {
  152. Ranges ranges;
  153. auto ret = detail::parse_range_header("bytes=10-1", ranges);
  154. EXPECT_FALSE(ret);
  155. }
  156. {
  157. Ranges ranges;
  158. auto ret = detail::parse_range_header("bytes=1-10, 100-", ranges);
  159. EXPECT_TRUE(ret);
  160. EXPECT_EQ(2u, ranges.size());
  161. EXPECT_EQ(1u, ranges[0].first);
  162. EXPECT_EQ(10u, ranges[0].second);
  163. EXPECT_EQ(100u, ranges[1].first);
  164. EXPECT_EQ(-1, ranges[1].second);
  165. }
  166. {
  167. Ranges ranges;
  168. auto ret =
  169. detail::parse_range_header("bytes=1-10, 100-200, 300-400", ranges);
  170. EXPECT_TRUE(ret);
  171. EXPECT_EQ(3u, ranges.size());
  172. EXPECT_EQ(1u, ranges[0].first);
  173. EXPECT_EQ(10u, ranges[0].second);
  174. EXPECT_EQ(100u, ranges[1].first);
  175. EXPECT_EQ(200u, ranges[1].second);
  176. EXPECT_EQ(300u, ranges[2].first);
  177. EXPECT_EQ(400u, ranges[2].second);
  178. }
  179. }
  180. TEST(ParseAcceptEncoding1, AcceptEncoding) {
  181. Request req;
  182. req.set_header("Accept-Encoding", "gzip");
  183. Response res;
  184. res.set_header("Content-Type", "text/plain");
  185. auto ret = detail::encoding_type(req, res);
  186. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  187. EXPECT_TRUE(ret == detail::EncodingType::Gzip);
  188. #else
  189. EXPECT_TRUE(ret == detail::EncodingType::None);
  190. #endif
  191. }
  192. TEST(ParseAcceptEncoding2, AcceptEncoding) {
  193. Request req;
  194. req.set_header("Accept-Encoding", "gzip, deflate, br");
  195. Response res;
  196. res.set_header("Content-Type", "text/plain");
  197. auto ret = detail::encoding_type(req, res);
  198. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  199. EXPECT_TRUE(ret == detail::EncodingType::Brotli);
  200. #elif CPPHTTPLIB_ZLIB_SUPPORT
  201. EXPECT_TRUE(ret == detail::EncodingType::Gzip);
  202. #else
  203. EXPECT_TRUE(ret == detail::EncodingType::None);
  204. #endif
  205. }
  206. TEST(ParseAcceptEncoding3, AcceptEncoding) {
  207. Request req;
  208. req.set_header("Accept-Encoding", "br;q=1.0, gzip;q=0.8, *;q=0.1");
  209. Response res;
  210. res.set_header("Content-Type", "text/plain");
  211. auto ret = detail::encoding_type(req, res);
  212. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  213. EXPECT_TRUE(ret == detail::EncodingType::Brotli);
  214. #elif CPPHTTPLIB_ZLIB_SUPPORT
  215. EXPECT_TRUE(ret == detail::EncodingType::Gzip);
  216. #else
  217. EXPECT_TRUE(ret == detail::EncodingType::None);
  218. #endif
  219. }
  220. TEST(BufferStreamTest, read) {
  221. detail::BufferStream strm1;
  222. Stream &strm = strm1;
  223. EXPECT_EQ(5, strm.write("hello"));
  224. char buf[512];
  225. EXPECT_EQ(2, strm.read(buf, 2));
  226. EXPECT_EQ('h', buf[0]);
  227. EXPECT_EQ('e', buf[1]);
  228. EXPECT_EQ(2, strm.read(buf, 2));
  229. EXPECT_EQ('l', buf[0]);
  230. EXPECT_EQ('l', buf[1]);
  231. EXPECT_EQ(1, strm.read(buf, 1));
  232. EXPECT_EQ('o', buf[0]);
  233. EXPECT_EQ(0, strm.read(buf, 1));
  234. }
  235. TEST(ChunkedEncodingTest, FromHTTPWatch) {
  236. auto host = "www.httpwatch.com";
  237. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  238. auto port = 443;
  239. SSLClient cli(host, port);
  240. #else
  241. auto port = 80;
  242. Client cli(host, port);
  243. #endif
  244. cli.set_connection_timeout(2);
  245. auto res =
  246. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
  247. ASSERT_TRUE(res != nullptr);
  248. std::string out;
  249. detail::read_file("./image.jpg", out);
  250. EXPECT_EQ(200, res->status);
  251. EXPECT_EQ(out, res->body);
  252. }
  253. TEST(ChunkedEncodingTest, WithContentReceiver) {
  254. auto host = "www.httpwatch.com";
  255. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  256. auto port = 443;
  257. SSLClient cli(host, port);
  258. #else
  259. auto port = 80;
  260. Client cli(host, port);
  261. #endif
  262. cli.set_connection_timeout(2);
  263. std::string body;
  264. auto res =
  265. cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
  266. [&](const char *data, size_t data_length) {
  267. body.append(data, data_length);
  268. return true;
  269. });
  270. ASSERT_TRUE(res != nullptr);
  271. std::string out;
  272. detail::read_file("./image.jpg", out);
  273. EXPECT_EQ(200, res->status);
  274. EXPECT_EQ(out, body);
  275. }
  276. TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
  277. auto host = "www.httpwatch.com";
  278. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  279. auto port = 443;
  280. SSLClient cli(host, port);
  281. #else
  282. auto port = 80;
  283. Client cli(host, port);
  284. #endif
  285. cli.set_connection_timeout(2);
  286. std::string body;
  287. auto res = cli.Get(
  288. "/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
  289. [&](const Response &response) {
  290. EXPECT_EQ(200, response.status);
  291. return true;
  292. },
  293. [&](const char *data, size_t data_length) {
  294. body.append(data, data_length);
  295. return true;
  296. });
  297. ASSERT_TRUE(res != nullptr);
  298. std::string out;
  299. detail::read_file("./image.jpg", out);
  300. EXPECT_EQ(200, res->status);
  301. EXPECT_EQ(out, body);
  302. }
  303. TEST(DefaultHeadersTest, FromHTTPBin) {
  304. Client cli("httpbin.org");
  305. cli.set_default_headers({make_range_header({{1, 10}})});
  306. cli.set_connection_timeout(5);
  307. {
  308. auto res = cli.Get("/range/32");
  309. ASSERT_TRUE(res != nullptr);
  310. EXPECT_EQ("bcdefghijk", res->body);
  311. EXPECT_EQ(206, res->status);
  312. }
  313. {
  314. auto res = cli.Get("/range/32");
  315. ASSERT_TRUE(res != nullptr);
  316. EXPECT_EQ("bcdefghijk", res->body);
  317. EXPECT_EQ(206, res->status);
  318. }
  319. }
  320. TEST(RangeTest, FromHTTPBin) {
  321. auto host = "httpbin.org";
  322. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  323. auto port = 443;
  324. SSLClient cli(host, port);
  325. #else
  326. auto port = 80;
  327. Client cli(host, port);
  328. #endif
  329. cli.set_connection_timeout(5);
  330. {
  331. auto res = cli.Get("/range/32");
  332. ASSERT_TRUE(res != nullptr);
  333. EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
  334. EXPECT_EQ(200, res->status);
  335. }
  336. {
  337. Headers headers = {make_range_header({{1, -1}})};
  338. auto res = cli.Get("/range/32", headers);
  339. ASSERT_TRUE(res != nullptr);
  340. EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
  341. EXPECT_EQ(206, res->status);
  342. }
  343. {
  344. Headers headers = {make_range_header({{1, 10}})};
  345. auto res = cli.Get("/range/32", headers);
  346. ASSERT_TRUE(res != nullptr);
  347. EXPECT_EQ("bcdefghijk", res->body);
  348. EXPECT_EQ(206, res->status);
  349. }
  350. {
  351. Headers headers = {make_range_header({{0, 31}})};
  352. auto res = cli.Get("/range/32", headers);
  353. ASSERT_TRUE(res != nullptr);
  354. EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
  355. EXPECT_EQ(200, res->status);
  356. }
  357. {
  358. Headers headers = {make_range_header({{0, -1}})};
  359. auto res = cli.Get("/range/32", headers);
  360. ASSERT_TRUE(res != nullptr);
  361. EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
  362. EXPECT_EQ(200, res->status);
  363. }
  364. {
  365. Headers headers = {make_range_header({{0, 32}})};
  366. auto res = cli.Get("/range/32", headers);
  367. ASSERT_TRUE(res != nullptr);
  368. EXPECT_EQ(416, res->status);
  369. }
  370. }
  371. TEST(ConnectionErrorTest, InvalidHost) {
  372. auto host = "-abcde.com";
  373. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  374. auto port = 443;
  375. SSLClient cli(host, port);
  376. #else
  377. auto port = 80;
  378. Client cli(host, port);
  379. #endif
  380. cli.set_connection_timeout(2);
  381. auto res = cli.Get("/");
  382. ASSERT_TRUE(res == nullptr);
  383. }
  384. TEST(ConnectionErrorTest, InvalidHost2) {
  385. auto host = "httpbin.org/";
  386. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  387. SSLClient cli(host);
  388. #else
  389. Client cli(host);
  390. #endif
  391. cli.set_connection_timeout(2);
  392. auto res = cli.Get("/");
  393. ASSERT_TRUE(res == nullptr);
  394. }
  395. TEST(ConnectionErrorTest, InvalidPort) {
  396. auto host = "localhost";
  397. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  398. auto port = 44380;
  399. SSLClient cli(host, port);
  400. #else
  401. auto port = 8080;
  402. Client cli(host, port);
  403. #endif
  404. cli.set_connection_timeout(2);
  405. auto res = cli.Get("/");
  406. ASSERT_TRUE(res == nullptr);
  407. }
  408. TEST(ConnectionErrorTest, Timeout) {
  409. auto host = "google.com";
  410. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  411. auto port = 44380;
  412. SSLClient cli(host, port);
  413. #else
  414. auto port = 8080;
  415. Client cli(host, port);
  416. #endif
  417. cli.set_connection_timeout(2);
  418. auto res = cli.Get("/");
  419. ASSERT_TRUE(res == nullptr);
  420. }
  421. TEST(CancelTest, NoCancel) {
  422. auto host = "httpbin.org";
  423. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  424. auto port = 443;
  425. SSLClient cli(host, port);
  426. #else
  427. auto port = 80;
  428. Client cli(host, port);
  429. #endif
  430. cli.set_connection_timeout(5);
  431. auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; });
  432. ASSERT_TRUE(res != nullptr);
  433. EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
  434. EXPECT_EQ(200, res->status);
  435. }
  436. TEST(CancelTest, WithCancelSmallPayload) {
  437. auto host = "httpbin.org";
  438. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  439. auto port = 443;
  440. SSLClient cli(host, port);
  441. #else
  442. auto port = 80;
  443. Client cli(host, port);
  444. #endif
  445. auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return false; });
  446. cli.set_connection_timeout(5);
  447. ASSERT_TRUE(res == nullptr);
  448. }
  449. TEST(CancelTest, WithCancelLargePayload) {
  450. auto host = "httpbin.org";
  451. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  452. auto port = 443;
  453. SSLClient cli(host, port);
  454. #else
  455. auto port = 80;
  456. Client cli(host, port);
  457. #endif
  458. cli.set_connection_timeout(5);
  459. uint32_t count = 0;
  460. auto res = cli.Get("/range/65536",
  461. [&count](uint64_t, uint64_t) { return (count++ == 0); });
  462. ASSERT_TRUE(res == nullptr);
  463. }
  464. TEST(BaseAuthTest, FromHTTPWatch) {
  465. auto host = "httpbin.org";
  466. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  467. auto port = 443;
  468. SSLClient cli(host, port);
  469. #else
  470. auto port = 80;
  471. Client cli(host, port);
  472. #endif
  473. {
  474. auto res = cli.Get("/basic-auth/hello/world");
  475. ASSERT_TRUE(res != nullptr);
  476. EXPECT_EQ(401, res->status);
  477. }
  478. {
  479. auto res = cli.Get("/basic-auth/hello/world",
  480. {make_basic_authentication_header("hello", "world")});
  481. ASSERT_TRUE(res != nullptr);
  482. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
  483. res->body);
  484. EXPECT_EQ(200, res->status);
  485. }
  486. {
  487. cli.set_basic_auth("hello", "world");
  488. auto res = cli.Get("/basic-auth/hello/world");
  489. ASSERT_TRUE(res != nullptr);
  490. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
  491. res->body);
  492. EXPECT_EQ(200, res->status);
  493. }
  494. {
  495. cli.set_basic_auth("hello", "bad");
  496. auto res = cli.Get("/basic-auth/hello/world");
  497. ASSERT_TRUE(res != nullptr);
  498. EXPECT_EQ(401, res->status);
  499. }
  500. {
  501. cli.set_basic_auth("bad", "world");
  502. auto res = cli.Get("/basic-auth/hello/world");
  503. ASSERT_TRUE(res != nullptr);
  504. EXPECT_EQ(401, res->status);
  505. }
  506. }
  507. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  508. TEST(DigestAuthTest, FromHTTPWatch) {
  509. auto host = "httpbin.org";
  510. auto port = 443;
  511. SSLClient cli(host, port);
  512. {
  513. auto res = cli.Get("/digest-auth/auth/hello/world");
  514. ASSERT_TRUE(res != nullptr);
  515. EXPECT_EQ(401, res->status);
  516. }
  517. {
  518. std::vector<std::string> paths = {
  519. "/digest-auth/auth/hello/world/MD5",
  520. "/digest-auth/auth/hello/world/SHA-256",
  521. "/digest-auth/auth/hello/world/SHA-512",
  522. "/digest-auth/auth-int/hello/world/MD5",
  523. };
  524. cli.set_digest_auth("hello", "world");
  525. for (auto path : paths) {
  526. auto res = cli.Get(path.c_str());
  527. ASSERT_TRUE(res != nullptr);
  528. EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
  529. res->body);
  530. EXPECT_EQ(200, res->status);
  531. }
  532. cli.set_digest_auth("hello", "bad");
  533. for (auto path : paths) {
  534. auto res = cli.Get(path.c_str());
  535. ASSERT_TRUE(res != nullptr);
  536. EXPECT_EQ(401, res->status);
  537. }
  538. // NOTE: Until httpbin.org fixes issue #46, the following test is commented
  539. // out. Plese see https://httpbin.org/digest-auth/auth/hello/world
  540. // cli.set_digest_auth("bad", "world");
  541. // for (auto path : paths) {
  542. // auto res = cli.Get(path.c_str());
  543. // ASSERT_TRUE(res != nullptr);
  544. // EXPECT_EQ(400, res->status);
  545. // }
  546. }
  547. }
  548. #endif
  549. #if 0
  550. TEST(AbsoluteRedirectTest, Redirect) {
  551. auto host = "httpbin.org";
  552. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  553. SSLClient cli(host);
  554. #else
  555. Client cli(host);
  556. #endif
  557. cli.set_follow_location(true);
  558. auto res = cli.Get("/absolute-redirect/3");
  559. ASSERT_TRUE(res != nullptr);
  560. EXPECT_EQ(200, res->status);
  561. }
  562. TEST(RedirectTest, Redirect) {
  563. auto host = "httpbin.org";
  564. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  565. SSLClient cli(host);
  566. #else
  567. Client cli(host);
  568. #endif
  569. cli.set_follow_location(true);
  570. auto res = cli.Get("/redirect/3");
  571. ASSERT_TRUE(res != nullptr);
  572. EXPECT_EQ(200, res->status);
  573. }
  574. TEST(RelativeRedirectTest, Redirect) {
  575. auto host = "httpbin.org";
  576. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  577. SSLClient cli(host);
  578. #else
  579. Client cli(host);
  580. #endif
  581. cli.set_follow_location(true);
  582. auto res = cli.Get("/relative-redirect/3");
  583. ASSERT_TRUE(res != nullptr);
  584. EXPECT_EQ(200, res->status);
  585. }
  586. TEST(TooManyRedirectTest, Redirect) {
  587. auto host = "httpbin.org";
  588. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  589. SSLClient cli(host);
  590. #else
  591. Client cli(host);
  592. #endif
  593. cli.set_follow_location(true);
  594. auto res = cli.Get("/redirect/21");
  595. ASSERT_TRUE(res == nullptr);
  596. }
  597. #endif
  598. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  599. TEST(YahooRedirectTest, Redirect) {
  600. Client cli("yahoo.com");
  601. auto res = cli.Get("/");
  602. ASSERT_TRUE(res != nullptr);
  603. EXPECT_EQ(301, res->status);
  604. cli.set_follow_location(true);
  605. res = cli.Get("/");
  606. ASSERT_TRUE(res != nullptr);
  607. EXPECT_EQ(200, res->status);
  608. }
  609. #if 0
  610. TEST(HttpsToHttpRedirectTest, Redirect) {
  611. SSLClient cli("httpbin.org");
  612. cli.set_follow_location(true);
  613. auto res =
  614. cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
  615. ASSERT_TRUE(res != nullptr);
  616. EXPECT_EQ(200, res->status);
  617. }
  618. #endif
  619. TEST(RedirectToDifferentPort, Redirect) {
  620. Server svr8080;
  621. Server svr8081;
  622. svr8080.Get("/1", [&](const Request & /*req*/, Response &res) {
  623. res.set_redirect("http://localhost:8081/2");
  624. });
  625. svr8081.Get("/2", [&](const Request & /*req*/, Response &res) {
  626. res.set_content("Hello World!", "text/plain");
  627. });
  628. auto thread8080 = std::thread([&]() { svr8080.listen("localhost", 8080); });
  629. auto thread8081 = std::thread([&]() { svr8081.listen("localhost", 8081); });
  630. while (!svr8080.is_running() || !svr8081.is_running()) {
  631. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  632. }
  633. // Give GET time to get a few messages.
  634. std::this_thread::sleep_for(std::chrono::seconds(1));
  635. Client cli("localhost", 8080);
  636. cli.set_follow_location(true);
  637. auto res = cli.Get("/1");
  638. ASSERT_TRUE(res != nullptr);
  639. EXPECT_EQ(200, res->status);
  640. EXPECT_EQ("Hello World!", res->body);
  641. svr8080.stop();
  642. svr8081.stop();
  643. thread8080.join();
  644. thread8081.join();
  645. ASSERT_FALSE(svr8080.is_running());
  646. ASSERT_FALSE(svr8081.is_running());
  647. }
  648. TEST(UrlWithSpace, Redirect) {
  649. SSLClient cli("edge.forgecdn.net");
  650. cli.set_follow_location(true);
  651. auto res = cli.Get("/files/2595/310/Neat 1.4-17.jar");
  652. ASSERT_TRUE(res != nullptr);
  653. EXPECT_EQ(200, res->status);
  654. EXPECT_EQ(18527, res->get_header_value<uint64_t>("Content-Length"));
  655. }
  656. #endif
  657. TEST(Server, BindDualStack) {
  658. Server svr;
  659. svr.Get("/1", [&](const Request & /*req*/, Response &res) {
  660. res.set_content("Hello World!", "text/plain");
  661. });
  662. auto thread = std::thread([&]() { svr.listen("::", PORT); });
  663. // Give GET time to get a few messages.
  664. std::this_thread::sleep_for(std::chrono::seconds(1));
  665. {
  666. Client cli("127.0.0.1", PORT);
  667. auto res = cli.Get("/1");
  668. ASSERT_TRUE(res != nullptr);
  669. EXPECT_EQ(200, res->status);
  670. EXPECT_EQ("Hello World!", res->body);
  671. }
  672. {
  673. Client cli("::1", PORT);
  674. auto res = cli.Get("/1");
  675. ASSERT_TRUE(res != nullptr);
  676. EXPECT_EQ(200, res->status);
  677. EXPECT_EQ("Hello World!", res->body);
  678. }
  679. svr.stop();
  680. thread.join();
  681. ASSERT_FALSE(svr.is_running());
  682. }
  683. TEST(Server, BindAndListenSeparately) {
  684. Server svr;
  685. int port = svr.bind_to_any_port("0.0.0.0");
  686. ASSERT_TRUE(svr.is_valid());
  687. ASSERT_TRUE(port > 0);
  688. svr.stop();
  689. }
  690. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  691. TEST(SSLServer, BindAndListenSeparately) {
  692. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  693. CLIENT_CA_CERT_DIR);
  694. int port = svr.bind_to_any_port("0.0.0.0");
  695. ASSERT_TRUE(svr.is_valid());
  696. ASSERT_TRUE(port > 0);
  697. svr.stop();
  698. }
  699. #endif
  700. class ServerTest : public ::testing::Test {
  701. protected:
  702. ServerTest()
  703. : cli_(HOST, PORT)
  704. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  705. ,
  706. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  707. #endif
  708. {
  709. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  710. cli_.enable_server_certificate_verification(false);
  711. #endif
  712. }
  713. virtual void SetUp() {
  714. svr_.set_mount_point("/", "./www");
  715. svr_.set_mount_point("/mount", "./www2");
  716. svr_.set_file_extension_and_mimetype_mapping("abcde", "text/abcde");
  717. svr_.Get("/hi",
  718. [&](const Request & /*req*/, Response &res) {
  719. res.set_content("Hello World!", "text/plain");
  720. })
  721. .Get("/http_response_splitting",
  722. [&](const Request & /*req*/, Response &res) {
  723. res.set_header("a", "1\r\nSet-Cookie: a=1");
  724. EXPECT_EQ(0, res.headers.size());
  725. EXPECT_FALSE(res.has_header("a"));
  726. res.set_header("a", "1\nSet-Cookie: a=1");
  727. EXPECT_EQ(0, res.headers.size());
  728. EXPECT_FALSE(res.has_header("a"));
  729. res.set_header("a", "1\rSet-Cookie: a=1");
  730. EXPECT_EQ(0, res.headers.size());
  731. EXPECT_FALSE(res.has_header("a"));
  732. res.set_header("a\r\nb", "0");
  733. EXPECT_EQ(0, res.headers.size());
  734. EXPECT_FALSE(res.has_header("a"));
  735. res.set_header("a\rb", "0");
  736. EXPECT_EQ(0, res.headers.size());
  737. EXPECT_FALSE(res.has_header("a"));
  738. res.set_header("a\nb", "0");
  739. EXPECT_EQ(0, res.headers.size());
  740. EXPECT_FALSE(res.has_header("a"));
  741. res.set_redirect("1\r\nSet-Cookie: a=1");
  742. EXPECT_EQ(0, res.headers.size());
  743. EXPECT_FALSE(res.has_header("Location"));
  744. })
  745. .Get("/slow",
  746. [&](const Request & /*req*/, Response &res) {
  747. std::this_thread::sleep_for(std::chrono::seconds(2));
  748. res.set_content("slow", "text/plain");
  749. })
  750. .Post("/slowpost",
  751. [&](const Request & /*req*/, Response &res) {
  752. std::this_thread::sleep_for(std::chrono::seconds(2));
  753. res.set_content("slow", "text/plain");
  754. })
  755. .Get("/remote_addr",
  756. [&](const Request &req, Response &res) {
  757. auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
  758. EXPECT_TRUE(req.has_header("REMOTE_PORT"));
  759. EXPECT_EQ(req.remote_addr, req.get_header_value("REMOTE_ADDR"));
  760. EXPECT_EQ(req.remote_port,
  761. std::stoi(req.get_header_value("REMOTE_PORT")));
  762. res.set_content(remote_addr.c_str(), "text/plain");
  763. })
  764. .Get("/endwith%",
  765. [&](const Request & /*req*/, Response &res) {
  766. res.set_content("Hello World!", "text/plain");
  767. })
  768. .Get("/a\\+\\+b",
  769. [&](const Request &req, Response &res) {
  770. ASSERT_TRUE(req.has_param("a +b"));
  771. auto val = req.get_param_value("a +b");
  772. res.set_content(val, "text/plain");
  773. })
  774. .Get("/", [&](const Request & /*req*/,
  775. Response &res) { res.set_redirect("/hi"); })
  776. .Post("/1", [](const Request & /*req*/,
  777. Response &res) { res.set_redirect("/2", 303); })
  778. .Get("/2",
  779. [](const Request & /*req*/, Response &res) {
  780. res.set_content("redirected.", "text/plain");
  781. res.status = 200;
  782. })
  783. .Post("/person",
  784. [&](const Request &req, Response &res) {
  785. if (req.has_param("name") && req.has_param("note")) {
  786. persons_[req.get_param_value("name")] =
  787. req.get_param_value("note");
  788. } else {
  789. res.status = 400;
  790. }
  791. })
  792. .Put("/person",
  793. [&](const Request &req, Response &res) {
  794. if (req.has_param("name") && req.has_param("note")) {
  795. persons_[req.get_param_value("name")] =
  796. req.get_param_value("note");
  797. } else {
  798. res.status = 400;
  799. }
  800. })
  801. .Get("/person/(.*)",
  802. [&](const Request &req, Response &res) {
  803. string name = req.matches[1];
  804. if (persons_.find(name) != persons_.end()) {
  805. auto note = persons_[name];
  806. res.set_content(note, "text/plain");
  807. } else {
  808. res.status = 404;
  809. }
  810. })
  811. .Post("/x-www-form-urlencoded-json",
  812. [&](const Request &req, Response &res) {
  813. auto json = req.get_param_value("json");
  814. ASSERT_EQ(JSON_DATA, json);
  815. res.set_content(json, "appliation/json");
  816. res.status = 200;
  817. })
  818. .Get("/streamed-chunked",
  819. [&](const Request & /*req*/, Response &res) {
  820. res.set_chunked_content_provider(
  821. "text/plain", [](size_t /*offset*/, DataSink &sink) {
  822. EXPECT_TRUE(sink.is_writable());
  823. sink.os << "123";
  824. sink.os << "456";
  825. sink.os << "789";
  826. sink.done();
  827. return true;
  828. });
  829. })
  830. .Get("/streamed-chunked2",
  831. [&](const Request & /*req*/, Response &res) {
  832. auto i = new int(0);
  833. res.set_chunked_content_provider(
  834. "text/plain",
  835. [i](size_t /*offset*/, DataSink &sink) {
  836. EXPECT_TRUE(sink.is_writable());
  837. switch (*i) {
  838. case 0: sink.os << "123"; break;
  839. case 1: sink.os << "456"; break;
  840. case 2: sink.os << "789"; break;
  841. case 3: sink.done(); break;
  842. }
  843. (*i)++;
  844. return true;
  845. },
  846. [i] { delete i; });
  847. })
  848. .Get("/streamed",
  849. [&](const Request & /*req*/, Response &res) {
  850. res.set_content_provider(
  851. 6, "text/plain",
  852. [](size_t offset, size_t /*length*/, DataSink &sink) {
  853. sink.os << (offset < 3 ? "a" : "b");
  854. return true;
  855. });
  856. })
  857. .Get("/streamed-with-range",
  858. [&](const Request & /*req*/, Response &res) {
  859. auto data = new std::string("abcdefg");
  860. res.set_content_provider(
  861. data->size(), "text/plain",
  862. [data](size_t offset, size_t length, DataSink &sink) {
  863. EXPECT_TRUE(sink.is_writable());
  864. size_t DATA_CHUNK_SIZE = 4;
  865. const auto &d = *data;
  866. auto out_len =
  867. std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
  868. sink.write(&d[static_cast<size_t>(offset)], out_len);
  869. return true;
  870. },
  871. [data] { delete data; });
  872. })
  873. .Get("/streamed-cancel",
  874. [&](const Request & /*req*/, Response &res) {
  875. res.set_content_provider(
  876. size_t(-1), "text/plain",
  877. [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
  878. EXPECT_TRUE(sink.is_writable());
  879. sink.os << "data_chunk";
  880. return true;
  881. });
  882. })
  883. .Get("/with-range",
  884. [&](const Request & /*req*/, Response &res) {
  885. res.set_content("abcdefg", "text/plain");
  886. })
  887. .Post("/chunked",
  888. [&](const Request &req, Response & /*res*/) {
  889. EXPECT_EQ(req.body, "dechunked post body");
  890. })
  891. .Post("/large-chunked",
  892. [&](const Request &req, Response & /*res*/) {
  893. std::string expected(6 * 30 * 1024u, 'a');
  894. EXPECT_EQ(req.body, expected);
  895. })
  896. .Post("/multipart",
  897. [&](const Request &req, Response & /*res*/) {
  898. EXPECT_EQ(5u, req.files.size());
  899. ASSERT_TRUE(!req.has_file("???"));
  900. ASSERT_TRUE(req.body.empty());
  901. {
  902. const auto &file = req.get_file_value("text1");
  903. EXPECT_TRUE(file.filename.empty());
  904. EXPECT_EQ("text default", file.content);
  905. }
  906. {
  907. const auto &file = req.get_file_value("text2");
  908. EXPECT_TRUE(file.filename.empty());
  909. EXPECT_EQ("aωb", file.content);
  910. }
  911. {
  912. const auto &file = req.get_file_value("file1");
  913. EXPECT_EQ("hello.txt", file.filename);
  914. EXPECT_EQ("text/plain", file.content_type);
  915. EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content);
  916. }
  917. {
  918. const auto &file = req.get_file_value("file3");
  919. EXPECT_TRUE(file.filename.empty());
  920. EXPECT_EQ("application/octet-stream", file.content_type);
  921. EXPECT_EQ(0u, file.content.size());
  922. }
  923. })
  924. .Post("/empty",
  925. [&](const Request &req, Response &res) {
  926. EXPECT_EQ(req.body, "");
  927. EXPECT_EQ("text/plain", req.get_header_value("Content-Type"));
  928. EXPECT_EQ("0", req.get_header_value("Content-Length"));
  929. res.set_content("empty", "text/plain");
  930. })
  931. .Post("/empty-no-content-type",
  932. [&](const Request &req, Response &res) {
  933. EXPECT_EQ(req.body, "");
  934. EXPECT_FALSE(req.has_header("Content-Type"));
  935. EXPECT_EQ("0", req.get_header_value("Content-Length"));
  936. res.set_content("empty-no-content-type", "text/plain");
  937. })
  938. .Put("/empty-no-content-type",
  939. [&](const Request &req, Response &res) {
  940. EXPECT_EQ(req.body, "");
  941. EXPECT_FALSE(req.has_header("Content-Type"));
  942. EXPECT_EQ("0", req.get_header_value("Content-Length"));
  943. res.set_content("empty-no-content-type", "text/plain");
  944. })
  945. .Put("/put",
  946. [&](const Request &req, Response &res) {
  947. EXPECT_EQ(req.body, "PUT");
  948. res.set_content(req.body, "text/plain");
  949. })
  950. .Put("/put-large",
  951. [&](const Request &req, Response &res) {
  952. EXPECT_EQ(req.body, LARGE_DATA);
  953. res.set_content(req.body, "text/plain");
  954. })
  955. .Patch("/patch",
  956. [&](const Request &req, Response &res) {
  957. EXPECT_EQ(req.body, "PATCH");
  958. res.set_content(req.body, "text/plain");
  959. })
  960. .Delete("/delete",
  961. [&](const Request & /*req*/, Response &res) {
  962. res.set_content("DELETE", "text/plain");
  963. })
  964. .Delete("/delete-body",
  965. [&](const Request &req, Response &res) {
  966. EXPECT_EQ(req.body, "content");
  967. res.set_content(req.body, "text/plain");
  968. })
  969. .Options(R"(\*)",
  970. [&](const Request & /*req*/, Response &res) {
  971. res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
  972. })
  973. .Get("/request-target",
  974. [&](const Request &req, Response & /*res*/) {
  975. EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
  976. EXPECT_EQ("bbb", req.get_param_value("aaa"));
  977. EXPECT_EQ("ddd", req.get_param_value("ccc"));
  978. })
  979. .Get("/long-query-value",
  980. [&](const Request &req, Response & /*res*/) {
  981. EXPECT_EQ(LONG_QUERY_URL, req.target);
  982. EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
  983. })
  984. .Get("/array-param",
  985. [&](const Request &req, Response & /*res*/) {
  986. EXPECT_EQ(3u, req.get_param_value_count("array"));
  987. EXPECT_EQ("value1", req.get_param_value("array", 0));
  988. EXPECT_EQ("value2", req.get_param_value("array", 1));
  989. EXPECT_EQ("value3", req.get_param_value("array", 2));
  990. })
  991. .Post("/validate-no-multiple-headers",
  992. [&](const Request &req, Response & /*res*/) {
  993. EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
  994. EXPECT_EQ("5", req.get_header_value("Content-Length"));
  995. })
  996. .Post("/content_receiver",
  997. [&](const Request &req, Response &res,
  998. const ContentReader &content_reader) {
  999. if (req.is_multipart_form_data()) {
  1000. MultipartFormDataItems files;
  1001. content_reader(
  1002. [&](const MultipartFormData &file) {
  1003. files.push_back(file);
  1004. return true;
  1005. },
  1006. [&](const char *data, size_t data_length) {
  1007. files.back().content.append(data, data_length);
  1008. return true;
  1009. });
  1010. EXPECT_EQ(5u, files.size());
  1011. {
  1012. const auto &file = get_file_value(files, "text1");
  1013. EXPECT_TRUE(file.filename.empty());
  1014. EXPECT_EQ("text default", file.content);
  1015. }
  1016. {
  1017. const auto &file = get_file_value(files, "text2");
  1018. EXPECT_TRUE(file.filename.empty());
  1019. EXPECT_EQ("aωb", file.content);
  1020. }
  1021. {
  1022. const auto &file = get_file_value(files, "file1");
  1023. EXPECT_EQ("hello.txt", file.filename);
  1024. EXPECT_EQ("text/plain", file.content_type);
  1025. EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content);
  1026. }
  1027. {
  1028. const auto &file = get_file_value(files, "file3");
  1029. EXPECT_TRUE(file.filename.empty());
  1030. EXPECT_EQ("application/octet-stream", file.content_type);
  1031. EXPECT_EQ(0u, file.content.size());
  1032. }
  1033. } else {
  1034. std::string body;
  1035. content_reader([&](const char *data, size_t data_length) {
  1036. EXPECT_EQ(data_length, 7);
  1037. body.append(data, data_length);
  1038. return true;
  1039. });
  1040. EXPECT_EQ(body, "content");
  1041. res.set_content(body, "text/plain");
  1042. }
  1043. })
  1044. .Put("/content_receiver",
  1045. [&](const Request & /*req*/, Response &res,
  1046. const ContentReader &content_reader) {
  1047. std::string body;
  1048. content_reader([&](const char *data, size_t data_length) {
  1049. body.append(data, data_length);
  1050. return true;
  1051. });
  1052. EXPECT_EQ(body, "content");
  1053. res.set_content(body, "text/plain");
  1054. })
  1055. .Patch("/content_receiver",
  1056. [&](const Request & /*req*/, Response &res,
  1057. const ContentReader &content_reader) {
  1058. std::string body;
  1059. content_reader([&](const char *data, size_t data_length) {
  1060. body.append(data, data_length);
  1061. return true;
  1062. });
  1063. EXPECT_EQ(body, "content");
  1064. res.set_content(body, "text/plain");
  1065. })
  1066. .Post("/query-string-and-body",
  1067. [&](const Request &req, Response & /*res*/) {
  1068. ASSERT_TRUE(req.has_param("key"));
  1069. EXPECT_EQ(req.get_param_value("key"), "value");
  1070. EXPECT_EQ(req.body, "content");
  1071. })
  1072. .Get("/last-request",
  1073. [&](const Request &req, Response & /*res*/) {
  1074. EXPECT_EQ("close", req.get_header_value("Connection"));
  1075. })
  1076. #if defined(CPPHTTPLIB_ZLIB_SUPPORT) || defined(CPPHTTPLIB_BROTLI_SUPPORT)
  1077. .Get("/compress",
  1078. [&](const Request & /*req*/, Response &res) {
  1079. res.set_content(
  1080. "12345678901234567890123456789012345678901234567890123456789"
  1081. "01234567890123456789012345678901234567890",
  1082. "text/plain");
  1083. })
  1084. .Get("/nocompress",
  1085. [&](const Request & /*req*/, Response &res) {
  1086. res.set_content(
  1087. "12345678901234567890123456789012345678901234567890123456789"
  1088. "01234567890123456789012345678901234567890",
  1089. "application/octet-stream");
  1090. })
  1091. .Post("/compress-multipart",
  1092. [&](const Request &req, Response & /*res*/) {
  1093. EXPECT_EQ(2u, req.files.size());
  1094. ASSERT_TRUE(!req.has_file("???"));
  1095. {
  1096. const auto &file = req.get_file_value("key1");
  1097. EXPECT_TRUE(file.filename.empty());
  1098. EXPECT_EQ("test", file.content);
  1099. }
  1100. {
  1101. const auto &file = req.get_file_value("key2");
  1102. EXPECT_TRUE(file.filename.empty());
  1103. EXPECT_EQ("--abcdefg123", file.content);
  1104. }
  1105. })
  1106. #endif
  1107. ;
  1108. persons_["john"] = "programmer";
  1109. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  1110. while (!svr_.is_running()) {
  1111. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  1112. }
  1113. }
  1114. virtual void TearDown() {
  1115. svr_.stop();
  1116. for (auto &t : request_threads_) {
  1117. t.join();
  1118. }
  1119. t_.join();
  1120. }
  1121. map<string, string> persons_;
  1122. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  1123. SSLClient cli_;
  1124. SSLServer svr_;
  1125. #else
  1126. Client cli_;
  1127. Server svr_;
  1128. #endif
  1129. thread t_;
  1130. std::vector<thread> request_threads_;
  1131. };
  1132. TEST_F(ServerTest, GetMethod200) {
  1133. auto res = cli_.Get("/hi");
  1134. ASSERT_TRUE(res != nullptr);
  1135. EXPECT_EQ("HTTP/1.1", res->version);
  1136. EXPECT_EQ(200, res->status);
  1137. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1138. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  1139. EXPECT_EQ("Hello World!", res->body);
  1140. }
  1141. TEST_F(ServerTest, GetMethod200withPercentEncoding) {
  1142. auto res = cli_.Get("/%68%69"); // auto res = cli_.Get("/hi");
  1143. ASSERT_TRUE(res != nullptr);
  1144. EXPECT_EQ("HTTP/1.1", res->version);
  1145. EXPECT_EQ(200, res->status);
  1146. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1147. EXPECT_EQ(1, res->get_header_value_count("Content-Type"));
  1148. EXPECT_EQ("Hello World!", res->body);
  1149. }
  1150. TEST_F(ServerTest, GetMethod302) {
  1151. auto res = cli_.Get("/");
  1152. ASSERT_TRUE(res != nullptr);
  1153. EXPECT_EQ(302, res->status);
  1154. EXPECT_EQ("/hi", res->get_header_value("Location"));
  1155. }
  1156. TEST_F(ServerTest, GetMethod302Redirect) {
  1157. cli_.set_follow_location(true);
  1158. auto res = cli_.Get("/");
  1159. ASSERT_TRUE(res != nullptr);
  1160. EXPECT_EQ(200, res->status);
  1161. EXPECT_EQ("Hello World!", res->body);
  1162. }
  1163. TEST_F(ServerTest, GetMethod404) {
  1164. auto res = cli_.Get("/invalid");
  1165. ASSERT_TRUE(res != nullptr);
  1166. EXPECT_EQ(404, res->status);
  1167. }
  1168. TEST_F(ServerTest, HeadMethod200) {
  1169. auto res = cli_.Head("/hi");
  1170. ASSERT_TRUE(res != nullptr);
  1171. EXPECT_EQ(200, res->status);
  1172. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1173. EXPECT_TRUE(res->body.empty());
  1174. }
  1175. TEST_F(ServerTest, HeadMethod200Static) {
  1176. auto res = cli_.Head("/mount/dir/index.html");
  1177. ASSERT_TRUE(res != nullptr);
  1178. EXPECT_EQ(200, res->status);
  1179. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1180. EXPECT_EQ(104, std::stoi(res->get_header_value("Content-Length")));
  1181. EXPECT_TRUE(res->body.empty());
  1182. }
  1183. TEST_F(ServerTest, HeadMethod404) {
  1184. auto res = cli_.Head("/invalid");
  1185. ASSERT_TRUE(res != nullptr);
  1186. EXPECT_EQ(404, res->status);
  1187. EXPECT_TRUE(res->body.empty());
  1188. }
  1189. TEST_F(ServerTest, GetMethodPersonJohn) {
  1190. auto res = cli_.Get("/person/john");
  1191. ASSERT_TRUE(res != nullptr);
  1192. EXPECT_EQ(200, res->status);
  1193. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1194. EXPECT_EQ("programmer", res->body);
  1195. }
  1196. TEST_F(ServerTest, PostMethod1) {
  1197. auto res = cli_.Get("/person/john1");
  1198. ASSERT_TRUE(res != nullptr);
  1199. ASSERT_EQ(404, res->status);
  1200. res = cli_.Post("/person", "name=john1&note=coder",
  1201. "application/x-www-form-urlencoded");
  1202. ASSERT_TRUE(res != nullptr);
  1203. ASSERT_EQ(200, res->status);
  1204. res = cli_.Get("/person/john1");
  1205. ASSERT_TRUE(res != nullptr);
  1206. ASSERT_EQ(200, res->status);
  1207. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  1208. ASSERT_EQ("coder", res->body);
  1209. }
  1210. TEST_F(ServerTest, PostMethod2) {
  1211. auto res = cli_.Get("/person/john2");
  1212. ASSERT_TRUE(res != nullptr);
  1213. ASSERT_EQ(404, res->status);
  1214. Params params;
  1215. params.emplace("name", "john2");
  1216. params.emplace("note", "coder");
  1217. res = cli_.Post("/person", params);
  1218. ASSERT_TRUE(res != nullptr);
  1219. ASSERT_EQ(200, res->status);
  1220. res = cli_.Get("/person/john2");
  1221. ASSERT_TRUE(res != nullptr);
  1222. ASSERT_EQ(200, res->status);
  1223. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  1224. ASSERT_EQ("coder", res->body);
  1225. }
  1226. TEST_F(ServerTest, PutMethod3) {
  1227. auto res = cli_.Get("/person/john3");
  1228. ASSERT_TRUE(res != nullptr);
  1229. ASSERT_EQ(404, res->status);
  1230. Params params;
  1231. params.emplace("name", "john3");
  1232. params.emplace("note", "coder");
  1233. res = cli_.Put("/person", params);
  1234. ASSERT_TRUE(res != nullptr);
  1235. ASSERT_EQ(200, res->status);
  1236. res = cli_.Get("/person/john3");
  1237. ASSERT_TRUE(res != nullptr);
  1238. ASSERT_EQ(200, res->status);
  1239. ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
  1240. ASSERT_EQ("coder", res->body);
  1241. }
  1242. TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
  1243. Params params;
  1244. params.emplace("json", JSON_DATA);
  1245. auto res = cli_.Post("/x-www-form-urlencoded-json", params);
  1246. ASSERT_TRUE(res != nullptr);
  1247. ASSERT_EQ(200, res->status);
  1248. ASSERT_EQ(JSON_DATA, res->body);
  1249. }
  1250. TEST_F(ServerTest, PostEmptyContent) {
  1251. auto res = cli_.Post("/empty", "", "text/plain");
  1252. ASSERT_TRUE(res != nullptr);
  1253. ASSERT_EQ(200, res->status);
  1254. ASSERT_EQ("empty", res->body);
  1255. }
  1256. TEST_F(ServerTest, PostEmptyContentWithNoContentType) {
  1257. auto res = cli_.Post("/empty-no-content-type");
  1258. ASSERT_TRUE(res != nullptr);
  1259. ASSERT_EQ(200, res->status);
  1260. ASSERT_EQ("empty-no-content-type", res->body);
  1261. }
  1262. TEST_F(ServerTest, PutEmptyContentWithNoContentType) {
  1263. auto res = cli_.Put("/empty-no-content-type");
  1264. ASSERT_TRUE(res != nullptr);
  1265. ASSERT_EQ(200, res->status);
  1266. ASSERT_EQ("empty-no-content-type", res->body);
  1267. }
  1268. TEST_F(ServerTest, GetMethodDir) {
  1269. auto res = cli_.Get("/dir/");
  1270. ASSERT_TRUE(res != nullptr);
  1271. EXPECT_EQ(200, res->status);
  1272. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1273. auto body = R"(<html>
  1274. <head>
  1275. </head>
  1276. <body>
  1277. <a href="/dir/test.html">Test</a>
  1278. <a href="/hi">hi</a>
  1279. </body>
  1280. </html>
  1281. )";
  1282. EXPECT_EQ(body, res->body);
  1283. }
  1284. TEST_F(ServerTest, GetMethodDirTest) {
  1285. auto res = cli_.Get("/dir/test.html");
  1286. ASSERT_TRUE(res != nullptr);
  1287. EXPECT_EQ(200, res->status);
  1288. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1289. EXPECT_EQ("test.html", res->body);
  1290. }
  1291. TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
  1292. auto res = cli_.Get("/dir/../dir/test.html");
  1293. ASSERT_TRUE(res != nullptr);
  1294. EXPECT_EQ(200, res->status);
  1295. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1296. EXPECT_EQ("test.html", res->body);
  1297. }
  1298. TEST_F(ServerTest, GetMethodInvalidPath) {
  1299. auto res = cli_.Get("/dir/../test.html");
  1300. ASSERT_TRUE(res != nullptr);
  1301. EXPECT_EQ(404, res->status);
  1302. }
  1303. TEST_F(ServerTest, GetMethodOutOfBaseDir) {
  1304. auto res = cli_.Get("/../www/dir/test.html");
  1305. ASSERT_TRUE(res != nullptr);
  1306. EXPECT_EQ(404, res->status);
  1307. }
  1308. TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
  1309. auto res = cli_.Get("/dir/../../www/dir/test.html");
  1310. ASSERT_TRUE(res != nullptr);
  1311. EXPECT_EQ(404, res->status);
  1312. }
  1313. TEST_F(ServerTest, GetMethodDirMountTest) {
  1314. auto res = cli_.Get("/mount/dir/test.html");
  1315. ASSERT_TRUE(res != nullptr);
  1316. EXPECT_EQ(200, res->status);
  1317. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1318. EXPECT_EQ("test.html", res->body);
  1319. }
  1320. TEST_F(ServerTest, GetMethodDirMountTestWithDoubleDots) {
  1321. auto res = cli_.Get("/mount/dir/../dir/test.html");
  1322. ASSERT_TRUE(res != nullptr);
  1323. EXPECT_EQ(200, res->status);
  1324. EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
  1325. EXPECT_EQ("test.html", res->body);
  1326. }
  1327. TEST_F(ServerTest, GetMethodInvalidMountPath) {
  1328. auto res = cli_.Get("/mount/dir/../test.html");
  1329. ASSERT_TRUE(res != nullptr);
  1330. EXPECT_EQ(404, res->status);
  1331. }
  1332. TEST_F(ServerTest, GetMethodOutOfBaseDirMount) {
  1333. auto res = cli_.Get("/mount/../www2/dir/test.html");
  1334. ASSERT_TRUE(res != nullptr);
  1335. EXPECT_EQ(404, res->status);
  1336. }
  1337. TEST_F(ServerTest, GetMethodOutOfBaseDirMount2) {
  1338. auto res = cli_.Get("/mount/dir/../../www2/dir/test.html");
  1339. ASSERT_TRUE(res != nullptr);
  1340. EXPECT_EQ(404, res->status);
  1341. }
  1342. TEST_F(ServerTest, PostMethod303) {
  1343. auto res = cli_.Post("/1", "body", "text/plain");
  1344. ASSERT_TRUE(res != nullptr);
  1345. EXPECT_EQ(303, res->status);
  1346. EXPECT_EQ("/2", res->get_header_value("Location"));
  1347. }
  1348. TEST_F(ServerTest, PostMethod303Redirect) {
  1349. cli_.set_follow_location(true);
  1350. auto res = cli_.Post("/1", "body", "text/plain");
  1351. ASSERT_TRUE(res != nullptr);
  1352. EXPECT_EQ(200, res->status);
  1353. EXPECT_EQ("redirected.", res->body);
  1354. }
  1355. TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
  1356. auto res = cli_.Get("/dir/test.abcde");
  1357. ASSERT_TRUE(res != nullptr);
  1358. EXPECT_EQ(200, res->status);
  1359. EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
  1360. EXPECT_EQ("abcde", res->body);
  1361. }
  1362. TEST_F(ServerTest, InvalidBaseDirMount) {
  1363. EXPECT_EQ(false, svr_.set_mount_point("invalid_mount_point", "./www3"));
  1364. }
  1365. TEST_F(ServerTest, EmptyRequest) {
  1366. auto res = cli_.Get("");
  1367. ASSERT_TRUE(res == nullptr);
  1368. }
  1369. TEST_F(ServerTest, LongRequest) {
  1370. std::string request;
  1371. for (size_t i = 0; i < 545; i++) {
  1372. request += "/TooLongRequest";
  1373. }
  1374. request += "OK";
  1375. auto res = cli_.Get(request.c_str());
  1376. ASSERT_TRUE(res != nullptr);
  1377. EXPECT_EQ(404, res->status);
  1378. }
  1379. TEST_F(ServerTest, TooLongRequest) {
  1380. std::string request;
  1381. for (size_t i = 0; i < 545; i++) {
  1382. request += "/TooLongRequest";
  1383. }
  1384. request += "_NG";
  1385. auto res = cli_.Get(request.c_str());
  1386. ASSERT_TRUE(res != nullptr);
  1387. EXPECT_EQ(414, res->status);
  1388. }
  1389. TEST_F(ServerTest, LongHeader) {
  1390. Request req;
  1391. req.method = "GET";
  1392. req.path = "/hi";
  1393. std::string host_and_port;
  1394. host_and_port += HOST;
  1395. host_and_port += ":";
  1396. host_and_port += std::to_string(PORT);
  1397. req.headers.emplace("Host", host_and_port.c_str());
  1398. req.headers.emplace("Accept", "*/*");
  1399. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1400. req.headers.emplace(
  1401. "Header-Name",
  1402. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1403. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1404. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1405. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1406. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1407. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1408. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1409. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1410. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1411. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1412. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1413. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1414. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1415. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1416. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1417. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1418. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1419. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1420. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1421. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1422. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1423. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1424. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1425. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1426. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1427. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1428. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1429. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1430. "@@@@@@@@@@@@@@@@");
  1431. auto res = std::make_shared<Response>();
  1432. auto ret = cli_.send(req, *res);
  1433. ASSERT_TRUE(ret);
  1434. EXPECT_EQ(200, res->status);
  1435. }
  1436. TEST_F(ServerTest, LongQueryValue) {
  1437. auto res = cli_.Get(LONG_QUERY_URL.c_str());
  1438. ASSERT_TRUE(res != nullptr);
  1439. EXPECT_EQ(414, res->status);
  1440. }
  1441. TEST_F(ServerTest, TooLongHeader) {
  1442. Request req;
  1443. req.method = "GET";
  1444. req.path = "/hi";
  1445. std::string host_and_port;
  1446. host_and_port += HOST;
  1447. host_and_port += ":";
  1448. host_and_port += std::to_string(PORT);
  1449. req.headers.emplace("Host", host_and_port.c_str());
  1450. req.headers.emplace("Accept", "*/*");
  1451. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1452. req.headers.emplace(
  1453. "Header-Name",
  1454. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1455. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1456. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1457. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1458. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1459. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1460. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1461. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1462. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1463. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1464. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1465. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1466. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1467. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1468. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1469. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1470. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1471. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1472. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1473. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1474. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1475. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1476. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1477. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1478. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1479. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1480. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1481. "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
  1482. "@@@@@@@@@@@@@@@@@");
  1483. auto res = std::make_shared<Response>();
  1484. auto ret = cli_.send(req, *res);
  1485. ASSERT_TRUE(ret);
  1486. EXPECT_EQ(200, res->status);
  1487. }
  1488. TEST_F(ServerTest, PercentEncoding) {
  1489. auto res = cli_.Get("/e%6edwith%");
  1490. ASSERT_TRUE(res != nullptr);
  1491. EXPECT_EQ(200, res->status);
  1492. }
  1493. TEST_F(ServerTest, PercentEncodingUnicode) {
  1494. auto res = cli_.Get("/e%u006edwith%");
  1495. ASSERT_TRUE(res != nullptr);
  1496. EXPECT_EQ(200, res->status);
  1497. }
  1498. TEST_F(ServerTest, InvalidPercentEncoding) {
  1499. auto res = cli_.Get("/%endwith%");
  1500. ASSERT_TRUE(res != nullptr);
  1501. EXPECT_EQ(404, res->status);
  1502. }
  1503. TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
  1504. auto res = cli_.Get("/%uendwith%");
  1505. ASSERT_TRUE(res != nullptr);
  1506. EXPECT_EQ(404, res->status);
  1507. }
  1508. TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
  1509. auto res = cli_.Get("/hello?aaa=bbb%");
  1510. ASSERT_TRUE(res != nullptr);
  1511. EXPECT_EQ(404, res->status);
  1512. }
  1513. TEST_F(ServerTest, PlusSignEncoding) {
  1514. auto res = cli_.Get("/a+%2Bb?a %2bb=a %2Bb");
  1515. ASSERT_TRUE(res != nullptr);
  1516. EXPECT_EQ(200, res->status);
  1517. EXPECT_EQ("a +b", res->body);
  1518. }
  1519. TEST_F(ServerTest, MultipartFormData) {
  1520. MultipartFormDataItems items = {
  1521. {"text1", "text default", "", ""},
  1522. {"text2", "aωb", "", ""},
  1523. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1524. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1525. {"file3", "", "", "application/octet-stream"},
  1526. };
  1527. auto res = cli_.Post("/multipart", items);
  1528. ASSERT_TRUE(res != nullptr);
  1529. EXPECT_EQ(200, res->status);
  1530. }
  1531. TEST_F(ServerTest, CaseInsensitiveHeaderName) {
  1532. auto res = cli_.Get("/hi");
  1533. ASSERT_TRUE(res != nullptr);
  1534. EXPECT_EQ(200, res->status);
  1535. EXPECT_EQ("text/plain", res->get_header_value("content-type"));
  1536. EXPECT_EQ("Hello World!", res->body);
  1537. }
  1538. TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
  1539. Request req;
  1540. req.method = "POST";
  1541. req.path = "/chunked";
  1542. std::string host_and_port;
  1543. host_and_port += HOST;
  1544. host_and_port += ":";
  1545. host_and_port += std::to_string(PORT);
  1546. req.headers.emplace("Host", host_and_port.c_str());
  1547. req.headers.emplace("Accept", "*/*");
  1548. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1549. req.headers.emplace("Content-Type", "text/plain");
  1550. req.headers.emplace("Content-Length", "0");
  1551. req.headers.emplace(
  1552. "Transfer-Encoding",
  1553. "Chunked"); // Note, "Chunked" rather than typical "chunked".
  1554. // Client does not chunk, so make a chunked body manually.
  1555. req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
  1556. auto res = std::make_shared<Response>();
  1557. auto ret = cli_.send(req, *res);
  1558. ASSERT_TRUE(ret);
  1559. EXPECT_EQ(200, res->status);
  1560. }
  1561. TEST_F(ServerTest, GetStreamed2) {
  1562. auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
  1563. ASSERT_TRUE(res != nullptr);
  1564. EXPECT_EQ(206, res->status);
  1565. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1566. EXPECT_EQ(std::string("ab"), res->body);
  1567. }
  1568. TEST_F(ServerTest, GetStreamed) {
  1569. auto res = cli_.Get("/streamed");
  1570. ASSERT_TRUE(res != nullptr);
  1571. EXPECT_EQ(200, res->status);
  1572. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1573. EXPECT_EQ(std::string("aaabbb"), res->body);
  1574. }
  1575. TEST_F(ServerTest, GetStreamedWithRange1) {
  1576. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
  1577. ASSERT_TRUE(res != nullptr);
  1578. EXPECT_EQ(206, res->status);
  1579. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1580. EXPECT_EQ(true, res->has_header("Content-Range"));
  1581. EXPECT_EQ(std::string("def"), res->body);
  1582. }
  1583. TEST_F(ServerTest, GetStreamedWithRange2) {
  1584. auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
  1585. ASSERT_TRUE(res != nullptr);
  1586. EXPECT_EQ(206, res->status);
  1587. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1588. EXPECT_EQ(true, res->has_header("Content-Range"));
  1589. EXPECT_EQ(std::string("bcdefg"), res->body);
  1590. }
  1591. TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
  1592. auto res =
  1593. cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1594. ASSERT_TRUE(res != nullptr);
  1595. EXPECT_EQ(206, res->status);
  1596. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1597. EXPECT_EQ(false, res->has_header("Content-Range"));
  1598. EXPECT_EQ(269, res->body.size());
  1599. }
  1600. TEST_F(ServerTest, GetStreamedEndless) {
  1601. uint64_t offset = 0;
  1602. auto res = cli_.Get("/streamed-cancel",
  1603. [&](const char * /*data*/, uint64_t data_length) {
  1604. if (offset < 100) {
  1605. offset += data_length;
  1606. return true;
  1607. }
  1608. return false;
  1609. });
  1610. ASSERT_TRUE(res == nullptr);
  1611. }
  1612. TEST_F(ServerTest, ClientStop) {
  1613. std::vector<std::thread> threads;
  1614. for (auto i = 0; i < 3; i++) {
  1615. threads.emplace_back(thread([&]() {
  1616. auto res = cli_.Get("/streamed-cancel",
  1617. [&](const char *, uint64_t) { return true; });
  1618. ASSERT_TRUE(res == nullptr);
  1619. }));
  1620. }
  1621. std::this_thread::sleep_for(std::chrono::seconds(1));
  1622. while (cli_.is_socket_open()) {
  1623. cli_.stop();
  1624. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  1625. }
  1626. for (auto &t : threads) {
  1627. t.join();
  1628. }
  1629. }
  1630. TEST_F(ServerTest, GetWithRange1) {
  1631. auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
  1632. ASSERT_TRUE(res != nullptr);
  1633. EXPECT_EQ(206, res->status);
  1634. EXPECT_EQ("3", res->get_header_value("Content-Length"));
  1635. EXPECT_EQ(true, res->has_header("Content-Range"));
  1636. EXPECT_EQ(std::string("def"), res->body);
  1637. }
  1638. TEST_F(ServerTest, GetWithRange2) {
  1639. auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
  1640. ASSERT_TRUE(res != nullptr);
  1641. EXPECT_EQ(206, res->status);
  1642. EXPECT_EQ("6", res->get_header_value("Content-Length"));
  1643. EXPECT_EQ(true, res->has_header("Content-Range"));
  1644. EXPECT_EQ(std::string("bcdefg"), res->body);
  1645. }
  1646. TEST_F(ServerTest, GetWithRange3) {
  1647. auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
  1648. ASSERT_TRUE(res != nullptr);
  1649. EXPECT_EQ(206, res->status);
  1650. EXPECT_EQ("1", res->get_header_value("Content-Length"));
  1651. EXPECT_EQ(true, res->has_header("Content-Range"));
  1652. EXPECT_EQ(std::string("a"), res->body);
  1653. }
  1654. TEST_F(ServerTest, GetWithRange4) {
  1655. auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
  1656. ASSERT_TRUE(res != nullptr);
  1657. EXPECT_EQ(206, res->status);
  1658. EXPECT_EQ("2", res->get_header_value("Content-Length"));
  1659. EXPECT_EQ(true, res->has_header("Content-Range"));
  1660. EXPECT_EQ(std::string("fg"), res->body);
  1661. }
  1662. TEST_F(ServerTest, GetWithRangeMultipart) {
  1663. auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
  1664. ASSERT_TRUE(res != nullptr);
  1665. EXPECT_EQ(206, res->status);
  1666. EXPECT_EQ("269", res->get_header_value("Content-Length"));
  1667. EXPECT_EQ(false, res->has_header("Content-Range"));
  1668. EXPECT_EQ(269, res->body.size());
  1669. }
  1670. TEST_F(ServerTest, GetStreamedChunked) {
  1671. auto res = cli_.Get("/streamed-chunked");
  1672. ASSERT_TRUE(res != nullptr);
  1673. EXPECT_EQ(200, res->status);
  1674. EXPECT_EQ(std::string("123456789"), res->body);
  1675. }
  1676. TEST_F(ServerTest, GetStreamedChunked2) {
  1677. auto res = cli_.Get("/streamed-chunked2");
  1678. ASSERT_TRUE(res != nullptr);
  1679. EXPECT_EQ(200, res->status);
  1680. EXPECT_EQ(std::string("123456789"), res->body);
  1681. }
  1682. TEST_F(ServerTest, LargeChunkedPost) {
  1683. Request req;
  1684. req.method = "POST";
  1685. req.path = "/large-chunked";
  1686. std::string host_and_port;
  1687. host_and_port += HOST;
  1688. host_and_port += ":";
  1689. host_and_port += std::to_string(PORT);
  1690. req.headers.emplace("Host", host_and_port.c_str());
  1691. req.headers.emplace("Accept", "*/*");
  1692. req.headers.emplace("User-Agent", "cpp-httplib/0.1");
  1693. req.headers.emplace("Content-Type", "text/plain");
  1694. req.headers.emplace("Content-Length", "0");
  1695. req.headers.emplace("Transfer-Encoding", "chunked");
  1696. std::string long_string(30 * 1024u, 'a');
  1697. std::string chunk = "7800\r\n" + long_string + "\r\n";
  1698. // Attempt to make a large enough post to exceed OS buffers, to test that
  1699. // the server handles short reads if the full chunk data isn't available.
  1700. req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
  1701. auto res = std::make_shared<Response>();
  1702. auto ret = cli_.send(req, *res);
  1703. ASSERT_TRUE(ret);
  1704. EXPECT_EQ(200, res->status);
  1705. }
  1706. TEST_F(ServerTest, GetMethodRemoteAddr) {
  1707. auto res = cli_.Get("/remote_addr");
  1708. ASSERT_TRUE(res != nullptr);
  1709. EXPECT_EQ(200, res->status);
  1710. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1711. EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
  1712. }
  1713. TEST_F(ServerTest, HTTPResponseSplitting) {
  1714. auto res = cli_.Get("/http_response_splitting");
  1715. ASSERT_TRUE(res != nullptr);
  1716. EXPECT_EQ(200, res->status);
  1717. }
  1718. TEST_F(ServerTest, SlowRequest) {
  1719. request_threads_.push_back(
  1720. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1721. request_threads_.push_back(
  1722. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1723. request_threads_.push_back(
  1724. std::thread([=]() { auto res = cli_.Get("/slow"); }));
  1725. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  1726. }
  1727. TEST_F(ServerTest, SlowPost) {
  1728. char buffer[64 * 1024];
  1729. memset(buffer, 0x42, sizeof(buffer));
  1730. auto res = cli_.Post(
  1731. "/slowpost", 64 * 1024 * 1024,
  1732. [&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
  1733. sink.write(buffer, sizeof(buffer));
  1734. return true;
  1735. },
  1736. "text/plain");
  1737. ASSERT_TRUE(res != nullptr);
  1738. EXPECT_EQ(200, res->status);
  1739. cli_.set_write_timeout(0, 0);
  1740. res = cli_.Post(
  1741. "/slowpost", 64 * 1024 * 1024,
  1742. [&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
  1743. sink.write(buffer, sizeof(buffer));
  1744. return true;
  1745. },
  1746. "text/plain");
  1747. ASSERT_FALSE(res != nullptr);
  1748. }
  1749. TEST_F(ServerTest, Put) {
  1750. auto res = cli_.Put("/put", "PUT", "text/plain");
  1751. ASSERT_TRUE(res != nullptr);
  1752. EXPECT_EQ(200, res->status);
  1753. EXPECT_EQ("PUT", res->body);
  1754. }
  1755. TEST_F(ServerTest, PutWithContentProvider) {
  1756. auto res = cli_.Put(
  1757. "/put", 3,
  1758. [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
  1759. EXPECT_TRUE(sink.is_writable());
  1760. sink.os << "PUT";
  1761. return true;
  1762. },
  1763. "text/plain");
  1764. ASSERT_TRUE(res != nullptr);
  1765. EXPECT_EQ(200, res->status);
  1766. EXPECT_EQ("PUT", res->body);
  1767. }
  1768. TEST_F(ServerTest, PostWithContentProviderAbort) {
  1769. auto res = cli_.Post(
  1770. "/post", 42,
  1771. [](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) {
  1772. return false;
  1773. },
  1774. "text/plain");
  1775. ASSERT_TRUE(res == nullptr);
  1776. }
  1777. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1778. TEST_F(ServerTest, PutWithContentProviderWithGzip) {
  1779. cli_.set_compress(true);
  1780. auto res = cli_.Put(
  1781. "/put", 3,
  1782. [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
  1783. EXPECT_TRUE(sink.is_writable());
  1784. sink.os << "PUT";
  1785. return true;
  1786. },
  1787. "text/plain");
  1788. ASSERT_TRUE(res != nullptr);
  1789. EXPECT_EQ(200, res->status);
  1790. EXPECT_EQ("PUT", res->body);
  1791. }
  1792. TEST_F(ServerTest, PostWithContentProviderWithGzipAbort) {
  1793. cli_.set_compress(true);
  1794. auto res = cli_.Post(
  1795. "/post", 42,
  1796. [](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) {
  1797. return false;
  1798. },
  1799. "text/plain");
  1800. ASSERT_TRUE(res == nullptr);
  1801. }
  1802. TEST_F(ServerTest, PutLargeFileWithGzip) {
  1803. cli_.set_compress(true);
  1804. auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain");
  1805. ASSERT_TRUE(res != nullptr);
  1806. EXPECT_EQ(200, res->status);
  1807. EXPECT_EQ(LARGE_DATA, res->body);
  1808. }
  1809. TEST_F(ServerTest, PutContentWithDeflate) {
  1810. cli_.set_compress(false);
  1811. Headers headers;
  1812. headers.emplace("Content-Encoding", "deflate");
  1813. // PUT in deflate format:
  1814. auto res = cli_.Put("/put", headers,
  1815. "\170\234\013\010\015\001\0\001\361\0\372", "text/plain");
  1816. ASSERT_TRUE(res != nullptr);
  1817. EXPECT_EQ(200, res->status);
  1818. EXPECT_EQ("PUT", res->body);
  1819. }
  1820. TEST_F(ServerTest, GetStreamedChunkedWithGzip) {
  1821. Headers headers;
  1822. headers.emplace("Accept-Encoding", "gzip, deflate");
  1823. auto res = cli_.Get("/streamed-chunked", headers);
  1824. ASSERT_TRUE(res != nullptr);
  1825. EXPECT_EQ(200, res->status);
  1826. EXPECT_EQ(std::string("123456789"), res->body);
  1827. }
  1828. TEST_F(ServerTest, GetStreamedChunkedWithGzip2) {
  1829. Headers headers;
  1830. headers.emplace("Accept-Encoding", "gzip, deflate");
  1831. auto res = cli_.Get("/streamed-chunked2", headers);
  1832. ASSERT_TRUE(res != nullptr);
  1833. EXPECT_EQ(200, res->status);
  1834. EXPECT_EQ(std::string("123456789"), res->body);
  1835. }
  1836. #endif
  1837. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  1838. TEST_F(ServerTest, GetStreamedChunkedWithBrotli) {
  1839. Headers headers;
  1840. headers.emplace("Accept-Encoding", "brotli");
  1841. auto res = cli_.Get("/streamed-chunked", headers);
  1842. ASSERT_TRUE(res != nullptr);
  1843. EXPECT_EQ(200, res->status);
  1844. EXPECT_EQ(std::string("123456789"), res->body);
  1845. }
  1846. TEST_F(ServerTest, GetStreamedChunkedWithBrotli2) {
  1847. Headers headers;
  1848. headers.emplace("Accept-Encoding", "brotli");
  1849. auto res = cli_.Get("/streamed-chunked2", headers);
  1850. ASSERT_TRUE(res != nullptr);
  1851. EXPECT_EQ(200, res->status);
  1852. EXPECT_EQ(std::string("123456789"), res->body);
  1853. }
  1854. #endif
  1855. TEST_F(ServerTest, Patch) {
  1856. auto res = cli_.Patch("/patch", "PATCH", "text/plain");
  1857. ASSERT_TRUE(res != nullptr);
  1858. EXPECT_EQ(200, res->status);
  1859. EXPECT_EQ("PATCH", res->body);
  1860. }
  1861. TEST_F(ServerTest, Delete) {
  1862. auto res = cli_.Delete("/delete");
  1863. ASSERT_TRUE(res != nullptr);
  1864. EXPECT_EQ(200, res->status);
  1865. EXPECT_EQ("DELETE", res->body);
  1866. }
  1867. TEST_F(ServerTest, DeleteContentReceiver) {
  1868. auto res = cli_.Delete("/delete-body", "content", "text/plain");
  1869. ASSERT_TRUE(res != nullptr);
  1870. EXPECT_EQ(200, res->status);
  1871. EXPECT_EQ("content", res->body);
  1872. }
  1873. TEST_F(ServerTest, Options) {
  1874. auto res = cli_.Options("*");
  1875. ASSERT_TRUE(res != nullptr);
  1876. EXPECT_EQ(200, res->status);
  1877. EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
  1878. EXPECT_TRUE(res->body.empty());
  1879. }
  1880. TEST_F(ServerTest, URL) {
  1881. auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
  1882. ASSERT_TRUE(res != nullptr);
  1883. EXPECT_EQ(200, res->status);
  1884. }
  1885. TEST_F(ServerTest, ArrayParam) {
  1886. auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
  1887. ASSERT_TRUE(res != nullptr);
  1888. EXPECT_EQ(200, res->status);
  1889. }
  1890. TEST_F(ServerTest, NoMultipleHeaders) {
  1891. Headers headers = {{"Content-Length", "5"}};
  1892. auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
  1893. "text/plain");
  1894. ASSERT_TRUE(res != nullptr);
  1895. EXPECT_EQ(200, res->status);
  1896. }
  1897. TEST_F(ServerTest, PostContentReceiver) {
  1898. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1899. ASSERT_TRUE(res != nullptr);
  1900. ASSERT_EQ(200, res->status);
  1901. ASSERT_EQ("content", res->body);
  1902. }
  1903. TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
  1904. MultipartFormDataItems items = {
  1905. {"text1", "text default", "", ""},
  1906. {"text2", "aωb", "", ""},
  1907. {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
  1908. {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
  1909. {"file3", "", "", "application/octet-stream"},
  1910. };
  1911. auto res = cli_.Post("/content_receiver", items);
  1912. ASSERT_TRUE(res != nullptr);
  1913. EXPECT_EQ(200, res->status);
  1914. }
  1915. TEST_F(ServerTest, PostContentReceiverGzip) {
  1916. cli_.set_compress(true);
  1917. auto res = cli_.Post("/content_receiver", "content", "text/plain");
  1918. ASSERT_TRUE(res != nullptr);
  1919. ASSERT_EQ(200, res->status);
  1920. ASSERT_EQ("content", res->body);
  1921. }
  1922. TEST_F(ServerTest, PutContentReceiver) {
  1923. auto res = cli_.Put("/content_receiver", "content", "text/plain");
  1924. ASSERT_TRUE(res != nullptr);
  1925. ASSERT_EQ(200, res->status);
  1926. ASSERT_EQ("content", res->body);
  1927. }
  1928. TEST_F(ServerTest, PatchContentReceiver) {
  1929. auto res = cli_.Patch("/content_receiver", "content", "text/plain");
  1930. ASSERT_TRUE(res != nullptr);
  1931. ASSERT_EQ(200, res->status);
  1932. ASSERT_EQ("content", res->body);
  1933. }
  1934. TEST_F(ServerTest, PostQueryStringAndBody) {
  1935. auto res =
  1936. cli_.Post("/query-string-and-body?key=value", "content", "text/plain");
  1937. ASSERT_TRUE(res != nullptr);
  1938. ASSERT_EQ(200, res->status);
  1939. }
  1940. TEST_F(ServerTest, HTTP2Magic) {
  1941. Request req;
  1942. req.method = "PRI";
  1943. req.path = "*";
  1944. req.body = "SM";
  1945. auto res = std::make_shared<Response>();
  1946. auto ret = cli_.send(req, *res);
  1947. ASSERT_TRUE(ret);
  1948. EXPECT_EQ(400, res->status);
  1949. }
  1950. TEST_F(ServerTest, KeepAlive) {
  1951. auto res = cli_.Get("/hi");
  1952. ASSERT_TRUE(res != nullptr);
  1953. EXPECT_EQ(200, res->status);
  1954. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1955. EXPECT_EQ("Hello World!", res->body);
  1956. res = cli_.Get("/hi");
  1957. ASSERT_TRUE(res != nullptr);
  1958. EXPECT_EQ(200, res->status);
  1959. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1960. EXPECT_EQ("Hello World!", res->body);
  1961. res = cli_.Get("/hi");
  1962. ASSERT_TRUE(res != nullptr);
  1963. EXPECT_EQ(200, res->status);
  1964. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1965. EXPECT_EQ("Hello World!", res->body);
  1966. res = cli_.Get("/not-exist");
  1967. ASSERT_TRUE(res != nullptr);
  1968. EXPECT_EQ(404, res->status);
  1969. res = cli_.Post("/empty", "", "text/plain");
  1970. ASSERT_TRUE(res != nullptr);
  1971. EXPECT_EQ(200, res->status);
  1972. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1973. EXPECT_EQ("empty", res->body);
  1974. EXPECT_EQ("close", res->get_header_value("Connection"));
  1975. res = cli_.Post(
  1976. "/empty", 0, [&](size_t, size_t, DataSink &) { return true; },
  1977. "text/plain");
  1978. ASSERT_TRUE(res != nullptr);
  1979. EXPECT_EQ(200, res->status);
  1980. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1981. EXPECT_EQ("empty", res->body);
  1982. cli_.set_keep_alive(false);
  1983. res = cli_.Get("/last-request");
  1984. ASSERT_TRUE(res != nullptr);
  1985. EXPECT_EQ(200, res->status);
  1986. EXPECT_EQ("close", res->get_header_value("Connection"));
  1987. }
  1988. #ifdef CPPHTTPLIB_ZLIB_SUPPORT
  1989. TEST_F(ServerTest, Gzip) {
  1990. Headers headers;
  1991. headers.emplace("Accept-Encoding", "gzip, deflate");
  1992. auto res = cli_.Get("/compress", headers);
  1993. ASSERT_TRUE(res != nullptr);
  1994. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  1995. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  1996. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  1997. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  1998. "7890123456789012345678901234567890",
  1999. res->body);
  2000. EXPECT_EQ(200, res->status);
  2001. }
  2002. TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
  2003. auto res = cli_.Get("/compress");
  2004. ASSERT_TRUE(res != nullptr);
  2005. EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
  2006. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  2007. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  2008. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  2009. "7890123456789012345678901234567890",
  2010. res->body);
  2011. EXPECT_EQ(200, res->status);
  2012. }
  2013. TEST_F(ServerTest, GzipWithContentReceiver) {
  2014. Headers headers;
  2015. headers.emplace("Accept-Encoding", "gzip, deflate");
  2016. std::string body;
  2017. auto res = cli_.Get("/compress", headers,
  2018. [&](const char *data, uint64_t data_length) {
  2019. EXPECT_EQ(data_length, 100);
  2020. body.append(data, data_length);
  2021. return true;
  2022. });
  2023. ASSERT_TRUE(res != nullptr);
  2024. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  2025. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  2026. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  2027. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  2028. "7890123456789012345678901234567890",
  2029. body);
  2030. EXPECT_EQ(200, res->status);
  2031. }
  2032. TEST_F(ServerTest, GzipWithoutDecompressing) {
  2033. Headers headers;
  2034. headers.emplace("Accept-Encoding", "gzip, deflate");
  2035. cli_.set_decompress(false);
  2036. auto res = cli_.Get("/compress", headers);
  2037. ASSERT_TRUE(res != nullptr);
  2038. EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
  2039. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  2040. EXPECT_EQ("33", res->get_header_value("Content-Length"));
  2041. EXPECT_EQ(33, res->body.size());
  2042. EXPECT_EQ(200, res->status);
  2043. }
  2044. TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
  2045. std::string body;
  2046. auto res = cli_.Get("/compress",
  2047. [&](const char *data, uint64_t data_length) {
  2048. EXPECT_EQ(data_length, 100);
  2049. body.append(data, data_length);
  2050. return true;
  2051. });
  2052. ASSERT_TRUE(res != nullptr);
  2053. EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
  2054. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  2055. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  2056. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  2057. "7890123456789012345678901234567890",
  2058. body);
  2059. EXPECT_EQ(200, res->status);
  2060. }
  2061. TEST_F(ServerTest, NoGzip) {
  2062. Headers headers;
  2063. headers.emplace("Accept-Encoding", "gzip, deflate");
  2064. auto res = cli_.Get("/nocompress", headers);
  2065. ASSERT_TRUE(res != nullptr);
  2066. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  2067. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  2068. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  2069. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  2070. "7890123456789012345678901234567890",
  2071. res->body);
  2072. EXPECT_EQ(200, res->status);
  2073. }
  2074. TEST_F(ServerTest, NoGzipWithContentReceiver) {
  2075. Headers headers;
  2076. headers.emplace("Accept-Encoding", "gzip, deflate");
  2077. std::string body;
  2078. auto res = cli_.Get("/nocompress", headers,
  2079. [&](const char *data, uint64_t data_length) {
  2080. EXPECT_EQ(data_length, 100);
  2081. body.append(data, data_length);
  2082. return true;
  2083. });
  2084. ASSERT_TRUE(res != nullptr);
  2085. EXPECT_EQ(false, res->has_header("Content-Encoding"));
  2086. EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
  2087. EXPECT_EQ("100", res->get_header_value("Content-Length"));
  2088. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  2089. "7890123456789012345678901234567890",
  2090. body);
  2091. EXPECT_EQ(200, res->status);
  2092. }
  2093. TEST_F(ServerTest, MultipartFormDataGzip) {
  2094. MultipartFormDataItems items = {
  2095. {"key1", "test", "", ""},
  2096. {"key2", "--abcdefg123", "", ""},
  2097. };
  2098. cli_.set_compress(true);
  2099. auto res = cli_.Post("/compress-multipart", items);
  2100. ASSERT_TRUE(res != nullptr);
  2101. EXPECT_EQ(200, res->status);
  2102. }
  2103. #endif
  2104. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  2105. TEST_F(ServerTest, Brotli) {
  2106. Headers headers;
  2107. headers.emplace("Accept-Encoding", "br");
  2108. auto res = cli_.Get("/compress", headers);
  2109. ASSERT_TRUE(res != nullptr);
  2110. EXPECT_EQ("brotli", res->get_header_value("Content-Encoding"));
  2111. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  2112. EXPECT_EQ("19", res->get_header_value("Content-Length"));
  2113. EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
  2114. "7890123456789012345678901234567890",
  2115. res->body);
  2116. EXPECT_EQ(200, res->status);
  2117. }
  2118. #endif
  2119. // Sends a raw request to a server listening at HOST:PORT.
  2120. static bool send_request(time_t read_timeout_sec, const std::string &req,
  2121. std::string *resp = nullptr) {
  2122. auto client_sock =
  2123. detail::create_client_socket(HOST, PORT, false, nullptr,
  2124. /*timeout_sec=*/5, 0, std::string());
  2125. if (client_sock == INVALID_SOCKET) { return false; }
  2126. auto ret = detail::process_client_socket(
  2127. client_sock, read_timeout_sec, 0, 0, 0, [&](Stream &strm) {
  2128. if (req.size() !=
  2129. static_cast<size_t>(strm.write(req.data(), req.size()))) {
  2130. return false;
  2131. }
  2132. char buf[512];
  2133. detail::stream_line_reader line_reader(strm, buf, sizeof(buf));
  2134. while (line_reader.getline()) {
  2135. if (resp) { *resp += line_reader.ptr(); }
  2136. }
  2137. return true;
  2138. });
  2139. detail::close_socket(client_sock);
  2140. return ret;
  2141. }
  2142. TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
  2143. Server svr;
  2144. std::string header_value;
  2145. svr.Get("/validate-ws-in-headers", [&](const Request &req, Response &res) {
  2146. header_value = req.get_header_value("foo");
  2147. res.set_content("ok", "text/plain");
  2148. });
  2149. thread t = thread([&] { svr.listen(HOST, PORT); });
  2150. while (!svr.is_running()) {
  2151. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2152. }
  2153. // Only space and horizontal tab are whitespace. Make sure other whitespace-
  2154. // like characters are not treated the same - use vertical tab and escape.
  2155. const std::string req = "GET /validate-ws-in-headers HTTP/1.1\r\n"
  2156. "foo: \t \v bar \e\t \r\n"
  2157. "Connection: close\r\n"
  2158. "\r\n";
  2159. ASSERT_TRUE(send_request(5, req));
  2160. svr.stop();
  2161. t.join();
  2162. EXPECT_EQ(header_value, "\v bar \e");
  2163. }
  2164. // Sends a raw request and verifies that there isn't a crash or exception.
  2165. static void test_raw_request(const std::string &req,
  2166. std::string *out = nullptr) {
  2167. Server svr;
  2168. svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
  2169. res.set_content("ok", "text/plain");
  2170. });
  2171. svr.Put("/put_hi", [&](const Request & /*req*/, Response &res) {
  2172. res.set_content("ok", "text/plain");
  2173. });
  2174. // Server read timeout must be longer than the client read timeout for the
  2175. // bug to reproduce, probably to force the server to process a request
  2176. // without a trailing blank line.
  2177. const time_t client_read_timeout_sec = 1;
  2178. svr.set_read_timeout(client_read_timeout_sec + 1, 0);
  2179. bool listen_thread_ok = false;
  2180. thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
  2181. while (!svr.is_running()) {
  2182. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2183. }
  2184. ASSERT_TRUE(send_request(client_read_timeout_sec, req, out));
  2185. svr.stop();
  2186. t.join();
  2187. EXPECT_TRUE(listen_thread_ok);
  2188. }
  2189. TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
  2190. // A certain header line causes an exception if the header property is parsed
  2191. // naively with a single regex. This occurs with libc++ but not libstdc++.
  2192. test_raw_request(
  2193. "GET /hi HTTP/1.1\r\n"
  2194. " : "
  2195. " "
  2196. " ");
  2197. }
  2198. TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity2) {
  2199. // A certain header line causes an exception if the header property *name* is
  2200. // parsed with a regular expression starting with "(.+?):" - this is a non-
  2201. // greedy matcher and requires backtracking when there are a lot of ":"
  2202. // characters.
  2203. // This occurs with libc++ but not libstdc++.
  2204. test_raw_request(
  2205. "GET /hi HTTP/1.1\r\n"
  2206. ":-:::::::::::::::::::::::::::-::::::::::::::::::::::::@-&&&&&&&&&&&"
  2207. "--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&"
  2208. "&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-:::::"
  2209. "::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-::::::::::::::::::::::::"
  2210. ":::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::"
  2211. "::::::::-:::::::::::::::::@-&&&&&&&--:::::::-::::::::::::::::::::::"
  2212. ":::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::"
  2213. "::::::::::-:::::::::::::::::@-&&&&&::::::::::::-:::::::::::::::::@-"
  2214. "&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::::::"
  2215. ":@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::"
  2216. "::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::@-&&"
  2217. "&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@"
  2218. "::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&"
  2219. "--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&"
  2220. "&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&"
  2221. "&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&"
  2222. "&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@"
  2223. "-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::"
  2224. "::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::"
  2225. ":::::@-&&&&&&&&&&&::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-::::::"
  2226. ":::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::"
  2227. "::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-"
  2228. ":::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&---&&:&"
  2229. "&&.0------------:-:::::::::::::::::::::::::::::-:::::::::::::::::@-"
  2230. "&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::::::"
  2231. ":@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::"
  2232. "::::@-&&&&&&&&&&&---&&:&&&.0------------O--------\rH PUTHTTP/1.1\r\n"
  2233. "&&&%%%");
  2234. }
  2235. TEST(ServerRequestParsingTest, ExcessiveWhitespaceInUnparseableHeaderLine) {
  2236. // Make sure this doesn't crash the server.
  2237. // In a previous version of the header line regex, the "\r" rendered the line
  2238. // unparseable and the regex engine repeatedly backtracked, trying to look for
  2239. // a new position where the leading white space ended and the field value
  2240. // began.
  2241. // The crash occurs with libc++ but not libstdc++.
  2242. test_raw_request("GET /hi HTTP/1.1\r\n"
  2243. "a:" +
  2244. std::string(2000, ' ') + '\r' + std::string(20, 'z') +
  2245. "\r\n"
  2246. "\r\n");
  2247. }
  2248. TEST(ServerRequestParsingTest, InvalidFirstChunkLengthInRequest) {
  2249. std::string out;
  2250. test_raw_request("PUT /put_hi HTTP/1.1\r\n"
  2251. "Content-Type: text/plain\r\n"
  2252. "Transfer-Encoding: chunked\r\n"
  2253. "\r\n"
  2254. "nothex\r\n",
  2255. &out);
  2256. EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
  2257. }
  2258. TEST(ServerRequestParsingTest, InvalidSecondChunkLengthInRequest) {
  2259. std::string out;
  2260. test_raw_request("PUT /put_hi HTTP/1.1\r\n"
  2261. "Content-Type: text/plain\r\n"
  2262. "Transfer-Encoding: chunked\r\n"
  2263. "\r\n"
  2264. "3\r\n"
  2265. "xyz\r\n"
  2266. "NaN\r\n",
  2267. &out);
  2268. EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
  2269. }
  2270. TEST(ServerRequestParsingTest, ChunkLengthTooHighInRequest) {
  2271. std::string out;
  2272. test_raw_request("PUT /put_hi HTTP/1.1\r\n"
  2273. "Content-Type: text/plain\r\n"
  2274. "Transfer-Encoding: chunked\r\n"
  2275. "\r\n"
  2276. // Length is too large for 64 bits.
  2277. "1ffffffffffffffff\r\n"
  2278. "xyz\r\n",
  2279. &out);
  2280. EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
  2281. }
  2282. TEST(ServerRequestParsingTest, InvalidHeaderTextWithExtraCR) {
  2283. test_raw_request("GET /hi HTTP/1.1\r\n"
  2284. "Content-Type: text/plain\r\n\r");
  2285. }
  2286. TEST(ServerStopTest, StopServerWithChunkedTransmission) {
  2287. Server svr;
  2288. svr.Get("/events", [](const Request & /*req*/, Response &res) {
  2289. res.set_header("Cache-Control", "no-cache");
  2290. res.set_chunked_content_provider("text/event-stream", [](size_t offset,
  2291. DataSink &sink) {
  2292. char buffer[27];
  2293. auto size = static_cast<size_t>(sprintf(buffer, "data:%ld\n\n", offset));
  2294. sink.write(buffer, size);
  2295. std::this_thread::sleep_for(std::chrono::seconds(1));
  2296. return true;
  2297. });
  2298. });
  2299. auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
  2300. while (!svr.is_running()) {
  2301. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2302. }
  2303. Client client(HOST, PORT);
  2304. const Headers headers = {{"Accept", "text/event-stream"}};
  2305. auto get_thread = std::thread([&client, &headers]() {
  2306. std::shared_ptr<Response> res = client.Get(
  2307. "/events", headers,
  2308. [](const char * /*data*/, size_t /*len*/) -> bool { return true; });
  2309. });
  2310. // Give GET time to get a few messages.
  2311. std::this_thread::sleep_for(std::chrono::seconds(2));
  2312. svr.stop();
  2313. listen_thread.join();
  2314. get_thread.join();
  2315. ASSERT_FALSE(svr.is_running());
  2316. }
  2317. TEST(MountTest, Unmount) {
  2318. Server svr;
  2319. auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
  2320. while (!svr.is_running()) {
  2321. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2322. }
  2323. // Give GET time to get a few messages.
  2324. std::this_thread::sleep_for(std::chrono::seconds(1));
  2325. Client cli("localhost", PORT);
  2326. svr.set_mount_point("/mount2", "./www2");
  2327. auto res = cli.Get("/");
  2328. ASSERT_TRUE(res != nullptr);
  2329. EXPECT_EQ(404, res->status);
  2330. res = cli.Get("/mount2/dir/test.html");
  2331. ASSERT_TRUE(res != nullptr);
  2332. EXPECT_EQ(200, res->status);
  2333. svr.set_mount_point("/", "./www");
  2334. res = cli.Get("/dir/");
  2335. ASSERT_TRUE(res != nullptr);
  2336. EXPECT_EQ(200, res->status);
  2337. svr.remove_mount_point("/");
  2338. res = cli.Get("/dir/");
  2339. ASSERT_TRUE(res != nullptr);
  2340. EXPECT_EQ(404, res->status);
  2341. svr.remove_mount_point("/mount2");
  2342. res = cli.Get("/mount2/dir/test.html");
  2343. ASSERT_TRUE(res != nullptr);
  2344. EXPECT_EQ(404, res->status);
  2345. svr.stop();
  2346. listen_thread.join();
  2347. ASSERT_FALSE(svr.is_running());
  2348. }
  2349. TEST(ExceptionTest, ThrowExceptionInHandler) {
  2350. Server svr;
  2351. svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
  2352. throw std::runtime_error("exception...");
  2353. res.set_content("Hello World!", "text/plain");
  2354. });
  2355. auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
  2356. while (!svr.is_running()) {
  2357. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2358. }
  2359. // Give GET time to get a few messages.
  2360. std::this_thread::sleep_for(std::chrono::seconds(1));
  2361. Client cli("localhost", PORT);
  2362. auto res = cli.Get("/hi");
  2363. ASSERT_TRUE(res != nullptr);
  2364. EXPECT_EQ(500, res->status);
  2365. ASSERT_FALSE(res->has_header("EXCEPTION_WHAT"));
  2366. svr.stop();
  2367. listen_thread.join();
  2368. ASSERT_FALSE(svr.is_running());
  2369. }
  2370. TEST(KeepAliveTest, ReadTimeout) {
  2371. Server svr;
  2372. svr.Get("/a", [&](const Request & /*req*/, Response &res) {
  2373. std::this_thread::sleep_for(std::chrono::seconds(2));
  2374. res.set_content("a", "text/plain");
  2375. });
  2376. svr.Get("/b", [&](const Request & /*req*/, Response &res) {
  2377. res.set_content("b", "text/plain");
  2378. });
  2379. auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
  2380. while (!svr.is_running()) {
  2381. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2382. }
  2383. // Give GET time to get a few messages.
  2384. std::this_thread::sleep_for(std::chrono::seconds(1));
  2385. Client cli("localhost", PORT);
  2386. cli.set_keep_alive(true);
  2387. cli.set_read_timeout(1);
  2388. auto resa = cli.Get("/a");
  2389. ASSERT_TRUE(resa == nullptr);
  2390. auto resb = cli.Get("/b");
  2391. ASSERT_TRUE(resb != nullptr);
  2392. EXPECT_EQ(200, resb->status);
  2393. EXPECT_EQ("b", resb->body);
  2394. svr.stop();
  2395. listen_thread.join();
  2396. ASSERT_FALSE(svr.is_running());
  2397. }
  2398. class ServerTestWithAI_PASSIVE : public ::testing::Test {
  2399. protected:
  2400. ServerTestWithAI_PASSIVE()
  2401. : cli_(HOST, PORT)
  2402. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2403. ,
  2404. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  2405. #endif
  2406. {
  2407. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2408. cli_.enable_server_certificate_verification(false);
  2409. #endif
  2410. }
  2411. virtual void SetUp() {
  2412. svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
  2413. res.set_content("Hello World!", "text/plain");
  2414. });
  2415. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(nullptr, PORT, AI_PASSIVE)); });
  2416. while (!svr_.is_running()) {
  2417. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2418. }
  2419. }
  2420. virtual void TearDown() {
  2421. svr_.stop();
  2422. t_.join();
  2423. }
  2424. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2425. SSLClient cli_;
  2426. SSLServer svr_;
  2427. #else
  2428. Client cli_;
  2429. Server svr_;
  2430. #endif
  2431. thread t_;
  2432. };
  2433. TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
  2434. auto res = cli_.Get("/hi");
  2435. ASSERT_TRUE(res != nullptr);
  2436. EXPECT_EQ(200, res->status);
  2437. EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
  2438. EXPECT_EQ("Hello World!", res->body);
  2439. }
  2440. class ServerUpDownTest : public ::testing::Test {
  2441. protected:
  2442. ServerUpDownTest() : cli_(HOST, PORT) {}
  2443. virtual void SetUp() {
  2444. t_ = thread([&]() {
  2445. svr_.bind_to_any_port(HOST);
  2446. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  2447. ASSERT_TRUE(svr_.listen_after_bind());
  2448. });
  2449. while (!svr_.is_running()) {
  2450. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2451. }
  2452. }
  2453. virtual void TearDown() {
  2454. svr_.stop();
  2455. t_.join();
  2456. }
  2457. Client cli_;
  2458. Server svr_;
  2459. thread t_;
  2460. };
  2461. TEST_F(ServerUpDownTest, QuickStartStop) {
  2462. // Should not crash, especially when run with
  2463. // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
  2464. }
  2465. class PayloadMaxLengthTest : public ::testing::Test {
  2466. protected:
  2467. PayloadMaxLengthTest()
  2468. : cli_(HOST, PORT)
  2469. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2470. ,
  2471. svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
  2472. #endif
  2473. {
  2474. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2475. cli_.enable_server_certificate_verification(false);
  2476. #endif
  2477. }
  2478. virtual void SetUp() {
  2479. svr_.set_payload_max_length(8);
  2480. svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
  2481. res.set_content("test", "text/plain");
  2482. });
  2483. t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
  2484. while (!svr_.is_running()) {
  2485. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2486. }
  2487. }
  2488. virtual void TearDown() {
  2489. svr_.stop();
  2490. t_.join();
  2491. }
  2492. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2493. SSLClient cli_;
  2494. SSLServer svr_;
  2495. #else
  2496. Client cli_;
  2497. Server svr_;
  2498. #endif
  2499. thread t_;
  2500. };
  2501. TEST_F(PayloadMaxLengthTest, ExceedLimit) {
  2502. auto res = cli_.Post("/test", "123456789", "text/plain");
  2503. ASSERT_TRUE(res != nullptr);
  2504. EXPECT_EQ(413, res->status);
  2505. res = cli_.Post("/test", "12345678", "text/plain");
  2506. ASSERT_TRUE(res != nullptr);
  2507. EXPECT_EQ(200, res->status);
  2508. }
  2509. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2510. TEST(SSLClientTest, ServerNameIndication) {
  2511. SSLClient cli("httpbin.org", 443);
  2512. auto res = cli.Get("/get");
  2513. ASSERT_TRUE(res != nullptr);
  2514. ASSERT_EQ(200, res->status);
  2515. }
  2516. TEST(SSLClientTest, ServerCertificateVerification1) {
  2517. SSLClient cli("google.com");
  2518. auto res = cli.Get("/");
  2519. ASSERT_TRUE(res != nullptr);
  2520. ASSERT_EQ(301, res->status);
  2521. }
  2522. TEST(SSLClientTest, ServerCertificateVerification2) {
  2523. SSLClient cli("google.com");
  2524. cli.enable_server_certificate_verification(true);
  2525. cli.set_ca_cert_path("hello");
  2526. auto res = cli.Get("/");
  2527. ASSERT_TRUE(res == nullptr);
  2528. }
  2529. TEST(SSLClientTest, ServerCertificateVerification3) {
  2530. SSLClient cli("google.com");
  2531. cli.set_ca_cert_path(CA_CERT_FILE);
  2532. auto res = cli.Get("/");
  2533. ASSERT_TRUE(res != nullptr);
  2534. ASSERT_EQ(301, res->status);
  2535. }
  2536. TEST(SSLClientTest, WildcardHostNameMatch) {
  2537. SSLClient cli("www.youtube.com");
  2538. cli.set_ca_cert_path(CA_CERT_FILE);
  2539. cli.enable_server_certificate_verification(true);
  2540. auto res = cli.Get("/");
  2541. ASSERT_TRUE(res != nullptr);
  2542. ASSERT_EQ(200, res->status);
  2543. }
  2544. TEST(SSLClientServerTest, ClientCertPresent) {
  2545. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  2546. CLIENT_CA_CERT_DIR);
  2547. ASSERT_TRUE(svr.is_valid());
  2548. svr.Get("/test", [&](const Request &req, Response &res) {
  2549. res.set_content("test", "text/plain");
  2550. svr.stop();
  2551. ASSERT_TRUE(true);
  2552. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  2553. ASSERT_TRUE(peer_cert != nullptr);
  2554. auto subject_name = X509_get_subject_name(peer_cert);
  2555. ASSERT_TRUE(subject_name != nullptr);
  2556. std::string common_name;
  2557. {
  2558. char name[BUFSIZ];
  2559. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  2560. name, sizeof(name));
  2561. common_name.assign(name, static_cast<size_t>(name_len));
  2562. }
  2563. EXPECT_EQ("Common Name", common_name);
  2564. X509_free(peer_cert);
  2565. });
  2566. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  2567. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2568. SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
  2569. cli.enable_server_certificate_verification(false);
  2570. cli.set_connection_timeout(30);
  2571. auto res = cli.Get("/test");
  2572. ASSERT_TRUE(res != nullptr);
  2573. ASSERT_EQ(200, res->status);
  2574. t.join();
  2575. }
  2576. TEST(SSLClientServerTest, MemoryClientCertPresent) {
  2577. X509 *server_cert;
  2578. EVP_PKEY *server_private_key;
  2579. X509_STORE *client_ca_cert_store;
  2580. X509 *client_cert;
  2581. EVP_PKEY *client_private_key;
  2582. FILE *f = fopen(SERVER_CERT_FILE, "r+");
  2583. server_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
  2584. fclose(f);
  2585. f = fopen(SERVER_PRIVATE_KEY_FILE, "r+");
  2586. server_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr);
  2587. fclose(f);
  2588. f = fopen(CLIENT_CA_CERT_FILE, "r+");
  2589. client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
  2590. client_ca_cert_store = X509_STORE_new();
  2591. X509_STORE_add_cert(client_ca_cert_store, client_cert);
  2592. X509_free(client_cert);
  2593. fclose(f);
  2594. f = fopen(CLIENT_CERT_FILE, "r+");
  2595. client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
  2596. fclose(f);
  2597. f = fopen(CLIENT_PRIVATE_KEY_FILE, "r+");
  2598. client_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr);
  2599. fclose(f);
  2600. SSLServer svr(server_cert, server_private_key, client_ca_cert_store);
  2601. ASSERT_TRUE(svr.is_valid());
  2602. svr.Get("/test", [&](const Request &req, Response &res) {
  2603. res.set_content("test", "text/plain");
  2604. svr.stop();
  2605. ASSERT_TRUE(true);
  2606. auto peer_cert = SSL_get_peer_certificate(req.ssl);
  2607. ASSERT_TRUE(peer_cert != nullptr);
  2608. auto subject_name = X509_get_subject_name(peer_cert);
  2609. ASSERT_TRUE(subject_name != nullptr);
  2610. std::string common_name;
  2611. {
  2612. char name[BUFSIZ];
  2613. auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
  2614. name, sizeof(name));
  2615. common_name.assign(name, static_cast<size_t>(name_len));
  2616. }
  2617. EXPECT_EQ("Common Name", common_name);
  2618. X509_free(peer_cert);
  2619. });
  2620. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  2621. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2622. SSLClient cli(HOST, PORT, client_cert, client_private_key);
  2623. cli.enable_server_certificate_verification(false);
  2624. cli.set_connection_timeout(30);
  2625. auto res = cli.Get("/test");
  2626. ASSERT_TRUE(res != nullptr);
  2627. ASSERT_EQ(200, res->status);
  2628. X509_free(server_cert);
  2629. EVP_PKEY_free(server_private_key);
  2630. X509_free(client_cert);
  2631. EVP_PKEY_free(client_private_key);
  2632. t.join();
  2633. }
  2634. TEST(SSLClientServerTest, ClientCertMissing) {
  2635. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
  2636. CLIENT_CA_CERT_DIR);
  2637. ASSERT_TRUE(svr.is_valid());
  2638. svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
  2639. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  2640. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2641. SSLClient cli(HOST, PORT);
  2642. auto res = cli.Get("/test");
  2643. cli.set_connection_timeout(30);
  2644. ASSERT_TRUE(res == nullptr);
  2645. svr.stop();
  2646. t.join();
  2647. }
  2648. TEST(SSLClientServerTest, TrustDirOptional) {
  2649. SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
  2650. ASSERT_TRUE(svr.is_valid());
  2651. svr.Get("/test", [&](const Request &, Response &res) {
  2652. res.set_content("test", "text/plain");
  2653. svr.stop();
  2654. });
  2655. thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
  2656. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  2657. SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
  2658. cli.enable_server_certificate_verification(false);
  2659. cli.set_connection_timeout(30);
  2660. auto res = cli.Get("/test");
  2661. ASSERT_TRUE(res != nullptr);
  2662. ASSERT_EQ(200, res->status);
  2663. t.join();
  2664. }
  2665. #endif
  2666. #ifdef _WIN32
  2667. TEST(CleanupTest, WSACleanup) {
  2668. int ret = WSACleanup();
  2669. ASSERT_EQ(0, ret);
  2670. }
  2671. #endif
  2672. // #ifndef CPPHTTPLIB_OPENSSL_SUPPORT
  2673. // TEST(NoSSLSupport, SimpleInterface) {
  2674. // Client cli("https://yahoo.com");
  2675. // ASSERT_FALSE(cli.is_valid());
  2676. // }
  2677. // #endif
  2678. #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
  2679. TEST(InvalidScheme, SimpleInterface) {
  2680. Client cli("scheme://yahoo.com");
  2681. ASSERT_FALSE(cli.is_valid());
  2682. }
  2683. TEST(NoScheme, SimpleInterface) {
  2684. Client cli("yahoo.com:80");
  2685. ASSERT_TRUE(cli.is_valid());
  2686. }
  2687. TEST(YahooRedirectTest2, SimpleInterface) {
  2688. Client cli("http://yahoo.com");
  2689. auto res = cli.Get("/");
  2690. ASSERT_TRUE(res != nullptr);
  2691. EXPECT_EQ(301, res->status);
  2692. cli.set_follow_location(true);
  2693. res = cli.Get("/");
  2694. ASSERT_TRUE(res != nullptr);
  2695. EXPECT_EQ(200, res->status);
  2696. }
  2697. TEST(YahooRedirectTest3, SimpleInterface) {
  2698. Client cli("https://yahoo.com");
  2699. auto res = cli.Get("/");
  2700. ASSERT_TRUE(res != nullptr);
  2701. EXPECT_EQ(301, res->status);
  2702. cli.set_follow_location(true);
  2703. res = cli.Get("/");
  2704. ASSERT_TRUE(res != nullptr);
  2705. EXPECT_EQ(200, res->status);
  2706. }
  2707. #ifdef CPPHTTPLIB_BROTLI_SUPPORT
  2708. TEST(DecodeWithChunkedEncoding, BrotliEncoding) {
  2709. Client cli("https://cdnjs.cloudflare.com");
  2710. auto res = cli.Get("/ajax/libs/jquery/3.5.1/jquery.js",
  2711. {{"Accept-Encoding", "brotli"}});
  2712. ASSERT_TRUE(res != nullptr);
  2713. EXPECT_EQ(200, res->status);
  2714. EXPECT_EQ(287630, res->body.size());
  2715. EXPECT_EQ("application/javascript; charset=utf-8",
  2716. res->get_header_value("Content-Type"));
  2717. }
  2718. #endif
  2719. #if 0
  2720. TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
  2721. auto res =
  2722. Client("https://httpbin.org")
  2723. .set_follow_location(true)
  2724. .Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
  2725. ASSERT_TRUE(res != nullptr);
  2726. EXPECT_EQ(200, res->status);
  2727. }
  2728. #endif
  2729. #endif