123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- #include <cstdint>
- #include <gtest/gtest.h>
- #include <memory>
- #include <string>
- #include <cpr/api.h>
- #include <cpr/cpr.h>
- #include <curl/curl.h>
- #include "httpServer.hpp"
- #include "httpsServer.hpp"
- using namespace cpr;
- static HttpServer* server = new HttpServer();
- bool write_data(std::string /*data*/, intptr_t /*userdata*/) {
- return true;
- }
- TEST(MultiperformAddSessionTests, MultiperformAddSingleSessionTest) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- MultiPerform multiperform;
- multiperform.AddSession(session);
- EXPECT_EQ(2, session.use_count());
- }
- TEST(MultiperformAddSessionTests, MultiperformAddMultipleSessionsTest) {
- MultiPerform multiperform;
- for (int i = 0; i < 10; ++i) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- multiperform.AddSession(session);
- EXPECT_EQ(2, session.use_count());
- }
- }
- TEST(MultiperformAddSessionTests, MultiperformAddMultipleNonDownloadSessionsTest) {
- MultiPerform multiperform;
- for (int i = 0; i < 10; ++i) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- multiperform.AddSession(session, MultiPerform::HttpMethod::GET_REQUEST);
- EXPECT_EQ(2, session.use_count());
- }
- }
- TEST(MultiperformAddSessionTests, MultiperformAddMultipleDownloadSessionsTest) {
- MultiPerform multiperform;
- for (int i = 0; i < 10; ++i) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- multiperform.AddSession(session, MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
- EXPECT_EQ(2, session.use_count());
- }
- }
- TEST(MultiperformAddSessionTests, MultiperformAddTwoMixedTypeSessionsTest) {
- std::shared_ptr<Session> session_1 = std::make_shared<Session>();
- std::shared_ptr<Session> session_2 = std::make_shared<Session>();
- MultiPerform multiperform;
- multiperform.AddSession(session_1, MultiPerform::HttpMethod::GET_REQUEST);
- EXPECT_EQ(2, session_1.use_count());
- EXPECT_THROW(multiperform.AddSession(session_2, MultiPerform::HttpMethod::DOWNLOAD_REQUEST), std::invalid_argument);
- }
- TEST(MultiperformAddSessionTests, MultiperformAddTwoMixedTypeSessionsReversTest) {
- std::shared_ptr<Session> session_1 = std::make_shared<Session>();
- std::shared_ptr<Session> session_2 = std::make_shared<Session>();
- MultiPerform multiperform;
- multiperform.AddSession(session_1, MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
- EXPECT_EQ(2, session_1.use_count());
- EXPECT_THROW(multiperform.AddSession(session_2, MultiPerform::HttpMethod::GET_REQUEST), std::invalid_argument);
- }
- TEST(MultiperformRemoveSessionTests, MultiperformRemoveSingleSessionTest) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- MultiPerform multiperform;
- multiperform.AddSession(session);
- EXPECT_EQ(2, session.use_count());
- multiperform.RemoveSession(session);
- EXPECT_EQ(1, session.use_count());
- }
- TEST(MultiperformRemoveSessionTests, MultiperformRemoveMultipleSessionsTest) {
- MultiPerform multiperform;
- for (int i = 0; i < 10; ++i) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- multiperform.AddSession(session);
- EXPECT_EQ(2, session.use_count());
- multiperform.RemoveSession(session);
- EXPECT_EQ(1, session.use_count());
- }
- }
- TEST(MultiperformRemoveSessionTests, MultiperformRemoveNonExistingSessionTest) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- MultiPerform multiperform;
- EXPECT_THROW(multiperform.RemoveSession(session), std::invalid_argument);
- }
- TEST(MultiperformGetTests, MultiperformSingleSessionGetTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- MultiPerform multiperform;
- multiperform.AddSession(session);
- std::vector<Response> responses = multiperform.Get();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformGetTests, MultiperformTwoSessionsGetTest) {
- MultiPerform multiperform;
- std::vector<Url> urls;
- urls.push_back({server->GetBaseUrl() + "/hello.html"});
- urls.push_back({server->GetBaseUrl() + "/error.html"});
- std::vector<std::shared_ptr<Session>> sessions;
- sessions.push_back(std::make_shared<Session>());
- sessions.push_back(std::make_shared<Session>());
- for (size_t i = 0; i < sessions.size(); ++i) {
- sessions.at(i)->SetUrl(urls.at(i));
- multiperform.AddSession(sessions.at(i));
- }
- std::vector<Response> responses = multiperform.Get();
- EXPECT_EQ(responses.size(), sessions.size());
- std::vector<std::string> expected_texts;
- expected_texts.emplace_back("Hello world!");
- expected_texts.emplace_back("Not Found");
- std::vector<std::string> expected_content_types;
- expected_content_types.emplace_back("text/html");
- expected_content_types.emplace_back("text/plain");
- std::vector<uint16_t> expected_status_codes;
- expected_status_codes.push_back(200);
- expected_status_codes.push_back(404);
- for (size_t i = 0; i < responses.size(); ++i) {
- EXPECT_EQ(expected_texts.at(i), responses.at(i).text);
- EXPECT_EQ(urls.at(i), responses.at(i).url);
- EXPECT_EQ(expected_content_types.at(i), responses.at(i).header["content-type"]);
- EXPECT_EQ(expected_status_codes.at(i), responses.at(i).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
- }
- }
- TEST(MultiperformGetTests, MultiperformRemoveSessionGetTest) {
- MultiPerform multiperform;
- std::vector<Url> urls;
- urls.push_back({server->GetBaseUrl() + "/hello.html"});
- urls.push_back({server->GetBaseUrl() + "/hello.html"});
- std::vector<std::shared_ptr<Session>> sessions;
- sessions.push_back(std::make_shared<Session>());
- sessions.push_back(std::make_shared<Session>());
- for (size_t i = 0; i < sessions.size(); ++i) {
- sessions.at(i)->SetUrl(urls.at(i));
- multiperform.AddSession(sessions.at(i));
- }
- multiperform.RemoveSession(sessions.at(0));
- std::vector<Response> responses = multiperform.Get();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(urls.at(0), responses.at(0).url);
- EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- #ifndef __APPLE__
- /**
- * This test case is currently disabled for macOS/Apple systems since it fails in an nondeterministic manner.
- * It is probably caused by a bug inside curl_multi_perform on macOS.
- * Needs further investigation.
- * Issue: https://github.com/libcpr/cpr/issues/841
- **/
- TEST(MultiperformGetTests, MultiperformTenSessionsGetTest) {
- const size_t sessionCount = 10;
- MultiPerform multiperform;
- Url url{server->GetBaseUrl() + "/hello.html"};
- for (size_t i = 0; i < sessionCount; ++i) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- multiperform.AddSession(session);
- }
- std::vector<Response> responses = multiperform.Get();
- EXPECT_EQ(responses.size(), sessionCount);
- for (Response& response : responses) {
- EXPECT_EQ(std::string{"Hello world!"}, 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);
- }
- }
- #endif
- TEST(MultiperformDeleteTests, MultiperformSingleSessionDeleteTest) {
- Url url{server->GetBaseUrl() + "/delete.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- MultiPerform multiperform;
- multiperform.AddSession(session);
- std::vector<Response> responses = multiperform.Delete();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{"Delete success"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformDownloadTests, MultiperformSingleSessionDownloadTest) {
- Url url{server->GetBaseUrl() + "/download_gzip.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
- MultiPerform multiperform;
- multiperform.AddSession(session);
- std::vector<Response> responses = multiperform.Download(WriteCallback{write_data, 0});
- EXPECT_EQ(responses.size(), 1);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(cpr::ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformDownloadTests, MultiperformSingleSessionDownloadNonMatchingArgumentsNumberTest) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- MultiPerform multiperform;
- multiperform.AddSession(session);
- EXPECT_THROW(std::vector<Response> responses = multiperform.Download(WriteCallback{write_data, 0}, WriteCallback{write_data, 0}), std::invalid_argument);
- }
- TEST(MultiperformDownloadTests, MultiperformTwoSessionsDownloadTest) {
- MultiPerform multiperform;
- std::vector<Url> urls;
- urls.push_back({server->GetBaseUrl() + "/download_gzip.html"});
- urls.push_back({server->GetBaseUrl() + "/download_gzip.html"});
- std::vector<std::shared_ptr<Session>> sessions;
- sessions.push_back(std::make_shared<Session>());
- sessions.push_back(std::make_shared<Session>());
- for (size_t i = 0; i < sessions.size(); ++i) {
- sessions.at(i)->SetUrl(urls.at(i));
- sessions.at(i)->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
- multiperform.AddSession(sessions.at(i));
- }
- std::vector<Response> responses = multiperform.Download(WriteCallback{write_data, 0}, WriteCallback{write_data, 0});
- EXPECT_EQ(responses.size(), sessions.size());
- for (size_t i = 0; i < responses.size(); ++i) {
- EXPECT_EQ(urls.at(i), responses.at(i).url);
- EXPECT_EQ(200, responses.at(i).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
- }
- }
- TEST(MultiperformPutTests, MultiperformSingleSessionPutTest) {
- Url url{server->GetBaseUrl() + "/put.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetPayload(Payload{{"x", "5"}});
- MultiPerform multiperform;
- multiperform.AddSession(session);
- std::vector<Response> responses = multiperform.Put();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformHeadTests, MultiperformSingleSessionHeadTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- MultiPerform multiperform;
- multiperform.AddSession(session);
- std::vector<Response> responses = multiperform.Head();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text;
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformOptionsTests, MultiperformSingleSessionOptionsTest) {
- Url url{server->GetBaseUrl() + "/"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- MultiPerform multiperform;
- multiperform.AddSession(session);
- std::vector<Response> responses = multiperform.Options();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text;
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, responses.at(0).header["Access-Control-Allow-Methods"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformPatchTests, MultiperformSingleSessionPatchTest) {
- Url url{server->GetBaseUrl() + "/patch.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetPayload(Payload{{"x", "5"}});
- MultiPerform multiperform;
- multiperform.AddSession(session);
- std::vector<Response> responses = multiperform.Patch();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformPostTests, MultiperformSingleSessionPostTest) {
- Url url{server->GetBaseUrl() + "/url_post.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetPayload(Payload{{"x", "5"}});
- MultiPerform multiperform;
- multiperform.AddSession(session);
- std::vector<Response> responses = multiperform.Post();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(201, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformPerformTests, MultiperformSingleGetPerformTest) {
- Url url{server->GetBaseUrl() + "/hello.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- MultiPerform multiperform;
- multiperform.AddSession(session, MultiPerform::HttpMethod::GET_REQUEST);
- std::vector<Response> responses = multiperform.Perform();
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformPerformTests, MultiperformTwoGetPerformTest) {
- MultiPerform multiperform;
- std::vector<Url> urls;
- urls.push_back({server->GetBaseUrl() + "/hello.html"});
- urls.push_back({server->GetBaseUrl() + "/error.html"});
- std::vector<std::shared_ptr<Session>> sessions;
- sessions.push_back(std::make_shared<Session>());
- sessions.push_back(std::make_shared<Session>());
- for (size_t i = 0; i < sessions.size(); ++i) {
- sessions.at(i)->SetUrl(urls.at(i));
- multiperform.AddSession(sessions.at(i), MultiPerform::HttpMethod::GET_REQUEST);
- }
- std::vector<Response> responses = multiperform.Perform();
- EXPECT_EQ(responses.size(), sessions.size());
- std::vector<std::string> expected_texts;
- expected_texts.emplace_back("Hello world!");
- expected_texts.emplace_back("Not Found");
- std::vector<std::string> expected_content_types;
- expected_content_types.emplace_back("text/html");
- expected_content_types.emplace_back("text/plain");
- std::vector<uint16_t> expected_status_codes;
- expected_status_codes.push_back(200);
- expected_status_codes.push_back(404);
- for (size_t i = 0; i < responses.size(); ++i) {
- EXPECT_EQ(expected_texts.at(i), responses.at(i).text);
- EXPECT_EQ(urls.at(i), responses.at(i).url);
- EXPECT_EQ(expected_content_types.at(i), responses.at(i).header["content-type"]);
- EXPECT_EQ(expected_status_codes.at(i), responses.at(i).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
- }
- }
- TEST(MultiperformPerformTests, MultiperformMixedMethodsPerformTest) {
- MultiPerform multiperform;
- std::vector<Url> urls;
- urls.push_back({server->GetBaseUrl() + "/hello.html"});
- urls.push_back({server->GetBaseUrl() + "/delete.html"});
- urls.push_back({server->GetBaseUrl() + "/error.html"});
- urls.push_back({server->GetBaseUrl() + "/url_post.html"});
- std::vector<std::shared_ptr<Session>> sessions;
- sessions.push_back(std::make_shared<Session>());
- sessions.push_back(std::make_shared<Session>());
- sessions.push_back(std::make_shared<Session>());
- sessions.push_back(std::make_shared<Session>());
- std::vector<MultiPerform::HttpMethod> methods;
- methods.push_back(MultiPerform::HttpMethod::GET_REQUEST);
- methods.push_back(MultiPerform::HttpMethod::DELETE_REQUEST);
- methods.push_back(MultiPerform::HttpMethod::GET_REQUEST);
- methods.push_back(MultiPerform::HttpMethod::POST_REQUEST);
- for (size_t i = 0; i < sessions.size(); ++i) {
- sessions.at(i)->SetUrl(urls.at(i));
- if (methods.at(i) == MultiPerform::HttpMethod::POST_REQUEST) {
- sessions.at(i)->SetPayload(Payload{{"x", "5"}});
- }
- multiperform.AddSession(sessions.at(i), methods.at(i));
- }
- std::vector<Response> responses = multiperform.Perform();
- EXPECT_EQ(responses.size(), sessions.size());
- std::vector<std::string> expected_texts;
- expected_texts.emplace_back("Hello world!");
- expected_texts.emplace_back("Delete success");
- expected_texts.emplace_back("Not Found");
- expected_texts.emplace_back(
- "{\n"
- " \"x\": 5\n"
- "}");
- std::vector<std::string> expected_content_types;
- expected_content_types.emplace_back("text/html");
- expected_content_types.emplace_back("text/html");
- expected_content_types.emplace_back("text/plain");
- expected_content_types.emplace_back("application/json");
- std::vector<uint16_t> expected_status_codes;
- expected_status_codes.push_back(200);
- expected_status_codes.push_back(200);
- expected_status_codes.push_back(404);
- expected_status_codes.push_back(201);
- for (size_t i = 0; i < responses.size(); ++i) {
- EXPECT_EQ(expected_texts.at(i), responses.at(i).text);
- EXPECT_EQ(urls.at(i), responses.at(i).url);
- EXPECT_EQ(expected_content_types.at(i), responses.at(i).header["content-type"]);
- EXPECT_EQ(expected_status_codes.at(i), responses.at(i).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
- }
- }
- TEST(MultiperformPerformDownloadTests, MultiperformSinglePerformDownloadTest) {
- Url url{server->GetBaseUrl() + "/download_gzip.html"};
- std::shared_ptr<Session> session = std::make_shared<Session>();
- session->SetUrl(url);
- session->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
- MultiPerform multiperform;
- multiperform.AddSession(session, MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
- std::vector<Response> responses = multiperform.PerformDownload(WriteCallback{write_data, 0});
- EXPECT_EQ(responses.size(), 1);
- EXPECT_EQ(url, responses.at(0).url);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(cpr::ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformDownloadTests, MultiperformSinglePerformDownloadNonMatchingArgumentsNumberTest) {
- std::shared_ptr<Session> session = std::make_shared<Session>();
- MultiPerform multiperform;
- multiperform.AddSession(session, MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
- EXPECT_THROW(std::vector<Response> responses = multiperform.PerformDownload(WriteCallback{write_data, 0}, WriteCallback{write_data, 0}), std::invalid_argument);
- }
- TEST(MultiperformPerformDownloadTests, MultiperformTwoPerformDownloadTest) {
- MultiPerform multiperform;
- std::vector<Url> urls;
- urls.push_back({server->GetBaseUrl() + "/download_gzip.html"});
- urls.push_back({server->GetBaseUrl() + "/download_gzip.html"});
- std::vector<std::shared_ptr<Session>> sessions;
- sessions.push_back(std::make_shared<Session>());
- sessions.push_back(std::make_shared<Session>());
- for (size_t i = 0; i < sessions.size(); ++i) {
- sessions.at(i)->SetUrl(urls.at(i));
- sessions.at(i)->SetHeader(cpr::Header{{"Accept-Encoding", "gzip"}});
- multiperform.AddSession(sessions.at(i), MultiPerform::HttpMethod::DOWNLOAD_REQUEST);
- }
- std::vector<Response> responses = multiperform.PerformDownload(WriteCallback{write_data, 0}, WriteCallback{write_data, 0});
- EXPECT_EQ(responses.size(), sessions.size());
- for (size_t i = 0; i < responses.size(); ++i) {
- EXPECT_EQ(urls.at(i), responses.at(i).url);
- EXPECT_EQ(200, responses.at(i).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(i).error.code);
- }
- }
- TEST(MultiperformAPITests, MultiperformApiSingleGetTest) {
- std::vector<Response> responses = MultiGet(std::tuple<Url>{Url{server->GetBaseUrl() + "/hello.html"}});
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{"Hello world!"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, responses.at(0).url);
- EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformAPITests, MultiperformApiTwoGetsTest) {
- std::vector<Response> responses = MultiGet(std::tuple<Url, Timeout>{Url{server->GetBaseUrl() + "/long_timeout.html"}, Timeout{1000}}, std::tuple<Url>{Url{server->GetBaseUrl() + "/error.html"}});
- EXPECT_EQ(responses.size(), 2);
- std::vector<Url> urls;
- urls.push_back({server->GetBaseUrl() + "/long_timeout.html"});
- urls.push_back({server->GetBaseUrl() + "/error.html"});
- std::vector<std::string> expected_texts;
- expected_texts.emplace_back("");
- expected_texts.emplace_back("Not Found");
- std::vector<std::string> expected_content_types;
- expected_content_types.emplace_back("");
- expected_content_types.emplace_back("text/plain");
- std::vector<uint16_t> expected_status_codes;
- expected_status_codes.push_back(0);
- expected_status_codes.push_back(404);
- std::vector<ErrorCode> expected_error_codes;
- expected_error_codes.push_back(ErrorCode::OPERATION_TIMEDOUT);
- expected_error_codes.push_back(ErrorCode::OK);
- for (size_t i = 0; i < responses.size(); ++i) {
- EXPECT_EQ(expected_texts.at(i), responses.at(i).text);
- EXPECT_EQ(urls.at(i), responses.at(i).url);
- EXPECT_EQ(expected_content_types.at(i), responses.at(i).header["content-type"]);
- EXPECT_EQ(expected_status_codes.at(i), responses.at(i).status_code);
- EXPECT_EQ(expected_error_codes.at(i), responses.at(i).error.code);
- }
- }
- TEST(MultiperformAPITests, MultiperformApiSingleDeleteTest) {
- std::vector<Response> responses = MultiDelete(std::tuple<Url>{Url{server->GetBaseUrl() + "/delete.html"}});
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{"Delete success"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/delete.html"}, responses.at(0).url);
- EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformAPITests, MultiperformApiSinglePutTest) {
- std::vector<Response> responses = MultiPut(std::tuple<Url, Payload>{Url{server->GetBaseUrl() + "/put.html"}, Payload{{"x", "5"}}});
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/put.html"}, responses.at(0).url);
- EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformAPITests, MultiperformApiSingleHeadTest) {
- std::vector<Response> responses = MultiHead(std::tuple<Url>{Url{server->GetBaseUrl() + "/hello.html"}});
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text;
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/hello.html"}, responses.at(0).url);
- EXPECT_EQ(std::string{"text/html"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformAPITests, MultiperformApiSingleOptionsTest) {
- std::vector<Response> responses = MultiOptions(std::tuple<Url>{Url{server->GetBaseUrl() + "/"}});
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text;
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/"}, responses.at(0).url);
- EXPECT_EQ(std::string{"GET, POST, PUT, DELETE, PATCH, OPTIONS"}, responses.at(0).header["Access-Control-Allow-Methods"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformAPITests, MultiperformApiSinglePatchTest) {
- std::vector<Response> responses = MultiPatch(std::tuple<Url, Payload>{Url{server->GetBaseUrl() + "/patch.html"}, Payload{{"x", "5"}}});
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/patch.html"}, responses.at(0).url);
- EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(200, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- TEST(MultiperformAPITests, MultiperformApiSinglePostTest) {
- std::vector<Response> responses = MultiPost(std::tuple<Url, Payload>{Url{server->GetBaseUrl() + "/url_post.html"}, Payload{{"x", "5"}}});
- EXPECT_EQ(responses.size(), 1);
- std::string expected_text{
- "{\n"
- " \"x\": 5\n"
- "}"};
- EXPECT_EQ(expected_text, responses.at(0).text);
- EXPECT_EQ(Url{server->GetBaseUrl() + "/url_post.html"}, responses.at(0).url);
- EXPECT_EQ(std::string{"application/json"}, responses.at(0).header["content-type"]);
- EXPECT_EQ(201, responses.at(0).status_code);
- EXPECT_EQ(ErrorCode::OK, responses.at(0).error.code);
- }
- int main(int argc, char** argv) {
- ::testing::InitGoogleTest(&argc, argv);
- ::testing::AddGlobalTestEnvironment(server);
- return RUN_ALL_TESTS();
- }
|