diff --git a/CHANGELOG.md b/CHANGELOG.md index e12c0ae3..818731fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed +- improved class members and test coverage ([#93]) - improved formatting and maintainability ([#92]) - dropped Hunter Package Manager in favor of git submodules in OS builds. ([#90]) - updated ArduinoJson 5.13.2 >> 6.10.1, usage patterns, and documentation/examples. ([#87]) diff --git a/src/host/host.cpp b/src/host/host.cpp index 7f61a2c3..583613d2 100644 --- a/src/host/host.cpp +++ b/src/host/host.cpp @@ -1,53 +1,50 @@ #include "host/host.h" -Ark::Client::Host::Host() : ip_(), port_(-1) {} - -/***/ +#include +#include -Ark::Client::Host::Host(const Host& other) : port_(other.port_) { - strncpy(this->ip_, other.ip_, sizeof(this->ip_) / sizeof(this->ip_[0])); -} +Ark::Client::Host::Host() : ip_(), port_(-1) {} -/***/ +/**/ -Ark::Client::Host& Ark::Client::Host::Host::operator=(const Host& other) noexcept { - if (this != &other) { - strncpy(this->ip_, other.ip_, sizeof(this->ip_) / sizeof(this->ip_[0])); - this->port_ = other.port_; - } - return *this; +Ark::Client::Host::Host( + const char* const newIP, + int newPort) : port_(newPort) { + strncpy(this->ip_, newIP, sizeof(this->ip_)); } -/***/ +/**/ -Ark::Client::Host::Host(const char* const newIP, int newPort) : port_(newPort) { - strncpy(this->ip_, newIP, sizeof(this->ip_) / sizeof(this->ip_[0])); +bool Ark::Client::Host::set( + const char* const newIP, + int newPort) { + strncpy(this->ip_, newIP, sizeof(this->ip_)); + this->port_ = newPort; + return + ((this->port_ == newPort) + && (strcmp(this->ip_, newIP) != 0)); } -/***/ +/**/ -const char* Ark::Client::Host::ip() const noexcept { +std::string Ark::Client::Host::ip() const noexcept { return this->ip_; }; -/***/ +/**/ int Ark::Client::Host::port() const noexcept { return this->port_; }; -/***/ +/**/ std::string Ark::Client::Host::toString() { char temp[36] = {}; - snprintf(temp, sizeof(this->ip_) + sizeof(this->port_), "%s:%d", this->ip_, this->port_); + snprintf( + temp, + sizeof(this->ip_) + sizeof(this->port_), + "%s:%d", + this->ip_, this->port_); return temp; } - -/***/ - -bool Ark::Client::Host::set(const char* const newIP, int newPort) { - strncpy(this->ip_, newIP, sizeof(this->ip_) / sizeof(this->ip_[0])); - this->port_ = newPort; - return ((this->port_ == newPort) && (strcmp(this->ip_, newIP) != 0)); -} diff --git a/src/include/cpp-client/host/host.h b/src/include/cpp-client/host/host.h index f430f223..9b48088d 100644 --- a/src/include/cpp-client/host/host.h +++ b/src/include/cpp-client/host/host.h @@ -24,21 +24,16 @@ class Host { char ip_[17]; int port_; - Host(Host&&) = delete; - Host& operator=(Host&&) = delete; - Host(const Host& other); - Host& operator=(const Host& other) noexcept; - public: Host(); Host(const char* const newIP, int newPort); bool set(const char* const newIP, int newPort); - const char* ip() const noexcept; + std::string ip() const noexcept; int port() const noexcept; - std::string toString(); // e.g. "167.114.29.49:4003" + std::string toString(); }; /**/ }; // namespace Client diff --git a/test/api/paths.cpp b/test/api/paths.cpp index bfe1a6dc..be60af3b 100644 --- a/test/api/paths.cpp +++ b/test/api/paths.cpp @@ -110,10 +110,13 @@ TEST(paths, test_transactions) { // NOLINT const auto types = Ark::Client::API::Paths::Transactions::types(testHost); ASSERT_STREQ("0.0.0.0:4003/api/v2/transactions/types", types.c_str()); - const std::map searchBody = {{"id", "dummy"}}; + const std::map searchBody = { + { "id", "dummy" }, + { "key", "value" } + }; const auto search = Ark::Client::API::Paths::Transactions::search(testHost, searchBody, 5, 1); ASSERT_STREQ("0.0.0.0:4003/api/v2/transactions/search?limit=5&page=1", search.first.c_str()); - ASSERT_STREQ("id=dummy", search.second.c_str()); + ASSERT_STREQ("id=dummy&key=value", search.second.c_str()); std::string jsonTransaction = "{" "\"id\":\"5ab523d18ac948da82700a71fc0b3c9e764fc0cba91927cb1aa63354564ad23f\"," diff --git a/test/connection/connection.cpp b/test/connection/connection.cpp index e7c1eaf2..c6ac613d 100644 --- a/test/connection/connection.cpp +++ b/test/connection/connection.cpp @@ -9,7 +9,7 @@ TEST(api, test_connection) { // NOLINT int version = connection.api.version(); ASSERT_EQ(2, version); - const char* ip = connection.host.ip(); + const auto ip = connection.host.ip().c_str(); ASSERT_STREQ("167.114.29.55", ip); int port = connection.host.port(); diff --git a/test/host/host.cpp b/test/host/host.cpp index 5bbc9f6d..030f37a1 100644 --- a/test/host/host.cpp +++ b/test/host/host.cpp @@ -1,17 +1,36 @@ + #include "host/host.h" #include "gtest/gtest.h" TEST(api, test_host) { // NOLINT - Ark::Client::Host host("167.114.29.55", 4003); - - const auto ip = host.ip(); const auto expectedIp = "167.114.29.55"; - ASSERT_STREQ(expectedIp, ip); + const auto expectedPort = 4003; + const auto expectedHost = "167.114.29.55:4003"; - int port = host.port(); - ASSERT_EQ(4003, port); + // create a test host + Ark::Client::Host host(expectedIp, expectedPort); + // check the host // + // check the IP + const auto ip = host.ip().c_str(); + ASSERT_STREQ(expectedIp, ip); + // check the Port + int port = host.port(); + ASSERT_EQ(expectedPort, port); + // check the host string const auto hostString = host.toString().c_str(); - const auto expectedHost = "167.114.29.55:4003"; ASSERT_STREQ(expectedHost, hostString); + + // test setting the host // + Ark::Client::Host setHost; + setHost.set(expectedIp, expectedPort); + ASSERT_STREQ(setHost.toString().c_str(), host.toString().c_str()); + + // test the copy constructor // + Ark::Client::Host copiedHost(host); + ASSERT_STREQ(copiedHost.toString().c_str(), host.toString().c_str()); + + // test the assignment constructor // + Ark::Client::Host assignedHost(host); + ASSERT_STREQ(assignedHost.toString().c_str(), host.toString().c_str()); } diff --git a/test/http/http.cpp b/test/http/http.cpp index 3e2b00d0..2f96fd79 100644 --- a/test/http/http.cpp +++ b/test/http/http.cpp @@ -47,6 +47,27 @@ TEST(api, test_http_post_body) { // NOLINT /**/ +// Tests invalid POSTing of HTTP body. +TEST(api, test_http_invalid_post_body) { // NOLINT + // Create the HTTP object + const auto http = Ark::Client::makeHTTP(); + + // Create a malformed Request URL and 'Post' body. + const auto request = "/167.114.29.55:4003/api/v2/wallets/search"; + const auto body = "username=baldninja"; + + // Post the 'request' and 'body' for a response using HTTP + const auto response = http->post(request, body); + + // Create a JSON object of the result + DynamicJsonDocument doc(100); + DeserializationError error = deserializeJson(doc, response); + // the empty response should cause deserialization to fail + ASSERT_TRUE(error); +} + +/**/ + // Tests POSTing of JSON. TEST(api, test_http_post_json) { // NOLINT // Create the HTTP object