diff --git a/cpr/multiperform.cpp b/cpr/multiperform.cpp index aa74485ac..76818c98a 100644 --- a/cpr/multiperform.cpp +++ b/cpr/multiperform.cpp @@ -9,8 +9,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -68,14 +68,19 @@ void MultiPerform::AddSession(std::shared_ptr& session, HttpMethod meth } void MultiPerform::RemoveSession(const std::shared_ptr& session) { + // Has to be handled before calling curl_multi_remove_handle to avoid it returning something != CURLM_OK. + if (sessions_.empty()) { + throw std::invalid_argument("Failed to find session!"); + } + // Remove easy handle from multihandle const CURLMcode error_code = curl_multi_remove_handle(multicurl_->handle, session->curl_->handle); - if (error_code) { + if (error_code != CURLM_OK) { std::cerr << "curl_multi_remove_handle() failed, code " << static_cast(error_code) << '\n'; return; } - // Unock session + // Unlock session session->isUsedInMultiPerform = false; // Remove session from sessions_ diff --git a/test/get_tests.cpp b/test/get_tests.cpp index 63b9b9c8e..ec34878a2 100644 --- a/test/get_tests.cpp +++ b/test/get_tests.cpp @@ -14,6 +14,13 @@ using namespace cpr; static HttpServer* server = new HttpServer(); +TEST(BasicTests, XXXTest) { + Url url{"https://getsolara.dev/api/endpoint.json"}; + Response response = cpr::Get(url); + EXPECT_EQ(200, response.status_code); + EXPECT_EQ(ErrorCode::OK, response.error.code); +} + TEST(BasicTests, HelloWorldTest) { Url url{server->GetBaseUrl() + "/hello.html"}; Response response = cpr::Get(url); diff --git a/test/multiperform_tests.cpp b/test/multiperform_tests.cpp index 9ef57f891..9cf1720fd 100644 --- a/test/multiperform_tests.cpp +++ b/test/multiperform_tests.cpp @@ -92,12 +92,21 @@ TEST(MultiperformRemoveSessionTests, MultiperformRemoveMultipleSessionsTest) { } } -TEST(MultiperformRemoveSessionTests, MultiperformRemoveNonExistingSessionTest) { +TEST(MultiperformRemoveSessionTests, MultiperformRemoveNonExistingSessionEmptyTest) { std::shared_ptr session = std::make_shared(); MultiPerform multiperform; EXPECT_THROW(multiperform.RemoveSession(session), std::invalid_argument); } +TEST(MultiperformRemoveSessionTests, MultiperformRemoveNonExistingSessionTest) { + MultiPerform multiperform; + std::shared_ptr session = std::make_shared(); + multiperform.AddSession(session); + + std::shared_ptr session2 = std::make_shared(); + EXPECT_THROW(multiperform.RemoveSession(session2), std::invalid_argument); +} + TEST(MultiperformGetTests, MultiperformSingleSessionGetTest) { Url url{server->GetBaseUrl() + "/hello.html"}; std::shared_ptr session = std::make_shared();