keepassxc/tests/TestNetworkRequest.cpp

248 lines
9.8 KiB
C++
Raw Normal View History

2023-05-09 18:59:16 -04:00
#include "TestNetworkRequest.h"
#include "core/NetworkRequest.h"
#include "mock/MockNetworkAccessManager.h"
#include <QSignalSpy>
#include <QTest>
#include <QUrl>
QTEST_GUILESS_MAIN(TestNetworkRequest)
2023-05-10 16:30:42 -04:00
using ContentTypeParameters_t = QHash<QString, QString>;
Q_DECLARE_METATYPE(ContentTypeParameters_t);
2023-05-25 11:10:00 -04:00
Q_DECLARE_METATYPE(std::chrono::milliseconds);
2023-05-10 16:30:42 -04:00
2023-05-09 18:59:16 -04:00
void TestNetworkRequest::testNetworkRequest()
{
QFETCH(QUrl, requestedURL);
QFETCH(QByteArray, expectedContent);
2023-05-10 16:30:42 -04:00
QFETCH(QString, responseContentType);
QFETCH(QString, expectedContentType);
QFETCH(ContentTypeParameters_t, expectedContentTypeParameters);
2023-05-09 18:59:16 -04:00
QFETCH(QString, expectedUserAgent);
QFETCH(bool, expectError);
QFETCH(QNetworkReply::NetworkError, error);
// Create mock reply
// Create and configure the mocked network access manager
MockNetworkAccess::Manager<QNetworkAccessManager> manager;
auto& reply = manager
.whenGet(requestedURL)
// Has right user agent?
.has(MockNetworkAccess::Predicates::HeaderMatching(QNetworkRequest::UserAgentHeader,
QRegularExpression(expectedUserAgent)))
.reply();
if (!expectError) {
2023-05-10 16:30:42 -04:00
reply.withBody(expectedContent).withHeader(QNetworkRequest::ContentTypeHeader, responseContentType);
2023-05-09 18:59:16 -04:00
} else {
reply.withError(error);
}
// Create request
2023-05-10 16:06:56 -04:00
NetworkRequest request = createRequest(requestedURL, 5, std::chrono::milliseconds{5000}, QList<QPair<QString, QString>>{}, &manager);
2023-05-09 18:59:16 -04:00
QString actualContent;
bool didError = false, didSucceed = false;
// Check request
QSignalSpy spy(&request, &NetworkRequest::success);
connect(&request, &NetworkRequest::success, [&actualContent, &didSucceed](QByteArray content) {
actualContent = QString(content);
didSucceed = true;
});
QSignalSpy errorSpy(&request, &NetworkRequest::failure);
connect(&request, &NetworkRequest::failure, [&didError]() { didError = true; });
QTest::qWait(3*100);
// Ensures that predicates match - i.e., the header was set correctly
QCOMPARE(manager.matchedRequests().length(), 1);
2023-05-10 16:06:56 -04:00
QCOMPARE(request.URL(), requestedURL);
2023-05-09 18:59:16 -04:00
if(!expectError) {
QCOMPARE(actualContent, expectedContent);
2023-05-10 16:30:42 -04:00
QCOMPARE(request.ContentType(), expectedContentType);
QCOMPARE(request.ContentTypeParameters(), expectedContentTypeParameters);
2023-05-09 18:59:16 -04:00
QCOMPARE(didSucceed, true);
QCOMPARE(didError, false);
2023-05-10 16:30:42 -04:00
QCOMPARE(request.Reply()->isFinished(), true);
2023-05-09 18:59:16 -04:00
} else {
QCOMPARE(didSucceed, false);
QCOMPARE(didError, true);
}
}
void TestNetworkRequest::testNetworkRequest_data()
{
QTest::addColumn<QUrl>("requestedURL");
QTest::addColumn<QByteArray>("expectedContent");
2023-05-10 16:30:42 -04:00
QTest::addColumn<QString>("responseContentType");
QTest::addColumn<QString>("expectedContentType");
QTest::addColumn<ContentTypeParameters_t>("expectedContentTypeParameters");
2023-05-09 18:59:16 -04:00
QTest::addColumn<QString>("expectedUserAgent");
QTest::addColumn<bool>("expectError");
QTest::addColumn<QNetworkReply::NetworkError>("error");
QString defaultUserAgent("KeePassXC");
const QUrl& exampleURL = QUrl{"https://example.com"};
const QByteArray& exampleContent = QString{"test-content"}.toUtf8();
2023-05-10 16:30:42 -04:00
QTest::newRow("successful request") << exampleURL << exampleContent << "text/plain"
<< "text/plain" << ContentTypeParameters_t{}
<< defaultUserAgent << false << QNetworkReply::NetworkError::NoError;
QTest::newRow("content type") << exampleURL << exampleContent << "application/test-content-type"
<< "application/test-content-type" << ContentTypeParameters_t{}
<< defaultUserAgent << false << QNetworkReply::NetworkError::NoError;
2023-05-25 11:10:00 -04:00
QTest::newRow("empty content type") << exampleURL << QByteArray{} << "" << "" << ContentTypeParameters_t{}
<< defaultUserAgent << false << QNetworkReply::NetworkError::NoError;
2023-05-10 16:30:42 -04:00
QTest::newRow("content type parameters") << exampleURL << exampleContent << "application/test-content-type;test-param=test-value"
<< "application/test-content-type" << ContentTypeParameters_t {{"test-param", "test-value"}}
<< defaultUserAgent << false << QNetworkReply::NetworkError::NoError;
QTest::newRow("content type parameters trimmed") << exampleURL << exampleContent << "application/test-content-type; test-param = test-value"
<< "application/test-content-type" << ContentTypeParameters_t {{"test-param", "test-value"}}
<< defaultUserAgent << false << QNetworkReply::NetworkError::NoError;
2023-05-09 18:59:16 -04:00
}
void TestNetworkRequest::testNetworkRequestTimeout()
{
2023-05-10 16:30:42 -04:00
// Timeout should work for single request
// Timeout should capture entire duration, including redirects
2023-05-25 11:10:00 -04:00
QFETCH(bool, expectError);
QFETCH(std::chrono::milliseconds, delay);
QFETCH(std::chrono::milliseconds, timeout);
const auto requestedURL = QUrl("https://example.com");
const auto expectedUserAgent = QString("KeePassXC");
// Create mock reply
// Create and configure the mocked network access manager
MockNetworkAccess::Manager<QNetworkAccessManager> manager;
auto& reply = manager
.whenGet(requestedURL)
// Has right user agent?
.has(MockNetworkAccess::Predicates::HeaderMatching(QNetworkRequest::UserAgentHeader,
QRegularExpression(expectedUserAgent)))
.reply();
// Timeout
QTimer timer;
timer.setSingleShot(true);
timer.setInterval(delay);
reply.withFinishDelayUntil(&timer, &QTimer::timeout);
// Create request
NetworkRequest request = createRequest(requestedURL, 5, timeout, QList<QPair<QString, QString>>{}, &manager);
// Start timer
timer.start();
bool didSucceed = false, didError = false;
// Check request
QSignalSpy spy(&request, &NetworkRequest::success);
connect(&request, &NetworkRequest::success, [&didSucceed](const QByteArray&) {
didSucceed = true;
});
QSignalSpy errorSpy(&request, &NetworkRequest::failure);
connect(&request, &NetworkRequest::failure, [&didError]() { didError = true; });
QTest::qWait(3*100);
QTEST_ASSERT(didError || didSucceed);
// Ensures that predicates match - i.e., the header was set correctly
QCOMPARE(manager.matchedRequests().length(), 1);
QCOMPARE(request.URL(), requestedURL);
QCOMPARE(didSucceed, !expectError);
QCOMPARE(didError, expectError);
}
void TestNetworkRequest::testNetworkRequestTimeout_data()
{
QTest::addColumn<bool>("expectError");
QTest::addColumn<std::chrono::milliseconds>("delay");
QTest::addColumn<std::chrono::milliseconds>("timeout");
QTest::newRow("timeout") << true << std::chrono::milliseconds{200} << std::chrono::milliseconds{100};
QTest::newRow("no timeout") << false << std::chrono::milliseconds{100} << std::chrono::milliseconds{200};
2023-05-09 18:59:16 -04:00
}
2023-05-25 11:10:00 -04:00
2023-05-09 18:59:16 -04:00
void TestNetworkRequest::testNetworkRequestRedirects()
{
2023-05-10 16:30:42 -04:00
// Should respect max number of redirects
// Headers, Reply, etc. should reflect final request
2023-05-26 10:08:59 -04:00
QFETCH(int, numRedirects);
QFETCH(int, maxRedirects);
const bool expectError = numRedirects > maxRedirects;
const auto requestedURL = QUrl("https://example.com");
const auto expectedUserAgent = QString("KeePassXC");
// Create mock reply
// Create and configure the mocked network access manager
MockNetworkAccess::Manager<QNetworkAccessManager> manager;
QStringList requestedUrls;
auto* reply = &manager
.whenGet(requestedURL)
// Has right user agent?
.has(MockNetworkAccess::Predicates::HeaderMatching(QNetworkRequest::UserAgentHeader,
QRegularExpression(expectedUserAgent)))
.reply();
for(int i = 0; i < numRedirects; ++i) {
auto redirectTarget = QUrl("https://example.com/redirect" + QString::number(i));
reply->withRedirect(redirectTarget);
reply = &manager.whenGet(redirectTarget)
// Has right user agent?
.has(MockNetworkAccess::Predicates::HeaderMatching(QNetworkRequest::UserAgentHeader,
QRegularExpression(expectedUserAgent)))
.reply();
}
reply->withBody(QString{"test-content"}.toUtf8());
// Create request
NetworkRequest request = createRequest(requestedURL, maxRedirects, std::chrono::milliseconds{5000}, QList<QPair<QString, QString>>{}, &manager);
bool didSucceed = false, didError = false;
// Check request
QSignalSpy spy(&request, &NetworkRequest::success);
connect(&request, &NetworkRequest::success, [&didSucceed](const QByteArray&) {
didSucceed = true;
});
QSignalSpy errorSpy(&request, &NetworkRequest::failure);
connect(&request, &NetworkRequest::failure, [&didError]() { didError = true; });
QTest::qWait(3*100);
QTEST_ASSERT(didError || didSucceed);
// Ensures that predicates match - i.e., the header was set correctly
QCOMPARE(didSucceed, !expectError);
QCOMPARE(didError, expectError);
if(didSucceed) {
QCOMPARE(manager.matchedRequests().length(), numRedirects + 1);
QCOMPARE(request.URL(), requestedURL);
}
2023-05-09 18:59:16 -04:00
}
2023-05-26 10:08:59 -04:00
void TestNetworkRequest::testNetworkRequestRedirects_data()
{
QTest::addColumn<int>("numRedirects");
QTest::addColumn<int>("maxRedirects");
QTest::newRow("all good (0)") << 0 << 5;
QTest::newRow("all good (1)") << 1 << 5;
QTest::newRow("all good (2)") << 2 << 5;
QTest::newRow("no good (1, 0)") << 1 << 0;
QTest::newRow("no good (2, 1)") << 2 << 1;
QTest::newRow("no good (3, 2)") << 3 << 2;
}