2024-01-15 21:28:44 +01:00
|
|
|
#include "common/network/NetworkRequest.hpp"
|
Sort and force grouping of includes (#4172)
This change enforces strict include grouping using IncludeCategories
In addition to adding this to the .clang-format file and applying it in the tests/src and src directories, I also did the following small changes:
In ChatterSet.hpp, I changed lrucache to a <>include
In Irc2.hpp, I change common/SignalVector.hpp to a "project-include"
In AttachedWindow.cpp, NativeMessaging.cpp, WindowsHelper.hpp, BaseWindow.cpp, and StreamerMode.cpp, I disabled clang-format for the windows-includes
In WindowDescriptors.hpp, I added the missing vector include. It was previously not needed because the include was handled by another file that was previously included first.
clang-format minimum version has been bumped, so Ubuntu version used in the check-formatting job has been bumped to 22.04 (which is the latest LTS)
2022-11-27 19:32:53 +01:00
|
|
|
|
2024-01-15 21:28:44 +01:00
|
|
|
#include "common/network/NetworkManager.hpp"
|
|
|
|
#include "common/network/NetworkResult.hpp"
|
2024-10-20 11:57:05 +02:00
|
|
|
#include "NetworkHelpers.hpp"
|
2024-05-05 15:01:07 +02:00
|
|
|
#include "Test.hpp"
|
2020-12-26 12:42:39 +01:00
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
#include <QCoreApplication>
|
2020-12-26 12:42:39 +01:00
|
|
|
|
|
|
|
using namespace chatterino;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-05-30 01:43:13 +02:00
|
|
|
QString getStatusURL(int code)
|
|
|
|
{
|
|
|
|
return QString("%1/status/%2").arg(HTTPBIN_BASE_URL).arg(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString getDelayURL(int delay)
|
2020-12-26 12:42:39 +01:00
|
|
|
{
|
2022-05-30 01:43:13 +02:00
|
|
|
return QString("%1/delay/%2").arg(HTTPBIN_BASE_URL).arg(delay);
|
2020-12-26 12:42:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(NetworkRequest, Success)
|
|
|
|
{
|
|
|
|
const std::vector<int> codes{200, 201, 202, 203, 204, 205, 206};
|
|
|
|
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2020-12-26 12:42:39 +01:00
|
|
|
|
|
|
|
for (const auto code : codes)
|
|
|
|
{
|
|
|
|
auto url = getStatusURL(code);
|
2023-06-24 15:03:27 +02:00
|
|
|
RequestWaiter waiter;
|
2020-12-26 12:42:39 +01:00
|
|
|
|
|
|
|
NetworkRequest(url)
|
2023-11-12 14:51:51 +01:00
|
|
|
.onSuccess([code, &waiter, url](const NetworkResult &result) {
|
|
|
|
EXPECT_EQ(result.status(), code);
|
|
|
|
waiter.requestDone();
|
|
|
|
})
|
2023-06-24 15:03:27 +02:00
|
|
|
.onError([&](const NetworkResult & /*result*/) {
|
2020-12-26 12:42:39 +01:00
|
|
|
// The codes should *not* throw an error
|
|
|
|
EXPECT_TRUE(false);
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.requestDone();
|
2020-12-26 12:42:39 +01:00
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.waitForRequest();
|
2020-12-26 12:42:39 +01:00
|
|
|
}
|
|
|
|
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2020-12-26 12:42:39 +01:00
|
|
|
}
|
|
|
|
|
2021-01-16 18:25:56 +01:00
|
|
|
TEST(NetworkRequest, FinallyCallbackOnSuccess)
|
|
|
|
{
|
|
|
|
const std::vector<int> codes{200, 201, 202, 203, 204, 205, 206};
|
|
|
|
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2021-01-16 18:25:56 +01:00
|
|
|
|
|
|
|
for (const auto code : codes)
|
|
|
|
{
|
|
|
|
auto url = getStatusURL(code);
|
2023-06-24 15:03:27 +02:00
|
|
|
RequestWaiter waiter;
|
2021-01-16 18:25:56 +01:00
|
|
|
|
|
|
|
bool finallyCalled = false;
|
|
|
|
|
|
|
|
NetworkRequest(url)
|
2023-06-24 15:03:27 +02:00
|
|
|
.finally([&waiter, &finallyCalled] {
|
|
|
|
finallyCalled = true;
|
|
|
|
waiter.requestDone();
|
|
|
|
})
|
2021-01-16 18:25:56 +01:00
|
|
|
.execute();
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.waitForRequest();
|
2021-01-16 18:25:56 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(finallyCalled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 12:42:39 +01:00
|
|
|
TEST(NetworkRequest, Error)
|
|
|
|
{
|
|
|
|
const std::vector<int> codes{
|
|
|
|
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410,
|
|
|
|
411, 412, 413, 414, 418, 500, 501, 502, 503, 504,
|
|
|
|
};
|
|
|
|
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2020-12-26 12:42:39 +01:00
|
|
|
|
|
|
|
for (const auto code : codes)
|
|
|
|
{
|
|
|
|
auto url = getStatusURL(code);
|
2023-06-24 15:03:27 +02:00
|
|
|
RequestWaiter waiter;
|
2020-12-26 12:42:39 +01:00
|
|
|
|
|
|
|
NetworkRequest(url)
|
2023-11-12 14:51:51 +01:00
|
|
|
.onSuccess([&waiter, url](const NetworkResult & /*result*/) {
|
|
|
|
// The codes should throw an error
|
|
|
|
EXPECT_TRUE(false);
|
|
|
|
waiter.requestDone();
|
|
|
|
})
|
2023-06-24 15:03:27 +02:00
|
|
|
.onError([code, &waiter, url](const NetworkResult &result) {
|
2020-12-26 12:42:39 +01:00
|
|
|
EXPECT_EQ(result.status(), code);
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.requestDone();
|
2020-12-26 12:42:39 +01:00
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.waitForRequest();
|
2020-12-26 12:42:39 +01:00
|
|
|
}
|
|
|
|
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2020-12-26 12:42:39 +01:00
|
|
|
}
|
|
|
|
|
2021-01-16 18:25:56 +01:00
|
|
|
TEST(NetworkRequest, FinallyCallbackOnError)
|
|
|
|
{
|
|
|
|
const std::vector<int> codes{
|
|
|
|
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410,
|
|
|
|
411, 412, 413, 414, 418, 500, 501, 502, 503, 504,
|
|
|
|
};
|
|
|
|
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2021-01-16 18:25:56 +01:00
|
|
|
|
|
|
|
for (const auto code : codes)
|
|
|
|
{
|
|
|
|
auto url = getStatusURL(code);
|
2023-06-24 15:03:27 +02:00
|
|
|
RequestWaiter waiter;
|
2021-01-16 18:25:56 +01:00
|
|
|
|
|
|
|
bool finallyCalled = false;
|
|
|
|
|
|
|
|
NetworkRequest(url)
|
2023-06-24 15:03:27 +02:00
|
|
|
.finally([&waiter, &finallyCalled] {
|
|
|
|
finallyCalled = true;
|
|
|
|
waiter.requestDone();
|
|
|
|
})
|
2021-01-16 18:25:56 +01:00
|
|
|
.execute();
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.waitForRequest();
|
2021-01-16 18:25:56 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(finallyCalled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-26 12:42:39 +01:00
|
|
|
TEST(NetworkRequest, TimeoutTimingOut)
|
|
|
|
{
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2020-12-26 12:42:39 +01:00
|
|
|
|
2022-05-30 01:43:13 +02:00
|
|
|
auto url = getDelayURL(5);
|
2023-06-24 15:03:27 +02:00
|
|
|
RequestWaiter waiter;
|
2020-12-26 12:42:39 +01:00
|
|
|
|
|
|
|
NetworkRequest(url)
|
|
|
|
.timeout(1000)
|
2023-11-12 14:51:51 +01:00
|
|
|
.onSuccess([&waiter](const NetworkResult & /*result*/) {
|
2020-12-26 12:42:39 +01:00
|
|
|
// The timeout should throw an error
|
|
|
|
EXPECT_TRUE(false);
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.requestDone();
|
2020-12-26 12:42:39 +01:00
|
|
|
})
|
2023-06-24 15:03:27 +02:00
|
|
|
.onError([&waiter, url](const NetworkResult &result) {
|
2020-12-26 12:42:39 +01:00
|
|
|
qDebug() << QTime::currentTime().toString()
|
|
|
|
<< "timeout request finish error";
|
2023-07-01 14:59:59 +02:00
|
|
|
EXPECT_EQ(result.error(),
|
|
|
|
NetworkResult::NetworkError::TimeoutError);
|
|
|
|
EXPECT_EQ(result.status(), std::nullopt);
|
2020-12-26 12:42:39 +01:00
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.requestDone();
|
2020-12-26 12:42:39 +01:00
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.waitForRequest();
|
2020-12-26 12:42:39 +01:00
|
|
|
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2020-12-26 12:42:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NetworkRequest, TimeoutNotTimingOut)
|
|
|
|
{
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2020-12-26 12:42:39 +01:00
|
|
|
|
2022-05-30 01:43:13 +02:00
|
|
|
auto url = getDelayURL(1);
|
2023-06-24 15:03:27 +02:00
|
|
|
RequestWaiter waiter;
|
2020-12-26 12:42:39 +01:00
|
|
|
|
|
|
|
NetworkRequest(url)
|
2023-06-24 15:03:27 +02:00
|
|
|
.timeout(3000)
|
2023-11-12 14:51:51 +01:00
|
|
|
.onSuccess([&waiter, url](const NetworkResult &result) {
|
2020-12-26 12:42:39 +01:00
|
|
|
EXPECT_EQ(result.status(), 200);
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.requestDone();
|
2020-12-26 12:42:39 +01:00
|
|
|
})
|
2023-06-24 15:03:27 +02:00
|
|
|
.onError([&waiter, url](const NetworkResult & /*result*/) {
|
2020-12-26 12:42:39 +01:00
|
|
|
// The timeout should *not* throw an error
|
|
|
|
EXPECT_TRUE(false);
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.requestDone();
|
2020-12-26 12:42:39 +01:00
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.waitForRequest();
|
2020-12-26 12:42:39 +01:00
|
|
|
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2020-12-26 12:42:39 +01:00
|
|
|
}
|
2021-01-16 18:25:56 +01:00
|
|
|
|
|
|
|
TEST(NetworkRequest, FinallyCallbackOnTimeout)
|
|
|
|
{
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2021-01-16 18:25:56 +01:00
|
|
|
|
2022-05-30 01:43:13 +02:00
|
|
|
auto url = getDelayURL(5);
|
2021-01-16 18:25:56 +01:00
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
RequestWaiter waiter;
|
2021-01-16 18:25:56 +01:00
|
|
|
bool finallyCalled = false;
|
|
|
|
bool onSuccessCalled = false;
|
|
|
|
bool onErrorCalled = false;
|
|
|
|
|
|
|
|
NetworkRequest(url)
|
|
|
|
.timeout(1000)
|
2023-11-12 14:51:51 +01:00
|
|
|
.onSuccess([&](const NetworkResult & /*result*/) {
|
2021-01-16 18:25:56 +01:00
|
|
|
onSuccessCalled = true;
|
|
|
|
})
|
2023-06-24 15:03:27 +02:00
|
|
|
.onError([&](const NetworkResult &result) {
|
2021-01-16 18:25:56 +01:00
|
|
|
onErrorCalled = true;
|
2023-07-01 14:59:59 +02:00
|
|
|
EXPECT_EQ(result.error(),
|
|
|
|
NetworkResult::NetworkError::TimeoutError);
|
|
|
|
EXPECT_EQ(result.status(), std::nullopt);
|
2021-01-16 18:25:56 +01:00
|
|
|
})
|
|
|
|
.finally([&] {
|
|
|
|
finallyCalled = true;
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.requestDone();
|
2021-01-16 18:25:56 +01:00
|
|
|
})
|
|
|
|
.execute();
|
|
|
|
|
2023-06-24 15:03:27 +02:00
|
|
|
waiter.waitForRequest();
|
2021-01-16 18:25:56 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(finallyCalled);
|
|
|
|
EXPECT_TRUE(onErrorCalled);
|
|
|
|
EXPECT_FALSE(onSuccessCalled);
|
2024-03-23 11:56:42 +01:00
|
|
|
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
2021-01-16 18:25:56 +01:00
|
|
|
}
|