Commit 979a6bb4 authored by Krzysztof Nowicki's avatar Krzysztof Nowicki Committed by Laurent Montel
Browse files

Add callback to override fake server response



This callback can be used to globally override responses for requests
before they get processed by the dialog handlers.
Signed-off-by: Krzysztof Nowicki's avatarKrzysztof Nowicki <krissn@op.pl>
parent faea32ee
/*
SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
SPDX-FileCopyrightText: 2015-2019 Krzysztof Nowicki <krissn@op.pl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -126,7 +126,19 @@ void FakeEwsConnection::dataAvailable()
return;
}
FakeEwsServer::DialogEntry::HttpResponse resp = parseRequest(QString::fromUtf8(mContent));
FakeEwsServer::DialogEntry::HttpResponse resp = FakeEwsServer::EmptyResponse;
const auto server = qobject_cast<FakeEwsServer *>(parent());
const auto overrideReplyCallback = server->overrideReplyCallback();
if (overrideReplyCallback) {
QXmlResultItems ri;
QXmlNamePool namePool;
resp = overrideReplyCallback(QString::fromUtf8(mContent), ri, namePool);
}
if (resp == FakeEwsServer::EmptyResponse) {
resp = parseRequest(QString::fromUtf8(mContent));
}
bool chunked = false;
if (resp == FakeEwsServer::EmptyResponse) {
......@@ -141,7 +153,6 @@ void FakeEwsConnection::dataAvailable()
}
}
auto server = qobject_cast<FakeEwsServer *>(parent());
auto defaultReplyCallback = server->defaultReplyCallback();
if (defaultReplyCallback && (resp == FakeEwsServer::EmptyResponse)) {
QXmlResultItems ri;
......
/*
SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
SPDX-FileCopyrightText: 2015-2019 Krzysztof Nowicki <krissn@op.pl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -61,6 +61,13 @@ void FakeEwsServer::setDefaultReplyCallback(const DialogEntry::ReplyCallback &de
mDefaultReplyCallback = defaultReplyCallback;
}
void FakeEwsServer::setOverrideReplyCallback(const DialogEntry::ReplyCallback &overrideReplyCallback)
{
QMutexLocker lock(&mMutex);
mOverrideReplyCallback = overrideReplyCallback;
}
void FakeEwsServer::queueEventsXml(const QStringList &events)
{
if (QThread::currentThread() != thread()) {
......@@ -107,6 +114,13 @@ const FakeEwsServer::DialogEntry::ReplyCallback FakeEwsServer::defaultReplyCallb
return mDefaultReplyCallback;
}
const FakeEwsServer::DialogEntry::ReplyCallback FakeEwsServer::overrideReplyCallback() const
{
QMutexLocker lock(&mMutex);
return mOverrideReplyCallback;
}
void FakeEwsServer::streamingConnectionStarted(FakeEwsConnection *conn)
{
if (mStreamingEventsConnection) {
......
/*
SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
SPDX-FileCopyrightText: 2015-2019 Krzysztof Nowicki <krissn@op.pl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -41,6 +41,7 @@ public:
~FakeEwsServer() override;
bool start();
void setDefaultReplyCallback(const DialogEntry::ReplyCallback &defaultReplyCallback);
void setOverrideReplyCallback(const DialogEntry::ReplyCallback &overrideReplyCallback);
void queueEventsXml(const QStringList &events);
void setDialog(const DialogEntry::List &dialog);
ushort portNumber() const;
......@@ -53,10 +54,12 @@ private:
void sendError(QTcpSocket *sock, const QString &msg, ushort code = 500);
const DialogEntry::List dialog() const;
const DialogEntry::ReplyCallback defaultReplyCallback() const;
const DialogEntry::ReplyCallback overrideReplyCallback() const;
QStringList retrieveEventsXml();
DialogEntry::List mDialog;
DialogEntry::ReplyCallback mDefaultReplyCallback;
DialogEntry::ReplyCallback mOverrideReplyCallback;
QStringList mEventQueue;
QPointer<FakeEwsConnection> mStreamingEventsConnection;
ushort mPortNumber;
......
/*
SPDX-FileCopyrightText: 2017 Krzysztof Nowicki <krissn@op.pl>
SPDX-FileCopyrightText: 2017-2019 Krzysztof Nowicki <krissn@op.pl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -64,6 +64,15 @@ void FakeEwsServerThread::setDefaultReplyCallback(const FakeEwsServer::DialogEnt
}
}
void FakeEwsServerThread::setOverrideReplyCallback(const FakeEwsServer::DialogEntry::ReplyCallback &overrideReplyCallback)
{
QMutexLocker lock(&mMutex);
if (mServer) {
mServer->setOverrideReplyCallback(overrideReplyCallback);
}
}
void FakeEwsServerThread::queueEventsXml(const QStringList &events)
{
QMutexLocker lock(&mMutex);
......
......@@ -32,6 +32,7 @@ public:
void setDialog(const FakeEwsServer::DialogEntry::List &dialog);
void setDefaultReplyCallback(const FakeEwsServer::DialogEntry::ReplyCallback &defaultReplyCallback);
void setOverrideReplyCallback(const FakeEwsServer::DialogEntry::ReplyCallback &overrideReplyCallback);
void queueEventsXml(const QStringList &events);
bool waitServerStarted() const;
Q_SIGNALS:
......
/*
SPDX-FileCopyrightText: 2015-2017 Krzysztof Nowicki <krissn@op.pl>
SPDX-FileCopyrightText: 2015-2019 Krzysztof Nowicki <krissn@op.pl>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -26,6 +26,7 @@ private Q_SLOTS:
void invalidMethod();
void emptyRequest();
void defaultCallback();
void overrideCallback();
void simpleResponse();
void callbackResponse();
void multipleResponses();
......@@ -151,6 +152,25 @@ void UtEwsFakeSrvTest::defaultCallback()
QCOMPARE(resp.second, static_cast<ushort>(200));
}
void UtEwsFakeSrvTest::overrideCallback()
{
const FakeEwsServer::DialogEntry::List dialog = {
{QStringLiteral("if (//test1/a = <a />) then (<b/>) else ()"), FakeEwsServer::DialogEntry::ReplyCallback(), QStringLiteral("Sample request 1")}};
QString receivedReq;
QScopedPointer<FakeEwsServer> srv(new FakeEwsServer(this));
srv->setDialog(dialog);
srv->setOverrideReplyCallback([&receivedReq](const QString &req, QXmlResultItems &, const QXmlNamePool &) {
receivedReq = req;
return FakeEwsServer::DialogEntry::HttpResponse(QStringLiteral("testresp"), 200);
});
QVERIFY(srv->start());
auto resp = synchronousHttpReq(QStringLiteral("<test1><a /></test1>"), srv->portNumber());
QCOMPARE(resp.first, QStringLiteral("testresp"));
QCOMPARE(resp.second, static_cast<ushort>(200));
}
void UtEwsFakeSrvTest::simpleResponse()
{
const FakeEwsServer::DialogEntry::List dialog = {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment