SyncEngineTest: Add network override
authorChristian Kamm <mail@ckamm.de>
Wed, 14 Jun 2017 10:03:40 +0000 (12:03 +0200)
committerChristian Kamm <mail@ckamm.de>
Thu, 15 Jun 2017 11:53:57 +0000 (13:53 +0200)
This is useful for monitoring what kind of network requests are
sent to the fake server. Such as "did this sync cause an upload?"
and "was there a propfind for this path?". It can also inject
custom replies.

test/syncenginetestutils.h

index fe4676821b33b12ef09e907e5ec191b65bfd46fd..ee15d8e33bd6e789297c3e6e7327209bcbb7eb3c 100644 (file)
@@ -706,10 +706,17 @@ public:
 
 class FakeQNAM : public QNetworkAccessManager
 {
+public:
+    using Override = std::function<QNetworkReply *(Operation, const QNetworkRequest &)>;
+
+private:
     FileInfo _remoteRootFileInfo;
     FileInfo _uploadFileInfo;
     // maps a path to an HTTP error
     QHash<QString, int> _errorPaths;
+    // monitor requests and optionally provide custom replies
+    Override _override;
+
 public:
     FakeQNAM(FileInfo initialRoot) : _remoteRootFileInfo{std::move(initialRoot)} { }
     FileInfo &currentRemoteState() { return _remoteRootFileInfo; }
@@ -717,6 +724,8 @@ public:
 
     QHash<QString, int> &errorPaths() { return _errorPaths; }
 
+    void setOverride(const Override &override) { _override = override; }
+
 protected:
     QNetworkReply *createRequest(Operation op, const QNetworkRequest &request,
                                          QIODevice *outgoingData = 0) {
@@ -728,8 +737,13 @@ protected:
         bool isUpload = request.url().path().startsWith(sUploadUrl.path());
         FileInfo &info = isUpload ? _uploadFileInfo : _remoteRootFileInfo;
 
+        if (_override) {
+            if (auto reply = _override(op, request))
+                return reply;
+        }
+
         auto verb = request.attribute(QNetworkRequest::CustomVerbAttribute);
-        if (verb == QLatin1String("PROPFIND"))
+        if (verb == "PROPFIND")
             // Ignore outgoingData always returning somethign good enough, works for now.
             return new FakePropfindReply{info, op, request, this};
         else if (verb == QLatin1String("GET") || op == QNetworkAccessManager::GetOperation)
@@ -825,6 +839,7 @@ public:
         void clear() { _qnam->errorPaths().clear(); }
     };
     ErrorList serverErrorPaths() { return {_fakeQnam}; }
+    void setServerOverride(const FakeQNAM::Override &override) { _fakeQnam->setOverride(override); }
 
     QString localPath() const {
         // SyncEngine wants a trailing slash