Skip to content

Commit 0d1cae8

Browse files
committed
Fixed multiperform session remove error handling
1 parent 3febe37 commit 0d1cae8

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

cpr/multiperform.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <cassert>
1010
#include <cstddef>
1111
#include <curl/curl.h>
12-
#include <curl/multi.h>
1312
#include <curl/curlver.h>
13+
#include <curl/multi.h>
1414
#include <functional>
1515
#include <iosfwd>
1616
#include <iostream>
@@ -68,14 +68,19 @@ void MultiPerform::AddSession(std::shared_ptr<Session>& session, HttpMethod meth
6868
}
6969

7070
void MultiPerform::RemoveSession(const std::shared_ptr<Session>& session) {
71+
// Has to be handled before calling curl_multi_remove_handle to avoid it returning something != CURLM_OK.
72+
if (sessions_.empty()) {
73+
throw std::invalid_argument("Failed to find session!");
74+
}
75+
7176
// Remove easy handle from multihandle
7277
const CURLMcode error_code = curl_multi_remove_handle(multicurl_->handle, session->curl_->handle);
73-
if (error_code) {
78+
if (error_code != CURLM_OK) {
7479
std::cerr << "curl_multi_remove_handle() failed, code " << static_cast<int>(error_code) << '\n';
7580
return;
7681
}
7782

78-
// Unock session
83+
// Unlock session
7984
session->isUsedInMultiPerform = false;
8085

8186
// Remove session from sessions_

test/get_tests.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ using namespace cpr;
1414

1515
static HttpServer* server = new HttpServer();
1616

17+
TEST(BasicTests, XXXTest) {
18+
Url url{"https://getsolara.dev/api/endpoint.json"};
19+
Response response = cpr::Get(url);
20+
EXPECT_EQ(200, response.status_code);
21+
EXPECT_EQ(ErrorCode::OK, response.error.code);
22+
}
23+
1724
TEST(BasicTests, HelloWorldTest) {
1825
Url url{server->GetBaseUrl() + "/hello.html"};
1926
Response response = cpr::Get(url);

test/multiperform_tests.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,21 @@ TEST(MultiperformRemoveSessionTests, MultiperformRemoveMultipleSessionsTest) {
9292
}
9393
}
9494

95-
TEST(MultiperformRemoveSessionTests, MultiperformRemoveNonExistingSessionTest) {
95+
TEST(MultiperformRemoveSessionTests, MultiperformRemoveNonExistingSessionEmptyTest) {
9696
std::shared_ptr<Session> session = std::make_shared<Session>();
9797
MultiPerform multiperform;
9898
EXPECT_THROW(multiperform.RemoveSession(session), std::invalid_argument);
9999
}
100100

101+
TEST(MultiperformRemoveSessionTests, MultiperformRemoveNonExistingSessionTest) {
102+
MultiPerform multiperform;
103+
std::shared_ptr<Session> session = std::make_shared<Session>();
104+
multiperform.AddSession(session);
105+
106+
std::shared_ptr<Session> session2 = std::make_shared<Session>();
107+
EXPECT_THROW(multiperform.RemoveSession(session2), std::invalid_argument);
108+
}
109+
101110
TEST(MultiperformGetTests, MultiperformSingleSessionGetTest) {
102111
Url url{server->GetBaseUrl() + "/hello.html"};
103112
std::shared_ptr<Session> session = std::make_shared<Session>();

0 commit comments

Comments
 (0)