1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462 |
- #include <cstdint>
- #include <cstdlib>
- #include <gtest/gtest.h>
- #include <chrono>
- #include <string>
- #include <cpr/cpr.h>
- #include <curl/curl.h>
- #include "httpServer.hpp"
- using namespace cpr;
- using namespace std::chrono_literals;
- static HttpServer* server = new HttpServer();
- std::chrono::milliseconds sleep_time{50};
- std::chrono::seconds zero{0};
- bool write_data(std::string /*data*/, intptr_t /*userdata*/) {
- return true;
- }
- TEST(RedirectTests, TemporaryDefaultRedirectTest) {
- Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
- Session session;
- session.SetUrl(url);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(RedirectTests, NoTemporaryRedirectTest) {
- Url url{server->GetBaseUrl() + "/temporary_redirect.html"};
- Session session;
- session.SetUrl(url);
- session.SetRedirect(Redirect(false));
- Response response = session.Get();
- std::string expected_text{"Moved Temporarily"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{}, response.header["content-type"]);
- EXPECT_EQ(302, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(RedirectTests, PermanentDefaultRedirectTest) {
- Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
- Session session;
- session.SetUrl(url);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(RedirectTests, NoPermanentRedirectTest) {
- Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
- Session session;
- session.SetUrl(url);
- session.SetRedirect(Redirect(false));
- Response response = session.Get();
- std::string expected_text{"Moved Permanently"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{}, response.header["content-type"]);
- EXPECT_EQ(301, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(MaxRedirectsTests, ZeroMaxRedirectsSuccessTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetRedirect(Redirect(0L));
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(MaxRedirectsTests, ZeroMaxRedirectsFailureTest) {
- Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
- Session session;
- session.SetUrl(url);
- session.SetRedirect(Redirect(0L));
- Response response = session.Get();
- EXPECT_EQ(std::string{}, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{}, response.header["content-type"]);
- EXPECT_EQ(301, response.status_code);
- EXPECT_EQ(ErrorCode::TOO_MANY_REDIRECTS, response.error.code);
- }
- TEST(MaxRedirectsTests, OneMaxRedirectsSuccessTest) {
- Url url{server->GetBaseUrl() + "/permanent_redirect.html"};
- Session session;
- session.SetUrl(url);
- session.SetRedirect(Redirect(1L));
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(MaxRedirectsTests, OneMaxRedirectsFailureTest) {
- Url url{server->GetBaseUrl() + "/two_redirects.html"};
- Session session;
- session.SetUrl(url);
- session.SetRedirect(Redirect(1L));
- Response response = session.Get();
- EXPECT_EQ(std::string{}, response.text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/permanent_redirect.html"}, response.url);
- EXPECT_EQ(std::string{}, response.header["content-type"]);
- EXPECT_EQ(301, response.status_code);
- EXPECT_EQ(ErrorCode::TOO_MANY_REDIRECTS, response.error.code);
- }
- TEST(MaxRedirectsTests, TwoMaxRedirectsSuccessTest) {
- Url url{server->GetBaseUrl() + "/two_redirects.html"};
- Session session;
- session.SetUrl(url);
- session.SetRedirect(Redirect(2L));
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(MultipleGetTests, BasicMultipleGetTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- for (size_t i = 0; i < 100; ++i) {
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(MultipleGetTests, UrlChangeMultipleGetTest) {
- Session session;
- {
- Url url{server->GetBaseUrl() + "/hello.html"};
- session.SetUrl(url);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Url url{server->GetBaseUrl() + "/basic.json"};
- session.SetUrl(url);
- Response response = session.Get();
- std::string expected_text{
- "[\n"
- " {\n"
- " \"first_key\": \"first_value\",\n"
- " \"second_key\": \"second_value\"\n"
- " }\n"
- "]"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(MultipleGetTests, HeaderMultipleGetTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Session session;
- session.SetUrl(url);
- session.SetHeader(Header{{"hello", "world"}});
- for (size_t i = 0; i < 100; ++i) {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(std::string{"world"}, response.header["hello"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(MultipleGetTests, HeaderChangeMultipleGetTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Session session;
- session.SetUrl(url);
- session.SetHeader(Header{{"hello", "world"}});
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(std::string{"world"}, response.header["hello"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- session.SetHeader(Header{{"key", "value"}});
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(std::string{"value"}, response.header["key"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(MultipleGetTests, ParameterMultipleGetTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetParameters({{"hello", "world"}});
- for (size_t i = 0; i < 100; ++i) {
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{url + "?hello=world"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(MultipleGetTests, ParameterChangeMultipleGetTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetParameters({{"hello", "world"}});
- {
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{url + "?hello=world"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- session.SetUrl(url);
- session.SetParameters({{"key", "value"}});
- {
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{url + "?key=value"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(MultipleGetTests, BasicAuthenticationMultipleGetTest) {
- Url url{server->GetBaseUrl() + "/basic_auth.html"};
- Session session;
- session.SetUrl(url);
- session.SetAuth(Authentication{"user", "password", AuthMode::BASIC});
- for (size_t i = 0; i < 100; ++i) {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(MultipleGetTests, BasicAuthenticationChangeMultipleGetTest) {
- Url url{server->GetBaseUrl() + "/basic_auth.html"};
- Session session;
- session.SetUrl(url);
- session.SetAuth(Authentication{"user", "password", AuthMode::BASIC});
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- session.SetAuth(Authentication{"user", "bad_password", AuthMode::BASIC});
- {
- Response response = session.Get();
- EXPECT_EQ(std::string{"Unauthorized"}, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
- EXPECT_EQ(401, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- session.SetAuth(Authentication{"bad_user", "password", AuthMode::BASIC});
- {
- Response response = session.Get();
- EXPECT_EQ(std::string{"Unauthorized"}, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
- EXPECT_EQ(401, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(ParameterTests, ParameterSingleTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- Parameters parameters{{"hello", "world"}};
- session.SetParameters(parameters);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{url + "?hello=world"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(ParameterTests, ParameterMultipleTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- Parameters parameters{{"hello", "world"}, {"key", "value"}};
- session.SetParameters(parameters);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(Url{url + "?hello=world&key=value"}, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(FullRequestUrlTest, GetFullRequestUrlNoParametersTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- std::string expected_text{server->GetBaseUrl() + "/hello.html"};
- EXPECT_EQ(expected_text, session.GetFullRequestUrl());
- }
- TEST(FullRequestUrlTest, GetFullRequestUrlOneParameterTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- Parameters parameters{{"hello", "world"}};
- session.SetParameters(parameters);
- std::string expected_text{server->GetBaseUrl() + "/hello.html" + "?hello=world"};
- EXPECT_EQ(expected_text, session.GetFullRequestUrl());
- }
- TEST(FullRequestUrlTest, GetFullRequestUrlMultipleParametersTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- Parameters parameters{{"hello", "world"}, {"key", "value"}};
- session.SetParameters(parameters);
- std::string expected_text{server->GetBaseUrl() + "/hello.html" + "?hello=world&key=value"};
- EXPECT_EQ(expected_text, session.GetFullRequestUrl());
- }
- TEST(TimeoutTests, SetTimeoutTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetTimeout(0L);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(TimeoutTests, SetTimeoutLongTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetTimeout(10000L);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(TimeoutTests, SetTimeoutLowSpeed) {
- Url url{server->GetBaseUrl() + "/low_speed_timeout.html"};
- Session session;
- session.SetUrl(url);
- session.SetTimeout(1000);
- Response response = session.Get();
- EXPECT_EQ(url, response.url);
- // Do not check for the HTTP status code, since libcurl always provides the status code of the header if it was received
- EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
- }
- TEST(TimeoutTests, SetChronoTimeoutTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetTimeout(std::chrono::milliseconds{0});
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(TimeoutTests, SetChronoTimeoutLongTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetTimeout(std::chrono::milliseconds{10000});
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- // Do not check for the HTTP status code, since libcurl always provides the status code of the header if it was received
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(TimeoutTests, SetChronoLiteralTimeoutTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetTimeout(2s);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(TimeoutTests, SetChronoLiteralTimeoutLowSpeed) {
- Url url{server->GetBaseUrl() + "/low_speed_timeout.html"};
- Session session;
- session.SetUrl(url);
- session.SetTimeout(1000ms);
- Response response = session.Get();
- EXPECT_EQ(url, response.url);
- // Do not check for the HTTP status code, since libcurl always provides the status code of the header if it was received
- EXPECT_EQ(ErrorCode::OPERATION_TIMEDOUT, response.error.code);
- }
- TEST(ConnectTimeoutTests, SetConnectTimeoutTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetConnectTimeout(0L);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(ConnectTimeoutTests, SetConnectTimeoutLongTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetConnectTimeout(10000L);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(ConnectTimeoutTests, SetChronoConnectTimeoutTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetConnectTimeout(std::chrono::milliseconds{0});
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(ConnectTimeoutTests, SetChronoConnectTimeoutLongTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetConnectTimeout(std::chrono::milliseconds{10000});
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(LowSpeedTests, SetLowSpeedTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetLowSpeed({1, 1});
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(PayloadTests, SetPayloadTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Session session;
- session.SetUrl(url);
- session.SetPayload({{"x", "5"}});
- Response response = session.Post();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(PayloadTests, SetPayloadLValueTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Session session;
- session.SetUrl(url);
- Payload payload{{"x", "5"}};
- session.SetPayload(payload);
- Response response = session.Post();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(MultipartTests, SetMultipartTest) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Session session;
- session.SetUrl(url);
- session.SetMultipart({{"x", "5"}});
- Response response = session.Post();
- std::string expected_text{
- "{\n"
- " \"x\": \"5\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(MultipartTests, SetMultipartValueTest) {
- Url url{server->GetBaseUrl() + "/form_post.html"};
- Session session;
- session.SetUrl(url);
- Multipart multipart{{"x", "5"}};
- session.SetMultipart(multipart);
- Response response = session.Post();
- std::string expected_text{
- "{\n"
- " \"x\": \"5\"\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(BodyTests, SetBodyTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Session session;
- session.SetUrl(url);
- session.SetBody(Body{"x=5"});
- Response response = session.Post();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(BodyTests, SetBodyValueTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Session session;
- session.SetUrl(url);
- Body body{"x=5"};
- session.SetBody(body);
- Response response = session.Post();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(DigestTests, SetDigestTest) {
- Url url{server->GetBaseUrl() + "/digest_auth.html"};
- Session session;
- session.SetUrl(url);
- session.SetAuth({"user", "password", AuthMode::DIGEST});
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UserAgentTests, SetUserAgentTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- UserAgent userAgent{"Test User Agent"};
- Session session;
- session.SetUrl(url);
- session.SetUserAgent(userAgent);
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(userAgent, response.header["User-Agent"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(UserAgentTests, SetUserAgentStringViewTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- UserAgent userAgent{std::string_view{"Test User Agent"}};
- Session session;
- session.SetUrl(url);
- session.SetUserAgent(userAgent);
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(userAgent, response.header["User-Agent"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(CookiesTests, BasicCookiesTest) {
- Url url{server->GetBaseUrl() + "/basic_cookies.html"};
- Session session{};
- session.SetUrl(url);
- Response response = session.Get();
- Cookies res_cookies{response.cookies};
- std::string expected_text{"Basic Cookies"};
- cpr::Cookies expectedCookies{
- {"SID", "31d4d96e407aad42", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
- {"lang", "en-US", "127.0.0.1", false, "/", true, std::chrono::system_clock::from_time_t(3905119080)},
- };
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(expected_text, response.text);
- for (auto cookie = res_cookies.begin(), expectedCookie = expectedCookies.begin(); cookie != res_cookies.end() && expectedCookie != expectedCookies.end(); cookie++, expectedCookie++) {
- EXPECT_EQ(expectedCookie->GetName(), cookie->GetName());
- EXPECT_EQ(expectedCookie->GetValue(), cookie->GetValue());
- EXPECT_EQ(expectedCookie->GetDomain(), cookie->GetDomain());
- EXPECT_EQ(expectedCookie->IsIncludingSubdomains(), cookie->IsIncludingSubdomains());
- EXPECT_EQ(expectedCookie->GetPath(), cookie->GetPath());
- EXPECT_EQ(expectedCookie->IsHttpsOnly(), cookie->IsHttpsOnly());
- EXPECT_EQ(expectedCookie->GetExpires(), cookie->GetExpires());
- }
- }
- TEST(CookiesTests, ClientSetCookiesTest) {
- Url url{server->GetBaseUrl() + "/cookies_reflect.html"};
- {
- Session session{};
- session.SetUrl(url);
- session.SetCookies(Cookies{
- {"SID", "31d4d96e407aad42"},
- {"lang", "en-US"},
- });
- Response response = session.Get();
- std::string expected_text{"SID=31d4d96e407aad42; lang=en-US;"};
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(expected_text, response.text);
- }
- {
- Session session{};
- session.SetUrl(url);
- Cookies cookie{
- {"SID", "31d4d96e407aad42"},
- {"lang", "en-US"},
- };
- session.SetCookies(cookie);
- Response response = session.Get();
- std::string expected_text{"SID=31d4d96e407aad42; lang=en-US;"};
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(expected_text, response.text);
- }
- }
- TEST(CookiesTests, RedirectionWithChangingCookiesTest) {
- Url url{server->GetBaseUrl() + "/redirection_with_changing_cookies.html"};
- {
- Session session{};
- session.SetUrl(url);
- session.SetCookies(Cookies{
- {"SID", "31d4d96e407aad42"},
- {"lang", "en-US"},
- });
- session.SetRedirect(Redirect(0L));
- Response response = session.Get();
- std::string expected_text{"Received cookies are: SID=31d4d96e407aad42; lang=en-US;"};
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(expected_text, response.text);
- }
- {
- Session session{};
- session.SetUrl(url);
- session.SetRedirect(Redirect(1L));
- Response response = session.Get();
- std::string expected_text{"Received cookies are: lang=en-US; SID=31d4d96e407aad42"};
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(expected_text, response.text);
- }
- {
- Session session{};
- session.SetUrl(url);
- session.SetCookies(Cookies{
- {"SID", "empty_sid"},
- });
- session.SetRedirect(Redirect(1L));
- Response response = session.Get();
- std::string expected_text{"Received cookies are: lang=en-US; SID=31d4d96e407aad42; SID=empty_sid;"};
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- EXPECT_EQ(expected_text, response.text);
- }
- }
- TEST(DifferentMethodTests, GetPostTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Session session;
- session.SetUrl(url);
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = session.Post();
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(DifferentMethodTests, PostGetTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Session session;
- session.SetUrl(url);
- {
- Response response = session.Post();
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(DifferentMethodTests, GetPostGetTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Session session;
- session.SetUrl(url);
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = session.Post();
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(DifferentMethodTests, PostGetPostTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Session session;
- session.SetUrl(url);
- {
- Response response = session.Post();
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = session.Post();
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(DifferentMethodTests, MultipleGetPostTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Session session;
- session.SetUrl(url);
- for (size_t i = 0; i < 100; ++i) {
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = session.Post();
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- }
- TEST(DifferentMethodTests, MultipleDeleteHeadPutGetPostTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Url urlPost{server->GetBaseUrl() + "/post_reflect.html"};
- Url urlPut{server->GetBaseUrl() + "/put.html"};
- Session session;
- for (size_t i = 0; i < 10; ++i) {
- {
- session.SetUrl(url);
- Response response = session.Delete();
- std::string expected_text{"Header reflect DELETE"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- session.SetUrl(urlPost);
- std::string expectedBody = "a1b2c3Post";
- session.SetBody(expectedBody);
- Response response = session.Post();
- EXPECT_EQ(expectedBody, response.text);
- EXPECT_EQ(urlPost, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- session.SetUrl(url);
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- session.SetUrl(urlPut);
- session.SetPayload({{"x", "5"}});
- Response response = session.Put();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(urlPut, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- session.SetUrl(url);
- Response response = session.Head();
- std::string expected_text{"Header reflect HEAD"};
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- }
- TEST(CurlHolderManipulateTests, CustomOptionTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- Session session;
- session.SetUrl(url);
- curl_easy_setopt(session.GetCurlHolder()->handle, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST | CURLSSLOPT_NO_REVOKE);
- {
- Response response = session.Get();
- std::string expected_text{"Header reflect GET"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- {
- Response response = session.Post();
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- }
- TEST(LocalPortTests, SetLocalPortTest) {
- Url url{server->GetBaseUrl() + "/local_port.html"};
- Session session;
- session.SetUrl(url);
- std::uint16_t const local_port = 60252; // beware of HttpServer::GetPort when changing
- std::uint16_t const local_port_range = 5000;
- session.SetLocalPort(local_port);
- session.SetLocalPortRange(local_port_range);
- // expected response: body contains port number in specified range
- // NOTE: even when trying up to 5000 ports there is the chance that all of them are occupied.
- // It would be possible to also check here for ErrorCode::INTERNAL_ERROR but that somehow seems
- // wrong as then this test would pass in case SetLocalPort does not work at all
- // or in other words: we have to assume that at least one port in the specified range is free.
- Response response = session.Get();
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- std::uint16_t port_from_response = std::strtoul(response.text.c_str(), nullptr, 10);
- EXPECT_EQ(errno, 0);
- EXPECT_GE(port_from_response, local_port);
- EXPECT_LE(port_from_response, local_port + local_port_range);
- }
- TEST(LocalPortTests, SetOptionTest) {
- Url url{server->GetBaseUrl() + "/local_port.html"};
- Session session;
- session.SetUrl(url);
- std::uint16_t const local_port = 60551; // beware of HttpServer::GetPort when changing
- std::uint16_t const local_port_range = 5000;
- session.SetOption(LocalPort(local_port));
- session.SetOption(LocalPortRange(local_port_range));
- // expected response: body contains port number in specified range
- // NOTE: even when trying up to 5000 ports there is the chance that all of them are occupied.
- // It would be possible to also check here for ErrorCode::INTERNAL_ERROR but that somehow seems
- // wrong as then this test would pass in case SetOption(LocalPort) does not work at all
- // or in other words: we have to assume that at least one port in the specified range is free.
- Response response = session.Get();
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- unsigned long port_from_response = std::strtoul(response.text.c_str(), nullptr, 10);
- EXPECT_EQ(errno, 0);
- EXPECT_GE(port_from_response, local_port);
- EXPECT_LE(port_from_response, local_port + local_port_range);
- }
- // The tests using the port of the server as a source port for curl fail for windows.
- // The reason probably is that Windows allows two sockets to bind to the same port if the full hostname is different.
- // In these tests, mongoose binds to http://127.0.0.1:61936, while libcurl binds to a different hostname, but still port 61936.
- // This seems to be okay for Windows, however, these tests expect an error like on Linux and MacOS
- // We therefore, simply skip the tests if Windows is used
- #ifndef _WIN32
- TEST(LocalPortTests, SetLocalPortTestOccupied) {
- Url url{server->GetBaseUrl() + "/local_port.html"};
- Session session;
- session.SetUrl(url);
- session.SetLocalPort(server->GetPort());
- // expected response: request cannot be made as port is already occupied
- Response response = session.Get();
- EXPECT_EQ(ErrorCode::INTERNAL_ERROR, response.error.code);
- }
- TEST(LocalPortTests, SetOptionTestOccupied) {
- Url url{server->GetBaseUrl() + "/local_port.html"};
- Session session;
- session.SetUrl(url);
- session.SetOption(LocalPort(server->GetPort()));
- // expected response: request cannot be made as port is already occupied
- Response response = session.Get();
- EXPECT_EQ(ErrorCode::INTERNAL_ERROR, response.error.code);
- }
- #endif // _WIN32
- TEST(BasicTests, ReserveResponseString) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- Session session;
- session.SetUrl(url);
- session.SetReserveSize(4096);
- Response response = session.Get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_GE(response.text.capacity(), 4096);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(BasicTests, AcceptEncodingTestWithMethodsStringMap) {
- Url url{server->GetBaseUrl() + "/check_accept_encoding.html"};
- Session session;
- session.SetUrl(url);
- session.SetAcceptEncoding({{AcceptEncodingMethods::deflate, AcceptEncodingMethods::gzip, AcceptEncodingMethods::zlib}});
- Response response = session.Get();
- std::string expected_text{"deflate, gzip, zlib"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(BasicTests, AcceptEncodingTestWithMethodsStringMapLValue) {
- Url url{server->GetBaseUrl() + "/check_accept_encoding.html"};
- Session session;
- session.SetUrl(url);
- AcceptEncoding accept_encoding{{AcceptEncodingMethods::deflate, AcceptEncodingMethods::gzip, AcceptEncodingMethods::zlib}};
- session.SetAcceptEncoding(accept_encoding);
- Response response = session.Get();
- std::string expected_text{"deflate, gzip, zlib"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(BasicTests, AcceptEncodingTestWithCostomizedString) {
- Url url{server->GetBaseUrl() + "/check_accept_encoding.html"};
- Session session;
- session.SetUrl(url);
- session.SetAcceptEncoding({{"deflate", "gzip", "zlib"}});
- Response response = session.Get();
- std::string expected_text{"deflate, gzip, zlib"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(BasicTests, AcceptEncodingTestWithCostomizedStringLValue) {
- Url url{server->GetBaseUrl() + "/check_accept_encoding.html"};
- Session session;
- session.SetUrl(url);
- AcceptEncoding accept_encoding{{"deflate", "gzip", "zlib"}};
- session.SetAcceptEncoding(accept_encoding);
- Response response = session.Get();
- std::string expected_text{"deflate, gzip, zlib"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(BasicTests, DisableHeaderExpect100ContinueTest) {
- Url url{server->GetBaseUrl() + "/check_expect_100_continue.html"};
- std::string filename{"test_file"};
- std::string content{std::string(1024 * 1024, 'a')};
- std::ofstream test_file;
- test_file.open(filename);
- test_file << content;
- test_file.close();
- Session session{};
- session.SetUrl(url);
- session.SetMultipart({{"file", File{"test_file"}}});
- Response response = session.Post();
- std::string expected_text{""};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(AsyncRequestsTests, AsyncGetTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- cpr::AsyncResponse future = session->GetAsync();
- std::string expected_text{"Hello world!"};
- cpr::Response response = future.get();
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- }
- TEST(AsyncRequestsTests, AsyncGetMultipleTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::vector<AsyncResponse> responses;
- std::vector<std::shared_ptr<Session>> sessions;
- for (size_t i = 0; i < 10; ++i) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- sessions.emplace_back(session);
- responses.emplace_back(session->GetAsync());
- }
- for (cpr::AsyncResponse& future : responses) {
- std::string expected_text{"Hello world!"};
- cpr::Response response = future.get();
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- }
- }
- TEST(AsyncRequestsTests, AsyncGetMultipleTemporarySessionTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::vector<AsyncResponse> responses;
- for (size_t i = 0; i < 10; ++i) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- responses.emplace_back(session->GetAsync());
- }
- for (cpr::AsyncResponse& future : responses) {
- std::string expected_text{"Hello world!"};
- cpr::Response response = future.get();
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- }
- }
- TEST(AsyncRequestsTests, AsyncGetMultipleReflectTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::vector<AsyncResponse> responses;
- for (size_t i = 0; i < 100; ++i) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetParameters({{"key", std::to_string(i)}});
- responses.emplace_back(session->GetAsync());
- }
- int i = 0;
- for (cpr::AsyncResponse& future : responses) {
- cpr::Response response = future.get();
- std::string expected_text{"Hello world!"};
- Url expected_url{url + "?key=" + std::to_string(i)};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(expected_url, response.url);
- EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- ++i;
- }
- }
- TEST(AsyncRequestsTests, AsyncWritebackDownloadTest) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- cpr::Url url{server->GetBaseUrl() + "/download_gzip.html"};
- session->SetUrl(url);
- session->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
- cpr::AsyncResponse future = session->DownloadAsync(cpr::WriteCallback{write_data, 0});
- cpr::Response response = future.get();
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(cpr::ErrorCode::OK, response.error.code);
- }
- TEST(AsyncRequestsTests, AsyncPostTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetPayload({{"x", "5"}});
- cpr::AsyncResponse future = session->PostAsync();
- cpr::Response response = future.get();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(201, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(AsyncRequestsTests, AsyncPutTest) {
- Url url{server->GetBaseUrl() + "/put.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetPayload({{"x", "5"}});
- cpr::AsyncResponse future = session->PutAsync();
- cpr::Response response = future.get();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(AsyncRequestsTests, AsyncHeadTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- cpr::AsyncResponse future = session->HeadAsync();
- cpr::Response response = future.get();
- std::string expected_text{""};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(AsyncRequestsTests, AsyncDeleteTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- cpr::AsyncResponse future = session->DeleteAsync();
- cpr::Response response = future.get();
- std::string expected_text{"Header reflect DELETE"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(AsyncRequestsTests, AsyncOptionsTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- cpr::AsyncResponse future = session->OptionsAsync();
- cpr::Response response = future.get();
- std::string expected_text{"Header reflect OPTIONS"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(AsyncRequestsTests, AsyncPatchTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- cpr::AsyncResponse future = session->PatchAsync();
- cpr::Response response = future.get();
- std::string expected_text{"Header reflect PATCH"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(CallbackTests, GetCallbackTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- auto future = session->GetCallback([](Response r) { return r; });
- std::this_thread::sleep_for(sleep_time);
- cpr::Response response = future.get();
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(CallbackTests, PostCallbackTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- auto future = session->PostCallback([](Response r) { return r; });
- std::this_thread::sleep_for(sleep_time);
- cpr::Response response = future.get();
- std::string expected_text{"Header reflect POST"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(CallbackTests, PutCallbackTest) {
- Url url{server->GetBaseUrl() + "/put.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetPayload({{"x", "5"}});
- auto future = session->PutCallback([](Response r) { return r; });
- std::this_thread::sleep_for(sleep_time);
- cpr::Response response = future.get();
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(CallbackTests, HeadCallbackTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- auto future = session->HeadCallback([](Response r) { return r; });
- std::this_thread::sleep_for(sleep_time);
- cpr::Response response = future.get();
- std::string expected_text{""};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(CallbackTests, DeleteCallbackTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- auto future = session->DeleteCallback([](Response r) { return r; });
- std::this_thread::sleep_for(sleep_time);
- cpr::Response response = future.get();
- std::string expected_text{"Header reflect DELETE"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(CallbackTests, OptionsCallbackTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- auto future = session->OptionsCallback([](Response r) { return r; });
- std::this_thread::sleep_for(sleep_time);
- cpr::Response response = future.get();
- std::string expected_text{"Header reflect OPTIONS"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- TEST(CallbackTests, PatchCallbackTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- auto future = session->PatchCallback([](Response r) { return r; });
- std::this_thread::sleep_for(sleep_time);
- cpr::Response response = future.get();
- std::string expected_text{"Header reflect PATCH"};
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- int main(int argc, char** argv) {
- ::testing::InitGoogleTest(&argc, argv);
- ::testing::AddGlobalTestEnvironment(server);
- return RUN_ALL_TESTS();
- }
|