File indexing completed on 2024-04-28 03:54:48
0001 /* 0002 0003 SPDX-FileCopyrightText: 2023 Nicolas Fella <nicolas.fella@gmx.de> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include <KIO/TransferJob> 0009 0010 #include <QSignalSpy> 0011 #include <QTest> 0012 0013 class RedirectTest : public QObject 0014 { 0015 Q_OBJECT 0016 private Q_SLOTS: 0017 void testRedirectGet(); 0018 void testRedirectGet_data(); 0019 void testPermanentRedirect(); 0020 void testPermanentRedirect_data(); 0021 0022 void testRedirectPost(); 0023 void testRedirectPost_data(); 0024 void testPermanentRedirectPost(); 0025 void testPermanentRedirectPost_data(); 0026 0027 void testRedirectPut(); 0028 void testRedirectPut_data(); 0029 void testPermanentRedirectPut(); 0030 void testPermanentRedirectPut_data(); 0031 }; 0032 0033 void RedirectTest::testRedirectGet_data() 0034 { 0035 QTest::addColumn<QString>("url"); 0036 QTest::addColumn<QString>("redirectUrl"); 0037 QTest::addColumn<QByteArray>("expectedData"); 0038 0039 QTest::addRow("redirect") << "http://localhost:5000/get/redirect" 0040 << "http://localhost:5000/get/redirected" << QByteArray("Itsa me, redirected\n"); 0041 QTest::addRow("redirect") << "http://localhost:5000/get/redirect_303" 0042 << "http://localhost:5000/get/redirected" << QByteArray("Itsa me, redirected\n"); 0043 QTest::addRow("redirect") << "http://localhost:5000/get/redirect_307" 0044 << "http://localhost:5000/get/redirected" << QByteArray("Itsa me, redirected\n"); 0045 } 0046 0047 void RedirectTest::testRedirectGet() 0048 { 0049 QFETCH(QString, url); 0050 QFETCH(QString, redirectUrl); 0051 QFETCH(QByteArray, expectedData); 0052 0053 auto *job = KIO::get(QUrl(url)); 0054 0055 QSignalSpy redirectSpy(job, &KIO::TransferJob::redirection); 0056 redirectSpy.wait(); 0057 QVERIFY(redirectSpy.count()); 0058 const QString actualRedirectUrl = redirectSpy.first().at(1).toString(); 0059 QCOMPARE(actualRedirectUrl, redirectUrl); 0060 0061 QSignalSpy dataSpy(job, &KIO::TransferJob::data); 0062 QSignalSpy finishedSpy(job, &KIO::TransferJob::finished); 0063 finishedSpy.wait(); 0064 QVERIFY(finishedSpy.count()); 0065 0066 QVERIFY(dataSpy.count()); 0067 const QByteArray actualData = dataSpy.first().at(1).toByteArray(); 0068 QCOMPARE(actualData, expectedData); 0069 } 0070 0071 void RedirectTest::testPermanentRedirect_data() 0072 { 0073 QTest::addColumn<QString>("url"); 0074 QTest::addColumn<QString>("redirectUrl"); 0075 QTest::addColumn<QByteArray>("expectedData"); 0076 0077 QTest::addRow("redirect_301") << "http://localhost:5000/get/permanent_redirect" 0078 << "http://localhost:5000/get/permanent_redirected" << QByteArray("Itsa me, redirected permanently\n"); 0079 QTest::addRow("redirect_308") << "http://localhost:5000/get/redirect_308" 0080 << "http://localhost:5000/get/permanent_redirected" << QByteArray("Itsa me, redirected permanently\n"); 0081 } 0082 0083 void RedirectTest::testPermanentRedirect() 0084 { 0085 QFETCH(QString, url); 0086 QFETCH(QString, redirectUrl); 0087 QFETCH(QByteArray, expectedData); 0088 0089 auto *job = KIO::get(QUrl(url)); 0090 0091 // Test redirection signal 0092 QSignalSpy redirectionSpy(job, &KIO::TransferJob::redirection); 0093 redirectionSpy.wait(); 0094 QVERIFY(redirectionSpy.count()); 0095 const QString actualRedirectUrl = redirectionSpy.first().at(1).toString(); 0096 QCOMPARE(actualRedirectUrl, redirectUrl); 0097 0098 // Test permanentRedirection signal 0099 QSignalSpy permanentRedirectionSpy(job, &KIO::TransferJob::permanentRedirection); 0100 permanentRedirectionSpy.wait(); 0101 QVERIFY(permanentRedirectionSpy.count()); 0102 0103 const QString actualRedirectedFrom = permanentRedirectionSpy.first().at(1).toString(); 0104 QCOMPARE(actualRedirectedFrom, url); 0105 0106 const QString actualRedirectedTo = permanentRedirectionSpy.first().at(2).toString(); 0107 QCOMPARE(actualRedirectedTo, redirectUrl); 0108 0109 // Test data and finished signal 0110 QSignalSpy dataSpy(job, &KIO::TransferJob::data); 0111 QSignalSpy finishedSpy(job, &KIO::TransferJob::finished); 0112 finishedSpy.wait(); 0113 QVERIFY(finishedSpy.count()); 0114 0115 QVERIFY(dataSpy.count()); 0116 const QByteArray actualData = dataSpy.first().at(1).toByteArray(); 0117 QCOMPARE(actualData, expectedData); 0118 } 0119 0120 void RedirectTest::testRedirectPost_data() 0121 { 0122 QTest::addColumn<QString>("url"); 0123 QTest::addColumn<QString>("redirectUrl"); 0124 QTest::addColumn<QByteArray>("expectedData"); 0125 0126 QTest::addRow("redirect") << "http://localhost:5000/post/redirect" 0127 << "http://localhost:5000/get/redirected" << QByteArray("Itsa me, redirected\n"); 0128 QTest::addRow("redirect_303") << "http://localhost:5000/post/redirect_303" 0129 << "http://localhost:5000/get/redirected" << QByteArray("Itsa me, redirected\n"); 0130 // TODO this should work but doesn't 0131 // QTest::addRow("redirect_307") << "http://localhost:5000/post/redirect_307" 0132 // << "http://localhost:5000/post/redirected" << QByteArray("Itsa me, redirected\n"); 0133 } 0134 0135 void RedirectTest::testRedirectPost() 0136 { 0137 QFETCH(QString, url); 0138 QFETCH(QString, redirectUrl); 0139 QFETCH(QByteArray, expectedData); 0140 0141 auto *job = KIO::http_post(QUrl(url), QByteArray()); 0142 0143 QSignalSpy redirectSpy(job, &KIO::TransferJob::redirection); 0144 redirectSpy.wait(); 0145 QVERIFY(redirectSpy.count()); 0146 const QString actualRedirectUrl = redirectSpy.first().at(1).toString(); 0147 QCOMPARE(actualRedirectUrl, redirectUrl); 0148 0149 QSignalSpy dataSpy(job, &KIO::TransferJob::data); 0150 QSignalSpy finishedSpy(job, &KIO::TransferJob::finished); 0151 finishedSpy.wait(); 0152 QVERIFY(finishedSpy.count()); 0153 0154 QVERIFY(dataSpy.count()); 0155 const QByteArray actualData = dataSpy.first().at(1).toByteArray(); 0156 QCOMPARE(actualData, expectedData); 0157 } 0158 0159 void RedirectTest::testPermanentRedirectPost_data() 0160 { 0161 QTest::addColumn<QString>("url"); 0162 QTest::addColumn<QString>("redirectUrl"); 0163 QTest::addColumn<QByteArray>("expectedData"); 0164 0165 QTest::addRow("redirect_301") << "http://localhost:5000/post/permanent_redirect" 0166 << "http://localhost:5000/get/permanent_redirected" << QByteArray("Itsa me, redirected permanently\n"); 0167 0168 // TODO this should work, but doesn't 0169 // QTest::addRow("redirect_308") << "http://localhost:5000/post/redirect_308" 0170 // << "http://localhost:5000/post/permanent_redirected" << QByteArray("Itsa me, redirected permanently\n"); 0171 } 0172 0173 void RedirectTest::testPermanentRedirectPost() 0174 { 0175 QFETCH(QString, url); 0176 QFETCH(QString, redirectUrl); 0177 QFETCH(QByteArray, expectedData); 0178 0179 auto *job = KIO::http_post(QUrl(url), QByteArray()); 0180 0181 // Test redirection signal 0182 QSignalSpy redirectionSpy(job, &KIO::TransferJob::redirection); 0183 redirectionSpy.wait(); 0184 QVERIFY(redirectionSpy.count()); 0185 const QString actualRedirectUrl = redirectionSpy.first().at(1).toString(); 0186 QCOMPARE(actualRedirectUrl, redirectUrl); 0187 0188 // Test permanentRedirection signal 0189 QSignalSpy permanentRedirectionSpy(job, &KIO::TransferJob::permanentRedirection); 0190 permanentRedirectionSpy.wait(); 0191 QVERIFY(permanentRedirectionSpy.count()); 0192 0193 const QString actualRedirectedFrom = permanentRedirectionSpy.first().at(1).toString(); 0194 QCOMPARE(actualRedirectedFrom, url); 0195 0196 const QString actualRedirectedTo = permanentRedirectionSpy.first().at(2).toString(); 0197 QCOMPARE(actualRedirectedTo, redirectUrl); 0198 0199 // Test data and finished signal 0200 QSignalSpy dataSpy(job, &KIO::TransferJob::data); 0201 QSignalSpy finishedSpy(job, &KIO::TransferJob::finished); 0202 finishedSpy.wait(); 0203 QVERIFY(finishedSpy.count()); 0204 0205 QVERIFY(dataSpy.count()); 0206 const QByteArray actualData = dataSpy.first().at(1).toByteArray(); 0207 QCOMPARE(actualData, expectedData); 0208 } 0209 0210 void RedirectTest::testRedirectPut_data() 0211 { 0212 QTest::addColumn<QString>("url"); 0213 QTest::addColumn<QString>("redirectUrl"); 0214 QTest::addColumn<QByteArray>("expectedData"); 0215 0216 // TODO what should happen here, redirect to GET or PUT? 0217 // QTest::addRow("redirect") << "http://localhost:5000/put/redirect" 0218 // << "http://localhost:5000/get/redirected" << QByteArray("Itsa me, redirected\n"); 0219 // QTest::addRow("redirect_303") << "http://localhost:5000/put/redirect_303" 0220 // << "http://localhost:5000/get/redirected" << QByteArray("Itsa me, redirected\n"); 0221 // TODO this should work but doesn't 0222 // QTest::addRow("redirect_307") << "http://localhost:5000/put/redirect_307" 0223 // << "http://localhost:5000/put/redirected" << QByteArray("Itsa me, redirected\n"); 0224 } 0225 0226 void RedirectTest::testRedirectPut() 0227 { 0228 QSKIP("TODO clarify expected behavior"); 0229 QFETCH(QString, url); 0230 QFETCH(QString, redirectUrl); 0231 QFETCH(QByteArray, expectedData); 0232 0233 auto *job = KIO::put(QUrl(url), -1); 0234 0235 QSignalSpy redirectSpy(job, &KIO::TransferJob::redirection); 0236 redirectSpy.wait(); 0237 QVERIFY(redirectSpy.count()); 0238 const QString actualRedirectUrl = redirectSpy.first().at(1).toString(); 0239 QCOMPARE(actualRedirectUrl, redirectUrl); 0240 0241 QSignalSpy dataSpy(job, &KIO::TransferJob::data); 0242 QSignalSpy finishedSpy(job, &KIO::TransferJob::finished); 0243 finishedSpy.wait(); 0244 QVERIFY(finishedSpy.count()); 0245 0246 QVERIFY(dataSpy.count()); 0247 const QByteArray actualData = dataSpy.first().at(1).toByteArray(); 0248 QCOMPARE(actualData, expectedData); 0249 } 0250 0251 void RedirectTest::testPermanentRedirectPut_data() 0252 { 0253 QTest::addColumn<QString>("url"); 0254 QTest::addColumn<QString>("redirectUrl"); 0255 QTest::addColumn<QByteArray>("expectedData"); 0256 0257 // TODO what should happen here, redirect to GET or PUT? 0258 // QTest::addRow("redirect_301") << "http://localhost:5000/put/permanent_redirect" 0259 // << "http://localhost:5000/get/permanent_redirected" << QByteArray("Itsa me, redirected permanently\n"); 0260 0261 // TODO this should work, but doesn't 0262 // QTest::addRow("redirect_308") << "http://localhost:5000/put/redirect_308" 0263 // << "http://localhost:5000/put/permanent_redirected" << QByteArray("Itsa me, redirected permanently\n"); 0264 } 0265 0266 void RedirectTest::testPermanentRedirectPut() 0267 { 0268 QSKIP("TODO clarify expected behavior"); 0269 QFETCH(QString, url); 0270 QFETCH(QString, redirectUrl); 0271 QFETCH(QByteArray, expectedData); 0272 0273 auto *job = KIO::put(QUrl(url), -1); 0274 0275 // Test redirection signal 0276 QSignalSpy redirectionSpy(job, &KIO::TransferJob::redirection); 0277 redirectionSpy.wait(); 0278 QVERIFY(redirectionSpy.count()); 0279 const QString actualRedirectUrl = redirectionSpy.first().at(1).toString(); 0280 QCOMPARE(actualRedirectUrl, redirectUrl); 0281 0282 // Test permanentRedirection signal 0283 QSignalSpy permanentRedirectionSpy(job, &KIO::TransferJob::permanentRedirection); 0284 permanentRedirectionSpy.wait(); 0285 QVERIFY(permanentRedirectionSpy.count()); 0286 0287 const QString actualRedirectedFrom = permanentRedirectionSpy.first().at(1).toString(); 0288 QCOMPARE(actualRedirectedFrom, url); 0289 0290 const QString actualRedirectedTo = permanentRedirectionSpy.first().at(2).toString(); 0291 QCOMPARE(actualRedirectedTo, redirectUrl); 0292 0293 // Test data and finished signal 0294 QSignalSpy dataSpy(job, &KIO::TransferJob::data); 0295 QSignalSpy finishedSpy(job, &KIO::TransferJob::finished); 0296 finishedSpy.wait(); 0297 QVERIFY(finishedSpy.count()); 0298 0299 QVERIFY(dataSpy.count()); 0300 const QByteArray actualData = dataSpy.first().at(1).toByteArray(); 0301 QCOMPARE(actualData, expectedData); 0302 } 0303 0304 QTEST_GUILESS_MAIN(RedirectTest) 0305 0306 #include "redirecttest.moc"