diff --git a/lib/Commands.cc b/lib/Commands.cc index bfd9a469..a04a386e 100644 --- a/lib/Commands.cc +++ b/lib/Commands.cc @@ -266,7 +266,7 @@ SharedBuffer Commands::newConnect(const AuthenticationPtr& authentication, const BaseCommand cmd; cmd.set_type(BaseCommand::CONNECT); CommandConnect* connect = cmd.mutable_connect(); - connect->set_client_version(PULSAR_VERSION_STR); + connect->set_client_version(std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR); connect->set_auth_method_name(authentication->getAuthMethodName()); connect->set_protocol_version(ProtocolVersion_MAX); @@ -294,7 +294,7 @@ SharedBuffer Commands::newAuthResponse(const AuthenticationPtr& authentication, BaseCommand cmd; cmd.set_type(BaseCommand::AUTH_RESPONSE); CommandAuthResponse* authResponse = cmd.mutable_authresponse(); - authResponse->set_client_version(PULSAR_VERSION_STR); + authResponse->set_client_version(std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR); AuthData* authData = authResponse->mutable_response(); authData->set_auth_method_name(authentication->getAuthMethodName()); diff --git a/tests/ClientTest.cc b/tests/ClientTest.cc index 07fe22fd..d59633f3 100644 --- a/tests/ClientTest.cc +++ b/tests/ClientTest.cc @@ -18,6 +18,7 @@ */ #include #include +#include #include #include @@ -34,6 +35,7 @@ DECLARE_LOG_OBJECT() using namespace pulsar; static std::string lookupUrl = "pulsar://localhost:6650"; +static std::string adminUrl = "http://localhost:8080/"; TEST(ClientTest, testChecksumComputation) { std::string data = "test"; @@ -308,3 +310,34 @@ TEST(ClientTest, testCloseClient) { client.close(); } } + +TEST(ClientTest, testClientVersion) { + const std::string topic = "testClientVersion" + std::to_string(time(nullptr)); + const std::string expectedVersion = std::string("Pulsar-CPP-v") + PULSAR_VERSION_STR; + + Client client(lookupUrl); + + std::string responseData; + + Producer producer; + Result result = client.createProducer(topic, producer); + ASSERT_EQ(ResultOk, result); + int res = + makeGetRequest(adminUrl + "admin/v2/persistent/public/default/" + topic + "/stats", responseData); + ASSERT_TRUE(res == 200) << "res: " << res; + + ASSERT_TRUE(responseData.find(expectedVersion) != std::string::npos); + producer.close(); + + responseData.clear(); + Consumer consumer; + result = client.subscribe(topic, "consumer-1", consumer); + ASSERT_EQ(ResultOk, result); + res = makeGetRequest(adminUrl + "admin/v2/persistent/public/default/" + topic + "/stats", responseData); + ASSERT_TRUE(res == 200) << "res: " << res; + + ASSERT_TRUE(responseData.find(expectedVersion) != std::string::npos); + consumer.close(); + + client.close(); +} diff --git a/tests/HttpHelper.cc b/tests/HttpHelper.cc index c4118e6e..69d59908 100644 --- a/tests/HttpHelper.cc +++ b/tests/HttpHelper.cc @@ -20,7 +20,13 @@ #include -static int makeRequest(const std::string& method, const std::string& url, const std::string& body) { +static size_t curlWriteCallback(void* contents, size_t size, size_t nmemb, void* responseDataPtr) { + ((std::string*)responseDataPtr)->append((char*)contents, size * nmemb); + return size * nmemb; +} + +static int makeRequest(const std::string& method, const std::string& url, const std::string& body, + const std::string& responseData) { CURL* curl = curl_easy_init(); struct curl_slist* list = NULL; @@ -33,6 +39,11 @@ static int makeRequest(const std::string& method, const std::string& url, const if (!body.empty()) { curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str()); } + + // Write callback + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlWriteCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData); + int res = curl_easy_perform(curl); curl_slist_free_all(list); /* free the list again */ @@ -46,10 +57,16 @@ static int makeRequest(const std::string& method, const std::string& url, const return (int)httpResult; } -int makePutRequest(const std::string& url, const std::string& body) { return makeRequest("PUT", url, body); } +int makePutRequest(const std::string& url, const std::string& body) { + return makeRequest("PUT", url, body, ""); +} int makePostRequest(const std::string& url, const std::string& body) { - return makeRequest("POST", url, body); + return makeRequest("POST", url, body, ""); } -int makeDeleteRequest(const std::string& url) { return makeRequest("DELETE", url, ""); } +int makeDeleteRequest(const std::string& url) { return makeRequest("DELETE", url, "", ""); } + +int makeGetRequest(const std::string& url, const std::string& responseData) { + return makeRequest("GET", url, "", responseData); +} diff --git a/tests/HttpHelper.h b/tests/HttpHelper.h index 68119a79..1a03ac51 100644 --- a/tests/HttpHelper.h +++ b/tests/HttpHelper.h @@ -24,5 +24,6 @@ int makePutRequest(const std::string& url, const std::string& body); int makePostRequest(const std::string& url, const std::string& body); int makeDeleteRequest(const std::string& url); +int makeGetRequest(const std::string& url, const std::string& responseData); #endif /* end of include guard: HTTP_HELPER */