File indexing completed on 2025-01-12 12:29:24
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2016 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include <QSignalSpy> 0009 #include <QStandardPaths> 0010 #include <QTest> 0011 0012 #include "httpserver_p.h" 0013 #include <kio/storedtransferjob.h> 0014 0015 class HTTPJobTest : public QObject 0016 { 0017 Q_OBJECT 0018 0019 private Q_SLOTS: 0020 void initTestCase(); 0021 void testBasicGet(); 0022 void testErrorPage(); 0023 void testMimeTypeDetermination(); 0024 }; 0025 0026 void HTTPJobTest::initTestCase() 0027 { 0028 QStandardPaths::setTestModeEnabled(true); 0029 0030 // To let ctest exit, we shouldn't start kio_http_cache_cleaner 0031 qputenv("KIO_DISABLE_CACHE_CLEANER", "yes"); 0032 } 0033 0034 void HTTPJobTest::testBasicGet() 0035 { 0036 static const char response[] = "Hello world"; 0037 HttpServerThread server(response, HttpServerThread::Public); 0038 KIO::StoredTransferJob *job = KIO::storedGet(QUrl(server.endPoint())); 0039 job->setUiDelegate(nullptr); 0040 QVERIFY(job->exec()); 0041 QCOMPARE(QString::fromLatin1(job->data()), QString::fromLatin1(response)); 0042 } 0043 0044 void HTTPJobTest::testErrorPage() 0045 { 0046 static const char response[] = "<html>This is a response\nFile not found</html>"; 0047 HttpServerThread server(response, HttpServerThread::Error404); 0048 server.setContentType("text/html"); 0049 0050 // First we get an error page 0051 KIO::StoredTransferJob *job = KIO::storedGet(QUrl(server.endPoint())); 0052 job->setUiDelegate(nullptr); 0053 QVERIFY(job->exec()); 0054 QCOMPARE(QString::fromLatin1(job->data()), QString::fromLatin1(response)); 0055 QVERIFY(job->isErrorPage()); 0056 QCOMPARE(job->error(), 0); 0057 0058 // Second we disable error page, and get the actual job error 0059 job = KIO::storedGet(QUrl(server.endPoint())); 0060 job->setUiDelegate(nullptr); 0061 job->addMetaData(QStringLiteral("errorPage"), QStringLiteral("false")); // maybe this should be a proper setter... 0062 QVERIFY(!job->exec()); 0063 QVERIFY(!job->isErrorPage()); 0064 QCOMPARE(job->error(), int(KIO::ERR_DOES_NOT_EXIST)); 0065 0066 // To check that kio_http did read and discard the body correctly, do another working download. 0067 server.setResponseData("<html>Some HTML page here</html>"); 0068 server.setFeatures(HttpServerThread::Public); 0069 server.setContentType(""); 0070 job = KIO::storedGet(QUrl(server.endPoint())); 0071 job->setUiDelegate(nullptr); 0072 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78) 0073 QSignalSpy mimeTypeSpy(job, qOverload<KIO::Job *, const QString &>(&KIO::TransferJob::mimetype)); 0074 #endif 0075 QSignalSpy mimeTypeFoundSpy(job, &KIO::TransferJob::mimeTypeFound); 0076 QVERIFY(job->exec()); 0077 QCOMPARE(job->error(), 0); 0078 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78) 0079 QCOMPARE(mimeTypeSpy.count(), 1); 0080 QCOMPARE(mimeTypeSpy.at(0).at(1).toString(), QStringLiteral("text/html")); 0081 #endif 0082 QCOMPARE(mimeTypeFoundSpy.count(), 1); 0083 QCOMPARE(mimeTypeFoundSpy.at(0).at(1).toString(), QStringLiteral("text/html")); 0084 } 0085 0086 void HTTPJobTest::testMimeTypeDetermination() 0087 { 0088 static const char response[] = "<html>Some HTML page here</html>"; 0089 HttpServerThread server(response, HttpServerThread::Public); 0090 // Add a trailing slash to ensure kio_http doesn't confuse QMimeDatabase with it. 0091 KIO::StoredTransferJob *job = KIO::storedGet(QUrl(server.endPoint() + '/')); 0092 job->setUiDelegate(nullptr); 0093 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78) 0094 QSignalSpy mimeTypeSpy(job, qOverload<KIO::Job *, const QString &>(&KIO::TransferJob::mimetype)); 0095 #endif 0096 QSignalSpy mimeTypeFoundSpy(job, &KIO::TransferJob::mimeTypeFound); 0097 QVERIFY(job->exec()); 0098 QCOMPARE(job->error(), 0); 0099 #if KIOCORE_BUILD_DEPRECATED_SINCE(5, 78) 0100 QCOMPARE(mimeTypeSpy.count(), 1); 0101 QCOMPARE(mimeTypeSpy.at(0).at(1).toString(), QStringLiteral("text/html")); 0102 #endif 0103 QCOMPARE(mimeTypeFoundSpy.count(), 1); 0104 QCOMPARE(mimeTypeFoundSpy.at(0).at(1).toString(), QStringLiteral("text/html")); 0105 } 0106 0107 QTEST_MAIN(HTTPJobTest) 0108 #include "http_jobtest.moc"