mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-01-29 15:53:38 -05:00
Implement first tests.
This commit is contained in:
parent
b4249ea941
commit
b829b67a38
@ -150,6 +150,8 @@ if(WITH_XC_NETWORKING)
|
||||
LIBS ${TEST_LIBRARIES})
|
||||
|
||||
add_unit_test(NAME testicondownloader SOURCES TestIconDownloader.cpp LIBS ${TEST_LIBRARIES})
|
||||
|
||||
add_unit_test(NAME testnetworkrequest SOURCES TestNetworkRequest.cpp mock/MockNetworkAccessManager.cpp LIBS ${TEST_LIBRARIES})
|
||||
endif()
|
||||
|
||||
if(WITH_XC_AUTOTYPE)
|
||||
|
97
tests/TestNetworkRequest.cpp
Normal file
97
tests/TestNetworkRequest.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
#include "TestNetworkRequest.h"
|
||||
#include "core/NetworkRequest.h"
|
||||
#include "mock/MockNetworkAccessManager.h"
|
||||
#include <QSignalSpy>
|
||||
#include <QTest>
|
||||
#include <QUrl>
|
||||
|
||||
QTEST_GUILESS_MAIN(TestNetworkRequest)
|
||||
|
||||
void TestNetworkRequest::testNetworkRequest()
|
||||
{
|
||||
QFETCH(QUrl, requestedURL);
|
||||
QFETCH(QByteArray, expectedContent);
|
||||
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) {
|
||||
reply.withBody(expectedContent);
|
||||
} else {
|
||||
reply.withError(error);
|
||||
}
|
||||
|
||||
// Create request
|
||||
NetworkRequest request = createRequest(5, std::chrono::milliseconds{5000}, QList<QPair<QString, QString>>{}, &manager);
|
||||
request.fetch(requestedURL);
|
||||
|
||||
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);
|
||||
if(!expectError) {
|
||||
// Ensures that NetworkRequest parses the reply properly
|
||||
// URL correct?
|
||||
QCOMPARE(request.url(), requestedURL);
|
||||
// Content correct?
|
||||
QCOMPARE(actualContent, expectedContent);
|
||||
QCOMPARE(didSucceed, true);
|
||||
QCOMPARE(didError, false);
|
||||
} else {
|
||||
// Ensures that NetworkRequest parses the reply properly
|
||||
// URL correct?
|
||||
QCOMPARE(request.url(), requestedURL);
|
||||
// Content correct?
|
||||
QCOMPARE(didSucceed, false);
|
||||
QCOMPARE(didError, true);
|
||||
}
|
||||
}
|
||||
void TestNetworkRequest::testNetworkRequest_data()
|
||||
{
|
||||
QTest::addColumn<QUrl>("requestedURL");
|
||||
QTest::addColumn<QByteArray>("expectedContent");
|
||||
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();
|
||||
|
||||
QTest::newRow("successful request") << exampleURL << exampleContent << defaultUserAgent
|
||||
<< false << QNetworkReply::NetworkError::NoError;
|
||||
}
|
||||
|
||||
void TestNetworkRequest::testNetworkRequestTimeout()
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
void TestNetworkRequest::testNetworkRequestRedirects()
|
||||
{
|
||||
// TODO
|
||||
}
|
19
tests/TestNetworkRequest.h
Normal file
19
tests/TestNetworkRequest.h
Normal file
@ -0,0 +1,19 @@
|
||||
|
||||
#ifndef KEEPASSXC_TESTNETWORKREQUEST_HPP
|
||||
#define KEEPASSXC_TESTNETWORKREQUEST_HPP
|
||||
|
||||
#include <QObject>
|
||||
|
||||
class TestNetworkRequest : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void testNetworkRequest();
|
||||
void testNetworkRequest_data();
|
||||
|
||||
void testNetworkRequestTimeout();
|
||||
void testNetworkRequestRedirects();
|
||||
};
|
||||
|
||||
#endif // KEEPASSXC_TESTNETWORKREQUEST_HPP
|
@ -1 +1 @@
|
||||
#include "MockNetworkAccessManager.hpp"
|
||||
#include "MockNetworkAccessManager.h"
|
7364
tests/mock/MockNetworkAccessManager.h
Normal file
7364
tests/mock/MockNetworkAccessManager.h
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user