123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- #include <cstddef>
- #include <gtest/gtest.h>
- #include <chrono>
- #include <string>
- #include <thread>
- #include <vector>
- #include <cpr/cpr.h>
- #include "cpr/cprtypes.h"
- #include "httpServer.hpp"
- using namespace cpr;
- static HttpServer* server = new HttpServer();
- std::chrono::milliseconds sleep_time{50};
- std::chrono::seconds zero{0};
- int status_callback(int& status_code, Response r) {
- status_code = r.status_code;
- return r.status_code;
- }
- int status_callback_ref(int& status_code, const Response& r) {
- status_code = r.status_code;
- return r.status_code;
- }
- std::string text_callback(std::string& expected_text, Response r) {
- expected_text = r.text;
- return r.text;
- }
- std::string text_callback_ref(std::string& expected_text, const Response& r) {
- expected_text = r.text;
- return r.text;
- }
- TEST(CallbackGetTests, CallbackGetLambdaStatusTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto future = cpr::GetCallback(
- [&status_code](Response r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackGetTests, CallbackGetLambdaTextTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto future = cpr::GetCallback(
- [&expected_text](Response r) {
- expected_text = r.text;
- return r.text;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackGetTests, CallbackGetLambdaStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto future = cpr::GetCallback(
- [&status_code](const Response& r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackGetTests, CallbackGetLambdaTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto future = cpr::GetCallback(
- [&expected_text](const Response& r) {
- expected_text = r.text;
- return r.text;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackGetTests, CallbackGetFunctionStatusTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::GetCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackGetTests, CallbackGetFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::GetCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackGetTests, CallbackGetFunctionStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::GetCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackGetTests, CallbackGetFunctionTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::GetCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackDeleteTests, CallbackDeleteLambdaStatusTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- int status_code = 0;
- auto future = cpr::DeleteCallback(
- [&status_code](Response r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackDeleteTests, CallbackDeleteLambdaTextTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- std::string expected_text{};
- auto future = cpr::DeleteCallback(
- [&expected_text](Response r) {
- expected_text = r.text;
- return r.text;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackDeleteTests, CallbackDeleteLambdaStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- int status_code = 0;
- auto future = cpr::DeleteCallback(
- [&status_code](const Response& r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackDeleteTests, CallbackDeleteLambdaTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- std::string expected_text{};
- auto future = cpr::DeleteCallback(
- [&expected_text](const Response& r) {
- expected_text = r.text;
- return r.text;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackDeleteTests, CallbackDeleteFunctionStatusTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::DeleteCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackDeleteTests, CallbackDeleteFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::DeleteCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackDeleteTests, CallbackDeleteFunctionStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::DeleteCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackDeleteTests, CallbackDeleteFunctionTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::DeleteCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackHeadTests, CallbackHeadLambdaStatusTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto future = cpr::HeadCallback(
- [&status_code](Response r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackHeadTests, CallbackHeadLambdaTextTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto future = cpr::HeadCallback(
- [&expected_text](Response r) {
- expected_text = r.text;
- return r.text;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackHeadTests, CallbackHeadLambdaStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto future = cpr::HeadCallback(
- [&status_code](const Response& r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackHeadTests, CallbackHeadLambdaTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto future = cpr::HeadCallback(
- [&expected_text](const Response& r) {
- expected_text = r.text;
- return r.text;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackHeadTests, CallbackHeadFunctionStatusTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::HeadCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackHeadTests, CallbackHeadFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::HeadCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackHeadTests, CallbackHeadFunctionStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::HeadCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackHeadTests, CallbackHeadFunctionTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::HeadCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPostTests, CallbackPostLambdaStatusTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto future = cpr::PostCallback(
- [&status_code](Response r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPostTests, CallbackPostLambdaTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto future = cpr::PostCallback(
- [&expected_text](Response r) {
- expected_text = r.text;
- return r.text;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPostTests, CallbackPostLambdaStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto future = cpr::PostCallback(
- [&status_code](const Response& r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPostTests, CallbackPostLambdaTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto future = cpr::PostCallback(
- [&expected_text](const Response& r) {
- expected_text = r.text;
- return r.text;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPostTests, CallbackPostFunctionStatusTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::PostCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPostTests, CallbackPostFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::PostCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPostTests, CallbackPostFunctionStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::PostCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPostTests, CallbackPostFunctionTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::PostCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPutTests, CallbackPutLambdaStatusTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto future = cpr::PutCallback(
- [&status_code](Response r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPutTests, CallbackPutLambdaTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto future = cpr::PutCallback(
- [&expected_text](Response r) {
- expected_text = r.text;
- return r.text;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPutTests, CallbackPutLambdaStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto future = cpr::PutCallback(
- [&status_code](const Response& r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPutTests, CallbackPutLambdaTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto future = cpr::PutCallback(
- [&expected_text](const Response& r) {
- expected_text = r.text;
- return r.text;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPutTests, CallbackPutFunctionStatusTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::PutCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPutTests, CallbackPutFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::PutCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPutTests, CallbackPutFunctionStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::PutCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPutTests, CallbackPutFunctionTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::PutCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackOptionsTests, CallbackOptionsLambdaStatusTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto future = cpr::OptionsCallback(
- [&status_code](Response r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackOptionsTests, CallbackOptionsLambdaTextTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto future = cpr::OptionsCallback(
- [&expected_text](Response r) {
- expected_text = r.text;
- return r.text;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackOptionsTests, CallbackOptionsLambdaStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto future = cpr::OptionsCallback(
- [&status_code](const Response& r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackOptionsTests, CallbackOptionsLambdaTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto future = cpr::OptionsCallback(
- [&expected_text](const Response& r) {
- expected_text = r.text;
- return r.text;
- },
- url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackOptionsTests, CallbackOptionsFunctionStatusTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::OptionsCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackOptionsTests, CallbackOptionsFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::OptionsCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackOptionsTests, CallbackOptionsFunctionStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::OptionsCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackOptionsTests, CallbackOptionsFunctionTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::OptionsCallback(callback, url);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPatchTests, CallbackPatchLambdaStatusTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto future = cpr::PatchCallback(
- [&status_code](Response r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPatchTests, CallbackPatchLambdaTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto future = cpr::PatchCallback(
- [&expected_text](Response r) {
- expected_text = r.text;
- return r.text;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPatchTests, CallbackPatchLambdaStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto future = cpr::PatchCallback(
- [&status_code](const Response& r) {
- status_code = r.status_code;
- return r.status_code;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPatchTests, CallbackPatchLambdaTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto future = cpr::PatchCallback(
- [&expected_text](const Response& r) {
- expected_text = r.text;
- return r.text;
- },
- url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPatchTests, CallbackPatchFunctionStatusTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::PatchCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPatchTests, CallbackPatchFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::PatchCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackPatchTests, CallbackPatchFunctionStatusReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- int status_code = 0;
- auto callback = std::function<int(Response)>(std::bind(status_callback_ref, std::ref(status_code), std::placeholders::_1));
- auto future = cpr::PatchCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(status_code, future.get());
- }
- TEST(CallbackPatchTests, CallbackPatchFunctionTextReferenceTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Payload payload{{"x", "5"}};
- std::string expected_text{};
- auto callback = std::function<std::string(Response)>(std::bind(text_callback_ref, std::ref(expected_text), std::placeholders::_1));
- auto future = cpr::PatchCallback(callback, url, payload);
- std::this_thread::sleep_for(sleep_time);
- EXPECT_EQ(future.wait_for(zero), std::future_status::ready);
- EXPECT_EQ(expected_text, future.get());
- }
- TEST(CallbackDataTests, CallbackReadFunctionCancelTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Response response = cpr::Post(url, cpr::ReadCallback([](char* /*buffer*/, size_t& /*size*/, intptr_t /*userdata*/) -> size_t { return false; }));
- EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
- }
- TEST(CallbackDataTests, CallbackReadFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- unsigned count = 0;
- Response response = cpr::Post(url, cpr::ReadCallback{3, [&](char* buffer, size_t& size, intptr_t /*userdata*/) -> size_t {
- std::string data;
- ++count;
- switch (count) {
- case 1:
- data = "x=";
- break;
- case 2:
- data = "5";
- break;
- default:
- return false;
- }
- std::copy(data.begin(), data.end(), buffer);
- size = data.size();
- return true;
- }});
- EXPECT_EQ(2, count);
- EXPECT_EQ(expected_text, response.text);
- }
- TEST(CallbackDataTests, CallbackReadFunctionTextTestPut) {
- Url url{server->GetBaseUrl() + "/put.html"};
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- unsigned count = 0;
- Response response = cpr::Put(url, cpr::ReadCallback{3, [&](char* buffer, size_t& size, intptr_t /*userdata*/) -> size_t {
- std::string data;
- ++count;
- switch (count) {
- case 1:
- data = "x=";
- break;
- case 2:
- data = "5";
- break;
- default:
- return false;
- }
- std::copy(data.begin(), data.end(), buffer);
- size = data.size();
- return true;
- }});
- EXPECT_EQ(2, count);
- EXPECT_EQ(expected_text, response.text);
- EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
- EXPECT_EQ(200, response.status_code);
- EXPECT_EQ(ErrorCode::OK, response.error.code);
- }
- /**
- * Checks if the "Transfer-Encoding" header will be kept when using headers and a read callback.
- * Issue: https://github.com/whoshuu/cpr/issues/517
- **/
- TEST(CallbackDataTests, CallbackReadFunctionHeaderTest) {
- Url url{server->GetBaseUrl() + "/header_reflect.html"};
- std::string data = "Test";
- Response response = cpr::Post(url,
- cpr::ReadCallback{-1,
- [&](char* /*buffer*/, size_t& size, intptr_t /*userdata*/) -> size_t {
- size = 0;
- return true;
- }},
- Header{{"TestHeader", "42"}});
- EXPECT_EQ(url, response.url);
- EXPECT_EQ(200, response.status_code);
- // Check Header:
- EXPECT_EQ(std::string{"42"}, response.header["TestHeader"]); // Set by us
- EXPECT_TRUE(response.header.find("TestHeader") != response.header.end());
- EXPECT_EQ(std::string{"chunked"}, response.header["Transfer-Encoding"]); // Set by the read callback
- EXPECT_TRUE(response.header.find("Transfer-Encoding") != response.header.end());
- }
- /* cesanta mongoose doesn't support chunked requests yet
- TEST(CallbackDataTests, CallbackReadFunctionChunkedTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- unsigned count = 0;
- Response response = cpr::Post(url, cpr::ReadCallback{[&count](char* buffer, size_t & size) -> size_t {
- std::string data;
- ++ count;
- switch (count) {
- case 1:
- data = "x=";
- break;
- case 2:
- data = "5";
- break;
- default:
- data = "";
- break;
- }
- std::copy(data.begin(), data.end(), buffer);
- size = data.size();
- return true;
- }});
- EXPECT_EQ(3, count);
- EXPECT_EQ(expected_text, response.text);
- }
- */
- TEST(CallbackDataTests, CallbackHeaderFunctionCancelTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Response response = Post(url, HeaderCallback{[](std::string /*header*/, intptr_t /*userdata*/) -> bool { return false; }});
- EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
- }
- TEST(CallbackDataTests, CallbackHeaderFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- std::vector<std::string> expected_headers{"HTTP/1.1 201 Created\r\n", "Content-Type: application/json\r\n", "\r\n"};
- std::set<std::string> response_headers;
- Post(url, HeaderCallback{[&response_headers](std::string header, intptr_t /*userdata*/) -> bool {
- response_headers.insert(header);
- return true;
- }});
- for (std::string& header : expected_headers) {
- std::cout << header << std::endl;
- EXPECT_TRUE(response_headers.count(header));
- }
- }
- TEST(CallbackDataTests, CallbackWriteFunctionCancelTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Response response = Post(url, WriteCallback{[](std::string /*header*/, intptr_t /*userdata*/) -> bool { return false; }});
- EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
- }
- TEST(CallbackDataTests, CallbackWriteFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- std::string response_text;
- Post(url, Payload{{"x", "5"}}, WriteCallback{[&response_text](std::string header, intptr_t /*userdata*/) -> bool {
- response_text.append(header);
- return true;
- }});
- EXPECT_EQ(expected_text, response_text);
- }
- TEST(CallbackDataTests, CallbackProgressFunctionCancelTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Response response = Post(url, ProgressCallback{[](size_t /*downloadTotal*/, size_t /*downloadNow*/, size_t /*uploadTotal*/, size_t /*uploadNow*/, intptr_t /*userdata*/) -> bool { return false; }});
- EXPECT_EQ(response.error.code, ErrorCode::REQUEST_CANCELLED);
- }
- TEST(CallbackDataTests, CallbackProgressFunctionTotalTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Body body{"x=5"};
- size_t response_upload = 0, response_download = 0;
- Response response = Post(url, body, ProgressCallback{[&](size_t downloadTotal, size_t /*downloadNow*/, size_t uploadTotal, size_t /*uploadNow*/, intptr_t /*userdata*/) -> bool {
- response_upload = uploadTotal;
- response_download = downloadTotal;
- return true;
- }});
- EXPECT_EQ(body.str().length(), response_upload);
- EXPECT_EQ(response.text.length(), response_download);
- }
- TEST(CallbackDataTests, CallbackDebugFunctionTextTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- Body body{"x=5"};
- std::string debug_body;
- Response response = Post(url, body, DebugCallback{[&](DebugCallback::InfoType type, std::string data, intptr_t /*userdata*/) {
- if (type == DebugCallback::InfoType::DATA_OUT) {
- debug_body = data;
- }
- }});
- EXPECT_EQ(body.str(), debug_body);
- }
- int main(int argc, char** argv) {
- ::testing::InitGoogleTest(&argc, argv);
- ::testing::AddGlobalTestEnvironment(server);
- return RUN_ALL_TESTS();
- }
|