mirror-chatterino2/tests/src/NetworkRequest.cpp

234 lines
5.9 KiB
C++
Raw Normal View History

#include "common/network/NetworkRequest.hpp"
#include "common/network/NetworkManager.hpp"
#include "common/network/NetworkResult.hpp"
#include "NetworkHelpers.hpp"
#include "Test.hpp"
2023-06-24 15:03:27 +02:00
#include <QCoreApplication>
using namespace chatterino;
namespace {
QString getStatusURL(int code)
{
return QString("%1/status/%2").arg(HTTPBIN_BASE_URL).arg(code);
}
QString getDelayURL(int delay)
{
return QString("%1/delay/%2").arg(HTTPBIN_BASE_URL).arg(delay);
}
} // namespace
TEST(NetworkRequest, Success)
{
const std::vector<int> codes{200, 201, 202, 203, 204, 205, 206};
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
for (const auto code : codes)
{
auto url = getStatusURL(code);
2023-06-24 15:03:27 +02:00
RequestWaiter waiter;
NetworkRequest(url)
.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*/) {
// The codes should *not* throw an error
EXPECT_TRUE(false);
2023-06-24 15:03:27 +02:00
waiter.requestDone();
})
.execute();
2023-06-24 15:03:27 +02:00
waiter.waitForRequest();
}
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
}
TEST(NetworkRequest, FinallyCallbackOnSuccess)
{
const std::vector<int> codes{200, 201, 202, 203, 204, 205, 206};
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
for (const auto code : codes)
{
auto url = getStatusURL(code);
2023-06-24 15:03:27 +02:00
RequestWaiter waiter;
bool finallyCalled = false;
NetworkRequest(url)
2023-06-24 15:03:27 +02:00
.finally([&waiter, &finallyCalled] {
finallyCalled = true;
waiter.requestDone();
})
.execute();
2023-06-24 15:03:27 +02:00
waiter.waitForRequest();
EXPECT_TRUE(finallyCalled);
}
}
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,
};
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
for (const auto code : codes)
{
auto url = getStatusURL(code);
2023-06-24 15:03:27 +02:00
RequestWaiter waiter;
NetworkRequest(url)
.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) {
EXPECT_EQ(result.status(), code);
2023-06-24 15:03:27 +02:00
waiter.requestDone();
})
.execute();
2023-06-24 15:03:27 +02:00
waiter.waitForRequest();
}
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
}
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,
};
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
for (const auto code : codes)
{
auto url = getStatusURL(code);
2023-06-24 15:03:27 +02:00
RequestWaiter waiter;
bool finallyCalled = false;
NetworkRequest(url)
2023-06-24 15:03:27 +02:00
.finally([&waiter, &finallyCalled] {
finallyCalled = true;
waiter.requestDone();
})
.execute();
2023-06-24 15:03:27 +02:00
waiter.waitForRequest();
EXPECT_TRUE(finallyCalled);
}
}
TEST(NetworkRequest, TimeoutTimingOut)
{
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
auto url = getDelayURL(5);
2023-06-24 15:03:27 +02:00
RequestWaiter waiter;
NetworkRequest(url)
.timeout(1000)
.onSuccess([&waiter](const NetworkResult & /*result*/) {
// The timeout should throw an error
EXPECT_TRUE(false);
2023-06-24 15:03:27 +02:00
waiter.requestDone();
})
2023-06-24 15:03:27 +02:00
.onError([&waiter, url](const NetworkResult &result) {
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);
2023-06-24 15:03:27 +02:00
waiter.requestDone();
})
.execute();
2023-06-24 15:03:27 +02:00
waiter.waitForRequest();
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
}
TEST(NetworkRequest, TimeoutNotTimingOut)
{
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
auto url = getDelayURL(1);
2023-06-24 15:03:27 +02:00
RequestWaiter waiter;
NetworkRequest(url)
2023-06-24 15:03:27 +02:00
.timeout(3000)
.onSuccess([&waiter, url](const NetworkResult &result) {
EXPECT_EQ(result.status(), 200);
2023-06-24 15:03:27 +02:00
waiter.requestDone();
})
2023-06-24 15:03:27 +02:00
.onError([&waiter, url](const NetworkResult & /*result*/) {
// The timeout should *not* throw an error
EXPECT_TRUE(false);
2023-06-24 15:03:27 +02:00
waiter.requestDone();
})
.execute();
2023-06-24 15:03:27 +02:00
waiter.waitForRequest();
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
}
TEST(NetworkRequest, FinallyCallbackOnTimeout)
{
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
auto url = getDelayURL(5);
2023-06-24 15:03:27 +02:00
RequestWaiter waiter;
bool finallyCalled = false;
bool onSuccessCalled = false;
bool onErrorCalled = false;
NetworkRequest(url)
.timeout(1000)
.onSuccess([&](const NetworkResult & /*result*/) {
onSuccessCalled = true;
})
2023-06-24 15:03:27 +02:00
.onError([&](const NetworkResult &result) {
onErrorCalled = true;
2023-07-01 14:59:59 +02:00
EXPECT_EQ(result.error(),
NetworkResult::NetworkError::TimeoutError);
EXPECT_EQ(result.status(), std::nullopt);
})
.finally([&] {
finallyCalled = true;
2023-06-24 15:03:27 +02:00
waiter.requestDone();
})
.execute();
2023-06-24 15:03:27 +02:00
waiter.waitForRequest();
EXPECT_TRUE(finallyCalled);
EXPECT_TRUE(onErrorCalled);
EXPECT_FALSE(onSuccessCalled);
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
}