Skip to content

Commit

Permalink
refactor: cleaned up unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 committed Feb 19, 2024
1 parent 9c549f5 commit 064ee8b
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 84 deletions.
94 changes: 94 additions & 0 deletions src/unittest/http.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#include "test.h"

void http_unit_tests(const std::string& token) {
dpp::http_connect_info hci;
set_test(HOSTINFO, false);

hci = dpp::https_client::get_host_info("https://test.com:444");
bool hci_test = (hci.scheme == "https" && hci.hostname == "test.com" && hci.port == 444 && hci.is_ssl == true);

hci = dpp::https_client::get_host_info("https://test.com");
hci_test = hci_test && (hci.scheme == "https" && hci.hostname == "test.com" && hci.port == 443 && hci.is_ssl == true);

hci = dpp::https_client::get_host_info("http://test.com");
hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 80 && hci.is_ssl == false);

hci = dpp::https_client::get_host_info("http://test.com:90");
hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 90 && hci.is_ssl == false);

hci = dpp::https_client::get_host_info("test.com:97");
hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 97 && hci.is_ssl == false);

hci = dpp::https_client::get_host_info("test.com");
hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 80 && hci.is_ssl == false);

set_test(HOSTINFO, hci_test);

set_test(HTTPS, false);
if (!offline) {
dpp::multipart_content multipart = dpp::https_client::build_multipart(
"{\"content\":\"test\"}", {"test.txt", "blob.blob"}, {"ABCDEFGHI", "BLOB!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"}, {"text/plain", "application/octet-stream"}
);
try {
dpp::https_client c("discord.com", 443, "/api/channels/" + std::to_string(TEST_TEXT_CHANNEL_ID) + "/messages", "POST", multipart.body,
{
{"Content-Type", multipart.mimetype},
{"Authorization", "Bot " + token}
}
);
std::string hdr1 = c.get_header("server");
std::string content1 = c.get_content();
set_test(HTTPS, hdr1 == "cloudflare" && c.get_status() == 200);
}
catch (const dpp::exception& e) {
std::cout << e.what() << "\n";
set_test(HTTPS, false);
}
}

set_test(HTTP, false);
try {
dpp::https_client c2("github.com", 80, "/", "GET", "", {}, true);
std::string hdr2 = c2.get_header("location");
std::string content2 = c2.get_content();
set_test(HTTP, hdr2 == "https://github.com/" && c2.get_status() == 301);
}
catch (const dpp::exception& e) {
std::cout << e.what() << "\n";
set_test(HTTP, false);
}

set_test(MULTIHEADER, false);
try {
dpp::https_client c2("dl.dpp.dev", 443, "/cookietest.php", "GET", "", {});
size_t count = c2.get_header_count("set-cookie");
size_t count_list = c2.get_header_list("set-cookie").size();
// Google sets a bunch of cookies when we start accessing it.
set_test(MULTIHEADER, c2.get_status() == 200 && count > 1 && count == count_list);
}
catch (const dpp::exception& e) {
std::cout << e.what() << "\n";
set_test(MULTIHEADER, false);
}
}
84 changes: 2 additions & 82 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,89 +213,9 @@ int main\\(\\) {\n\
\\`\\`\\`\n\
Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* block\\`\n");

set_test(URLENC, false);
set_test(URLENC, dpp::utility::url_encode("ABC123_+\\|$*/AAA[]😄") == "ABC123_%2B%5C%7C%24%2A%2FAAA%5B%5D%F0%9F%98%84");
http_unit_tests(token);

set_test(BASE64ENC, false);
set_test(BASE64ENC,
dpp::base64_encode(reinterpret_cast<unsigned char const*>("a"), 1) == "YQ==" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("bc"), 2) == "YmM=" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("def"), 3) == "ZGVm" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("ghij"), 4) == "Z2hpag==" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("klmno"), 5) == "a2xtbm8=" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("pqrstu"), 6) == "cHFyc3R1" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("vwxyz12"), 7) == "dnd4eXoxMg=="
);

dpp::http_connect_info hci;
set_test(HOSTINFO, false);

hci = dpp::https_client::get_host_info("https://test.com:444");
bool hci_test = (hci.scheme == "https" && hci.hostname == "test.com" && hci.port == 444 && hci.is_ssl == true);

hci = dpp::https_client::get_host_info("https://test.com");
hci_test = hci_test && (hci.scheme == "https" && hci.hostname == "test.com" && hci.port == 443 && hci.is_ssl == true);

hci = dpp::https_client::get_host_info("http://test.com");
hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 80 && hci.is_ssl == false);

hci = dpp::https_client::get_host_info("http://test.com:90");
hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 90 && hci.is_ssl == false);

