Add test that routes can send back data in multiple parts asynchronously

Add test to ensure that a route can be added that will be executed
asynchronously and can send back data to the client in multiple parts.

Task-number: QTBUG-108127
Change-Id: I3262da98559ff1a458e0305dff729da8538ecae5
Reviewed-by: Matthias Rauter <matthias.rauter@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Øystein Heskestad 2024-12-02 16:07:56 +01:00
parent f318606b6a
commit 334a92f459
1 changed files with 51 additions and 0 deletions

View File

@ -239,6 +239,7 @@ private slots:
void parallelFutureRequests();
void slowReader();
void concurrentRequestBack();
void concurrentMultipart();
void multipleResponses();
void contextObjectInOtherThreadWarning();
void keepAliveTimeout();
@ -658,6 +659,25 @@ httpserver.route("/concurrent-request-back2", this,
return request.body();
});
});
httpserver.route(
"/concurrent-multipart-back/<arg>/<arg>/<arg>", this,
[](int times, int ms, QString message, QHttpServerResponder &&responder) {
return QtConcurrent::run(
[=, r = std::make_shared<QHttpServerResponder>(std::move(responder))] {
if (times > 0) {
QByteArray ba = message.toUtf8();
r->writeBeginChunked("text/plain"_ba);
for (int i = 1; i < times; ++i) {
r->writeChunk(ba);
QThread::msleep(ms);
}
r->writeEndChunked(ba);
} else {
r->write();
}
});
});
#endif
#if QT_CONFIG(localserver)
@ -1807,6 +1827,37 @@ void tst_QHttpServer::concurrentRequestBack()
#endif // QT_CONFIG(concurrent)
}
void tst_QHttpServer::concurrentMultipart()
{
#if QT_CONFIG(concurrent)
QFETCH_GLOBAL(bool, useSsl);
QFETCH_GLOBAL(bool, useHttp2);
QString urlBase = useSsl ? sslUrlBase : clearUrlBase;
constexpr qsizetype NumberOfTasks = 10;
std::array<QString, NumberOfTasks> messages = {
"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eight", "Ninth", "Tenth"
};
QCOMPARE(messages.size(), NumberOfTasks);
std::array<QNetworkReply *, NumberOfTasks> replies;
QThreadPool::globalInstance()->setMaxThreadCount(NumberOfTasks);
for (qsizetype i = 0; i < NumberOfTasks; ++i) {
QString path = u"/concurrent-multipart-back/100/10/%1"_s.arg(messages[i]);
QNetworkRequest req(QUrl(urlBase.arg(path.toUtf8())));
req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
req.setAttribute(QNetworkRequest::Http2AllowedAttribute, useHttp2);
req.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain"_ba);
replies[i] = networkAccessManager.get(req);
}
for (qsizetype i = 0; i < NumberOfTasks; i++) {
QString body = messages[i].repeated(100);
checkReply(replies[i], body);
}
#else
QSKIP("QtConcurrent is not available, skipping test");
#endif // QT_CONFIG(concurrent)
}
void tst_QHttpServer::multipleResponses()
{
QFETCH_GLOBAL(bool, useSsl);