From 4369853ddb21525a9b83f83ef3d44dd838a17d8d Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 11 Dec 2017 17:03:24 +0100 Subject: TestSystem: Add QIODevice in the serverOverride function, and add a DelayedReply Preparing to add test that needs the QIODevice. Also make the DelayedReply so we can generalize the existing delay on FakeChunkMoveReply to any reply. --- test/syncenginetestutils.h | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'test/syncenginetestutils.h') diff --git a/test/syncenginetestutils.h b/test/syncenginetestutils.h index c9f4e088a..9122040be 100644 --- a/test/syncenginetestutils.h +++ b/test/syncenginetestutils.h @@ -441,7 +441,8 @@ public: QMetaObject::invokeMethod(this, "respond", Qt::QueuedConnection); } - Q_INVOKABLE void respond() { + Q_INVOKABLE virtual void respond() + { emit uploadProgress(fileInfo->size, fileInfo->size); setRawHeader("OC-ETag", fileInfo->etag.toLatin1()); setRawHeader("ETag", fileInfo->etag.toLatin1()); @@ -615,7 +616,7 @@ class FakeChunkMoveReply : public QNetworkReply public: FakeChunkMoveReply(FileInfo &uploadsFileInfo, FileInfo &remoteRootFileInfo, QNetworkAccessManager::Operation op, const QNetworkRequest &request, - quint64 delayMs, QObject *parent) + QObject *parent) : QNetworkReply{ parent } { setRequest(request); @@ -675,10 +676,11 @@ public: fileInfo->lastModified = OCC::Utility::qDateTimeFromTime_t(request.rawHeader("X-OC-Mtime").toLongLong()); remoteRootFileInfo.find(fileName, /*invalidate_etags=*/true); - QTimer::singleShot(delayMs, this, &FakeChunkMoveReply::respond); + QTimer::singleShot(0, this, &FakeChunkMoveReply::respond); } - Q_INVOKABLE void respond() { + Q_INVOKABLE virtual void respond() + { setAttribute(QNetworkRequest::HttpStatusCodeAttribute, 201); setRawHeader("OC-ETag", fileInfo->etag.toLatin1()); setRawHeader("ETag", fileInfo->etag.toLatin1()); @@ -744,10 +746,32 @@ public: qint64 readData(char *, qint64) override { return 0; } }; +// A delayed reply +template +class DelayedReply : public OriginalReply +{ +public: + template + explicit DelayedReply(quint64 delayMS, Args &&... args) + : OriginalReply(std::forward(args)...) + , _delayMs(delayMS) + { + } + quint64 _delayMs; + + void respond() override + { + QTimer::singleShot(_delayMs, static_cast(this), [this] { + // Explicit call to bases's respond(); + this->OriginalReply::respond(); + }); + } +}; + class FakeQNAM : public QNetworkAccessManager { public: - using Override = std::function; + using Override = std::function; private: FileInfo _remoteRootFileInfo; @@ -770,7 +794,7 @@ protected: QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData = 0) { if (_override) { - if (auto reply = _override(op, request)) + if (auto reply = _override(op, request, outgoingData)) return reply; } const QString fileName = getFilePathFromUrl(request.url()); @@ -796,7 +820,7 @@ protected: else if (verb == QLatin1String("MOVE") && !isUpload) return new FakeMoveReply{info, op, request, this}; else if (verb == QLatin1String("MOVE") && isUpload) - return new FakeChunkMoveReply{ info, _remoteRootFileInfo, op, request, 0, this }; + return new FakeChunkMoveReply{ info, _remoteRootFileInfo, op, request, this }; else { qDebug() << verb << outgoingData; Q_UNREACHABLE(); -- cgit v1.2.3