SyncEngineTest: Add network override

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.
This commit is contained in:
Christian Kamm 2017-06-14 12:03:40 +02:00
Родитель b28e06608c
Коммит d50d8b86cf
1 изменённых файлов: 16 добавлений и 1 удалений

Просмотреть файл

@ -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