hci = dpp::https_client::get_host_info("test.com:97");
hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 97 && hci.is_ssl == false);

hci = dpp::https_client::get_host_info("test.com");
hci_test = hci_test && (hci.scheme == "http" && hci.hostname == "test.com" && hci.port == 80 && hci.is_ssl == false);

set_test(HOSTINFO, hci_test);

set_test(HTTPS, false);
if (!offline) {
dpp::multipart_content multipart = dpp::https_client::build_multipart(
"{\"content\":\"test\"}", {"test.txt", "blob.blob"}, {"ABCDEFGHI", "BLOB!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"}, {"text/plain", "application/octet-stream"}
);
try {
dpp::https_client c("discord.com", 443, "/api/channels/" + std::to_string(TEST_TEXT_CHANNEL_ID) + "/messages", "POST", multipart.body,
{
{"Content-Type", multipart.mimetype},
{"Authorization", "Bot " + token}
}
);
std::string hdr1 = c.get_header("server");
std::string content1 = c.get_content();
set_test(HTTPS, hdr1 == "cloudflare" && c.get_status() == 200);
}
catch (const dpp::exception& e) {
std::cout << e.what() << "\n";
set_test(HTTPS, false);
}
}

set_test(HTTP, false);
try {
dpp::https_client c2("github.com", 80, "/", "GET", "", {}, true);
std::string hdr2 = c2.get_header("location");
std::string content2 = c2.get_content();
set_test(HTTP, hdr2 == "https://github.com/" && c2.get_status() == 301);
}
catch (const dpp::exception& e) {
std::cout << e.what() << "\n";
set_test(HTTP, false);
}

set_test(MULTIHEADER, false);
try {
dpp::https_client c2("dl.dpp.dev", 443, "/cookietest.php", "GET", "", {});
size_t count = c2.get_header_count("set-cookie");
size_t count_list = c2.get_header_list("set-cookie").size();
// Google sets a bunch of cookies when we start accessing it.
set_test(MULTIHEADER, c2.get_status() == 200 && count > 1 && count == count_list);
}
catch (const dpp::exception& e) {
std::cout << e.what() << "\n";
set_test(MULTIHEADER, false);
}
utilities_unit_tests();

std::vector<uint8_t> testaudio = load_test_audio();

Expand Down
8 changes: 6 additions & 2 deletions src/unittest/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* D++, A Lightweight C++ library for Discord
*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2021 Craig Edwards and D++ contributors
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -181,7 +181,7 @@ DPP_TEST(USER_GET_AVATAR_URL, "user::get_avatar_url", tf_offline);
DPP_TEST(CHANNEL_SET_TYPE, "channel::set_type", tf_offline);
DPP_TEST(CHANNEL_GET_MENTION, "channel::get_mention", tf_offline);
DPP_TEST(CHANNEL_GET_URL, "channel::get_url", tf_offline);
DPP_TEST(MESSAGE_GET_URL,"message::get_url",tf_offline);
DPP_TEST(MESSAGE_GET_URL, "message::get_url", tf_offline);
DPP_TEST(UTILITY_GUILD_NAVIGATION, "utility::guild_navigation", tf_offline);
DPP_TEST(UTILITY_ICONHASH, "utility::iconhash", tf_offline);
DPP_TEST(UTILITY_MAKE_URL_PARAMETERS, "utility::make_url_parameters", tf_offline);
Expand Down Expand Up @@ -507,6 +507,10 @@ double get_start_time();
*/
double get_time();

void http_unit_tests(const std::string& token);

void utilities_unit_tests();

/**
* @brief A test version of the message collector for use in unit tests
*/
Expand Down
38 changes: 38 additions & 0 deletions src/unittest/utilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/
#include "test.h"

void utilities_unit_tests() {
set_test(URLENC, false);
set_test(URLENC, dpp::utility::url_encode("ABC123_+\\|$*/AAA[]😄") == "ABC123_%2B%5C%7C%24%2A%2FAAA%5B%5D%F0%9F%98%84");

set_test(BASE64ENC, false);
set_test(BASE64ENC,
dpp::base64_encode(reinterpret_cast<unsigned char const*>("a"), 1) == "YQ==" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("bc"), 2) == "YmM=" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("def"), 3) == "ZGVm" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("ghij"), 4) == "Z2hpag==" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("klmno"), 5) == "a2xtbm8=" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("pqrstu"), 6) == "cHFyc3R1" &&
dpp::base64_encode(reinterpret_cast<unsigned char const*>("vwxyz12"), 7) == "dnd4eXoxMg=="
);
}

0 comments on commit 064ee8b

Please sign in to comment.