Warning, file /frameworks/kio/autotests/accessmanagertest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@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 <QBuffer> 0009 #include <QNetworkReply> 0010 #include <QProcess> 0011 #include <QSignalSpy> 0012 #include <QStandardPaths> 0013 #include <QTest> 0014 #include <accessmanager.h> 0015 0016 /** 0017 * Unit test for AccessManager 0018 */ 0019 class AccessManagerTest : public QObject 0020 { 0021 Q_OBJECT 0022 private Q_SLOTS: 0023 void initTestCase() 0024 { 0025 qputenv("KIOSLAVE_ENABLE_TESTMODE", "1"); // ensure the KIO workers call QStandardPaths::setTestModeEnabled too 0026 QStandardPaths::setTestModeEnabled(true); 0027 } 0028 0029 void testGet() 0030 { 0031 const QString aFile = QFINDTESTDATA("accessmanagertest.cpp"); 0032 QNetworkReply *reply = manager()->get(QNetworkRequest(QUrl::fromLocalFile(aFile))); 0033 QSignalSpy spy(reply, &QNetworkReply::finished); 0034 QVERIFY(spy.wait()); 0035 0036 QFile f(aFile); 0037 QVERIFY(f.open(QIODevice::ReadOnly)); 0038 QCOMPARE(f.readAll(), reply->readAll()); 0039 } 0040 0041 void testPut() 0042 { 0043 const QString aDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation); 0044 QVERIFY(QDir::temp().mkpath(aDir)); 0045 const QString aFile = aDir + QStringLiteral("/accessmanagertest-data"); 0046 const QByteArray content = "We love free software!"; 0047 QBuffer buffer; 0048 buffer.setData(content); 0049 QVERIFY(buffer.open(QIODevice::ReadOnly)); 0050 0051 QFile::remove(aFile); 0052 0053 QNetworkReply *reply = manager()->put(QNetworkRequest(QUrl::fromLocalFile(aFile)), &buffer); 0054 QSignalSpy spy(reply, &QNetworkReply::finished); 0055 QVERIFY(reply->isRunning()); 0056 QVERIFY(spy.wait()); 0057 0058 QVERIFY(QFile::exists(aFile)); 0059 QFile f(aFile); 0060 QVERIFY(f.open(QIODevice::ReadOnly)); 0061 QCOMPARE(f.readAll(), content); 0062 0063 QFile::remove(aFile); 0064 } 0065 0066 void testPutSequential() 0067 { 0068 const QString aDir = QStandardPaths::writableLocation(QStandardPaths::TempLocation); 0069 QVERIFY(QDir::temp().mkpath(aDir)); 0070 const QString aFile = aDir + QStringLiteral("/accessmanagertest-data2"); 0071 const QString putDataContents = "We love free software! " + QString(24000, 'c'); 0072 QProcess process; 0073 process.start(QStandardPaths::findExecutable(QStringLiteral("echo")), QStringList{putDataContents}); 0074 0075 QFile::remove(aFile); 0076 0077 QNetworkReply *reply = manager()->put(QNetworkRequest(QUrl::fromLocalFile(aFile)), &process); 0078 QSignalSpy spy(reply, &QNetworkReply::finished); 0079 QVERIFY(spy.wait()); 0080 QVERIFY(QFile::exists(aFile)); 0081 0082 QFile f(aFile); 0083 QVERIFY(f.open(QIODevice::ReadOnly)); 0084 0085 QByteArray cts = f.readAll(); 0086 cts.chop(1); // we remove the eof 0087 QCOMPARE(QString::fromUtf8(cts).size(), putDataContents.size()); 0088 QCOMPARE(QString::fromUtf8(cts), putDataContents); 0089 0090 QFile::remove(aFile); 0091 } 0092 0093 private: 0094 /** 0095 * we want to run the tests both on QNAM and KIO::AccessManager 0096 * to make sure they behave the same way. 0097 */ 0098 QNetworkAccessManager *manager() 0099 { 0100 static QNetworkAccessManager *ret = nullptr; 0101 if (!ret) { 0102 #ifdef USE_QNAM 0103 ret = new QNetworkAccessManager(this); 0104 #else 0105 ret = new KIO::Integration::AccessManager(this); 0106 #endif 0107 } 0108 return ret; 0109 } 0110 }; 0111 0112 QTEST_MAIN(AccessManagerTest) 0113 0114 #include "accessmanagertest.moc"