| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882 |
- #include <httplib.h>
- #include <signal.h>
- #include <gtest/gtest.h>
- #include <atomic>
- #include <chrono>
- #include <future>
- #include <memory>
- #include <sstream>
- #include <stdexcept>
- #include <thread>
- #include <type_traits>
- #define SERVER_CERT_FILE "./cert.pem"
- #define SERVER_CERT2_FILE "./cert2.pem"
- #define SERVER_PRIVATE_KEY_FILE "./key.pem"
- #define CA_CERT_FILE "./ca-bundle.crt"
- #define CLIENT_CA_CERT_FILE "./rootCA.cert.pem"
- #define CLIENT_CA_CERT_DIR "."
- #define CLIENT_CERT_FILE "./client.cert.pem"
- #define CLIENT_PRIVATE_KEY_FILE "./client.key.pem"
- #define SERVER_ENCRYPTED_CERT_FILE "./cert_encrypted.pem"
- #define SERVER_ENCRYPTED_PRIVATE_KEY_FILE "./key_encrypted.pem"
- #define SERVER_ENCRYPTED_PRIVATE_KEY_PASS "test123!"
- using namespace std;
- using namespace httplib;
- const char *HOST = "localhost";
- const int PORT = 1234;
- const string LONG_QUERY_VALUE = string(25000, '@');
- const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
- const std::string JSON_DATA = "{\"hello\":\"world\"}";
- const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB
- MultipartFormData &get_file_value(MultipartFormDataItems &files,
- const char *key) {
- auto it = std::find_if(
- files.begin(), files.end(),
- [&](const MultipartFormData &file) { return file.name == key; });
- #ifdef CPPHTTPLIB_NO_EXCEPTIONS
- return *it;
- #else
- if (it != files.end()) { return *it; }
- throw std::runtime_error("invalid mulitpart form data name error");
- #endif
- }
- TEST(ConstructorTest, MoveConstructible) {
- EXPECT_FALSE(std::is_copy_constructible<Client>::value);
- EXPECT_TRUE(std::is_nothrow_move_constructible<Client>::value);
- }
- #ifdef _WIN32
- TEST(StartupTest, WSAStartup) {
- WSADATA wsaData;
- int ret = WSAStartup(0x0002, &wsaData);
- ASSERT_EQ(0, ret);
- }
- #endif
- TEST(DecodeURLTest, PercentCharacter) {
- EXPECT_EQ(
- detail::decode_url(
- R"(descrip=Gastos%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA%C3%B1%C3%91%206)",
- false),
- R"(descrip=Gastos áéíóúñÑ 6)");
- }
- TEST(EncodeQueryParamTest, ParseUnescapedChararactersTest) {
- string unescapedCharacters = "-_.!~*'()";
- EXPECT_EQ(detail::encode_query_param(unescapedCharacters), "-_.!~*'()");
- }
- TEST(EncodeQueryParamTest, ParseReservedCharactersTest) {
- string reservedCharacters = ";,/?:@&=+$";
- EXPECT_EQ(detail::encode_query_param(reservedCharacters),
- "%3B%2C%2F%3F%3A%40%26%3D%2B%24");
- }
- TEST(EncodeQueryParamTest, TestUTF8Characters) {
- string chineseCharacters = "中国語";
- string russianCharacters = "дом";
- string brazilianCharacters = "óculos";
- EXPECT_EQ(detail::encode_query_param(chineseCharacters),
- "%E4%B8%AD%E5%9B%BD%E8%AA%9E");
- EXPECT_EQ(detail::encode_query_param(russianCharacters),
- "%D0%B4%D0%BE%D0%BC");
- EXPECT_EQ(detail::encode_query_param(brazilianCharacters), "%C3%B3culos");
- }
- TEST(TrimTests, TrimStringTests) {
- EXPECT_EQ("abc", detail::trim_copy("abc"));
- EXPECT_EQ("abc", detail::trim_copy(" abc "));
- EXPECT_TRUE(detail::trim_copy("").empty());
- }
- TEST(SplitTest, ParseQueryString) {
- string s = "key1=val1&key2=val2&key3=val3";
- Params dic;
- detail::split(s.c_str(), s.c_str() + s.size(), '&',
- [&](const char *b, const char *e) {
- string key, val;
- detail::split(b, e, '=', [&](const char *b2, const char *e2) {
- if (key.empty()) {
- key.assign(b2, e2);
- } else {
- val.assign(b2, e2);
- }
- });
- dic.emplace(key, val);
- });
- EXPECT_EQ("val1", dic.find("key1")->second);
- EXPECT_EQ("val2", dic.find("key2")->second);
- EXPECT_EQ("val3", dic.find("key3")->second);
- }
- TEST(SplitTest, ParseInvalidQueryTests) {
- {
- string s = " ";
- Params dict;
- detail::parse_query_text(s, dict);
- EXPECT_TRUE(dict.empty());
- }
- {
- string s = " = =";
- Params dict;
- detail::parse_query_text(s, dict);
- EXPECT_TRUE(dict.empty());
- }
- }
- TEST(ParseQueryTest, ParseQueryString) {
- string s = "key1=val1&key2=val2&key3=val3";
- Params dic;
- detail::parse_query_text(s, dic);
- EXPECT_EQ("val1", dic.find("key1")->second);
- EXPECT_EQ("val2", dic.find("key2")->second);
- EXPECT_EQ("val3", dic.find("key3")->second);
- }
- TEST(ParamsToQueryTest, ConvertParamsToQuery) {
- Params dic;
- EXPECT_EQ(detail::params_to_query_str(dic), "");
- dic.emplace("key1", "val1");
- EXPECT_EQ(detail::params_to_query_str(dic), "key1=val1");
- dic.emplace("key2", "val2");
- dic.emplace("key3", "val3");
- EXPECT_EQ(detail::params_to_query_str(dic), "key1=val1&key2=val2&key3=val3");
- }
- TEST(GetHeaderValueTest, DefaultValue) {
- Headers headers = {{"Dummy", "Dummy"}};
- auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
- EXPECT_STREQ("text/plain", val);
- }
- TEST(GetHeaderValueTest, DefaultValueInt) {
- Headers headers = {{"Dummy", "Dummy"}};
- auto val =
- detail::get_header_value<uint64_t>(headers, "Content-Length", 0, 100);
- EXPECT_EQ(100ull, val);
- }
- TEST(GetHeaderValueTest, RegularValue) {
- Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
- auto val = detail::get_header_value(headers, "Content-Type", 0, "text/plain");
- EXPECT_STREQ("text/html", val);
- }
- TEST(GetHeaderValueTest, RegularValueWithDifferentCase) {
- Headers headers = {{"Content-Type", "text/html"}, {"Dummy", "Dummy"}};
- auto val = detail::get_header_value(headers, "content-type", 0, "text/plain");
- EXPECT_STREQ("text/html", val);
- }
- TEST(GetHeaderValueTest, SetContent) {
- Response res;
- res.set_content("html", "text/html");
- EXPECT_EQ("text/html", res.get_header_value("Content-Type"));
- res.set_content("text", "text/plain");
- EXPECT_EQ(1U, res.get_header_value_count("Content-Type"));
- EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
- }
- TEST(GetHeaderValueTest, RegularValueInt) {
- Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
- auto val =
- detail::get_header_value<uint64_t>(headers, "Content-Length", 0, 0);
- EXPECT_EQ(100ull, val);
- }
- TEST(GetHeaderValueTest, Range) {
- {
- Headers headers = {make_range_header({{1, -1}})};
- auto val = detail::get_header_value(headers, "Range", 0, 0);
- EXPECT_STREQ("bytes=1-", val);
- }
- {
- Headers headers = {make_range_header({{-1, 1}})};
- auto val = detail::get_header_value(headers, "Range", 0, 0);
- EXPECT_STREQ("bytes=-1", val);
- }
- {
- Headers headers = {make_range_header({{1, 10}})};
- auto val = detail::get_header_value(headers, "Range", 0, 0);
- EXPECT_STREQ("bytes=1-10", val);
- }
- {
- Headers headers = {make_range_header({{1, 10}, {100, -1}})};
- auto val = detail::get_header_value(headers, "Range", 0, 0);
- EXPECT_STREQ("bytes=1-10, 100-", val);
- }
- {
- Headers headers = {make_range_header({{1, 10}, {100, 200}})};
- auto val = detail::get_header_value(headers, "Range", 0, 0);
- EXPECT_STREQ("bytes=1-10, 100-200", val);
- }
- {
- Headers headers = {make_range_header({{0, 0}, {-1, 1}})};
- auto val = detail::get_header_value(headers, "Range", 0, 0);
- EXPECT_STREQ("bytes=0-0, -1", val);
- }
- }
- TEST(ParseHeaderValueTest, Range) {
- {
- Ranges ranges;
- auto ret = detail::parse_range_header("bytes=1-", ranges);
- EXPECT_TRUE(ret);
- EXPECT_EQ(1u, ranges.size());
- EXPECT_EQ(1u, ranges[0].first);
- EXPECT_EQ(-1, ranges[0].second);
- }
- {
- Ranges ranges;
- auto ret = detail::parse_range_header("bytes=-1", ranges);
- EXPECT_TRUE(ret);
- EXPECT_EQ(1u, ranges.size());
- EXPECT_EQ(-1, ranges[0].first);
- EXPECT_EQ(1u, ranges[0].second);
- }
- {
- Ranges ranges;
- auto ret = detail::parse_range_header("bytes=1-10", ranges);
- EXPECT_TRUE(ret);
- EXPECT_EQ(1u, ranges.size());
- EXPECT_EQ(1u, ranges[0].first);
- EXPECT_EQ(10u, ranges[0].second);
- }
- {
- Ranges ranges;
- auto ret = detail::parse_range_header("bytes=10-1", ranges);
- EXPECT_FALSE(ret);
- }
- {
- Ranges ranges;
- auto ret = detail::parse_range_header("bytes=1-10, 100-", ranges);
- EXPECT_TRUE(ret);
- EXPECT_EQ(2u, ranges.size());
- EXPECT_EQ(1u, ranges[0].first);
- EXPECT_EQ(10u, ranges[0].second);
- EXPECT_EQ(100u, ranges[1].first);
- EXPECT_EQ(-1, ranges[1].second);
- }
- {
- Ranges ranges;
- auto ret =
- detail::parse_range_header("bytes=1-10, 100-200, 300-400", ranges);
- EXPECT_TRUE(ret);
- EXPECT_EQ(3u, ranges.size());
- EXPECT_EQ(1u, ranges[0].first);
- EXPECT_EQ(10u, ranges[0].second);
- EXPECT_EQ(100u, ranges[1].first);
- EXPECT_EQ(200u, ranges[1].second);
- EXPECT_EQ(300u, ranges[2].first);
- EXPECT_EQ(400u, ranges[2].second);
- }
- }
- TEST(ParseAcceptEncoding1, AcceptEncoding) {
- Request req;
- req.set_header("Accept-Encoding", "gzip");
- Response res;
- res.set_header("Content-Type", "text/plain");
- auto ret = detail::encoding_type(req, res);
- #ifdef CPPHTTPLIB_ZLIB_SUPPORT
- EXPECT_TRUE(ret == detail::EncodingType::Gzip);
- #else
- EXPECT_TRUE(ret == detail::EncodingType::None);
- #endif
- }
- TEST(ParseAcceptEncoding2, AcceptEncoding) {
- Request req;
- req.set_header("Accept-Encoding", "gzip, deflate, br");
- Response res;
- res.set_header("Content-Type", "text/plain");
- auto ret = detail::encoding_type(req, res);
- #ifdef CPPHTTPLIB_BROTLI_SUPPORT
- EXPECT_TRUE(ret == detail::EncodingType::Brotli);
- #elif CPPHTTPLIB_ZLIB_SUPPORT
- EXPECT_TRUE(ret == detail::EncodingType::Gzip);
- #else
- EXPECT_TRUE(ret == detail::EncodingType::None);
- #endif
- }
- TEST(ParseAcceptEncoding3, AcceptEncoding) {
- Request req;
- req.set_header("Accept-Encoding", "br;q=1.0, gzip;q=0.8, *;q=0.1");
- Response res;
- res.set_header("Content-Type", "text/plain");
- auto ret = detail::encoding_type(req, res);
- #ifdef CPPHTTPLIB_BROTLI_SUPPORT
- EXPECT_TRUE(ret == detail::EncodingType::Brotli);
- #elif CPPHTTPLIB_ZLIB_SUPPORT
- EXPECT_TRUE(ret == detail::EncodingType::Gzip);
- #else
- EXPECT_TRUE(ret == detail::EncodingType::None);
- #endif
- }
- TEST(BufferStreamTest, read) {
- detail::BufferStream strm1;
- Stream &strm = strm1;
- EXPECT_EQ(5, strm.write("hello"));
- char buf[512];
- EXPECT_EQ(2, strm.read(buf, 2));
- EXPECT_EQ('h', buf[0]);
- EXPECT_EQ('e', buf[1]);
- EXPECT_EQ(2, strm.read(buf, 2));
- EXPECT_EQ('l', buf[0]);
- EXPECT_EQ('l', buf[1]);
- EXPECT_EQ(1, strm.read(buf, 1));
- EXPECT_EQ('o', buf[0]);
- EXPECT_EQ(0, strm.read(buf, 1));
- }
- TEST(ChunkedEncodingTest, FromHTTPWatch_Online) {
- auto host = "www.httpwatch.com";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(2);
- auto res =
- cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137");
- ASSERT_TRUE(res);
- std::string out;
- detail::read_file("./image.jpg", out);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(out, res->body);
- }
- TEST(HostnameToIPConversionTest, HTTPWatch_Online) {
- auto host = "www.httpwatch.com";
- auto ip = hosted_at(host);
- EXPECT_EQ("191.236.16.12", ip);
- std::vector<std::string> addrs;
- hosted_at(host, addrs);
- EXPECT_EQ(1u, addrs.size());
- }
- #if 0 // It depends on each test environment...
- TEST(HostnameToIPConversionTest, YouTube_Online) {
- auto host = "www.youtube.com";
- std::vector<std::string> addrs;
- hosted_at(host, addrs);
- EXPECT_EQ(20u, addrs.size());
- auto it = std::find(addrs.begin(), addrs.end(), "2607:f8b0:4006:809::200e");
- EXPECT_TRUE(it != addrs.end());
- }
- #endif
- TEST(ChunkedEncodingTest, WithContentReceiver_Online) {
- auto host = "www.httpwatch.com";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(2);
- std::string body;
- auto res =
- cli.Get("/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
- [&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- std::string out;
- detail::read_file("./image.jpg", out);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(out, body);
- }
- TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver_Online) {
- auto host = "www.httpwatch.com";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(2);
- std::string body;
- auto res = cli.Get(
- "/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
- [&](const Response &response) {
- EXPECT_EQ(200, response.status);
- return true;
- },
- [&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- std::string out;
- detail::read_file("./image.jpg", out);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(out, body);
- }
- TEST(RangeTest, FromHTTPBin_Online) {
- auto host = "httpbin.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(5);
- {
- auto res = cli.Get("/range/32");
- ASSERT_TRUE(res);
- EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
- EXPECT_EQ(200, res->status);
- }
- {
- Headers headers = {make_range_header({{1, -1}})};
- auto res = cli.Get("/range/32", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
- EXPECT_EQ(206, res->status);
- }
- {
- Headers headers = {make_range_header({{1, 10}})};
- auto res = cli.Get("/range/32", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ("bcdefghijk", res->body);
- EXPECT_EQ(206, res->status);
- }
- {
- Headers headers = {make_range_header({{0, 31}})};
- auto res = cli.Get("/range/32", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
- EXPECT_EQ(200, res->status);
- }
- {
- Headers headers = {make_range_header({{0, -1}})};
- auto res = cli.Get("/range/32", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
- EXPECT_EQ(200, res->status);
- }
- {
- Headers headers = {make_range_header({{0, 32}})};
- auto res = cli.Get("/range/32", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ(416, res->status);
- }
- }
- TEST(ConnectionErrorTest, InvalidHost) {
- auto host = "-abcde.com";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(std::chrono::seconds(2));
- auto res = cli.Get("/");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Connection, res.error());
- }
- TEST(ConnectionErrorTest, InvalidHost2) {
- auto host = "httpbin.org/";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host);
- #else
- Client cli(host);
- #endif
- cli.set_connection_timeout(std::chrono::seconds(2));
- auto res = cli.Get("/");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Connection, res.error());
- }
- TEST(ConnectionErrorTest, InvalidHostCheckResultErrorToString) {
- auto host = "httpbin.org/";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host);
- #else
- Client cli(host);
- #endif
- cli.set_connection_timeout(std::chrono::seconds(2));
- auto res = cli.Get("/");
- ASSERT_TRUE(!res);
- stringstream s;
- s << "error code: " << res.error();
- EXPECT_EQ("error code: Could not establish connection (2)", s.str());
- }
- TEST(ConnectionErrorTest, InvalidPort) {
- auto host = "localhost";
- auto port = 44380;
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host, port);
- #else
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(std::chrono::seconds(2));
- auto res = cli.Get("/");
- ASSERT_TRUE(!res);
- EXPECT_TRUE(Error::Connection == res.error() ||
- Error::ConnectionTimeout == res.error());
- }
- TEST(ConnectionErrorTest, Timeout_Online) {
- auto host = "google.com";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 44380;
- SSLClient cli(host, port);
- #else
- auto port = 8080;
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(std::chrono::seconds(2));
- // only probe one address type so that the error reason
- // correlates to the timed-out IPv4, not the unsupported
- // IPv6 connection attempt
- cli.set_address_family(AF_INET);
- auto res = cli.Get("/");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::ConnectionTimeout, res.error());
- }
- TEST(CancelTest, NoCancel_Online) {
- auto host = "httpbin.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(std::chrono::seconds(5));
- auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return true; });
- ASSERT_TRUE(res);
- EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
- EXPECT_EQ(200, res->status);
- }
- TEST(CancelTest, WithCancelSmallPayload_Online) {
- auto host = "httpbin.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- auto res = cli.Get("/range/32", [](uint64_t, uint64_t) { return false; });
- cli.set_connection_timeout(std::chrono::seconds(5));
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Canceled, res.error());
- }
- TEST(CancelTest, WithCancelLargePayload_Online) {
- auto host = "httpbin.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- cli.set_connection_timeout(std::chrono::seconds(5));
- uint32_t count = 0;
- auto res = cli.Get("/range/65536",
- [&count](uint64_t, uint64_t) { return (count++ == 0); });
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Canceled, res.error());
- }
- TEST(BaseAuthTest, FromHTTPWatch_Online) {
- auto host = "httpbin.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- auto port = 443;
- SSLClient cli(host, port);
- #else
- auto port = 80;
- Client cli(host, port);
- #endif
- {
- auto res = cli.Get("/basic-auth/hello/world");
- ASSERT_TRUE(res);
- EXPECT_EQ(401, res->status);
- }
- {
- auto res = cli.Get("/basic-auth/hello/world",
- {make_basic_authentication_header("hello", "world")});
- ASSERT_TRUE(res);
- EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
- res->body);
- EXPECT_EQ(200, res->status);
- }
- {
- cli.set_basic_auth("hello", "world");
- auto res = cli.Get("/basic-auth/hello/world");
- ASSERT_TRUE(res);
- EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
- res->body);
- EXPECT_EQ(200, res->status);
- }
- {
- cli.set_basic_auth("hello", "bad");
- auto res = cli.Get("/basic-auth/hello/world");
- ASSERT_TRUE(res);
- EXPECT_EQ(401, res->status);
- }
- {
- cli.set_basic_auth("bad", "world");
- auto res = cli.Get("/basic-auth/hello/world");
- ASSERT_TRUE(res);
- EXPECT_EQ(401, res->status);
- }
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(DigestAuthTest, FromHTTPWatch_Online) {
- auto host = "httpbin.org";
- auto port = 443;
- SSLClient cli(host, port);
- {
- auto res = cli.Get("/digest-auth/auth/hello/world");
- ASSERT_TRUE(res);
- EXPECT_EQ(401, res->status);
- }
- {
- std::vector<std::string> paths = {
- "/digest-auth/auth/hello/world/MD5",
- "/digest-auth/auth/hello/world/SHA-256",
- "/digest-auth/auth/hello/world/SHA-512",
- "/digest-auth/auth-int/hello/world/MD5",
- };
- cli.set_digest_auth("hello", "world");
- for (auto path : paths) {
- auto res = cli.Get(path.c_str());
- ASSERT_TRUE(res);
- EXPECT_EQ("{\n \"authenticated\": true, \n \"user\": \"hello\"\n}\n",
- res->body);
- EXPECT_EQ(200, res->status);
- }
- cli.set_digest_auth("hello", "bad");
- for (auto path : paths) {
- auto res = cli.Get(path.c_str());
- ASSERT_TRUE(res);
- EXPECT_EQ(401, res->status);
- }
- // NOTE: Until httpbin.org fixes issue #46, the following test is commented
- // out. Plese see https://httpbin.org/digest-auth/auth/hello/world
- // cli.set_digest_auth("bad", "world");
- // for (auto path : paths) {
- // auto res = cli.Get(path.c_str());
- // ASSERT_TRUE(res);
- // EXPECT_EQ(400, res->status);
- // }
- }
- }
- #endif
- TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) {
- auto host = "google.com";
- auto another_host = "example.com";
- auto wrong_ip = "0.0.0.0";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host);
- #else
- Client cli(host);
- #endif
- cli.set_hostname_addr_map({{another_host, wrong_ip}});
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- ASSERT_EQ(301, res->status);
- }
- TEST(SpecifyServerIPAddressTest, RealHostname_Online) {
- auto host = "google.com";
- auto wrong_ip = "0.0.0.0";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host);
- #else
- Client cli(host);
- #endif
- cli.set_hostname_addr_map({{host, wrong_ip}});
- auto res = cli.Get("/");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Connection, res.error());
- }
- TEST(AbsoluteRedirectTest, Redirect_Online) {
- auto host = "nghttp2.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host);
- #else
- Client cli(host);
- #endif
- cli.set_follow_location(true);
- auto res = cli.Get("/httpbin/absolute-redirect/3");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(RedirectTest, Redirect_Online) {
- auto host = "nghttp2.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host);
- #else
- Client cli(host);
- #endif
- cli.set_follow_location(true);
- auto res = cli.Get("/httpbin/redirect/3");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(RelativeRedirectTest, Redirect_Online) {
- auto host = "nghttp2.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host);
- #else
- Client cli(host);
- #endif
- cli.set_follow_location(true);
- auto res = cli.Get("/httpbin/relative-redirect/3");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(TooManyRedirectTest, Redirect_Online) {
- auto host = "nghttp2.org";
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(host);
- #else
- Client cli(host);
- #endif
- cli.set_follow_location(true);
- auto res = cli.Get("/httpbin/redirect/21");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::ExceedRedirectCount, res.error());
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(YahooRedirectTest, Redirect_Online) {
- Client cli("yahoo.com");
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(301, res->status);
- cli.set_follow_location(true);
- res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("https://yahoo.com/", res->location);
- }
- TEST(HttpsToHttpRedirectTest, Redirect_Online) {
- SSLClient cli("nghttp2.org");
- cli.set_follow_location(true);
- auto res = cli.Get(
- "/httpbin/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(HttpsToHttpRedirectTest2, Redirect_Online) {
- SSLClient cli("nghttp2.org");
- cli.set_follow_location(true);
- Params params;
- params.emplace("url", "http://www.google.com");
- params.emplace("status_code", "302");
- auto res = cli.Get("/httpbin/redirect-to", params, Headers{});
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(HttpsToHttpRedirectTest3, Redirect_Online) {
- SSLClient cli("nghttp2.org");
- cli.set_follow_location(true);
- Params params;
- params.emplace("url", "http://www.google.com");
- auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{});
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(UrlWithSpace, Redirect_Online) {
- SSLClient cli("edge.forgecdn.net");
- cli.set_follow_location(true);
- auto res = cli.Get("/files/2595/310/Neat 1.4-17.jar");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(18527U, res->get_header_value<uint64_t>("Content-Length"));
- }
- #endif
- #if !defined(_WIN32) && !defined(_WIN64)
- TEST(ReceiveSignals, Signal) {
- auto setupSignalHandlers = []() {
- struct sigaction act;
- sigemptyset(&act.sa_mask);
- act.sa_flags = SA_SIGINFO;
- act.sa_sigaction = [](int sig, siginfo_t *, void *) {
- switch (sig) {
- case SIGINT:
- default: break;
- }
- };
- ::sigaction(SIGINT, &act, nullptr);
- };
- Server svr;
- int port = 0;
- auto thread = std::thread([&]() {
- setupSignalHandlers();
- port = svr.bind_to_any_port("localhost");
- svr.listen_after_bind();
- });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- ASSERT_TRUE(svr.is_running());
- pthread_kill(thread.native_handle(), SIGINT);
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- ASSERT_TRUE(svr.is_running());
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #endif
- TEST(RedirectToDifferentPort, Redirect) {
- Server svr1;
- svr1.Get("/1", [&](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!", "text/plain");
- });
- int svr1_port = 0;
- auto thread1 = std::thread([&]() {
- svr1_port = svr1.bind_to_any_port("localhost");
- svr1.listen_after_bind();
- });
- Server svr2;
- svr2.Get("/2", [&](const Request & /*req*/, Response &res) {
- res.set_redirect("http://localhost:" + std::to_string(svr1_port) + "/1");
- });
- int svr2_port = 0;
- auto thread2 = std::thread([&]() {
- svr2_port = svr2.bind_to_any_port("localhost");
- svr2.listen_after_bind();
- });
- while (!svr1.is_running() || !svr2.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli("localhost", svr2_port);
- cli.set_follow_location(true);
- auto res = cli.Get("/2");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("Hello World!", res->body);
- svr1.stop();
- svr2.stop();
- thread1.join();
- thread2.join();
- ASSERT_FALSE(svr1.is_running());
- ASSERT_FALSE(svr2.is_running());
- }
- TEST(RedirectFromPageWithContent, Redirect) {
- Server svr;
- svr.Get("/1", [&](const Request & /*req*/, Response &res) {
- res.set_content("___", "text/plain");
- res.set_redirect("/2");
- });
- svr.Get("/2", [&](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!", "text/plain");
- });
- auto th = std::thread([&]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli("localhost", PORT);
- cli.set_follow_location(true);
- std::string body;
- auto res = cli.Get("/1", [&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("Hello World!", body);
- }
- {
- Client cli("localhost", PORT);
- std::string body;
- auto res = cli.Get("/1", [&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- EXPECT_EQ(302, res->status);
- EXPECT_EQ("___", body);
- }
- svr.stop();
- th.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(RedirectFromPageWithContentIP6, Redirect) {
- Server svr;
- svr.Get("/1", [&](const Request & /*req*/, Response &res) {
- res.set_content("___", "text/plain");
- // res.set_redirect("/2");
- res.set_redirect("http://[::1]:1234/2");
- });
- svr.Get("/2", [&](const Request &req, Response &res) {
- auto host_header = req.headers.find("Host");
- ASSERT_TRUE(host_header != req.headers.end());
- EXPECT_EQ("[::1]:1234", host_header->second);
- res.set_content("Hello World!", "text/plain");
- });
- auto th = std::thread([&]() { svr.listen("::1", 1234); });
- // When IPV6 support isn't available svr.listen("::1", 1234) never
- // actually starts anything, so the condition !svr.is_running() will
- // always remain true, and the loop never stops.
- // This basically counts how many milliseconds have passed since the
- // call to svr.listen(), and if after 5 seconds nothing started yet
- // aborts the test.
- for (unsigned int milliseconds = 0; !svr.is_running(); milliseconds++) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- ASSERT_LT(milliseconds, 5000U);
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli("http://[::1]:1234");
- cli.set_follow_location(true);
- std::string body;
- auto res = cli.Get("/1", [&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("Hello World!", body);
- }
- {
- Client cli("http://[::1]:1234");
- std::string body;
- auto res = cli.Get("/1", [&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- EXPECT_EQ(302, res->status);
- EXPECT_EQ("___", body);
- }
- svr.stop();
- th.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(PathUrlEncodeTest, PathUrlEncode) {
- Server svr;
- svr.Get("/foo", [](const Request &req, Response &res) {
- auto a = req.params.find("a");
- if (a != req.params.end()) {
- res.set_content((*a).second, "text/plain");
- res.status = 200;
- } else {
- res.status = 400;
- }
- });
- auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli(HOST, PORT);
- cli.set_url_encode(false);
- auto res = cli.Get("/foo?a=explicitly+encoded");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- // This expects it back with a space, as the `+` won't have been
- // url-encoded, and server-side the params get decoded turning `+`
- // into spaces.
- EXPECT_EQ("explicitly encoded", res->body);
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(BindServerTest, BindDualStack) {
- Server svr;
- svr.Get("/1", [&](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!", "text/plain");
- });
- auto thread = std::thread([&]() { svr.listen("::", PORT); });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli("127.0.0.1", PORT);
- auto res = cli.Get("/1");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("Hello World!", res->body);
- }
- {
- Client cli("::1", PORT);
- auto res = cli.Get("/1");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("Hello World!", res->body);
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(BindServerTest, BindAndListenSeparately) {
- Server svr;
- int port = svr.bind_to_any_port("0.0.0.0");
- ASSERT_TRUE(svr.is_valid());
- ASSERT_TRUE(port > 0);
- svr.stop();
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(BindServerTest, BindAndListenSeparatelySSL) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
- CLIENT_CA_CERT_DIR);
- int port = svr.bind_to_any_port("0.0.0.0");
- ASSERT_TRUE(svr.is_valid());
- ASSERT_TRUE(port > 0);
- svr.stop();
- }
- #endif
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(BindServerTest, BindAndListenSeparatelySSLEncryptedKey) {
- SSLServer svr(SERVER_ENCRYPTED_CERT_FILE, SERVER_ENCRYPTED_PRIVATE_KEY_FILE,
- nullptr, nullptr, SERVER_ENCRYPTED_PRIVATE_KEY_PASS);
- int port = svr.bind_to_any_port("0.0.0.0");
- ASSERT_TRUE(svr.is_valid());
- ASSERT_TRUE(port > 0);
- svr.stop();
- }
- #endif
- TEST(ErrorHandlerTest, ContentLength) {
- Server svr;
- svr.set_error_handler([](const Request & /*req*/, Response &res) {
- res.status = 200;
- res.set_content("abcdefghijklmnopqrstuvwxyz",
- "text/html"); // <= Content-Length still 13
- });
- svr.Get("/hi", [](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!\n", "text/plain");
- res.status = 524;
- });
- auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli(HOST, PORT);
- auto res = cli.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- EXPECT_EQ("26", res->get_header_value("Content-Length"));
- EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body);
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #ifndef CPPHTTPLIB_NO_EXCEPTIONS
- TEST(ExceptionHandlerTest, ContentLength) {
- Server svr;
- svr.set_exception_handler([](const Request & /*req*/, Response &res,
- std::exception_ptr ep) {
- EXPECT_FALSE(ep == nullptr);
- try {
- std::rethrow_exception(ep);
- } catch (std::exception &e) { EXPECT_EQ("abc", std::string(e.what())); }
- res.status = 500;
- res.set_content("abcdefghijklmnopqrstuvwxyz",
- "text/html"); // <= Content-Length still 13 at this point
- });
- svr.Get("/hi", [](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!\n", "text/plain");
- throw std::runtime_error("abc");
- });
- auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- for (size_t i = 0; i < 10; i++) {
- Client cli(HOST, PORT);
- for (size_t j = 0; j < 100; j++) {
- auto res = cli.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(500, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- EXPECT_EQ("26", res->get_header_value("Content-Length"));
- EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body);
- }
- cli.set_keep_alive(true);
- for (size_t j = 0; j < 100; j++) {
- auto res = cli.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(500, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- EXPECT_EQ("26", res->get_header_value("Content-Length"));
- EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body);
- }
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #endif
- TEST(NoContentTest, ContentLength) {
- Server svr;
- svr.Get("/hi",
- [](const Request & /*req*/, Response &res) { res.status = 204; });
- auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli(HOST, PORT);
- auto res = cli.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(204, res->status);
- EXPECT_EQ("0", res->get_header_value("Content-Length"));
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(RoutingHandlerTest, PreRoutingHandler) {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
- ASSERT_TRUE(svr.is_valid());
- #else
- Server svr;
- #endif
- svr.set_pre_routing_handler([](const Request &req, Response &res) {
- if (req.path == "/routing_handler") {
- res.set_header("PRE_ROUTING", "on");
- res.set_content("Routing Handler", "text/plain");
- return httplib::Server::HandlerResponse::Handled;
- }
- return httplib::Server::HandlerResponse::Unhandled;
- });
- svr.set_error_handler([](const Request & /*req*/, Response &res) {
- res.set_content("Error", "text/html");
- });
- svr.set_post_routing_handler([](const Request &req, Response &res) {
- if (req.path == "/routing_handler") {
- res.set_header("POST_ROUTING", "on");
- }
- });
- svr.Get("/hi", [](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!\n", "text/plain");
- });
- auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(HOST, PORT);
- cli.enable_server_certificate_verification(false);
- #else
- Client cli(HOST, PORT);
- #endif
- auto res = cli.Get("/routing_handler");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("Routing Handler", res->body);
- EXPECT_EQ(1U, res->get_header_value_count("PRE_ROUTING"));
- EXPECT_EQ("on", res->get_header_value("PRE_ROUTING"));
- EXPECT_EQ(1U, res->get_header_value_count("POST_ROUTING"));
- EXPECT_EQ("on", res->get_header_value("POST_ROUTING"));
- }
- {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(HOST, PORT);
- cli.enable_server_certificate_verification(false);
- #else
- Client cli(HOST, PORT);
- #endif
- auto res = cli.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("Hello World!\n", res->body);
- EXPECT_EQ(0U, res->get_header_value_count("PRE_ROUTING"));
- EXPECT_EQ(0U, res->get_header_value_count("POST_ROUTING"));
- }
- {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli(HOST, PORT);
- cli.enable_server_certificate_verification(false);
- #else
- Client cli(HOST, PORT);
- #endif
- auto res = cli.Get("/aaa");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- EXPECT_EQ("Error", res->body);
- EXPECT_EQ(0U, res->get_header_value_count("PRE_ROUTING"));
- EXPECT_EQ(0U, res->get_header_value_count("POST_ROUTING"));
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(InvalidFormatTest, StatusCode) {
- Server svr;
- svr.Get("/hi", [](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!\n", "text/plain");
- res.status = 9999; // Status should be a three-digit code...
- });
- auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli(HOST, PORT);
- auto res = cli.Get("/hi");
- ASSERT_FALSE(res);
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(URLFragmentTest, WithFragment) {
- Server svr;
- svr.Get("/hi", [](const Request &req, Response & /*res*/) {
- EXPECT_TRUE(req.target == "/hi");
- });
- auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli(HOST, PORT);
- auto res = cli.Get("/hi#key1=val1=key2=val2");
- EXPECT_TRUE(res);
- EXPECT_EQ(200, res->status);
- res = cli.Get("/hi%23key1=val1=key2=val2");
- EXPECT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- class ServerTest : public ::testing::Test {
- protected:
- ServerTest()
- : cli_(HOST, PORT)
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- ,
- svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
- #endif
- {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- cli_.enable_server_certificate_verification(false);
- #endif
- }
- virtual void SetUp() {
- svr_.set_mount_point("/", "./www");
- svr_.set_mount_point("/mount", "./www2");
- svr_.set_file_extension_and_mimetype_mapping("abcde", "text/abcde");
- svr_.Get("/hi",
- [&](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!", "text/plain");
- })
- .Get("/http_response_splitting",
- [&](const Request & /*req*/, Response &res) {
- res.set_header("a", "1\r\nSet-Cookie: a=1");
- EXPECT_EQ(0U, res.headers.size());
- EXPECT_FALSE(res.has_header("a"));
- res.set_header("a", "1\nSet-Cookie: a=1");
- EXPECT_EQ(0U, res.headers.size());
- EXPECT_FALSE(res.has_header("a"));
- res.set_header("a", "1\rSet-Cookie: a=1");
- EXPECT_EQ(0U, res.headers.size());
- EXPECT_FALSE(res.has_header("a"));
- res.set_header("a\r\nb", "0");
- EXPECT_EQ(0U, res.headers.size());
- EXPECT_FALSE(res.has_header("a"));
- res.set_header("a\rb", "0");
- EXPECT_EQ(0U, res.headers.size());
- EXPECT_FALSE(res.has_header("a"));
- res.set_header("a\nb", "0");
- EXPECT_EQ(0U, res.headers.size());
- EXPECT_FALSE(res.has_header("a"));
- res.set_redirect("1\r\nSet-Cookie: a=1");
- EXPECT_EQ(0U, res.headers.size());
- EXPECT_FALSE(res.has_header("Location"));
- })
- .Get("/slow",
- [&](const Request & /*req*/, Response &res) {
- std::this_thread::sleep_for(std::chrono::seconds(2));
- res.set_content("slow", "text/plain");
- })
- #if 0
- .Post("/slowpost",
- [&](const Request & /*req*/, Response &res) {
- std::this_thread::sleep_for(std::chrono::seconds(2));
- res.set_content("slow", "text/plain");
- })
- #endif
- .Get("/remote_addr",
- [&](const Request &req, Response &res) {
- auto remote_addr = req.headers.find("REMOTE_ADDR")->second;
- EXPECT_TRUE(req.has_header("REMOTE_PORT"));
- EXPECT_EQ(req.remote_addr, req.get_header_value("REMOTE_ADDR"));
- EXPECT_EQ(req.remote_port,
- std::stoi(req.get_header_value("REMOTE_PORT")));
- res.set_content(remote_addr.c_str(), "text/plain");
- })
- .Get("/local_addr",
- [&](const Request &req, Response &res) {
- EXPECT_TRUE(req.has_header("LOCAL_PORT"));
- EXPECT_TRUE(req.has_header("LOCAL_ADDR"));
- auto local_addr = req.get_header_value("LOCAL_ADDR");
- auto local_port = req.get_header_value("LOCAL_PORT");
- EXPECT_EQ(req.local_addr, local_addr);
- EXPECT_EQ(req.local_port, std::stoi(local_port));
- res.set_content(local_addr.append(":").append(local_port),
- "text/plain");
- })
- .Get("/endwith%",
- [&](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!", "text/plain");
- })
- .Get("/a\\+\\+b",
- [&](const Request &req, Response &res) {
- ASSERT_TRUE(req.has_param("a +b"));
- auto val = req.get_param_value("a +b");
- res.set_content(val, "text/plain");
- })
- .Get("/", [&](const Request & /*req*/,
- Response &res) { res.set_redirect("/hi"); })
- .Post("/1", [](const Request & /*req*/,
- Response &res) { res.set_redirect("/2", 303); })
- .Get("/2",
- [](const Request & /*req*/, Response &res) {
- res.set_content("redirected.", "text/plain");
- res.status = 200;
- })
- .Post("/person",
- [&](const Request &req, Response &res) {
- if (req.has_param("name") && req.has_param("note")) {
- persons_[req.get_param_value("name")] =
- req.get_param_value("note");
- } else {
- res.status = 400;
- }
- })
- .Put("/person",
- [&](const Request &req, Response &res) {
- if (req.has_param("name") && req.has_param("note")) {
- persons_[req.get_param_value("name")] =
- req.get_param_value("note");
- } else {
- res.status = 400;
- }
- })
- .Get("/person/(.*)",
- [&](const Request &req, Response &res) {
- string name = req.matches[1];
- if (persons_.find(name) != persons_.end()) {
- auto note = persons_[name];
- res.set_content(note, "text/plain");
- } else {
- res.status = 404;
- }
- })
- .Post("/x-www-form-urlencoded-json",
- [&](const Request &req, Response &res) {
- auto json = req.get_param_value("json");
- ASSERT_EQ(JSON_DATA, json);
- res.set_content(json, "appliation/json");
- res.status = 200;
- })
- .Get("/streamed-chunked",
- [&](const Request & /*req*/, Response &res) {
- res.set_chunked_content_provider(
- "text/plain", [](size_t /*offset*/, DataSink &sink) {
- sink.os << "123";
- sink.os << "456";
- sink.os << "789";
- sink.done();
- return true;
- });
- })
- .Get("/streamed-chunked2",
- [&](const Request & /*req*/, Response &res) {
- auto i = new int(0);
- res.set_chunked_content_provider(
- "text/plain",
- [i](size_t /*offset*/, DataSink &sink) {
- switch (*i) {
- case 0: sink.os << "123"; break;
- case 1: sink.os << "456"; break;
- case 2: sink.os << "789"; break;
- case 3: sink.done(); break;
- }
- (*i)++;
- return true;
- },
- [i](bool success) {
- EXPECT_TRUE(success);
- delete i;
- });
- })
- .Get("/streamed",
- [&](const Request & /*req*/, Response &res) {
- res.set_content_provider(
- 6, "text/plain",
- [](size_t offset, size_t /*length*/, DataSink &sink) {
- sink.os << (offset < 3 ? "a" : "b");
- return true;
- });
- })
- .Get("/streamed-with-range",
- [&](const Request & /*req*/, Response &res) {
- auto data = new std::string("abcdefg");
- res.set_content_provider(
- data->size(), "text/plain",
- [data](size_t offset, size_t length, DataSink &sink) {
- size_t DATA_CHUNK_SIZE = 4;
- const auto &d = *data;
- auto out_len =
- std::min(static_cast<size_t>(length), DATA_CHUNK_SIZE);
- auto ret =
- sink.write(&d[static_cast<size_t>(offset)], out_len);
- EXPECT_TRUE(ret);
- return true;
- },
- [data](bool success) {
- EXPECT_TRUE(success);
- delete data;
- });
- })
- .Get("/streamed-cancel",
- [&](const Request & /*req*/, Response &res) {
- res.set_content_provider(
- size_t(-1), "text/plain",
- [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
- sink.os << "data_chunk";
- return true;
- });
- })
- .Get("/with-range",
- [&](const Request & /*req*/, Response &res) {
- res.set_content("abcdefg", "text/plain");
- })
- .Post("/chunked",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ(req.body, "dechunked post body");
- })
- .Post("/large-chunked",
- [&](const Request &req, Response & /*res*/) {
- std::string expected(6 * 30 * 1024u, 'a');
- EXPECT_EQ(req.body, expected);
- })
- .Post("/multipart",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ(6u, req.files.size());
- ASSERT_TRUE(!req.has_file("???"));
- ASSERT_TRUE(req.body.empty());
- {
- const auto &file = req.get_file_value("text1");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ("text default", file.content);
- }
- {
- const auto &file = req.get_file_value("text2");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ("aωb", file.content);
- }
- {
- const auto &file = req.get_file_value("file1");
- EXPECT_EQ("hello.txt", file.filename);
- EXPECT_EQ("text/plain", file.content_type);
- EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content);
- }
- {
- const auto &file = req.get_file_value("file3");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ("application/octet-stream", file.content_type);
- EXPECT_EQ(0u, file.content.size());
- }
- {
- const auto &file = req.get_file_value("file4");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ(0u, file.content.size());
- EXPECT_EQ("application/json tmp-string", file.content_type);
- }
- })
- .Post("/multipart/multi_file_values",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ(5u, req.files.size());
- ASSERT_TRUE(!req.has_file("???"));
- ASSERT_TRUE(req.body.empty());
- {
- const auto &text_value = req.get_file_values("text");
- EXPECT_EQ(text_value.size(), 1);
- auto &text = text_value[0];
- EXPECT_TRUE(text.filename.empty());
- EXPECT_EQ("defalut text", text.content);
- }
- {
- const auto &text1_values = req.get_file_values("multi_text1");
- EXPECT_EQ(text1_values.size(), 2);
- EXPECT_EQ("aaaaa", text1_values[0].content);
- EXPECT_EQ("bbbbb", text1_values[1].content);
- }
- {
- const auto &file1_values = req.get_file_values("multi_file1");
- EXPECT_EQ(file1_values.size(), 2);
- auto file1 = file1_values[0];
- EXPECT_EQ(file1.filename, "hello.txt");
- EXPECT_EQ(file1.content_type, "text/plain");
- EXPECT_EQ("h\ne\n\nl\nl\no\n", file1.content);
- auto file2 = file1_values[1];
- EXPECT_EQ(file2.filename, "world.json");
- EXPECT_EQ(file2.content_type, "application/json");
- EXPECT_EQ("{\n \"world\", true\n}\n", file2.content);
- }
- })
- .Post("/empty",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, "");
- EXPECT_EQ("text/plain", req.get_header_value("Content-Type"));
- EXPECT_EQ("0", req.get_header_value("Content-Length"));
- res.set_content("empty", "text/plain");
- })
- .Post("/empty-no-content-type",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, "");
- EXPECT_FALSE(req.has_header("Content-Type"));
- EXPECT_EQ("0", req.get_header_value("Content-Length"));
- res.set_content("empty-no-content-type", "text/plain");
- })
- .Post("/path-only",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, "");
- EXPECT_EQ("", req.get_header_value("Content-Type"));
- EXPECT_EQ("0", req.get_header_value("Content-Length"));
- res.set_content("path-only", "text/plain");
- })
- .Post("/path-headers-only",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, "");
- EXPECT_EQ("", req.get_header_value("Content-Type"));
- EXPECT_EQ("0", req.get_header_value("Content-Length"));
- EXPECT_EQ("world", req.get_header_value("hello"));
- EXPECT_EQ("world2", req.get_header_value("hello2"));
- res.set_content("path-headers-only", "text/plain");
- })
- .Post("/post-large",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, LARGE_DATA);
- res.set_content(req.body, "text/plain");
- })
- .Put("/empty-no-content-type",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, "");
- EXPECT_FALSE(req.has_header("Content-Type"));
- EXPECT_EQ("0", req.get_header_value("Content-Length"));
- res.set_content("empty-no-content-type", "text/plain");
- })
- .Put("/put",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, "PUT");
- res.set_content(req.body, "text/plain");
- })
- .Put("/put-large",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, LARGE_DATA);
- res.set_content(req.body, "text/plain");
- })
- .Patch("/patch",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, "PATCH");
- res.set_content(req.body, "text/plain");
- })
- .Delete("/delete",
- [&](const Request & /*req*/, Response &res) {
- res.set_content("DELETE", "text/plain");
- })
- .Delete("/delete-body",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(req.body, "content");
- res.set_content(req.body, "text/plain");
- })
- .Options(R"(\*)",
- [&](const Request & /*req*/, Response &res) {
- res.set_header("Allow", "GET, POST, HEAD, OPTIONS");
- })
- .Get("/request-target",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ("/request-target?aaa=bbb&ccc=ddd", req.target);
- EXPECT_EQ("bbb", req.get_param_value("aaa"));
- EXPECT_EQ("ddd", req.get_param_value("ccc"));
- })
- .Get("/long-query-value",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ(LONG_QUERY_URL, req.target);
- EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
- })
- .Get("/array-param",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ(3u, req.get_param_value_count("array"));
- EXPECT_EQ("value1", req.get_param_value("array", 0));
- EXPECT_EQ("value2", req.get_param_value("array", 1));
- EXPECT_EQ("value3", req.get_param_value("array", 2));
- })
- .Post("/validate-no-multiple-headers",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ(1u, req.get_header_value_count("Content-Length"));
- EXPECT_EQ("5", req.get_header_value("Content-Length"));
- })
- .Post("/content_receiver",
- [&](const Request &req, Response &res,
- const ContentReader &content_reader) {
- if (req.is_multipart_form_data()) {
- MultipartFormDataItems files;
- content_reader(
- [&](const MultipartFormData &file) {
- files.push_back(file);
- return true;
- },
- [&](const char *data, size_t data_length) {
- files.back().content.append(data, data_length);
- return true;
- });
- EXPECT_EQ(5u, files.size());
- {
- const auto &file = get_file_value(files, "text1");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ("text default", file.content);
- }
- {
- const auto &file = get_file_value(files, "text2");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ("aωb", file.content);
- }
- {
- const auto &file = get_file_value(files, "file1");
- EXPECT_EQ("hello.txt", file.filename);
- EXPECT_EQ("text/plain", file.content_type);
- EXPECT_EQ("h\ne\n\nl\nl\no\n", file.content);
- }
- {
- const auto &file = get_file_value(files, "file3");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ("application/octet-stream", file.content_type);
- EXPECT_EQ(0u, file.content.size());
- }
- } else {
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- EXPECT_EQ(7U, data_length);
- body.append(data, data_length);
- return true;
- });
- EXPECT_EQ(body, "content");
- res.set_content(body, "text/plain");
- }
- })
- .Put("/content_receiver",
- [&](const Request & /*req*/, Response &res,
- const ContentReader &content_reader) {
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- EXPECT_EQ(body, "content");
- res.set_content(body, "text/plain");
- })
- .Patch("/content_receiver",
- [&](const Request & /*req*/, Response &res,
- const ContentReader &content_reader) {
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- EXPECT_EQ(body, "content");
- res.set_content(body, "text/plain");
- })
- .Post("/query-string-and-body",
- [&](const Request &req, Response & /*res*/) {
- ASSERT_TRUE(req.has_param("key"));
- EXPECT_EQ(req.get_param_value("key"), "value");
- EXPECT_EQ(req.body, "content");
- })
- .Get("/last-request",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ("close", req.get_header_value("Connection"));
- })
- .Get(R"(/redirect/(\d+))",
- [&](const Request &req, Response &res) {
- auto num = std::stoi(req.matches[1]) + 1;
- std::string url = "/redirect/" + std::to_string(num);
- res.set_redirect(url);
- })
- .Post("/binary",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(4U, req.body.size());
- EXPECT_EQ("application/octet-stream",
- req.get_header_value("Content-Type"));
- EXPECT_EQ("4", req.get_header_value("Content-Length"));
- res.set_content(req.body, "application/octet-stream");
- })
- .Put("/binary",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(4U, req.body.size());
- EXPECT_EQ("application/octet-stream",
- req.get_header_value("Content-Type"));
- EXPECT_EQ("4", req.get_header_value("Content-Length"));
- res.set_content(req.body, "application/octet-stream");
- })
- .Patch("/binary",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(4U, req.body.size());
- EXPECT_EQ("application/octet-stream",
- req.get_header_value("Content-Type"));
- EXPECT_EQ("4", req.get_header_value("Content-Length"));
- res.set_content(req.body, "application/octet-stream");
- })
- .Delete("/binary",
- [&](const Request &req, Response &res) {
- EXPECT_EQ(4U, req.body.size());
- EXPECT_EQ("application/octet-stream",
- req.get_header_value("Content-Type"));
- EXPECT_EQ("4", req.get_header_value("Content-Length"));
- res.set_content(req.body, "application/octet-stream");
- })
- #if defined(CPPHTTPLIB_ZLIB_SUPPORT) || defined(CPPHTTPLIB_BROTLI_SUPPORT)
- .Get("/compress",
- [&](const Request & /*req*/, Response &res) {
- res.set_content(
- "12345678901234567890123456789012345678901234567890123456789"
- "01234567890123456789012345678901234567890",
- "text/plain");
- })
- .Get("/nocompress",
- [&](const Request & /*req*/, Response &res) {
- res.set_content(
- "12345678901234567890123456789012345678901234567890123456789"
- "01234567890123456789012345678901234567890",
- "application/octet-stream");
- })
- .Post("/compress-multipart",
- [&](const Request &req, Response & /*res*/) {
- EXPECT_EQ(2u, req.files.size());
- ASSERT_TRUE(!req.has_file("???"));
- {
- const auto &file = req.get_file_value("key1");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ("test", file.content);
- }
- {
- const auto &file = req.get_file_value("key2");
- EXPECT_TRUE(file.filename.empty());
- EXPECT_EQ("--abcdefg123", file.content);
- }
- })
- #endif
- ;
- persons_["john"] = "programmer";
- t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
- while (!svr_.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- virtual void TearDown() {
- svr_.stop();
- if (!request_threads_.empty()) {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- for (auto &t : request_threads_) {
- t.join();
- }
- }
- t_.join();
- }
- map<string, string> persons_;
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli_;
- SSLServer svr_;
- #else
- Client cli_;
- Server svr_;
- #endif
- thread t_;
- std::vector<thread> request_threads_;
- };
- TEST_F(ServerTest, GetMethod200) {
- auto res = cli_.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ("HTTP/1.1", res->version);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("OK", res->reason);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ(1U, res->get_header_value_count("Content-Type"));
- EXPECT_EQ("Hello World!", res->body);
- }
- TEST_F(ServerTest, GetMethod200withPercentEncoding) {
- auto res = cli_.Get("/%68%69"); // auto res = cli_.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ("HTTP/1.1", res->version);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ(1U, res->get_header_value_count("Content-Type"));
- EXPECT_EQ("Hello World!", res->body);
- }
- TEST_F(ServerTest, GetMethod302) {
- auto res = cli_.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(302, res->status);
- EXPECT_EQ("/hi", res->get_header_value("Location"));
- }
- TEST_F(ServerTest, GetMethod302Redirect) {
- cli_.set_follow_location(true);
- auto res = cli_.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("Hello World!", res->body);
- EXPECT_EQ("/hi", res->location);
- }
- TEST_F(ServerTest, GetMethod404) {
- auto res = cli_.Get("/invalid");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, HeadMethod200) {
- auto res = cli_.Head("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_TRUE(res->body.empty());
- }
- TEST_F(ServerTest, HeadMethod200Static) {
- auto res = cli_.Head("/mount/dir/index.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- EXPECT_EQ(104, std::stoi(res->get_header_value("Content-Length")));
- EXPECT_TRUE(res->body.empty());
- }
- TEST_F(ServerTest, HeadMethod404) {
- auto res = cli_.Head("/invalid");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- EXPECT_TRUE(res->body.empty());
- }
- TEST_F(ServerTest, GetMethodPersonJohn) {
- auto res = cli_.Get("/person/john");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("programmer", res->body);
- }
- TEST_F(ServerTest, PostMethod1) {
- auto res = cli_.Get("/person/john1");
- ASSERT_TRUE(res);
- ASSERT_EQ(404, res->status);
- res = cli_.Post("/person", "name=john1¬e=coder",
- "application/x-www-form-urlencoded");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- res = cli_.Get("/person/john1");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
- ASSERT_EQ("coder", res->body);
- }
- TEST_F(ServerTest, PostMethod2) {
- auto res = cli_.Get("/person/john2");
- ASSERT_TRUE(res);
- ASSERT_EQ(404, res->status);
- Params params;
- params.emplace("name", "john2");
- params.emplace("note", "coder");
- res = cli_.Post("/person", params);
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- res = cli_.Get("/person/john2");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
- ASSERT_EQ("coder", res->body);
- }
- TEST_F(ServerTest, PutMethod3) {
- auto res = cli_.Get("/person/john3");
- ASSERT_TRUE(res);
- ASSERT_EQ(404, res->status);
- Params params;
- params.emplace("name", "john3");
- params.emplace("note", "coder");
- res = cli_.Put("/person", params);
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- res = cli_.Get("/person/john3");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
- ASSERT_EQ("coder", res->body);
- }
- TEST_F(ServerTest, PostWwwFormUrlEncodedJson) {
- Params params;
- params.emplace("json", JSON_DATA);
- auto res = cli_.Post("/x-www-form-urlencoded-json", params);
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(JSON_DATA, res->body);
- }
- TEST_F(ServerTest, PostEmptyContent) {
- auto res = cli_.Post("/empty", "", "text/plain");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("empty", res->body);
- }
- TEST_F(ServerTest, PostEmptyContentWithNoContentType) {
- auto res = cli_.Post("/empty-no-content-type");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("empty-no-content-type", res->body);
- }
- TEST_F(ServerTest, PostPathOnly) {
- auto res = cli_.Post("/path-only");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("path-only", res->body);
- }
- TEST_F(ServerTest, PostPathAndHeadersOnly) {
- auto res = cli_.Post("/path-headers-only",
- Headers({{"hello", "world"}, {"hello2", "world2"}}));
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("path-headers-only", res->body);
- }
- TEST_F(ServerTest, PostLarge) {
- auto res = cli_.Post("/post-large", LARGE_DATA, "text/plain");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- EXPECT_EQ(LARGE_DATA, res->body);
- }
- TEST_F(ServerTest, PutEmptyContentWithNoContentType) {
- auto res = cli_.Put("/empty-no-content-type");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("empty-no-content-type", res->body);
- }
- TEST_F(ServerTest, GetMethodDir) {
- auto res = cli_.Get("/dir/");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- auto body = R"(<html>
- <head>
- </head>
- <body>
- <a href="/dir/test.html">Test</a>
- <a href="/hi">hi</a>
- </body>
- </html>
- )";
- EXPECT_EQ(body, res->body);
- }
- TEST_F(ServerTest, GetMethodDirTest) {
- auto res = cli_.Get("/dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- EXPECT_EQ("test.html", res->body);
- }
- TEST_F(ServerTest, GetMethodDirTestWithDoubleDots) {
- auto res = cli_.Get("/dir/../dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- EXPECT_EQ("test.html", res->body);
- }
- TEST_F(ServerTest, GetMethodInvalidPath) {
- auto res = cli_.Get("/dir/../test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, GetMethodOutOfBaseDir) {
- auto res = cli_.Get("/../www/dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, GetMethodOutOfBaseDir2) {
- auto res = cli_.Get("/dir/../../www/dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, GetMethodDirMountTest) {
- auto res = cli_.Get("/mount/dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- EXPECT_EQ("test.html", res->body);
- }
- TEST_F(ServerTest, GetMethodDirMountTestWithDoubleDots) {
- auto res = cli_.Get("/mount/dir/../dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
- EXPECT_EQ("test.html", res->body);
- }
- TEST_F(ServerTest, GetMethodInvalidMountPath) {
- auto res = cli_.Get("/mount/dir/../test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, GetMethodOutOfBaseDirMount) {
- auto res = cli_.Get("/mount/../www2/dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, GetMethodOutOfBaseDirMount2) {
- auto res = cli_.Get("/mount/dir/../../www2/dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, PostMethod303) {
- auto res = cli_.Post("/1", "body", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(303, res->status);
- EXPECT_EQ("/2", res->get_header_value("Location"));
- }
- TEST_F(ServerTest, PostMethod303Redirect) {
- cli_.set_follow_location(true);
- auto res = cli_.Post("/1", "body", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("redirected.", res->body);
- EXPECT_EQ("/2", res->location);
- }
- TEST_F(ServerTest, UserDefinedMIMETypeMapping) {
- auto res = cli_.Get("/dir/test.abcde");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
- EXPECT_EQ("abcde", res->body);
- }
- TEST_F(ServerTest, StaticFileRange) {
- auto res = cli_.Get("/dir/test.abcde", {{make_range_header({{2, 3}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("text/abcde", res->get_header_value("Content-Type"));
- EXPECT_EQ("2", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("cd"), res->body);
- }
- TEST_F(ServerTest, InvalidBaseDirMount) {
- EXPECT_EQ(false, svr_.set_mount_point("invalid_mount_point", "./www3"));
- }
- TEST_F(ServerTest, Binary) {
- std::vector<char> binary{0x00, 0x01, 0x02, 0x03};
- auto res = cli_.Post("/binary", binary.data(), binary.size(),
- "application/octet-stream");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(4U, res->body.size());
- res = cli_.Put("/binary", binary.data(), binary.size(),
- "application/octet-stream");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(4U, res->body.size());
- res = cli_.Patch("/binary", binary.data(), binary.size(),
- "application/octet-stream");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(4U, res->body.size());
- res = cli_.Delete("/binary", binary.data(), binary.size(),
- "application/octet-stream");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(4U, res->body.size());
- }
- TEST_F(ServerTest, BinaryString) {
- auto binary = std::string("\x00\x01\x02\x03", 4);
- auto res = cli_.Post("/binary", binary, "application/octet-stream");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(4U, res->body.size());
- res = cli_.Put("/binary", binary, "application/octet-stream");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(4U, res->body.size());
- res = cli_.Patch("/binary", binary, "application/octet-stream");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(4U, res->body.size());
- res = cli_.Delete("/binary", binary, "application/octet-stream");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ(4U, res->body.size());
- }
- TEST_F(ServerTest, EmptyRequest) {
- auto res = cli_.Get("");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Connection, res.error());
- }
- TEST_F(ServerTest, LongRequest) {
- std::string request;
- for (size_t i = 0; i < 545; i++) {
- request += "/TooLongRequest";
- }
- request += "OK";
- auto res = cli_.Get(request.c_str());
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, TooLongRequest) {
- std::string request;
- for (size_t i = 0; i < 545; i++) {
- request += "/TooLongRequest";
- }
- request += "_NG";
- auto res = cli_.Get(request.c_str());
- ASSERT_TRUE(res);
- EXPECT_EQ(414, res->status);
- }
- TEST_F(ServerTest, LongHeader) {
- Request req;
- req.method = "GET";
- req.path = "/hi";
- std::string host_and_port;
- host_and_port += HOST;
- host_and_port += ":";
- host_and_port += std::to_string(PORT);
- req.headers.emplace("Host", host_and_port.c_str());
- req.headers.emplace("Accept", "*/*");
- req.headers.emplace("User-Agent", "cpp-httplib/0.1");
- req.headers.emplace(
- "Header-Name",
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@");
- auto res = std::make_shared<Response>();
- auto error = Error::Success;
- auto ret = cli_.send(req, *res, error);
- ASSERT_TRUE(ret);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, LongQueryValue) {
- auto res = cli_.Get(LONG_QUERY_URL.c_str());
- ASSERT_TRUE(res);
- EXPECT_EQ(414, res->status);
- }
- TEST_F(ServerTest, TooLongHeader) {
- Request req;
- req.method = "GET";
- req.path = "/hi";
- std::string host_and_port;
- host_and_port += HOST;
- host_and_port += ":";
- host_and_port += std::to_string(PORT);
- req.headers.emplace("Host", host_and_port.c_str());
- req.headers.emplace("Accept", "*/*");
- req.headers.emplace("User-Agent", "cpp-httplib/0.1");
- req.headers.emplace(
- "Header-Name",
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- "@@@@@@@@@@@@@@@@@");
- auto res = std::make_shared<Response>();
- auto error = Error::Success;
- auto ret = cli_.send(req, *res, error);
- ASSERT_TRUE(ret);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, PercentEncoding) {
- auto res = cli_.Get("/e%6edwith%");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, PercentEncodingUnicode) {
- auto res = cli_.Get("/e%u006edwith%");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, InvalidPercentEncoding) {
- auto res = cli_.Get("/%endwith%");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, InvalidPercentEncodingUnicode) {
- auto res = cli_.Get("/%uendwith%");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, EndWithPercentCharacterInQuery) {
- auto res = cli_.Get("/hello?aaa=bbb%");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- }
- TEST_F(ServerTest, PlusSignEncoding) {
- auto res = cli_.Get("/a+%2Bb?a %2bb=a %2Bb");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("a +b", res->body);
- }
- TEST_F(ServerTest, MultipartFormData) {
- MultipartFormDataItems items = {
- {"text1", "text default", "", ""},
- {"text2", "aωb", "", ""},
- {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
- {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
- {"file3", "", "", "application/octet-stream"},
- {"file4", "", "", " application/json tmp-string "}};
- auto res = cli_.Post("/multipart", items);
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, MultipartFormDataMultiFileValues) {
- MultipartFormDataItems items = {
- {"text", "defalut text", "", ""},
- {"multi_text1", "aaaaa", "", ""},
- {"multi_text1", "bbbbb", "", ""},
- {"multi_file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
- {"multi_file1", "{\n \"world\", true\n}\n", "world.json", "application/json"},
- };
- auto res = cli_.Post("/multipart/multi_file_values", items);
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, CaseInsensitiveHeaderName) {
- auto res = cli_.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("content-type"));
- EXPECT_EQ("Hello World!", res->body);
- }
- TEST_F(ServerTest, CaseInsensitiveTransferEncoding) {
- Request req;
- req.method = "POST";
- req.path = "/chunked";
- std::string host_and_port;
- host_and_port += HOST;
- host_and_port += ":";
- host_and_port += std::to_string(PORT);
- req.headers.emplace("Host", host_and_port.c_str());
- req.headers.emplace("Accept", "*/*");
- req.headers.emplace("User-Agent", "cpp-httplib/0.1");
- req.headers.emplace("Content-Type", "text/plain");
- req.headers.emplace("Content-Length", "0");
- req.headers.emplace(
- "Transfer-Encoding",
- "Chunked"); // Note, "Chunked" rather than typical "chunked".
- // Client does not chunk, so make a chunked body manually.
- req.body = "4\r\ndech\r\nf\r\nunked post body\r\n0\r\n\r\n";
- auto res = std::make_shared<Response>();
- auto error = Error::Success;
- auto ret = cli_.send(req, *res, error);
- ASSERT_TRUE(ret);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, GetStreamed2) {
- auto res = cli_.Get("/streamed", {{make_range_header({{2, 3}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("2", res->get_header_value("Content-Length"));
- EXPECT_EQ(std::string("ab"), res->body);
- }
- TEST_F(ServerTest, GetStreamed) {
- auto res = cli_.Get("/streamed");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("6", res->get_header_value("Content-Length"));
- EXPECT_EQ(std::string("aaabbb"), res->body);
- }
- TEST_F(ServerTest, GetStreamedWithRange1) {
- auto res = cli_.Get("/streamed-with-range", {{make_range_header({{3, 5}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("3", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("def"), res->body);
- }
- TEST_F(ServerTest, GetStreamedWithRange2) {
- auto res = cli_.Get("/streamed-with-range", {{make_range_header({{1, -1}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("6", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("bcdefg"), res->body);
- }
- TEST_F(ServerTest, GetStreamedWithRangeSuffix1) {
- auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-3"}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("3", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("efg"), res->body);
- }
- TEST_F(ServerTest, GetStreamedWithRangeSuffix2) {
- auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-9999"}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("7", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("abcdefg"), res->body);
- }
- TEST_F(ServerTest, GetStreamedWithRangeError) {
- auto res = cli_.Get("/streamed-with-range",
- {{"Range", "bytes=92233720368547758079223372036854775806-"
- "92233720368547758079223372036854775807"}});
- ASSERT_TRUE(res);
- EXPECT_EQ(416, res->status);
- }
- TEST_F(ServerTest, GetRangeWithMaxLongLength) {
- auto res =
- cli_.Get("/with-range", {{"Range", "bytes=0-9223372036854775807"}});
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("7", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("abcdefg"), res->body);
- }
- TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
- auto res =
- cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("269", res->get_header_value("Content-Length"));
- EXPECT_EQ(false, res->has_header("Content-Range"));
- EXPECT_EQ(269U, res->body.size());
- }
- TEST_F(ServerTest, GetStreamedEndless) {
- uint64_t offset = 0;
- auto res = cli_.Get("/streamed-cancel",
- [&](const char * /*data*/, uint64_t data_length) {
- if (offset < 100) {
- offset += data_length;
- return true;
- }
- return false;
- });
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Canceled, res.error());
- }
- TEST_F(ServerTest, ClientStop) {
- std::vector<std::thread> threads;
- for (auto i = 0; i < 3; i++) {
- threads.emplace_back(thread([&]() {
- auto res = cli_.Get("/streamed-cancel",
- [&](const char *, uint64_t) { return true; });
- ASSERT_TRUE(!res);
- EXPECT_TRUE(res.error() == Error::Canceled ||
- res.error() == Error::Read || res.error() == Error::Write);
- }));
- }
- std::this_thread::sleep_for(std::chrono::seconds(2));
- while (cli_.is_socket_open()) {
- cli_.stop();
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- }
- for (auto &t : threads) {
- t.join();
- }
- }
- TEST_F(ServerTest, GetWithRange1) {
- auto res = cli_.Get("/with-range", {{make_range_header({{3, 5}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("3", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("def"), res->body);
- }
- TEST_F(ServerTest, GetWithRange2) {
- auto res = cli_.Get("/with-range", {{make_range_header({{1, -1}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("6", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("bcdefg"), res->body);
- }
- TEST_F(ServerTest, GetWithRange3) {
- auto res = cli_.Get("/with-range", {{make_range_header({{0, 0}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("1", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("a"), res->body);
- }
- TEST_F(ServerTest, GetWithRange4) {
- auto res = cli_.Get("/with-range", {{make_range_header({{-1, 2}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("2", res->get_header_value("Content-Length"));
- EXPECT_EQ(true, res->has_header("Content-Range"));
- EXPECT_EQ(std::string("fg"), res->body);
- }
- TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) {
- auto res = cli_.Get("/with-range", {{make_range_header({{10000, 20000}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(416, res->status);
- }
- TEST_F(ServerTest, GetWithRangeMultipart) {
- auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(206, res->status);
- EXPECT_EQ("269", res->get_header_value("Content-Length"));
- EXPECT_EQ(false, res->has_header("Content-Range"));
- EXPECT_EQ(269U, res->body.size());
- }
- TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
- auto res =
- cli_.Get("/with-range", {{make_range_header({{-1, 2}, {10000, 30000}})}});
- ASSERT_TRUE(res);
- EXPECT_EQ(416, res->status);
- }
- TEST_F(ServerTest, GetStreamedChunked) {
- auto res = cli_.Get("/streamed-chunked");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(std::string("123456789"), res->body);
- }
- TEST_F(ServerTest, GetStreamedChunked2) {
- auto res = cli_.Get("/streamed-chunked2");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(std::string("123456789"), res->body);
- }
- TEST_F(ServerTest, LargeChunkedPost) {
- Request req;
- req.method = "POST";
- req.path = "/large-chunked";
- std::string host_and_port;
- host_and_port += HOST;
- host_and_port += ":";
- host_and_port += std::to_string(PORT);
- req.headers.emplace("Host", host_and_port.c_str());
- req.headers.emplace("Accept", "*/*");
- req.headers.emplace("User-Agent", "cpp-httplib/0.1");
- req.headers.emplace("Content-Type", "text/plain");
- req.headers.emplace("Content-Length", "0");
- req.headers.emplace("Transfer-Encoding", "chunked");
- std::string long_string(30 * 1024u, 'a');
- std::string chunk = "7800\r\n" + long_string + "\r\n";
- // Attempt to make a large enough post to exceed OS buffers, to test that
- // the server handles short reads if the full chunk data isn't available.
- req.body = chunk + chunk + chunk + chunk + chunk + chunk + "0\r\n\r\n";
- auto res = std::make_shared<Response>();
- auto error = Error::Success;
- auto ret = cli_.send(req, *res, error);
- ASSERT_TRUE(ret);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, GetMethodRemoteAddr) {
- auto res = cli_.Get("/remote_addr");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_TRUE(res->body == "::1" || res->body == "127.0.0.1");
- }
- TEST_F(ServerTest, GetMethodLocalAddr) {
- auto res = cli_.Get("/local_addr");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_TRUE(res->body == std::string("::1:").append(to_string(PORT)) ||
- res->body == std::string("127.0.0.1:").append(to_string(PORT)));
- }
- TEST_F(ServerTest, HTTPResponseSplitting) {
- auto res = cli_.Get("/http_response_splitting");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, SlowRequest) {
- request_threads_.push_back(
- std::thread([=]() { auto res = cli_.Get("/slow"); }));
- request_threads_.push_back(
- std::thread([=]() { auto res = cli_.Get("/slow"); }));
- request_threads_.push_back(
- std::thread([=]() { auto res = cli_.Get("/slow"); }));
- }
- #if 0
- TEST_F(ServerTest, SlowPost) {
- char buffer[64 * 1024];
- memset(buffer, 0x42, sizeof(buffer));
- auto res = cli_.Post(
- "/slowpost", 64 * 1024 * 1024,
- [&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
- auto ret = sink.write(buffer, sizeof(buffer));
- EXPECT_TRUE(ret);
- return true;
- },
- "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, SlowPostFail) {
- char buffer[64 * 1024];
- memset(buffer, 0x42, sizeof(buffer));
- cli_.set_write_timeout(std::chrono::seconds(0));
- auto res = cli_.Post(
- "/slowpost", 64 * 1024 * 1024,
- [&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
- sink.write(buffer, sizeof(buffer));
- return true;
- },
- "text/plain");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Write, res.error());
- }
- #endif
- TEST_F(ServerTest, Put) {
- auto res = cli_.Put("/put", "PUT", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("PUT", res->body);
- }
- TEST_F(ServerTest, PutWithContentProvider) {
- auto res = cli_.Put(
- "/put", 3,
- [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
- sink.os << "PUT";
- return true;
- },
- "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("PUT", res->body);
- }
- TEST_F(ServerTest, PostWithContentProviderAbort) {
- auto res = cli_.Post(
- "/post", 42,
- [](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) {
- return false;
- },
- "text/plain");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Canceled, res.error());
- }
- TEST_F(ServerTest, PutWithContentProviderWithoutLength) {
- auto res = cli_.Put(
- "/put",
- [](size_t /*offset*/, DataSink &sink) {
- sink.os << "PUT";
- sink.done();
- return true;
- },
- "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("PUT", res->body);
- }
- TEST_F(ServerTest, PostWithContentProviderWithoutLengthAbort) {
- auto res = cli_.Post(
- "/post", [](size_t /*offset*/, DataSink & /*sink*/) { return false; },
- "text/plain");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Canceled, res.error());
- }
- #ifdef CPPHTTPLIB_ZLIB_SUPPORT
- TEST_F(ServerTest, PutWithContentProviderWithGzip) {
- cli_.set_compress(true);
- auto res = cli_.Put(
- "/put", 3,
- [](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
- sink.os << "PUT";
- return true;
- },
- "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("PUT", res->body);
- }
- TEST_F(ServerTest, PostWithContentProviderWithGzipAbort) {
- cli_.set_compress(true);
- auto res = cli_.Post(
- "/post", 42,
- [](size_t /*offset*/, size_t /*length*/, DataSink & /*sink*/) {
- return false;
- },
- "text/plain");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Canceled, res.error());
- }
- TEST_F(ServerTest, PutWithContentProviderWithoutLengthWithGzip) {
- cli_.set_compress(true);
- auto res = cli_.Put(
- "/put",
- [](size_t /*offset*/, DataSink &sink) {
- sink.os << "PUT";
- sink.done();
- return true;
- },
- "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("PUT", res->body);
- }
- TEST_F(ServerTest, PostWithContentProviderWithoutLengthWithGzipAbort) {
- cli_.set_compress(true);
- auto res = cli_.Post(
- "/post", [](size_t /*offset*/, DataSink & /*sink*/) { return false; },
- "text/plain");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::Canceled, res.error());
- }
- TEST_F(ServerTest, PutLargeFileWithGzip) {
- cli_.set_compress(true);
- auto res = cli_.Put("/put-large", LARGE_DATA, "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(LARGE_DATA, res->body);
- }
- TEST_F(ServerTest, PutLargeFileWithGzip2) {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- std::string s = std::string("https://") + HOST + ":" + std::to_string(PORT);
- Client cli(s.c_str());
- cli.enable_server_certificate_verification(false);
- #else
- std::string s = std::string("http://") + HOST + ":" + std::to_string(PORT);
- Client cli(s.c_str());
- #endif
- cli.set_compress(true);
- auto res = cli.Put("/put-large", LARGE_DATA, "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(LARGE_DATA, res->body);
- EXPECT_EQ(101942u, res.get_request_header_value<uint64_t>("Content-Length"));
- EXPECT_EQ("gzip", res.get_request_header_value("Content-Encoding"));
- }
- TEST_F(ServerTest, PutContentWithDeflate) {
- cli_.set_compress(false);
- Headers headers;
- headers.emplace("Content-Encoding", "deflate");
- // PUT in deflate format:
- auto res = cli_.Put("/put", headers,
- "\170\234\013\010\015\001\0\001\361\0\372", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("PUT", res->body);
- }
- TEST_F(ServerTest, GetStreamedChunkedWithGzip) {
- Headers headers;
- headers.emplace("Accept-Encoding", "gzip, deflate");
- auto res = cli_.Get("/streamed-chunked", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(std::string("123456789"), res->body);
- }
- TEST_F(ServerTest, GetStreamedChunkedWithGzip2) {
- Headers headers;
- headers.emplace("Accept-Encoding", "gzip, deflate");
- auto res = cli_.Get("/streamed-chunked2", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(std::string("123456789"), res->body);
- }
- TEST(GzipDecompressor, ChunkedDecompression) {
- std::string data;
- for (size_t i = 0; i < 32 * 1024; ++i) {
- data.push_back(static_cast<char>('a' + i % 26));
- }
- std::string compressed_data;
- {
- httplib::detail::gzip_compressor compressor;
- bool result = compressor.compress(
- data.data(), data.size(),
- /*last=*/true,
- [&](const char *compressed_data_chunk, size_t compressed_data_size) {
- compressed_data.insert(compressed_data.size(), compressed_data_chunk,
- compressed_data_size);
- return true;
- });
- ASSERT_TRUE(result);
- }
- std::string decompressed_data;
- {
- httplib::detail::gzip_decompressor decompressor;
- // Chunk size is chosen specificaly to have a decompressed chunk size equal
- // to 16384 bytes 16384 bytes is the size of decompressor output buffer
- size_t chunk_size = 130;
- for (size_t chunk_begin = 0; chunk_begin < compressed_data.size();
- chunk_begin += chunk_size) {
- size_t current_chunk_size =
- std::min(compressed_data.size() - chunk_begin, chunk_size);
- bool result = decompressor.decompress(
- compressed_data.data() + chunk_begin, current_chunk_size,
- [&](const char *decompressed_data_chunk,
- size_t decompressed_data_chunk_size) {
- decompressed_data.insert(decompressed_data.size(),
- decompressed_data_chunk,
- decompressed_data_chunk_size);
- return true;
- });
- ASSERT_TRUE(result);
- }
- }
- ASSERT_EQ(data, decompressed_data);
- }
- #ifdef _WIN32
- TEST(GzipDecompressor, LargeRandomData) {
- // prepare large random data that is difficult to be compressed and is
- // expected to have large size even when compressed
- std::random_device seed_gen;
- std::mt19937 random(seed_gen());
- constexpr auto large_size_byte = 4294967296UL; // 4GiB
- constexpr auto data_size = large_size_byte + 134217728UL; // + 128MiB
- std::vector<std::uint32_t> data(data_size / sizeof(std::uint32_t));
- std::generate(data.begin(), data.end(), [&]() { return random(); });
- // compress data over 4GiB
- std::string compressed_data;
- compressed_data.reserve(large_size_byte + 536870912UL); // + 512MiB reserved
- httplib::detail::gzip_compressor compressor;
- auto result = compressor.compress(reinterpret_cast<const char *>(data.data()),
- data.size() * sizeof(std::uint32_t), true,
- [&](const char *data, size_t size) {
- compressed_data.insert(
- compressed_data.size(), data, size);
- return true;
- });
- ASSERT_TRUE(result);
- // FIXME: compressed data size is expected to be greater than 4GiB,
- // but there is no guarantee
- // ASSERT_TRUE(compressed_data.size() >= large_size_byte);
- // decompress data over 4GiB
- std::string decompressed_data;
- decompressed_data.reserve(data_size);
- httplib::detail::gzip_decompressor decompressor;
- result = decompressor.decompress(
- compressed_data.data(), compressed_data.size(),
- [&](const char *data, size_t size) {
- decompressed_data.insert(decompressed_data.size(), data, size);
- return true;
- });
- ASSERT_TRUE(result);
- // compare
- ASSERT_EQ(data_size, decompressed_data.size());
- ASSERT_TRUE(std::memcmp(data.data(), decompressed_data.data(), data_size) ==
- 0);
- }
- #endif
- #endif
- #ifdef CPPHTTPLIB_BROTLI_SUPPORT
- TEST_F(ServerTest, GetStreamedChunkedWithBrotli) {
- Headers headers;
- headers.emplace("Accept-Encoding", "br");
- auto res = cli_.Get("/streamed-chunked", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(std::string("123456789"), res->body);
- }
- TEST_F(ServerTest, GetStreamedChunkedWithBrotli2) {
- Headers headers;
- headers.emplace("Accept-Encoding", "br");
- auto res = cli_.Get("/streamed-chunked2", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(std::string("123456789"), res->body);
- }
- #endif
- TEST_F(ServerTest, Patch) {
- auto res = cli_.Patch("/patch", "PATCH", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("PATCH", res->body);
- }
- TEST_F(ServerTest, Delete) {
- auto res = cli_.Delete("/delete");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("DELETE", res->body);
- }
- TEST_F(ServerTest, DeleteContentReceiver) {
- auto res = cli_.Delete("/delete-body", "content", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("content", res->body);
- }
- TEST_F(ServerTest, Options) {
- auto res = cli_.Options("*");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("GET, POST, HEAD, OPTIONS", res->get_header_value("Allow"));
- EXPECT_TRUE(res->body.empty());
- }
- TEST_F(ServerTest, URL) {
- auto res = cli_.Get("/request-target?aaa=bbb&ccc=ddd");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, ArrayParam) {
- auto res = cli_.Get("/array-param?array=value1&array=value2&array=value3");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, NoMultipleHeaders) {
- Headers headers = {{"Content-Length", "5"}};
- auto res = cli_.Post("/validate-no-multiple-headers", headers, "hello",
- "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, PostContentReceiver) {
- auto res = cli_.Post("/content_receiver", "content", "text/plain");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("content", res->body);
- }
- TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
- MultipartFormDataItems items = {
- {"text1", "text default", "", ""},
- {"text2", "aωb", "", ""},
- {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
- {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
- {"file3", "", "", "application/octet-stream"},
- };
- auto res = cli_.Post("/content_receiver", items);
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, PostMulitpartPlusBoundary) {
- MultipartFormDataItems items = {
- {"text1", "text default", "", ""},
- {"text2", "aωb", "", ""},
- {"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
- {"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
- {"file3", "", "", "application/octet-stream"},
- };
- auto boundary = std::string("+++++");
- std::string body;
- for (const auto &item : items) {
- body += "--" + boundary + "\r\n";
- body += "Content-Disposition: form-data; name=\"" + item.name + "\"";
- if (!item.filename.empty()) {
- body += "; filename=\"" + item.filename + "\"";
- }
- body += "\r\n";
- if (!item.content_type.empty()) {
- body += "Content-Type: " + item.content_type + "\r\n";
- }
- body += "\r\n";
- body += item.content + "\r\n";
- }
- body += "--" + boundary + "--\r\n";
- std::string content_type = "multipart/form-data; boundary=" + boundary;
- auto res = cli_.Post("/content_receiver", body, content_type.c_str());
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, PostContentReceiverGzip) {
- cli_.set_compress(true);
- auto res = cli_.Post("/content_receiver", "content", "text/plain");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("content", res->body);
- }
- TEST_F(ServerTest, PutContentReceiver) {
- auto res = cli_.Put("/content_receiver", "content", "text/plain");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("content", res->body);
- }
- TEST_F(ServerTest, PatchContentReceiver) {
- auto res = cli_.Patch("/content_receiver", "content", "text/plain");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- ASSERT_EQ("content", res->body);
- }
- TEST_F(ServerTest, PostQueryStringAndBody) {
- auto res =
- cli_.Post("/query-string-and-body?key=value", "content", "text/plain");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- TEST_F(ServerTest, HTTP2Magic) {
- Request req;
- req.method = "PRI";
- req.path = "*";
- req.body = "SM";
- auto res = std::make_shared<Response>();
- auto error = Error::Success;
- auto ret = cli_.send(req, *res, error);
- ASSERT_TRUE(ret);
- EXPECT_EQ(400, res->status);
- }
- TEST_F(ServerTest, KeepAlive) {
- auto res = cli_.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("Hello World!", res->body);
- res = cli_.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("Hello World!", res->body);
- res = cli_.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("Hello World!", res->body);
- res = cli_.Get("/not-exist");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- res = cli_.Post("/empty", "", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("empty", res->body);
- EXPECT_EQ("close", res->get_header_value("Connection"));
- res = cli_.Post(
- "/empty", 0, [&](size_t, size_t, DataSink &) { return true; },
- "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("empty", res->body);
- cli_.set_keep_alive(false);
- res = cli_.Get("/last-request");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("close", res->get_header_value("Connection"));
- }
- TEST_F(ServerTest, TooManyRedirect) {
- cli_.set_follow_location(true);
- auto res = cli_.Get("/redirect/0");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::ExceedRedirectCount, res.error());
- }
- #ifdef CPPHTTPLIB_ZLIB_SUPPORT
- TEST_F(ServerTest, Gzip) {
- Headers headers;
- headers.emplace("Accept-Encoding", "gzip, deflate");
- auto res = cli_.Get("/compress", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("33", res->get_header_value("Content-Length"));
- EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
- "7890123456789012345678901234567890",
- res->body);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
- auto res = cli_.Get("/compress");
- ASSERT_TRUE(res);
- EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("100", res->get_header_value("Content-Length"));
- EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
- "7890123456789012345678901234567890",
- res->body);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, GzipWithContentReceiver) {
- Headers headers;
- headers.emplace("Accept-Encoding", "gzip, deflate");
- std::string body;
- auto res = cli_.Get("/compress", headers,
- [&](const char *data, uint64_t data_length) {
- EXPECT_EQ(100U, data_length);
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("33", res->get_header_value("Content-Length"));
- EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
- "7890123456789012345678901234567890",
- body);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, GzipWithoutDecompressing) {
- Headers headers;
- headers.emplace("Accept-Encoding", "gzip, deflate");
- cli_.set_decompress(false);
- auto res = cli_.Get("/compress", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ("gzip", res->get_header_value("Content-Encoding"));
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("33", res->get_header_value("Content-Length"));
- EXPECT_EQ(33U, res->body.size());
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
- std::string body;
- auto res = cli_.Get("/compress", [&](const char *data, uint64_t data_length) {
- EXPECT_EQ(100U, data_length);
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("100", res->get_header_value("Content-Length"));
- EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
- "7890123456789012345678901234567890",
- body);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, NoGzip) {
- Headers headers;
- headers.emplace("Accept-Encoding", "gzip, deflate");
- auto res = cli_.Get("/nocompress", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ(false, res->has_header("Content-Encoding"));
- EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
- EXPECT_EQ("100", res->get_header_value("Content-Length"));
- EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
- "7890123456789012345678901234567890",
- res->body);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, NoGzipWithContentReceiver) {
- Headers headers;
- headers.emplace("Accept-Encoding", "gzip, deflate");
- std::string body;
- auto res = cli_.Get("/nocompress", headers,
- [&](const char *data, uint64_t data_length) {
- EXPECT_EQ(100U, data_length);
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- EXPECT_EQ(false, res->has_header("Content-Encoding"));
- EXPECT_EQ("application/octet-stream", res->get_header_value("Content-Type"));
- EXPECT_EQ("100", res->get_header_value("Content-Length"));
- EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
- "7890123456789012345678901234567890",
- body);
- EXPECT_EQ(200, res->status);
- }
- TEST_F(ServerTest, MultipartFormDataGzip) {
- MultipartFormDataItems items = {
- {"key1", "test", "", ""},
- {"key2", "--abcdefg123", "", ""},
- };
- cli_.set_compress(true);
- auto res = cli_.Post("/compress-multipart", items);
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- #endif
- #ifdef CPPHTTPLIB_BROTLI_SUPPORT
- TEST_F(ServerTest, Brotli) {
- Headers headers;
- headers.emplace("Accept-Encoding", "br");
- auto res = cli_.Get("/compress", headers);
- ASSERT_TRUE(res);
- EXPECT_EQ("br", res->get_header_value("Content-Encoding"));
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("19", res->get_header_value("Content-Length"));
- EXPECT_EQ("123456789012345678901234567890123456789012345678901234567890123456"
- "7890123456789012345678901234567890",
- res->body);
- EXPECT_EQ(200, res->status);
- }
- #endif
- // Sends a raw request to a server listening at HOST:PORT.
- static bool send_request(time_t read_timeout_sec, const std::string &req,
- std::string *resp = nullptr) {
- auto error = Error::Success;
- auto client_sock = detail::create_client_socket(
- HOST, "", PORT, AF_UNSPEC, false, nullptr,
- /*connection_timeout_sec=*/5, 0,
- /*read_timeout_sec=*/5, 0,
- /*write_timeout_sec=*/5, 0, std::string(), error);
- if (client_sock == INVALID_SOCKET) { return false; }
- auto ret = detail::process_client_socket(
- client_sock, read_timeout_sec, 0, 0, 0, [&](Stream &strm) {
- if (req.size() !=
- static_cast<size_t>(strm.write(req.data(), req.size()))) {
- return false;
- }
- char buf[512];
- detail::stream_line_reader line_reader(strm, buf, sizeof(buf));
- while (line_reader.getline()) {
- if (resp) { *resp += line_reader.ptr(); }
- }
- return true;
- });
- detail::close_socket(client_sock);
- return ret;
- }
- TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
- Server svr;
- std::string header_value;
- svr.Get("/validate-ws-in-headers", [&](const Request &req, Response &res) {
- header_value = req.get_header_value("foo");
- res.set_content("ok", "text/plain");
- });
- thread t = thread([&] { svr.listen(HOST, PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Only space and horizontal tab are whitespace. Make sure other whitespace-
- // like characters are not treated the same - use vertical tab and escape.
- const std::string req = "GET /validate-ws-in-headers HTTP/1.1\r\n"
- "foo: \t \v bar \x1B\t \r\n"
- "Connection: close\r\n"
- "\r\n";
- ASSERT_TRUE(send_request(5, req));
- svr.stop();
- t.join();
- EXPECT_EQ(header_value, "\v bar \x1B");
- }
- // Sends a raw request and verifies that there isn't a crash or exception.
- static void test_raw_request(const std::string &req,
- std::string *out = nullptr) {
- Server svr;
- svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
- res.set_content("ok", "text/plain");
- });
- svr.Put("/put_hi", [&](const Request & /*req*/, Response &res) {
- res.set_content("ok", "text/plain");
- });
- // Server read timeout must be longer than the client read timeout for the
- // bug to reproduce, probably to force the server to process a request
- // without a trailing blank line.
- const time_t client_read_timeout_sec = 1;
- svr.set_read_timeout(std::chrono::seconds(client_read_timeout_sec + 1));
- bool listen_thread_ok = false;
- thread t = thread([&] { listen_thread_ok = svr.listen(HOST, PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- ASSERT_TRUE(send_request(client_read_timeout_sec, req, out));
- svr.stop();
- t.join();
- EXPECT_TRUE(listen_thread_ok);
- }
- TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
- // A certain header line causes an exception if the header property is parsed
- // naively with a single regex. This occurs with libc++ but not libstdc++.
- test_raw_request(
- "GET /hi HTTP/1.1\r\n"
- " : "
- " "
- " ");
- }
- TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity2) {
- // A certain header line causes an exception if the header property *name* is
- // parsed with a regular expression starting with "(.+?):" - this is a non-
- // greedy matcher and requires backtracking when there are a lot of ":"
- // characters.
- // This occurs with libc++ but not libstdc++.
- test_raw_request(
- "GET /hi HTTP/1.1\r\n"
- ":-:::::::::::::::::::::::::::-::::::::::::::::::::::::@-&&&&&&&&&&&"
- "--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&"
- "&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-:::::"
- "::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-::::::::::::::::::::::::"
- ":::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::"
- "::::::::-:::::::::::::::::@-&&&&&&&--:::::::-::::::::::::::::::::::"
- ":::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::"
- "::::::::::-:::::::::::::::::@-&&&&&::::::::::::-:::::::::::::::::@-"
- "&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::::::"
- ":@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::"
- "::::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::@-&&"
- "&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@"
- "::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&"
- "--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&"
- "&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&"
- "&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@-&&"
- "&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::::@"
- "-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::::"
- "::@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::"
- ":::::@-&&&&&&&&&&&::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-::::::"
- ":::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-:::"
- "::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&--:::::::-"
- ":::::::::::::::::::::::::::::-:::::::::::::::::@-&&&&&&&&&&&---&&:&"
- "&&.0------------:-:::::::::::::::::::::::::::::-:::::::::::::::::@-"
- "&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-::::::::::::::::"
- ":@-&&&&&&&&&&&--:::::::-:::::::::::::::::::::::::::::-:::::::::::::"
- "::::@-&&&&&&&&&&&---&&:&&&.0------------O--------\rH PUTHTTP/1.1\r\n"
- "&&&%%%");
- }
- TEST(ServerRequestParsingTest, ExcessiveWhitespaceInUnparseableHeaderLine) {
- // Make sure this doesn't crash the server.
- // In a previous version of the header line regex, the "\r" rendered the line
- // unparseable and the regex engine repeatedly backtracked, trying to look for
- // a new position where the leading white space ended and the field value
- // began.
- // The crash occurs with libc++ but not libstdc++.
- test_raw_request("GET /hi HTTP/1.1\r\n"
- "a:" +
- std::string(2000, ' ') + '\r' + std::string(20, 'z') +
- "\r\n"
- "\r\n");
- }
- TEST(ServerRequestParsingTest, InvalidFirstChunkLengthInRequest) {
- std::string out;
- test_raw_request("PUT /put_hi HTTP/1.1\r\n"
- "Content-Type: text/plain\r\n"
- "Transfer-Encoding: chunked\r\n"
- "\r\n"
- "nothex\r\n",
- &out);
- EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
- }
- TEST(ServerRequestParsingTest, InvalidSecondChunkLengthInRequest) {
- std::string out;
- test_raw_request("PUT /put_hi HTTP/1.1\r\n"
- "Content-Type: text/plain\r\n"
- "Transfer-Encoding: chunked\r\n"
- "\r\n"
- "3\r\n"
- "xyz\r\n"
- "NaN\r\n",
- &out);
- EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
- }
- TEST(ServerRequestParsingTest, ChunkLengthTooHighInRequest) {
- std::string out;
- test_raw_request("PUT /put_hi HTTP/1.1\r\n"
- "Content-Type: text/plain\r\n"
- "Transfer-Encoding: chunked\r\n"
- "\r\n"
- // Length is too large for 64 bits.
- "1ffffffffffffffff\r\n"
- "xyz\r\n",
- &out);
- EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
- }
- TEST(ServerRequestParsingTest, InvalidHeaderTextWithExtraCR) {
- test_raw_request("GET /hi HTTP/1.1\r\n"
- "Content-Type: text/plain\r\n\r");
- }
- TEST(ServerRequestParsingTest, InvalidSpaceInURL) {
- std::string out;
- test_raw_request("GET /h i HTTP/1.1\r\n\r\n", &out);
- EXPECT_EQ("HTTP/1.1 400 Bad Request", out.substr(0, 24));
- }
- TEST(ServerStopTest, StopServerWithChunkedTransmission) {
- Server svr;
- svr.Get("/events", [](const Request & /*req*/, Response &res) {
- res.set_header("Cache-Control", "no-cache");
- res.set_chunked_content_provider("text/event-stream", [](size_t offset,
- DataSink &sink) {
- char buffer[27];
- auto size = static_cast<size_t>(sprintf(buffer, "data:%zd\n\n", offset));
- auto ret = sink.write(buffer, size);
- EXPECT_TRUE(ret);
- std::this_thread::sleep_for(std::chrono::seconds(1));
- return true;
- });
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- Client client(HOST, PORT);
- const Headers headers = {{"Accept", "text/event-stream"}};
- auto get_thread = std::thread([&client, &headers]() {
- auto res = client.Get(
- "/events", headers,
- [](const char * /*data*/, size_t /*len*/) -> bool { return true; });
- });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(2));
- svr.stop();
- listen_thread.join();
- get_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(StreamingTest, NoContentLengthStreaming) {
- Server svr;
- svr.Get("/stream", [](const Request & /*req*/, Response &res) {
- res.set_content_provider("text/plain", [](size_t offset, DataSink &sink) {
- if (offset < 6) {
- sink.os << (offset < 3 ? "a" : "b");
- } else {
- sink.done();
- }
- return true;
- });
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- Client client(HOST, PORT);
- auto get_thread = std::thread([&client]() {
- std::string s;
- auto res =
- client.Get("/stream", [&s](const char *data, size_t len) -> bool {
- s += std::string(data, len);
- return true;
- });
- EXPECT_EQ("aaabbb", s);
- });
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
- svr.stop();
- listen_thread.join();
- get_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(MountTest, Unmount) {
- Server svr;
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli("localhost", PORT);
- svr.set_mount_point("/mount2", "./www2");
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- res = cli.Get("/mount2/dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- svr.set_mount_point("/", "./www");
- res = cli.Get("/dir/");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- svr.remove_mount_point("/");
- res = cli.Get("/dir/");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- svr.remove_mount_point("/mount2");
- res = cli.Get("/mount2/dir/test.html");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #ifndef CPPHTTPLIB_NO_EXCEPTIONS
- TEST(ExceptionTest, ThrowExceptionInHandler) {
- Server svr;
- svr.Get("/exception", [&](const Request & /*req*/, Response & /*res*/) {
- throw std::runtime_error("exception...");
- });
- svr.Get("/unknown", [&](const Request & /*req*/, Response & /*res*/) {
- throw std::runtime_error("exception\r\n...");
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli("localhost", PORT);
- {
- auto res = cli.Get("/exception");
- ASSERT_TRUE(res);
- EXPECT_EQ(500, res->status);
- ASSERT_TRUE(res->has_header("EXCEPTION_WHAT"));
- EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT"));
- }
- {
- auto res = cli.Get("/unknown");
- ASSERT_TRUE(res);
- EXPECT_EQ(500, res->status);
- ASSERT_TRUE(res->has_header("EXCEPTION_WHAT"));
- EXPECT_EQ("exception\\r\\n...", res->get_header_value("EXCEPTION_WHAT"));
- }
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #endif
- TEST(KeepAliveTest, ReadTimeout) {
- Server svr;
- svr.Get("/a", [&](const Request & /*req*/, Response &res) {
- std::this_thread::sleep_for(std::chrono::seconds(2));
- res.set_content("a", "text/plain");
- });
- svr.Get("/b", [&](const Request & /*req*/, Response &res) {
- res.set_content("b", "text/plain");
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli("localhost", PORT);
- cli.set_keep_alive(true);
- cli.set_read_timeout(std::chrono::seconds(1));
- auto resa = cli.Get("/a");
- ASSERT_TRUE(!resa);
- EXPECT_EQ(Error::Read, resa.error());
- auto resb = cli.Get("/b");
- ASSERT_TRUE(resb);
- EXPECT_EQ(200, resb->status);
- EXPECT_EQ("b", resb->body);
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(KeepAliveTest, Issue1041) {
- const auto resourcePath = "/hi";
- Server svr;
- svr.set_keep_alive_timeout(3);
- svr.Get(resourcePath, [](const httplib::Request &, httplib::Response &res) {
- res.set_content("Hello World!", "text/plain");
- });
- auto a2 = std::async(std::launch::async, [&svr] { svr.listen(HOST, PORT); });
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
- Client cli(HOST, PORT);
- cli.set_keep_alive(true);
- auto result = cli.Get(resourcePath);
- ASSERT_TRUE(result);
- EXPECT_EQ(200, result->status);
- std::this_thread::sleep_for(std::chrono::seconds(5));
- result = cli.Get(resourcePath);
- ASSERT_TRUE(result);
- EXPECT_EQ(200, result->status);
- svr.stop();
- a2.wait();
- }
- TEST(ClientProblemDetectionTest, ContentProvider) {
- Server svr;
- size_t content_length = 1024 * 1024;
- svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
- res.set_content_provider(
- content_length, "text/plain",
- [&](size_t offset, size_t length, DataSink &sink) {
- auto out_len = std::min(length, static_cast<size_t>(1024));
- std::string out(out_len, '@');
- sink.write(out.data(), out_len);
- return offset < 4096;
- },
- [](bool success) { ASSERT_FALSE(success); });
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli("localhost", PORT);
- auto res = cli.Get("/hi", [&](const char * /*data*/, size_t /*data_length*/) {
- return false;
- });
- ASSERT_FALSE(res);
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(ErrorHandlerWithContentProviderTest, ErrorHandler) {
- Server svr;
- svr.set_error_handler([](Request const &, Response &res) -> void {
- res.set_chunked_content_provider(
- "text/plain", [](std::size_t const, DataSink &sink) -> bool {
- sink.os << "hello";
- sink.os << "world";
- sink.done();
- return true;
- });
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli("localhost", PORT);
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(404, res->status);
- EXPECT_EQ("helloworld", res->body);
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(GetWithParametersTest, GetWithParameters) {
- Server svr;
- svr.Get("/", [&](const Request &req, Response &) {
- EXPECT_EQ("world", req.get_param_value("hello"));
- EXPECT_EQ("world2", req.get_param_value("hello2"));
- EXPECT_EQ("world3", req.get_param_value("hello3"));
- });
- svr.Get("/params", [&](const Request &req, Response &) {
- EXPECT_EQ("world", req.get_param_value("hello"));
- EXPECT_EQ("world2", req.get_param_value("hello2"));
- EXPECT_EQ("world3", req.get_param_value("hello3"));
- });
- svr.Get(R"(/resources/([a-z0-9\\-]+))", [&](const Request &req, Response &) {
- EXPECT_EQ("resource-id", req.matches[1]);
- EXPECT_EQ("foo", req.get_param_value("param1"));
- EXPECT_EQ("bar", req.get_param_value("param2"));
- });
- auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli(HOST, PORT);
- Params params;
- params.emplace("hello", "world");
- params.emplace("hello2", "world2");
- params.emplace("hello3", "world3");
- auto res = cli.Get("/", params, Headers{});
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- {
- Client cli(HOST, PORT);
- auto res = cli.Get("/params?hello=world&hello2=world2&hello3=world3");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- {
- Client cli(HOST, PORT);
- auto res = cli.Get("/resources/resource-id?param1=foo¶m2=bar");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(GetWithParametersTest, GetWithParameters2) {
- Server svr;
- svr.Get("/", [&](const Request &req, Response &res) {
- auto text = req.get_param_value("hello");
- res.set_content(text, "text/plain");
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli("localhost", PORT);
- Params params;
- params.emplace("hello", "world");
- std::string body;
- auto res = cli.Get("/", params, Headers{},
- [&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("world", body);
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) {
- Client cli("httpbin.org");
- cli.set_default_headers({make_range_header({{1, 10}})});
- cli.set_connection_timeout(5);
- {
- auto res = cli.Get("/range/32");
- ASSERT_TRUE(res);
- EXPECT_EQ("bcdefghijk", res->body);
- EXPECT_EQ(206, res->status);
- }
- {
- auto res = cli.Get("/range/32");
- ASSERT_TRUE(res);
- EXPECT_EQ("bcdefghijk", res->body);
- EXPECT_EQ(206, res->status);
- }
- }
- TEST(ServerDefaultHeadersTest, DefaultHeaders) {
- Server svr;
- svr.set_default_headers({{"Hello", "World"}});
- svr.Get("/", [&](const Request & /*req*/, Response &res) {
- res.set_content("ok", "text/plain");
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli("localhost", PORT);
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("ok", res->body);
- EXPECT_EQ("World", res->get_header_value("Hello"));
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(KeepAliveTest, ReadTimeoutSSL) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/a", [&](const Request & /*req*/, Response &res) {
- std::this_thread::sleep_for(std::chrono::seconds(2));
- res.set_content("a", "text/plain");
- });
- svr.Get("/b", [&](const Request & /*req*/, Response &res) {
- res.set_content("b", "text/plain");
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- SSLClient cli("localhost", PORT);
- cli.enable_server_certificate_verification(false);
- cli.set_keep_alive(true);
- cli.set_read_timeout(std::chrono::seconds(1));
- auto resa = cli.Get("/a");
- ASSERT_TRUE(!resa);
- EXPECT_EQ(Error::Read, resa.error());
- auto resb = cli.Get("/b");
- ASSERT_TRUE(resb);
- EXPECT_EQ(200, resb->status);
- EXPECT_EQ("b", resb->body);
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #endif
- class ServerTestWithAI_PASSIVE : public ::testing::Test {
- protected:
- ServerTestWithAI_PASSIVE()
- : cli_(HOST, PORT)
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- ,
- svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
- #endif
- {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- cli_.enable_server_certificate_verification(false);
- #endif
- }
- virtual void SetUp() {
- svr_.Get("/hi", [&](const Request & /*req*/, Response &res) {
- res.set_content("Hello World!", "text/plain");
- });
- t_ = thread(
- [&]() { ASSERT_TRUE(svr_.listen(std::string(), PORT, AI_PASSIVE)); });
- while (!svr_.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- virtual void TearDown() {
- svr_.stop();
- t_.join();
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli_;
- SSLServer svr_;
- #else
- Client cli_;
- Server svr_;
- #endif
- thread t_;
- };
- TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) {
- auto res = cli_.Get("/hi");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
- EXPECT_EQ("Hello World!", res->body);
- }
- class ServerUpDownTest : public ::testing::Test {
- protected:
- ServerUpDownTest() : cli_(HOST, PORT) {}
- virtual void SetUp() {
- t_ = thread([&]() {
- svr_.bind_to_any_port(HOST);
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
- ASSERT_TRUE(svr_.listen_after_bind());
- });
- while (!svr_.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- virtual void TearDown() {
- svr_.stop();
- t_.join();
- }
- Client cli_;
- Server svr_;
- thread t_;
- };
- TEST_F(ServerUpDownTest, QuickStartStop) {
- // Should not crash, especially when run with
- // --gtest_filter=ServerUpDownTest.QuickStartStop --gtest_repeat=1000
- }
- class PayloadMaxLengthTest : public ::testing::Test {
- protected:
- PayloadMaxLengthTest()
- : cli_(HOST, PORT)
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- ,
- svr_(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE)
- #endif
- {
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- cli_.enable_server_certificate_verification(false);
- #endif
- }
- virtual void SetUp() {
- svr_.set_payload_max_length(8);
- svr_.Post("/test", [&](const Request & /*req*/, Response &res) {
- res.set_content("test", "text/plain");
- });
- t_ = thread([&]() { ASSERT_TRUE(svr_.listen(HOST, PORT)); });
- while (!svr_.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- }
- virtual void TearDown() {
- svr_.stop();
- t_.join();
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- SSLClient cli_;
- SSLServer svr_;
- #else
- Client cli_;
- Server svr_;
- #endif
- thread t_;
- };
- TEST_F(PayloadMaxLengthTest, ExceedLimit) {
- auto res = cli_.Post("/test", "123456789", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(413, res->status);
- res = cli_.Post("/test", "12345678", "text/plain");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(SSLClientTest, UpdateCAStore) {
- httplib::SSLClient httplib_client("www.google.com");
- auto ca_store_1 = X509_STORE_new();
- X509_STORE_load_locations(ca_store_1, "/etc/ssl/certs/ca-certificates.crt",
- nullptr);
- httplib_client.set_ca_cert_store(ca_store_1);
- auto ca_store_2 = X509_STORE_new();
- X509_STORE_load_locations(ca_store_2, "/etc/ssl/certs/ca-certificates.crt",
- nullptr);
- httplib_client.set_ca_cert_store(ca_store_2);
- }
- TEST(SSLClientTest, ServerNameIndication_Online) {
- SSLClient cli("httpbin.org", 443);
- auto res = cli.Get("/get");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- TEST(SSLClientTest, ServerCertificateVerification1_Online) {
- SSLClient cli("google.com");
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- ASSERT_EQ(301, res->status);
- }
- TEST(SSLClientTest, ServerCertificateVerification2_Online) {
- SSLClient cli("google.com");
- cli.enable_server_certificate_verification(true);
- cli.set_ca_cert_path("hello");
- auto res = cli.Get("/");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::SSLLoadingCerts, res.error());
- }
- TEST(SSLClientTest, ServerCertificateVerification3_Online) {
- SSLClient cli("google.com");
- cli.set_ca_cert_path(CA_CERT_FILE);
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- ASSERT_EQ(301, res->status);
- }
- TEST(SSLClientTest, ServerCertificateVerification4) {
- SSLServer svr(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE);
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/test", [&](const Request &, Response &res) {
- res.set_content("test", "text/plain");
- svr.stop();
- ASSERT_TRUE(true);
- });
- thread t = thread([&]() { ASSERT_TRUE(svr.listen("127.0.0.1", PORT)); });
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- SSLClient cli("127.0.0.1", PORT);
- cli.set_ca_cert_path(SERVER_CERT2_FILE);
- cli.enable_server_certificate_verification(true);
- cli.set_connection_timeout(30);
- auto res = cli.Get("/test");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- t.join();
- }
- TEST(SSLClientTest, WildcardHostNameMatch_Online) {
- SSLClient cli("www.youtube.com");
- cli.set_ca_cert_path(CA_CERT_FILE);
- cli.enable_server_certificate_verification(true);
- cli.set_follow_location(true);
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- #if 0
- TEST(SSLClientTest, SetInterfaceWithINET6) {
- auto cli = std::make_shared<httplib::Client>("https://httpbin.org");
- ASSERT_TRUE(cli != nullptr);
- cli->set_address_family(AF_INET6);
- cli->set_interface("en0");
- auto res = cli->Get("/get");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- #endif
- TEST(SSLClientServerTest, ClientCertPresent) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
- CLIENT_CA_CERT_DIR);
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/test", [&](const Request &req, Response &res) {
- res.set_content("test", "text/plain");
- svr.stop();
- ASSERT_TRUE(true);
- auto peer_cert = SSL_get_peer_certificate(req.ssl);
- ASSERT_TRUE(peer_cert != nullptr);
- auto subject_name = X509_get_subject_name(peer_cert);
- ASSERT_TRUE(subject_name != nullptr);
- std::string common_name;
- {
- char name[BUFSIZ];
- auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
- name, sizeof(name));
- common_name.assign(name, static_cast<size_t>(name_len));
- }
- EXPECT_EQ("Common Name", common_name);
- X509_free(peer_cert);
- });
- thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
- cli.enable_server_certificate_verification(false);
- cli.set_connection_timeout(30);
- auto res = cli.Get("/test");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- t.join();
- }
- #if !defined(_WIN32) || defined(OPENSSL_USE_APPLINK)
- TEST(SSLClientServerTest, MemoryClientCertPresent) {
- X509 *server_cert;
- EVP_PKEY *server_private_key;
- X509_STORE *client_ca_cert_store;
- X509 *client_cert;
- EVP_PKEY *client_private_key;
- FILE *f = fopen(SERVER_CERT_FILE, "r+");
- server_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
- fclose(f);
- f = fopen(SERVER_PRIVATE_KEY_FILE, "r+");
- server_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr);
- fclose(f);
- f = fopen(CLIENT_CA_CERT_FILE, "r+");
- client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
- client_ca_cert_store = X509_STORE_new();
- X509_STORE_add_cert(client_ca_cert_store, client_cert);
- X509_free(client_cert);
- fclose(f);
- f = fopen(CLIENT_CERT_FILE, "r+");
- client_cert = PEM_read_X509(f, nullptr, nullptr, nullptr);
- fclose(f);
- f = fopen(CLIENT_PRIVATE_KEY_FILE, "r+");
- client_private_key = PEM_read_PrivateKey(f, nullptr, nullptr, nullptr);
- fclose(f);
- SSLServer svr(server_cert, server_private_key, client_ca_cert_store);
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/test", [&](const Request &req, Response &res) {
- res.set_content("test", "text/plain");
- svr.stop();
- ASSERT_TRUE(true);
- auto peer_cert = SSL_get_peer_certificate(req.ssl);
- ASSERT_TRUE(peer_cert != nullptr);
- auto subject_name = X509_get_subject_name(peer_cert);
- ASSERT_TRUE(subject_name != nullptr);
- std::string common_name;
- {
- char name[BUFSIZ];
- auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
- name, sizeof(name));
- common_name.assign(name, static_cast<size_t>(name_len));
- }
- EXPECT_EQ("Common Name", common_name);
- X509_free(peer_cert);
- });
- thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- SSLClient cli(HOST, PORT, client_cert, client_private_key);
- cli.enable_server_certificate_verification(false);
- cli.set_connection_timeout(30);
- auto res = cli.Get("/test");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- X509_free(server_cert);
- EVP_PKEY_free(server_private_key);
- X509_free(client_cert);
- EVP_PKEY_free(client_private_key);
- t.join();
- }
- #endif
- TEST(SSLClientServerTest, ClientCertMissing) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
- CLIENT_CA_CERT_DIR);
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/test", [&](const Request &, Response &) { ASSERT_TRUE(false); });
- thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- SSLClient cli(HOST, PORT);
- auto res = cli.Get("/test");
- cli.set_connection_timeout(30);
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::SSLServerVerification, res.error());
- svr.stop();
- t.join();
- }
- TEST(SSLClientServerTest, TrustDirOptional) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE);
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/test", [&](const Request &, Response &res) {
- res.set_content("test", "text/plain");
- svr.stop();
- });
- thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
- cli.enable_server_certificate_verification(false);
- cli.set_connection_timeout(30);
- auto res = cli.Get("/test");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- t.join();
- }
- TEST(SSLClientServerTest, SSLConnectTimeout) {
- class NoListenSSLServer : public SSLServer {
- public:
- NoListenSSLServer(const char *cert_path, const char *private_key_path,
- const char *client_ca_cert_file_path,
- const char *client_ca_cert_dir_path = nullptr)
- : SSLServer(cert_path, private_key_path, client_ca_cert_file_path,
- client_ca_cert_dir_path),
- stop_(false) {}
- bool stop_;
- private:
- bool process_and_close_socket(socket_t /*sock*/) override {
- // Don't create SSL context
- while (!stop_) {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- return true;
- }
- };
- NoListenSSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
- CLIENT_CA_CERT_FILE);
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/test", [&](const Request &, Response &res) {
- res.set_content("test", "text/plain");
- });
- thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
- cli.enable_server_certificate_verification(false);
- cli.set_connection_timeout(1);
- auto res = cli.Get("/test");
- ASSERT_TRUE(!res);
- EXPECT_EQ(Error::SSLConnection, res.error());
- svr.stop_ = true;
- svr.stop();
- t.join();
- }
- TEST(SSLClientServerTest, CustomizeServerSSLCtx) {
- auto setup_ssl_ctx_callback = [](SSL_CTX &ssl_ctx) {
- SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_COMPRESSION);
- SSL_CTX_set_options(&ssl_ctx,
- SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
- SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_SSLv2);
- SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_SSLv3);
- SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_TLSv1);
- SSL_CTX_set_options(&ssl_ctx, SSL_OP_NO_TLSv1_1);
- auto ciphers = "ECDHE-RSA-AES128-SHA256:"
- "ECDHE-DSS-AES128-SHA256:"
- "ECDHE-RSA-AES256-SHA256:"
- "ECDHE-DSS-AES256-SHA256:";
- SSL_CTX_set_cipher_list(&ssl_ctx, ciphers);
- if (SSL_CTX_use_certificate_chain_file(&ssl_ctx, SERVER_CERT_FILE) != 1 ||
- SSL_CTX_use_PrivateKey_file(&ssl_ctx, SERVER_PRIVATE_KEY_FILE,
- SSL_FILETYPE_PEM) != 1) {
- return false;
- }
- SSL_CTX_load_verify_locations(&ssl_ctx, CLIENT_CA_CERT_FILE,
- CLIENT_CA_CERT_DIR);
- SSL_CTX_set_verify(
- &ssl_ctx,
- SSL_VERIFY_PEER |
- SSL_VERIFY_FAIL_IF_NO_PEER_CERT, // SSL_VERIFY_CLIENT_ONCE,
- nullptr);
- return true;
- };
- SSLServer svr(setup_ssl_ctx_callback);
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/test", [&](const Request &req, Response &res) {
- res.set_content("test", "text/plain");
- svr.stop();
- ASSERT_TRUE(true);
- auto peer_cert = SSL_get_peer_certificate(req.ssl);
- ASSERT_TRUE(peer_cert != nullptr);
- auto subject_name = X509_get_subject_name(peer_cert);
- ASSERT_TRUE(subject_name != nullptr);
- std::string common_name;
- {
- char name[BUFSIZ];
- auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
- name, sizeof(name));
- common_name.assign(name, static_cast<size_t>(name_len));
- }
- EXPECT_EQ("Common Name", common_name);
- X509_free(peer_cert);
- });
- thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
- cli.enable_server_certificate_verification(false);
- cli.set_connection_timeout(30);
- auto res = cli.Get("/test");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- t.join();
- }
- // Disabled due to the out-of-memory problem on GitHub Actions Workflows
- TEST(SSLClientServerTest, DISABLED_LargeDataTransfer) {
- // prepare large data
- std::random_device seed_gen;
- std::mt19937 random(seed_gen());
- constexpr auto large_size_byte = 2147483648UL + 1048576UL; // 2GiB + 1MiB
- std::vector<std::uint32_t> binary(large_size_byte / sizeof(std::uint32_t));
- std::generate(binary.begin(), binary.end(), [&random]() { return random(); });
- // server
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
- ASSERT_TRUE(svr.is_valid());
- svr.Post("/binary", [&](const Request &req, Response &res) {
- EXPECT_EQ(large_size_byte, req.body.size());
- EXPECT_EQ(0, std::memcmp(binary.data(), req.body.data(), large_size_byte));
- res.set_content(req.body, "application/octet-stream");
- });
- auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // client POST
- SSLClient cli("localhost", PORT);
- cli.enable_server_certificate_verification(false);
- cli.set_read_timeout(std::chrono::seconds(100));
- cli.set_write_timeout(std::chrono::seconds(100));
- auto res = cli.Post("/binary", reinterpret_cast<char *>(binary.data()),
- large_size_byte, "application/octet-stream");
- // compare
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(large_size_byte, res->body.size());
- EXPECT_EQ(0, std::memcmp(binary.data(), res->body.data(), large_size_byte));
- // cleanup
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #endif
- #ifdef _WIN32
- TEST(CleanupTest, WSACleanup) {
- int ret = WSACleanup();
- ASSERT_EQ(0, ret);
- }
- #endif
- #ifndef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(NoSSLSupport, SimpleInterface) {
- ASSERT_ANY_THROW(Client cli("https://yahoo.com"));
- }
- #endif
- #ifndef CPPHTTPLIB_NO_EXCEPTIONS
- TEST(InvalidScheme, SimpleInterface) {
- ASSERT_ANY_THROW(Client cli("scheme://yahoo.com"));
- }
- #endif
- TEST(NoScheme, SimpleInterface) {
- Client cli("yahoo.com:80");
- ASSERT_TRUE(cli.is_valid());
- }
- TEST(SendAPI, SimpleInterface_Online) {
- Client cli("http://yahoo.com");
- Request req;
- req.method = "GET";
- req.path = "/";
- auto res = cli.send(req);
- ASSERT_TRUE(res);
- EXPECT_EQ(301, res->status);
- }
- TEST(ClientImplMethods, GetSocketTest) {
- httplib::Server svr;
- svr.Get("/", [&](const httplib::Request & /*req*/, httplib::Response &res) {
- res.status = 200;
- });
- auto thread = std::thread([&]() { svr.listen("127.0.0.1", 3333); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(5));
- }
- {
- httplib::Client cli("http://127.0.0.1:3333");
- cli.set_keep_alive(true);
- // Use the behavior of cpp-httplib of opening the connection
- // only when the first request happens. If that changes,
- // this test would be obsolete.
- EXPECT_EQ(cli.socket(), INVALID_SOCKET);
- // This also implicitly tests the server. But other tests would fail much
- // earlier than this one to be considered.
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- ASSERT_TRUE(cli.socket() != INVALID_SOCKET);
- }
- svr.stop();
- thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- // Disabled due to out-of-memory problem on GitHub Actions
- #ifdef _WIN64
- TEST(ServerLargeContentTest, DISABLED_SendLargeContent) {
- // allocate content size larger than 2GB in memory
- const size_t content_size = 2LL * 1024LL * 1024LL * 1024LL + 1LL;
- char *content = (char *)malloc(content_size);
- ASSERT_TRUE(content);
- Server svr;
- svr.Get("/foo", [=](const httplib::Request &req, httplib::Response &resp) {
- resp.set_content(content, content_size, "application/octet-stream");
- });
- auto listen_thread = std::thread([&svr]() { svr.listen(HOST, PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- // Give GET time to get a few messages.
- std::this_thread::sleep_for(std::chrono::seconds(1));
- Client cli(HOST, PORT);
- auto res = cli.Get("/foo");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(content_size, res->body.length());
- free(content);
- svr.stop();
- listen_thread.join();
- ASSERT_FALSE(svr.is_running());
- }
- #endif
- #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
- TEST(YahooRedirectTest2, SimpleInterface_Online) {
- Client cli("http://yahoo.com");
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(301, res->status);
- cli.set_follow_location(true);
- res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("https://yahoo.com/", res->location);
- }
- TEST(YahooRedirectTest3, SimpleInterface_Online) {
- Client cli("https://yahoo.com");
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(301, res->status);
- cli.set_follow_location(true);
- res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("https://www.yahoo.com/", res->location);
- }
- TEST(YahooRedirectTest3, NewResultInterface_Online) {
- Client cli("https://yahoo.com");
- auto res = cli.Get("/");
- ASSERT_TRUE(res);
- ASSERT_FALSE(!res);
- ASSERT_TRUE(res);
- ASSERT_FALSE(res == nullptr);
- ASSERT_TRUE(res != nullptr);
- EXPECT_EQ(Error::Success, res.error());
- EXPECT_EQ(301, res.value().status);
- EXPECT_EQ(301, (*res).status);
- EXPECT_EQ(301, res->status);
- cli.set_follow_location(true);
- res = cli.Get("/");
- ASSERT_TRUE(res);
- EXPECT_EQ(Error::Success, res.error());
- EXPECT_EQ(200, res.value().status);
- EXPECT_EQ(200, (*res).status);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ("https://www.yahoo.com/", res->location);
- }
- #ifdef CPPHTTPLIB_BROTLI_SUPPORT
- TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) {
- Client cli("https://cdnjs.cloudflare.com");
- auto res =
- cli.Get("/ajax/libs/jquery/3.5.1/jquery.js", {{"Accept-Encoding", "br"}});
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- EXPECT_EQ(287630U, res->body.size());
- EXPECT_EQ("application/javascript; charset=utf-8",
- res->get_header_value("Content-Type"));
- }
- #endif
- TEST(HttpsToHttpRedirectTest, SimpleInterface_Online) {
- Client cli("https://nghttp2.org");
- cli.set_follow_location(true);
- auto res =
- cli.Get("/httpbin/"
- "redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) {
- Client cli("https://nghttp2.org");
- cli.set_follow_location(true);
- Params params;
- params.emplace("url", "http://www.google.com");
- params.emplace("status_code", "302");
- auto res = cli.Get("/httpbin/redirect-to", params, Headers{});
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) {
- Client cli("https://nghttp2.org");
- cli.set_follow_location(true);
- Params params;
- params.emplace("url", "http://www.google.com");
- auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{});
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- }
- TEST(HttpToHttpsRedirectTest, CertFile) {
- Server svr;
- ASSERT_TRUE(svr.is_valid());
- svr.Get("/index", [&](const Request &, Response &res) {
- res.set_redirect("https://127.0.0.1:1235/index");
- svr.stop();
- });
- SSLServer ssl_svr(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE);
- ASSERT_TRUE(ssl_svr.is_valid());
- ssl_svr.Get("/index", [&](const Request &, Response &res) {
- res.set_content("test", "text/plain");
- ssl_svr.stop();
- });
- thread t = thread([&]() { ASSERT_TRUE(svr.listen("127.0.0.1", PORT)); });
- thread t2 = thread([&]() { ASSERT_TRUE(ssl_svr.listen("127.0.0.1", 1235)); });
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- Client cli("127.0.0.1", PORT);
- cli.set_ca_cert_path(SERVER_CERT2_FILE);
- cli.enable_server_certificate_verification(true);
- cli.set_follow_location(true);
- cli.set_connection_timeout(30);
- auto res = cli.Get("/index");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- t.join();
- t2.join();
- }
- TEST(MultipartFormDataTest, LargeData) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
- svr.Post("/post", [&](const Request &req, Response & /*res*/,
- const ContentReader &content_reader) {
- if (req.is_multipart_form_data()) {
- MultipartFormDataItems files;
- content_reader(
- [&](const MultipartFormData &file) {
- files.push_back(file);
- return true;
- },
- [&](const char *data, size_t data_length) {
- files.back().content.append(data, data_length);
- return true;
- });
- EXPECT_TRUE(std::string(files[0].name) == "document");
- EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size());
- EXPECT_TRUE(files[0].filename == "2MB_data");
- EXPECT_TRUE(files[0].content_type == "application/octet-stream");
- EXPECT_TRUE(files[1].name == "hello");
- EXPECT_TRUE(files[1].content == "world");
- EXPECT_TRUE(files[1].filename == "");
- EXPECT_TRUE(files[1].content_type == "");
- } else {
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- }
- });
- auto t = std::thread([&]() { svr.listen("localhost", 8080); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- std::string data(1024 * 1024 * 2, '.');
- std::stringstream buffer;
- buffer << data;
- Client cli("https://localhost:8080");
- cli.enable_server_certificate_verification(false);
- MultipartFormDataItems items{
- {"document", buffer.str(), "2MB_data", "application/octet-stream"},
- {"hello", "world", "", ""},
- };
- auto res = cli.Post("/post", items);
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- svr.stop();
- t.join();
- }
- TEST(MultipartFormDataTest, DataProviderItems) {
- std::random_device seed_gen;
- std::mt19937 random(seed_gen());
- std::string rand1;
- rand1.resize(1000);
- std::generate(rand1.begin(), rand1.end(), [&]() { return random(); });
- std::string rand2;
- rand2.resize(3000);
- std::generate(rand2.begin(), rand2.end(), [&]() { return random(); });
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
- svr.Post("/post-none", [&](const Request &req, Response & /*res*/,
- const ContentReader &content_reader) {
- ASSERT_FALSE(req.is_multipart_form_data());
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- EXPECT_EQ(body, "");
- });
- svr.Post("/post-items", [&](const Request &req, Response & /*res*/,
- const ContentReader &content_reader) {
- ASSERT_TRUE(req.is_multipart_form_data());
- MultipartFormDataItems files;
- content_reader(
- [&](const MultipartFormData &file) {
- files.push_back(file);
- return true;
- },
- [&](const char *data, size_t data_length) {
- files.back().content.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(files.size() == 2);
- EXPECT_EQ(std::string(files[0].name), "name1");
- EXPECT_EQ(files[0].content, "Testing123");
- EXPECT_EQ(files[0].filename, "filename1");
- EXPECT_EQ(files[0].content_type, "application/octet-stream");
- EXPECT_EQ(files[1].name, "name2");
- EXPECT_EQ(files[1].content, "Testing456");
- EXPECT_EQ(files[1].filename, "");
- EXPECT_EQ(files[1].content_type, "");
- });
- svr.Post("/post-providers", [&](const Request &req, Response & /*res*/,
- const ContentReader &content_reader) {
- ASSERT_TRUE(req.is_multipart_form_data());
- MultipartFormDataItems files;
- content_reader(
- [&](const MultipartFormData &file) {
- files.push_back(file);
- return true;
- },
- [&](const char *data, size_t data_length) {
- files.back().content.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(files.size() == 2);
- EXPECT_EQ(files[0].name, "name3");
- EXPECT_EQ(files[0].content, rand1);
- EXPECT_EQ(files[0].filename, "filename3");
- EXPECT_EQ(files[0].content_type, "");
- EXPECT_EQ(files[1].name, "name4");
- EXPECT_EQ(files[1].content, rand2);
- EXPECT_EQ(files[1].filename, "filename4");
- EXPECT_EQ(files[1].content_type, "");
- });
- svr.Post("/post-both", [&](const Request &req, Response & /*res*/,
- const ContentReader &content_reader) {
- ASSERT_TRUE(req.is_multipart_form_data());
- MultipartFormDataItems files;
- content_reader(
- [&](const MultipartFormData &file) {
- files.push_back(file);
- return true;
- },
- [&](const char *data, size_t data_length) {
- files.back().content.append(data, data_length);
- return true;
- });
- ASSERT_TRUE(files.size() == 4);
- EXPECT_EQ(std::string(files[0].name), "name1");
- EXPECT_EQ(files[0].content, "Testing123");
- EXPECT_EQ(files[0].filename, "filename1");
- EXPECT_EQ(files[0].content_type, "application/octet-stream");
- EXPECT_EQ(files[1].name, "name2");
- EXPECT_EQ(files[1].content, "Testing456");
- EXPECT_EQ(files[1].filename, "");
- EXPECT_EQ(files[1].content_type, "");
- EXPECT_EQ(files[2].name, "name3");
- EXPECT_EQ(files[2].content, rand1);
- EXPECT_EQ(files[2].filename, "filename3");
- EXPECT_EQ(files[2].content_type, "");
- EXPECT_EQ(files[3].name, "name4");
- EXPECT_EQ(files[3].content, rand2);
- EXPECT_EQ(files[3].filename, "filename4");
- EXPECT_EQ(files[3].content_type, "");
- });
- auto t = std::thread([&]() { svr.listen("localhost", 8080); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- Client cli("https://localhost:8080");
- cli.enable_server_certificate_verification(false);
- MultipartFormDataItems items{
- {"name1", "Testing123", "filename1", "application/octet-stream"},
- {"name2", "Testing456", "", ""}, // not a file
- };
- {
- auto res = cli.Post("/post-none", {}, {}, {});
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- MultipartFormDataProviderItems providers;
- {
- auto res =
- cli.Post("/post-items", {}, items, providers); // empty providers
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- providers.push_back({"name3",
- [&](size_t offset, httplib::DataSink &sink) -> bool {
- // test the offset is given correctly at each step
- if (!offset)
- sink.os.write(rand1.data(), 30);
- else if (offset == 30)
- sink.os.write(rand1.data() + 30, 300);
- else if (offset == 330)
- sink.os.write(rand1.data() + 330, 670);
- else if (offset == rand1.size())
- sink.done();
- return true;
- },
- "filename3",
- {}});
- providers.push_back({"name4",
- [&](size_t offset, httplib::DataSink &sink) -> bool {
- // test the offset is given correctly at each step
- if (!offset)
- sink.os.write(rand2.data(), 2000);
- else if (offset == 2000)
- sink.os.write(rand2.data() + 2000, 1);
- else if (offset == 2001)
- sink.os.write(rand2.data() + 2001, 999);
- else if (offset == rand2.size())
- sink.done();
- return true;
- },
- "filename4",
- {}});
- {
- auto res = cli.Post("/post-providers", {}, {}, providers);
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- {
- auto res = cli.Post("/post-both", {}, items, providers);
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- }
- svr.stop();
- t.join();
- }
- TEST(MultipartFormDataTest, BadHeader) {
- Server svr;
- svr.Post("/post", [&](const Request & /*req*/, Response &res) {
- res.set_content("ok", "text/plain");
- });
- thread t = thread([&] { svr.listen(HOST, PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- const std::string body =
- "This is the preamble. It is to be ignored, though it\r\n"
- "is a handy place for composition agents to include an\r\n"
- "explanatory note to non-MIME conformant readers.\r\n"
- "\r\n"
- "\r\n"
- "--simple boundary\r\n"
- "Content-Disposition: form-data; name=\"field1\"\r\n"
- ": BAD...\r\n"
- "\r\n"
- "value1\r\n"
- "--simple boundary\r\n"
- "Content-Disposition: form-data; name=\"field2\"; "
- "filename=\"example.txt\"\r\n"
- "\r\n"
- "value2\r\n"
- "--simple boundary--\r\n"
- "This is the epilogue. It is also to be ignored.\r\n";
- std::string content_type =
- R"(multipart/form-data; boundary="simple boundary")";
- Client cli(HOST, PORT);
- auto res = cli.Post("/post", body, content_type.c_str());
- ASSERT_TRUE(res);
- EXPECT_EQ(400, res->status);
- svr.stop();
- t.join();
- }
- TEST(MultipartFormDataTest, WithPreamble) {
- Server svr;
- svr.Post("/post", [&](const Request & /*req*/, Response &res) {
- res.set_content("ok", "text/plain");
- });
- thread t = thread([&] { svr.listen(HOST, PORT); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- const std::string body =
- "This is the preamble. It is to be ignored, though it\r\n"
- "is a handy place for composition agents to include an\r\n"
- "explanatory note to non-MIME conformant readers.\r\n"
- "\r\n"
- "\r\n"
- "--simple boundary\r\n"
- "Content-Disposition: form-data; name=\"field1\"\r\n"
- "\r\n"
- "value1\r\n"
- "--simple boundary\r\n"
- "Content-Disposition: form-data; name=\"field2\"; "
- "filename=\"example.txt\"\r\n"
- "\r\n"
- "value2\r\n"
- "--simple boundary--\r\n"
- "This is the epilogue. It is also to be ignored.\r\n";
- std::string content_type =
- R"(multipart/form-data; boundary="simple boundary")";
- Client cli(HOST, PORT);
- auto res = cli.Post("/post", body, content_type.c_str());
- ASSERT_TRUE(res);
- EXPECT_EQ(200, res->status);
- svr.stop();
- t.join();
- }
- TEST(MultipartFormDataTest, PostCustomBoundary) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
- svr.Post("/post_customboundary", [&](const Request &req, Response & /*res*/,
- const ContentReader &content_reader) {
- if (req.is_multipart_form_data()) {
- MultipartFormDataItems files;
- content_reader(
- [&](const MultipartFormData &file) {
- files.push_back(file);
- return true;
- },
- [&](const char *data, size_t data_length) {
- files.back().content.append(data, data_length);
- return true;
- });
- EXPECT_TRUE(std::string(files[0].name) == "document");
- EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size());
- EXPECT_TRUE(files[0].filename == "2MB_data");
- EXPECT_TRUE(files[0].content_type == "application/octet-stream");
- EXPECT_TRUE(files[1].name == "hello");
- EXPECT_TRUE(files[1].content == "world");
- EXPECT_TRUE(files[1].filename == "");
- EXPECT_TRUE(files[1].content_type == "");
- } else {
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- }
- });
- auto t = std::thread([&]() { svr.listen("localhost", 8080); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- std::string data(1024 * 1024 * 2, '.');
- std::stringstream buffer;
- buffer << data;
- Client cli("https://localhost:8080");
- cli.enable_server_certificate_verification(false);
- MultipartFormDataItems items{
- {"document", buffer.str(), "2MB_data", "application/octet-stream"},
- {"hello", "world", "", ""},
- };
- auto res = cli.Post("/post_customboundary", {}, items, "abc-abc");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- svr.stop();
- t.join();
- }
- TEST(MultipartFormDataTest, PostInvalidBoundaryChars) {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- std::string data(1024 * 1024 * 2, '&');
- std::stringstream buffer;
- buffer << data;
- Client cli("https://localhost:8080");
- MultipartFormDataItems items{
- {"document", buffer.str(), "2MB_data", "application/octet-stream"},
- {"hello", "world", "", ""},
- };
- for (const char &c : " \t\r\n") {
- auto res =
- cli.Post("/invalid_boundary", {}, items, string("abc123").append(1, c));
- ASSERT_EQ(Error::UnsupportedMultipartBoundaryChars, res.error());
- ASSERT_FALSE(res);
- }
- }
- TEST(MultipartFormDataTest, PutFormData) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
- svr.Put("/put", [&](const Request &req, const Response & /*res*/,
- const ContentReader &content_reader) {
- if (req.is_multipart_form_data()) {
- MultipartFormDataItems files;
- content_reader(
- [&](const MultipartFormData &file) {
- files.push_back(file);
- return true;
- },
- [&](const char *data, size_t data_length) {
- files.back().content.append(data, data_length);
- return true;
- });
- EXPECT_TRUE(std::string(files[0].name) == "document");
- EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size());
- EXPECT_TRUE(files[0].filename == "2MB_data");
- EXPECT_TRUE(files[0].content_type == "application/octet-stream");
- EXPECT_TRUE(files[1].name == "hello");
- EXPECT_TRUE(files[1].content == "world");
- EXPECT_TRUE(files[1].filename == "");
- EXPECT_TRUE(files[1].content_type == "");
- } else {
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- }
- });
- auto t = std::thread([&]() { svr.listen("localhost", 8080); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- std::string data(1024 * 1024 * 2, '&');
- std::stringstream buffer;
- buffer << data;
- Client cli("https://localhost:8080");
- cli.enable_server_certificate_verification(false);
- MultipartFormDataItems items{
- {"document", buffer.str(), "2MB_data", "application/octet-stream"},
- {"hello", "world", "", ""},
- };
- auto res = cli.Put("/put", items);
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- svr.stop();
- t.join();
- }
- TEST(MultipartFormDataTest, PutFormDataCustomBoundary) {
- SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
- svr.Put("/put_customboundary",
- [&](const Request &req, const Response & /*res*/,
- const ContentReader &content_reader) {
- if (req.is_multipart_form_data()) {
- MultipartFormDataItems files;
- content_reader(
- [&](const MultipartFormData &file) {
- files.push_back(file);
- return true;
- },
- [&](const char *data, size_t data_length) {
- files.back().content.append(data, data_length);
- return true;
- });
- EXPECT_TRUE(std::string(files[0].name) == "document");
- EXPECT_EQ(size_t(1024 * 1024 * 2), files[0].content.size());
- EXPECT_TRUE(files[0].filename == "2MB_data");
- EXPECT_TRUE(files[0].content_type == "application/octet-stream");
- EXPECT_TRUE(files[1].name == "hello");
- EXPECT_TRUE(files[1].content == "world");
- EXPECT_TRUE(files[1].filename == "");
- EXPECT_TRUE(files[1].content_type == "");
- } else {
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- }
- });
- auto t = std::thread([&]() { svr.listen("localhost", 8080); });
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- std::this_thread::sleep_for(std::chrono::seconds(1));
- {
- std::string data(1024 * 1024 * 2, '&');
- std::stringstream buffer;
- buffer << data;
- Client cli("https://localhost:8080");
- cli.enable_server_certificate_verification(false);
- MultipartFormDataItems items{
- {"document", buffer.str(), "2MB_data", "application/octet-stream"},
- {"hello", "world", "", ""},
- };
- auto res = cli.Put("/put_customboundary", {}, items, "abc-abc_");
- ASSERT_TRUE(res);
- ASSERT_EQ(200, res->status);
- }
- svr.stop();
- t.join();
- }
- TEST(MultipartFormDataTest, PutInvalidBoundaryChars) {
- std::this_thread::sleep_for(std::chrono::seconds(1));
- std::string data(1024 * 1024 * 2, '&');
- std::stringstream buffer;
- buffer << data;
- Client cli("https://localhost:8080");
- cli.enable_server_certificate_verification(false);
- MultipartFormDataItems items{
- {"document", buffer.str(), "2MB_data", "application/octet-stream"},
- {"hello", "world", "", ""},
- };
- for (const char &c : " \t\r\n") {
- auto res = cli.Put("/put", {}, items, string("abc123").append(1, c));
- ASSERT_EQ(Error::UnsupportedMultipartBoundaryChars, res.error());
- ASSERT_FALSE(res);
- }
- }
- #endif
- #ifndef _WIN32
- class UnixSocketTest : public ::testing::Test {
- protected:
- void TearDown() override { std::remove(pathname_.c_str()); }
- void client_GET(const std::string &addr) {
- httplib::Client cli{addr};
- cli.set_address_family(AF_UNIX);
- ASSERT_TRUE(cli.is_valid());
- const auto &result = cli.Get(pattern_);
- ASSERT_TRUE(result) << "error: " << result.error();
- const auto &resp = result.value();
- EXPECT_EQ(resp.status, 200);
- EXPECT_EQ(resp.body, content_);
- }
- const std::string pathname_{"./httplib-server.sock"};
- const std::string pattern_{"/hi"};
- const std::string content_{"Hello World!"};
- };
- TEST_F(UnixSocketTest, pathname) {
- httplib::Server svr;
- svr.Get(pattern_, [&](const httplib::Request &, httplib::Response &res) {
- res.set_content(content_, "text/plain");
- });
- std::thread t{[&] {
- ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80));
- }};
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- ASSERT_TRUE(svr.is_running());
- client_GET(pathname_);
- svr.stop();
- t.join();
- }
- #if defined(__linux__) || \
- /* __APPLE__ */ (defined(SOL_LOCAL) && defined(SO_PEERPID))
- TEST_F(UnixSocketTest, PeerPid) {
- httplib::Server svr;
- std::string remote_port_val;
- svr.Get(pattern_, [&](const httplib::Request &req, httplib::Response &res) {
- res.set_content(content_, "text/plain");
- remote_port_val = req.get_header_value("REMOTE_PORT");
- });
- std::thread t{[&] {
- ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(pathname_, 80));
- }};
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- ASSERT_TRUE(svr.is_running());
- client_GET(pathname_);
- EXPECT_EQ(std::to_string(getpid()), remote_port_val);
- svr.stop();
- t.join();
- }
- #endif
- #ifdef __linux__
- TEST_F(UnixSocketTest, abstract) {
- constexpr char svr_path[]{"\x00httplib-server.sock"};
- const std::string abstract_addr{svr_path, sizeof(svr_path) - 1};
- httplib::Server svr;
- svr.Get(pattern_, [&](const httplib::Request &, httplib::Response &res) {
- res.set_content(content_, "text/plain");
- });
- std::thread t{[&] {
- ASSERT_TRUE(svr.set_address_family(AF_UNIX).listen(abstract_addr, 80));
- }};
- while (!svr.is_running()) {
- std::this_thread::sleep_for(std::chrono::milliseconds(1));
- }
- ASSERT_TRUE(svr.is_running());
- client_GET(abstract_addr);
- svr.stop();
- t.join();
- }
- #endif
- TEST(SocketStream, is_writable_UNIX) {
- int fds[2];
- ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
- const auto asSocketStream = [&](socket_t fd,
- std::function<bool(Stream &)> func) {
- return detail::process_client_socket(fd, 0, 0, 0, 0, func);
- };
- asSocketStream(fds[0], [&](Stream &s0) {
- EXPECT_EQ(s0.socket(), fds[0]);
- EXPECT_TRUE(s0.is_writable());
- EXPECT_EQ(0, close(fds[1]));
- EXPECT_FALSE(s0.is_writable());
- return true;
- });
- EXPECT_EQ(0, close(fds[0]));
- }
- TEST(SocketStream, is_writable_INET) {
- sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(PORT + 1);
- addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- int disconnected_svr_sock = -1;
- std::thread svr{[&] {
- const int s = socket(AF_INET, SOCK_STREAM, 0);
- ASSERT_LE(0, s);
- ASSERT_EQ(0, ::bind(s, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)));
- ASSERT_EQ(0, listen(s, 1));
- ASSERT_LE(0, disconnected_svr_sock = accept(s, nullptr, nullptr));
- ASSERT_EQ(0, close(s));
- }};
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- std::thread cli{[&] {
- const int s = socket(AF_INET, SOCK_STREAM, 0);
- ASSERT_LE(0, s);
- ASSERT_EQ(0, connect(s, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)));
- ASSERT_EQ(0, close(s));
- }};
- cli.join();
- svr.join();
- ASSERT_NE(disconnected_svr_sock, -1);
- const auto asSocketStream = [&](socket_t fd,
- std::function<bool(Stream &)> func) {
- return detail::process_client_socket(fd, 0, 0, 0, 0, func);
- };
- asSocketStream(disconnected_svr_sock, [&](Stream &ss) {
- EXPECT_EQ(ss.socket(), disconnected_svr_sock);
- EXPECT_FALSE(ss.is_writable());
- return true;
- });
- ASSERT_EQ(0, close(disconnected_svr_sock));
- }
- #endif // #ifndef _WIN32
- TEST(TaskQueueTest, IncreaseAtomicInteger) {
- static constexpr unsigned int number_of_task{1000000};
- std::atomic_uint count{0};
- std::unique_ptr<TaskQueue> task_queue{
- new ThreadPool{CPPHTTPLIB_THREAD_POOL_COUNT}};
- for (unsigned int i = 0; i < number_of_task; ++i) {
- task_queue->enqueue(
- [&count] { count.fetch_add(1, std::memory_order_relaxed); });
- }
- EXPECT_NO_THROW(task_queue->shutdown());
- EXPECT_EQ(number_of_task, count.load());
- }
|