test.cc 97 KB

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