Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F1034836
D210.1747134139.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D210.1747134139.diff
View Options
diff --git a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -77,6 +77,9 @@
set(CMARK_TARGET_NAME cmark)
endif()
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(LIBHTTPSERVER REQUIRED libhttpserver)
+
kde_enable_exceptions()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -foperator-names -DQT_NO_EMIT")
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -32,7 +32,8 @@
matrix-event-list-test.cpp
matrix-event-test.cpp
kazv-file-test.cpp
- LINK_LIBRARIES Qt${QT_MAJOR_VERSION}::Test kazvtestlib
+ kazv-io-job-test.cpp
+ LINK_LIBRARIES Qt${QT_MAJOR_VERSION}::Test kazvtestlib ${LIBHTTPSERVER_LINK_LIBRARIES}
)
ecm_add_test(
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,102 @@
+/*
+ * 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 <QTcpServer>
+#include <QTemporaryFile>
+#include <QCryptographicHash>
+#include <QSignalSpy>
+
+#include <httpserver.hpp>
+
+#include <memory>
+#include <string>
+#include <chrono>
+
+using namespace httpserver;
+
+class KazvIOJobTest : public QObject
+{
+ Q_OBJECT
+
+private:
+ class FileRes : public http_resource
+ {
+ public:
+ FileRes(const std::string &fileName)
+ : http_resource()
+ , m_fileName(fileName)
+ {}
+
+ std::shared_ptr<http_response> render(const http_request&) override {
+ return std::shared_ptr<http_response>(
+ new file_response(m_fileName));
+ }
+
+ private:
+ std::string m_fileName;
+ };
+
+ std::unique_ptr<webserver> ws;
+ std::unique_ptr<FileRes> fr;
+ quint16 port;
+ QTemporaryFile file;
+ QCryptographicHash hash{QCryptographicHash::Sha256};
+
+private Q_SLOTS:
+ void initTestCase();
+ void testDownload();
+};
+
+void KazvIOJobTest::initTestCase()
+{
+ // Get a free port
+ QTcpServer ts;
+ ts.listen();
+ port = ts.serverPort();
+ ts.close();
+
+ ws.reset(new webserver{create_webserver{port}});
+ file.open();
+ file.write("some content");
+ file.close();
+ hash.addData(&file);
+ fr.reset(new FileRes{file.fileName().toStdString()});
+ ws->register_resource("/download", fr.get());
+ ws->start(false);
+}
+
+void KazvIOJobTest::testDownload()
+{
+ QTemporaryFile saveFile{};
+ saveFile.open();
+ auto urlStr = QStringLiteral("http://localhost:")
+ .append(QString::number(port))
+ .append(QStringLiteral("/download"));
+ KazvIODownloadJob job{saveFile.fileName(), QUrl{urlStr}, false,
+ QString::fromUtf8(hash.result().toBase64(
+ QByteArray::Base64Encoding
+ | QByteArray::OmitTrailingEquals))};
+ QSignalSpy qs{&job, &KazvIODownloadJob::result};
+ qs.wait(std::chrono::seconds(5));
+
+ auto args = qs.takeFirst();
+ QCOMPARE(qvariant_cast<KazvIOBaseJob::ErrorCode>(args.at(0)),
+ KazvIOBaseJob::NoError);
+
+ file.open();
+ QTRY_COMPARE(file.readAll(), saveFile.readAll());
+ file.close();
+ saveFile.close();
+}
+
+QTEST_MAIN(KazvIOJobTest)
+
+#include "kazv-io-job-test.moc"
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, May 13, 4:02 AM (8 h, 15 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
165906
Default Alt Text
D210.1747134139.diff (3 KB)
Attached To
Mode
D210: Add kazv-io-job-test
Attached
Detach File
Event Timeline
Log In to Comment