Page MenuHomePhorge

D210.1743772049.diff
No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None

D210.1743772049.diff

diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -52,7 +52,7 @@
find_package(Qt${QT_MAJOR_VERSION} ${QT_MIN_VERSION} REQUIRED COMPONENTS
Core Gui Qml QuickControls2 Svg Concurrent Widgets
- Multimedia Test Network QuickTest
+ Multimedia Test Network QuickTest HttpServer
)
qt6_policy(SET QTP0001 NEW)
set(kazv_KF_EXTRA_MODULES)
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -35,6 +35,11 @@
LINK_LIBRARIES Qt${QT_MAJOR_VERSION}::Test kazvtestlib
)
+ecm_add_test(
+ kazv-io-job-test.cpp
+ LINK_LIBRARIES Qt${QT_MAJOR_VERSION}::Test Qt${QT_MAJOR_VERSION}::HttpServer kazvtestlib
+)
+
ecm_add_test(
quick-test.cpp
TEST_NAME quicktest
diff --git a/src/tests/kazv-io-job-test.cpp b/src/tests/kazv-io-job-test.cpp
new file mode 100644
--- /dev/null
+++ b/src/tests/kazv-io-job-test.cpp
@@ -0,0 +1,150 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2025 nannanko <nannanko@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <kazv-defs.hpp>
+#include <kazv-io-job.hpp>
+
+#include <QObject>
+#include <QtTest>
+#include <QHttpServer>
+#include <QTcpServer>
+#include <QThread>
+#include <QTemporaryFile>
+#include <QCryptographicHash>
+#include <QString>
+#include <QHttpServerResponse>
+#include <QHttpServerRequest>
+#include <QSignalSpy>
+#include <QJsonObject>
+
+#define DOWNLOAD_ENDPOINT "/download"
+#define UPLOAD_ENDPOINT "/_matrix/media/v3/upload"
+#define FILE_CONTENT "some content"
+#define SERVER_URL_WITHOUT_PORT "http://localhost:"
+
+class KazvIOJobTest : public QObject
+{
+ Q_OBJECT
+
+private:
+ QHttpServer httpServer;
+ QTcpServer tcpServer;
+ QThread serverThread;
+ quint16 port;
+
+ QTemporaryFile downloadFile;
+ QTemporaryFile uploadFile;
+ QCryptographicHash downloadFileHash{QCryptographicHash::Sha256};
+
+private Q_SLOTS:
+ void initTestCase();
+ void cleanupTestCase();
+ void testDownload();
+ void testUpload();
+
+Q_SIGNALS:
+ void readyPause();
+ void readyResume();
+};
+
+void KazvIOJobTest::initTestCase()
+{
+ downloadFile.open();
+ downloadFile.write(FILE_CONTENT);
+ downloadFile.close();
+ downloadFileHash.addData(&downloadFile);
+
+ httpServer.route(QStringLiteral(DOWNLOAD_ENDPOINT),
+ [this](QHttpServerResponder &responder) {
+ downloadFile.open();
+ responder.writeBeginChunked("application/octet-stream");
+ responder.writeChunk(downloadFile.read(1));
+ Q_EMIT readyPause();
+ QSignalSpy qs{this, &KazvIOJobTest::readyResume};
+ qs.wait();
+ responder.writeEndChunked(downloadFile.readAll());
+ downloadFile.close();
+ });
+ httpServer.route(QStringLiteral(UPLOAD_ENDPOINT),
+ [this](const QHttpServerRequest &req) {
+ uploadFile.open();
+ uploadFile.write(req.body());
+ uploadFile.close();
+ auto resJson = QJsonObject{
+ {QStringLiteral("content_uri"), QStringLiteral("mxc://uri")}};
+ return QHttpServerResponse{resJson,
+ QHttpServerResponse::StatusCode::Ok};
+ });
+ if (!tcpServer.listen() || !httpServer.bind(&tcpServer)) {
+ QFAIL("Cannot start the Http server!");
+ }
+ port = tcpServer.serverPort();
+ httpServer.moveToThread(&serverThread);
+ serverThread.start();
+
+}
+
+void KazvIOJobTest::cleanupTestCase()
+{
+ serverThread.quit();
+ serverThread.wait();
+}
+
+void KazvIOJobTest::testDownload()
+{
+ QTemporaryFile saveFile{};
+ saveFile.open();
+ auto urlStr = QStringLiteral(SERVER_URL_WITHOUT_PORT)
+ .append(QString::number(port))
+ .append(QStringLiteral(DOWNLOAD_ENDPOINT));
+ KazvIODownloadJob job{saveFile.fileName(), QUrl{urlStr}, false,
+ QString::fromUtf8(downloadFileHash.result().toBase64(
+ QByteArray::Base64Encoding
+ | QByteArray::OmitTrailingEquals))};
+ QSignalSpy qsPause{this, &KazvIOJobTest::readyPause};
+ QVERIFY(qsPause.wait());
+ job.suspend();
+ QVERIFY(job.isSuspended());
+ job.resume();
+ Q_EMIT readyResume();
+ QSignalSpy qsResult{&job, &KazvIOBaseJob::result};
+ QVERIFY(qsResult.wait());
+
+ auto args = qsResult.takeFirst();
+ QCOMPARE(qvariant_cast<KazvIOBaseJob::ErrorCode>(args.at(0)),
+ KazvIOBaseJob::NoError);
+
+ downloadFile.open();
+ QTRY_COMPARE(downloadFile.readAll(), saveFile.readAll());
+ downloadFile.close();
+ saveFile.close();
+}
+
+void KazvIOJobTest::testUpload()
+{
+ QTemporaryFile file;
+ file.open();
+ file.write(FILE_CONTENT);
+ auto urlStr = QStringLiteral(SERVER_URL_WITHOUT_PORT)
+ .append(QString::number(port));
+ KazvIOUploadJob job{file.fileName(), QUrl{urlStr},
+ false, nullptr, QStringLiteral(""), QStringLiteral("token")};
+ QSignalSpy qs{&job, &KazvIOBaseJob::result};
+ qs.wait(std::chrono::seconds(5));
+
+ auto args = qs.takeFirst();
+ QCOMPARE(qvariant_cast<KazvIOBaseJob::ErrorCode>(args.at(0)),
+ KazvIOBaseJob::NoError);
+
+ uploadFile.open();
+ QTRY_COMPARE(uploadFile.readAll(), file.readAll());
+ uploadFile.close();
+ file.close();
+}
+
+QTEST_MAIN(KazvIOJobTest)
+
+#include "kazv-io-job-test.moc"

File Metadata

Mime Type
text/plain
Expires
Fri, Apr 4, 6:07 AM (1 h, 20 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
85134
Default Alt Text
D210.1743772049.diff (5 KB)

Event Timeline