diff --git a/src/network/access/qrestreply.cpp b/src/network/access/qrestreply.cpp index 77707377598..82be72d2d42 100644 --- a/src/network/access/qrestreply.cpp +++ b/src/network/access/qrestreply.cpp @@ -57,7 +57,24 @@ Q_DECLARE_LOGGING_CATEGORY(lcQrest) See \l QNetworkReply::downloadProgress() documentation for more details. - \sa bytesAvailable(), readyRead() + \sa bytesAvailable(), readyRead(), uploadProgress() +*/ + +/*! + \fn void QRestReply::uploadProgress(qint64 bytesSent, qint64 bytesTotal, + QRestReply* reply) + + This signal is emitted to indicate the progress of the upload part of + \a reply. + + The \a bytesSent parameter indicates the number of bytes already uploaded, + while \a bytesTotal indicates the total number of bytes still to upload. + + If the number of bytes to upload is not known, \a bytesTotal will be -1. + + See \l QNetworkReply::uploadProgress() documentation for more details. + + \sa QNetworkReply::uploadProgress(), downloadProgress() */ /*! @@ -98,6 +115,10 @@ QRestReply::QRestReply(QNetworkReply *reply, QObject *parent) [this](qint64 bytesReceived, qint64 bytesTotal) { emit downloadProgress(bytesReceived, bytesTotal, this); }); + QObject::connect(reply, &QNetworkReply::uploadProgress, this, + [this] (qint64 bytesSent, qint64 bytesTotal) { + emit uploadProgress(bytesSent, bytesTotal, this); + }); } /*! diff --git a/src/network/access/qrestreply.h b/src/network/access/qrestreply.h index 91e8d17cc3a..eb547f9b5d1 100644 --- a/src/network/access/qrestreply.h +++ b/src/network/access/qrestreply.h @@ -45,6 +45,7 @@ Q_SIGNALS: void errorOccurred(QRestReply *reply); void readyRead(QRestReply *reply); void downloadProgress(qint64 bytesReceived, qint64 bytesTotal, QRestReply *reply); + void uploadProgress(qint64 bytesSent, qint64 bytesTotal, QRestReply* reply); private: friend class QRestAccessManagerPrivate; diff --git a/tests/auto/network/access/qrestaccessmanager/tst_qrestaccessmanager.cpp b/tests/auto/network/access/qrestaccessmanager/tst_qrestaccessmanager.cpp index a49161359e3..9834818ad79 100644 --- a/tests/auto/network/access/qrestaccessmanager/tst_qrestaccessmanager.cpp +++ b/tests/auto/network/access/qrestaccessmanager/tst_qrestaccessmanager.cpp @@ -39,6 +39,7 @@ private slots: void json(); void text(); void download(); + void upload(); private: void memberHandler(QRestReply *reply); @@ -862,5 +863,35 @@ void tst_QRestAccessManager::download() QCOMPARE(receivedBytes, expectedData.size()); } +void tst_QRestAccessManager::upload() +{ + // This test tests uploadProgress signal + QRestAccessManager manager; + manager.setDeletesRepliesOnFinished(false); + HttpTestServer server; + QTRY_VERIFY(server.isListening()); + QNetworkRequest request(server.url()); + request.setRawHeader("Content-Type"_ba, "text/plain"); // To silence missing content-type warn + QByteArray expectedData{1 * 1024 * 1024, 0}; // 1 MB + server.setHandler([&](HttpData, HttpData &, ResponseControl &) {}); + + QRestReply* reply = manager.post(request, expectedData); + QSignalSpy uploadProgressSpy(reply, &QRestReply::uploadProgress); + QTRY_VERIFY(reply->isFinished()); + QVERIFY(!uploadProgressSpy.isEmpty()); + reply->deleteLater(); + + // Check that bytesTotal is correct already in the first signal + const QList first = uploadProgressSpy.first(); + QCOMPARE(first.size(), 3); + QCOMPARE(first.at(1).toLongLong(), expectedData.size()); + + // Check that we sent all bytes + const QList last = uploadProgressSpy.last(); + QCOMPARE(last.size(), 3); + QEXPECT_FAIL("", "Fails due to QTBUG-44782", Continue); + QCOMPARE(last.at(0).toLongLong(), expectedData.size()); +} + QTEST_MAIN(tst_QRestAccessManager) #include "tst_qrestaccessmanager.moc"