TestDownloader.cpp
103 lines
| 2.7 KiB
| text/x-c
|
CppLexer
r0 | #include <Network/Downloader.h> | |||
#include <QtTest> | ||||
#include <QObject> | ||||
r34 | #include <QtConcurrent/QtConcurrent> | |||
r0 | ||||
r34 | Response get(const QString& url) | |||
{ | ||||
return Downloader::get(url); | ||||
} | ||||
r0 | ||||
class TestDownloader : public QObject | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
explicit TestDownloader(QObject *parent = nullptr) : QObject(parent){} | ||||
signals: | ||||
private slots: | ||||
void initTestCase(){} | ||||
void cleanupTestCase(){} | ||||
void simpleGet() | ||||
{ | ||||
auto resp = Downloader::get("https://httpbin.org/user-agent"); | ||||
QCOMPARE(resp.status_code(), 200); | ||||
QCOMPARE(resp.data(), QString("{\n \"user-agent\": \"SciQLop 1.0\"\n}\n")); | ||||
} | ||||
r34 | void multipleGetThreaded() | |||
{ | ||||
QList<QString> urls; | ||||
for(int i=0;i<32;i++) | ||||
{ | ||||
urls.append("https://httpbin.org/user-agent"); | ||||
} | ||||
QList<Response> responses = QtConcurrent::blockingMapped(urls, get); | ||||
for(auto &resp:responses) | ||||
{ | ||||
QCOMPARE(resp.status_code(), 200); | ||||
QCOMPARE(resp.data(), QString("{\n \"user-agent\": \"SciQLop 1.0\"\n}\n")); | ||||
} | ||||
} | ||||
r0 | void simpleAsyncGet() | |||
{ | ||||
bool done = false; | ||||
int status = -1; | ||||
QByteArray data; | ||||
auto callback = [&done, &status, &data](QUuid uuid,Response resp) | ||||
{ | ||||
status = resp.status_code(); | ||||
done = true; | ||||
data = resp.data(); | ||||
}; | ||||
auto uuid = Downloader::getAsync("http://ovh.net/files/1Mio.dat", callback); | ||||
QCOMPARE(Downloader::downloadFinished(uuid), false); | ||||
while (!done) | ||||
{ | ||||
QCoreApplication::processEvents(); | ||||
} | ||||
QCOMPARE(Downloader::downloadFinished(uuid), true); | ||||
QCOMPARE(status, 200); | ||||
QCOMPARE(data[0],'\xBA'); | ||||
QCOMPARE(data[data.length()-1],'\x20'); | ||||
} | ||||
void wrongUrl() | ||||
{ | ||||
auto resp = Downloader::get("https://lpp.polytechniqe2.fr"); | ||||
QCOMPARE(resp.status_code(), 0); | ||||
resp = Downloader::get("https://hephaistos.lpp.polytechnique.fr/will_never_exist"); | ||||
QCOMPARE(resp.status_code(), 404); | ||||
} | ||||
void authGet_data() | ||||
{ | ||||
QTest::addColumn<QString>("url"); | ||||
QTest::addColumn<int>("code"); | ||||
QTest::newRow("basic-auth") << "https://httpbin.org/basic-auth/user/passwd" << 200; | ||||
QTest::newRow("digest-auth") << "https://httpbin.org/digest-auth/auth/user/passwd" << 200; | ||||
QTest::newRow("hidden-basic-auth") << "https://httpbin.org/hidden-basic-auth/user/passwd" << 404; | ||||
} | ||||
void authGet() | ||||
{ | ||||
QFETCH(QString, url); | ||||
QFETCH(int, code); | ||||
auto resp = Downloader::get(url, "user", "passwd"); | ||||
QCOMPARE(resp.status_code(), code); | ||||
} | ||||
private: | ||||
}; | ||||
QTEST_MAIN(TestDownloader) | ||||
#include "TestDownloader.moc" | ||||