1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT
#include "testhttpserver.h"
#include "testobject.h"
#include "qcoroiodevice_macros.h"
#include "qcoro/network/qcoronetworkreply.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QTcpServer>
class QCoroNetworkReplyTest : public QCoro::TestObject<QCoroNetworkReplyTest> {
Q_OBJECT
private:
QCoro::Task<> testTriggers_coro(QCoro::TestContext) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(nam.get(buildRequest()));
co_await reply.get();
QCORO_VERIFY(reply->isFinished());
QCORO_COMPARE(reply->error(), QNetworkReply::NoError);
QCORO_COMPARE(reply->readAll(), "abcdef");
}
QCoro::Task<> testQCoroWrapperTriggers_coro(QCoro::TestContext) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(nam.get(buildRequest()));
co_await qCoro(reply.get()).waitForFinished();
QCORO_VERIFY(reply->isFinished());
QCORO_COMPARE(reply->error(), QNetworkReply::NoError);
QCORO_COMPARE(reply->readAll(), "abcdef");
}
void testThenQCoroWrapperTriggers_coro(TestLoop &el) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(nam.get(buildRequest()));
bool called = false;
qCoro(reply.get()).waitForFinished().then([&](bool finished) {
called = true;
el.quit();
QVERIFY(finished);
});
el.exec();
QVERIFY(reply->isFinished());
QCOMPARE(reply->error(), QNetworkReply::NoError);
QCOMPARE(reply->readAll(), "abcdef");
QVERIFY(called);
}
QCoro::Task<> testDoesntBlockEventLoop_coro(QCoro::TestContext) {
QCoro::EventLoopChecker eventLoopResponsive;
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(
nam.get(buildRequest(QStringLiteral("block"))));
co_await reply.get();
QCORO_VERIFY(eventLoopResponsive);
QCORO_VERIFY(reply->isFinished());
QCORO_COMPARE(reply->error(), QNetworkReply::NoError);
QCORO_COMPARE(reply->readAll(), "abcdef");
}
QCoro::Task<> testDoesntCoAwaitNullReply_coro(QCoro::TestContext test) {
test.setShouldNotSuspend();
mServer.setExpectTimeout(true);
QNetworkReply *reply = nullptr;
co_await reply;
delete reply;
}
QCoro::Task<> testDoesntCoAwaitFinishedReply_coro(QCoro::TestContext test) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(nam.get(buildRequest()));
co_await reply.get();
QCORO_VERIFY(reply->isFinished());
test.setShouldNotSuspend();
co_await reply.get();
}
QCoro::Task<> testReadAllTriggers_coro(QCoro::TestContext) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(
nam.get(buildRequest(QStringLiteral("stream"))));
QCORO_TEST_IODEVICE_READALL(*reply);
QCORO_COMPARE(data.size(), reply->rawHeader("Content-Length").toInt());
}
void testThenReadAllTriggers_coro(TestLoop &el) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(
nam.get(buildRequest(QStringLiteral("block"))));
bool called = false;
qCoro(reply.get()).readAll().then([&](const QByteArray &data) {
called = true;
el.quit();
QVERIFY(!data.isEmpty());
});
el.exec();
QVERIFY(called);
}
QCoro::Task<> testReadTriggers_coro(QCoro::TestContext) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(
nam.get(buildRequest(QStringLiteral("stream"))));
QCORO_TEST_IODEVICE_READ(*reply);
QCORO_COMPARE(data.size(), reply->rawHeader("Content-Length").toInt());
}
void testThenReadTriggers_coro(TestLoop &el) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(
nam.get(buildRequest(QStringLiteral("block"))));
bool called = false;
qCoro(reply.get()).read(1).then([&](const QByteArray &data) {
called = true;
el.quit();
QCOMPARE(data.size(), 1);
});
el.exec();
QVERIFY(called);
}
QCoro::Task<> testReadLineTriggers_coro(QCoro::TestContext) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(
nam.get(buildRequest(QStringLiteral("stream"))));
QCORO_TEST_IODEVICE_READLINE(*reply);
QCORO_COMPARE(lines.size(), 10);
}
void testThenReadLineTriggers_coro(TestLoop &el) {
QNetworkAccessManager nam;
auto reply = std::unique_ptr<QNetworkReply>(
nam.get(buildRequest(QStringLiteral("block"))));
bool called = false;
qCoro(reply.get()).readLine().then([&](const QByteArray &data) {
called = true;
el.quit();
QVERIFY(!data.isEmpty());
});
el.exec();
QVERIFY(called);
}
// See https://github.com/danvratil/qcoro/issues/231
QCoro::Task<> testAbortOnTimeout_coro(QCoro::TestContext) {
auto request = buildRequest(QStringLiteral("block"));
request.setTransferTimeout(300);
QNetworkAccessManager nam;
auto reply = co_await nam.get(request);
QCORO_VERIFY(reply != nullptr);
QCORO_VERIFY(reply->isFinished());
QCORO_COMPARE(reply->error(), QNetworkReply::OperationCanceledError);
// QNAM is destroyed here and so is all its associated state, which could
// crash (or cause invalid memory access)
}
private Q_SLOTS:
void init() {
mServer.start(QHostAddress::LocalHost);
}
void cleanup() {
mServer.stop();
}
addTest(Triggers)
addCoroAndThenTests(QCoroWrapperTriggers)
addTest(DoesntBlockEventLoop)
addTest(DoesntCoAwaitNullReply)
addTest(DoesntCoAwaitFinishedReply)
addCoroAndThenTests(ReadAllTriggers)
addCoroAndThenTests(ReadTriggers)
addCoroAndThenTests(ReadLineTriggers)
addTest(AbortOnTimeout)
private:
QNetworkRequest buildRequest(const QString &path = QString()) {
return QNetworkRequest{
QUrl{QStringLiteral("http://127.0.0.1:%1/%2").arg(mServer.port()).arg(path)}
};
}
TestHttpServer<QTcpServer> mServer;
};
QTEST_GUILESS_MAIN(QCoroNetworkReplyTest)
#include "qcoronetworkreply.moc"
|