Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cleaned up unit-tests #1090

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions src/unittest/errors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/************************************************************************************
*
* 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 errors_unit_tests() {
set_test(ERRORS, false);

/* Prepare a confirmation_callback_t in error state (400) */
dpp::confirmation_callback_t error_test;
bool error_message_success = false;
error_test.http_info.status = 400;

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"options\": {\
\"0\": {\
\"name\": {\
\"_errors\": [\
{\
\"code\": \"STRING_TYPE_REGEX\",\
\"message\": \"String value did not match validation regex.\"\
},\
{\
\"code\": \"APPLICATION_COMMAND_INVALID_NAME\",\
\"message\": \"Command name is invalid\"\
}\
]\
}\
}\
}\
}\
}";
error_message_success = (error_test.get_error().human_readable == "50035: Invalid Form Body\n\t- options[0].name: String value did not match validation regex. (STRING_TYPE_REGEX)\n\t- options[0].name: Command name is invalid (APPLICATION_COMMAND_INVALID_NAME)");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"type\": {\
\"_errors\": [\
{\
\"code\": \"BASE_TYPE_CHOICES\",\
\"message\": \"Value must be one of {4, 5, 9, 10, 11}.\"\
}\
]\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - type: Value must be one of {4, 5, 9, 10, 11}. (BASE_TYPE_CHOICES)");

error_test.http_info.body = "{\
\"message\": \"Unknown Guild\",\
\"code\": 10004\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "10004: Unknown Guild");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"allowed_mentions\": {\
\"_errors\": [\
{\
\"code\": \"MESSAGE_ALLOWED_MENTIONS_PARSE_EXCLUSIVE\",\
\"message\": \"parse:[\\\"users\\\"] and users: [ids...] are mutually exclusive.\"\
}\
]\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - allowed_mentions: parse:[\"users\"] and users: [ids...] are mutually exclusive. (MESSAGE_ALLOWED_MENTIONS_PARSE_EXCLUSIVE)");

error_test.http_info.body = "{\
\"message\": \"Invalid Form Body\",\
\"code\": 50035,\
\"errors\": {\
\"1\": {\
\"options\": {\
\"1\": {\
\"description\": {\
\"_errors\": [\
{\
\"code\": \"BASE_TYPE_BAD_LENGTH\",\
\"message\": \"Must be between 1 and 100 in length.\"\
}\
]\
}\
}\
}\
}\
}\
}";
error_message_success = (error_message_success && error_test.get_error().human_readable == "50035: Invalid Form Body - <array>[1].options[1].description: Must be between 1 and 100 in length. (BASE_TYPE_BAD_LENGTH)");

set_test(ERRORS, error_message_success);
}
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);
}
}
Loading
Loading