Remove all JSON marshalling code from QObjectHandler.
This commit is contained in:
@@ -20,11 +20,8 @@
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QObject>
|
||||
#include <QTest>
|
||||
#include <QVariantMap>
|
||||
|
||||
#include <QHttpEngine/QHttpSocket>
|
||||
#include <QHttpEngine/QObjectHandler>
|
||||
@@ -38,12 +35,11 @@ class DummyAPI : public QObject
|
||||
|
||||
public Q_SLOTS:
|
||||
|
||||
int invalidReturnValue() { return 0; }
|
||||
QVariantMap invalidArguments(int) { return QVariantMap(); }
|
||||
QVariantMap noParameters() { return QVariantMap(); }
|
||||
QVariantMap oneParameter(QHttpSocket *) { return QVariantMap(); }
|
||||
QVariantMap twoParameters(QHttpSocket *, QVariantMap) { return QVariantMap(); }
|
||||
QVariantMap echoPost(QHttpSocket *, QVariantMap d) { return d; }
|
||||
void wrongArgumentCount() {}
|
||||
void wrongArgumentType(int) {}
|
||||
void valid(QHttpSocket *socket) {
|
||||
socket->writeError(QHttpSocket::OK);
|
||||
}
|
||||
};
|
||||
|
||||
class TestQObjectHandler : public QObject
|
||||
@@ -59,75 +55,31 @@ private Q_SLOTS:
|
||||
|
||||
void TestQObjectHandler::testOldConnection_data()
|
||||
{
|
||||
QTest::addColumn<bool>("registerPost");
|
||||
QTest::addColumn<bool>("requestPost");
|
||||
QTest::addColumn<QByteArray>("slot");
|
||||
QTest::addColumn<int>("statusCode");
|
||||
QTest::addColumn<QVariantMap>("data");
|
||||
|
||||
QTest::newRow("invalid return")
|
||||
<< false
|
||||
<< false
|
||||
<< QByteArray(SLOT(invalidReturnValue()))
|
||||
<< static_cast<int>(QHttpSocket::InternalServerError)
|
||||
<< QVariantMap();
|
||||
QTest::newRow("wrong argument count")
|
||||
<< QByteArray(SLOT(wrongArgumentCount()))
|
||||
<< static_cast<int>(QHttpSocket::InternalServerError);
|
||||
|
||||
QTest::newRow("invalid arguments")
|
||||
<< false
|
||||
<< false
|
||||
<< QByteArray(SLOT(invalidArguments(int)))
|
||||
<< static_cast<int>(QHttpSocket::InternalServerError)
|
||||
<< QVariantMap();
|
||||
QTest::newRow("wrong argument type")
|
||||
<< QByteArray(SLOT(wrongArgumentType(int)))
|
||||
<< static_cast<int>(QHttpSocket::InternalServerError);
|
||||
|
||||
QTest::newRow("no parameters")
|
||||
<< false
|
||||
<< false
|
||||
<< QByteArray(SLOT(noParameters()))
|
||||
<< static_cast<int>(QHttpSocket::OK)
|
||||
<< QVariantMap();
|
||||
|
||||
QTest::newRow("one parameter")
|
||||
<< false
|
||||
<< false
|
||||
<< QByteArray(SLOT(oneParameter(QHttpSocket*)))
|
||||
<< static_cast<int>(QHttpSocket::OK)
|
||||
<< QVariantMap();
|
||||
|
||||
QTest::newRow("two parameters")
|
||||
<< false
|
||||
<< false
|
||||
<< QByteArray(SLOT(twoParameters(QHttpSocket*,QVariantMap)))
|
||||
<< static_cast<int>(QHttpSocket::OK)
|
||||
<< QVariantMap();
|
||||
|
||||
QTest::newRow("invalid method")
|
||||
<< true
|
||||
<< false
|
||||
<< QByteArray(SLOT(echoPost(QHttpSocket*,QVariantMap)))
|
||||
<< static_cast<int>(QHttpSocket::MethodNotAllowed)
|
||||
<< QVariantMap();
|
||||
|
||||
QTest::newRow("post data")
|
||||
<< true
|
||||
<< true
|
||||
<< QByteArray(SLOT(echoPost(QHttpSocket*,QVariantMap)))
|
||||
<< static_cast<int>(QHttpSocket::OK)
|
||||
<< QVariantMap{{"a", "a"}, {"b", 1}};
|
||||
QTest::newRow("valid")
|
||||
<< QByteArray(SLOT(valid(QHttpSocket*)))
|
||||
<< static_cast<int>(QHttpSocket::OK);
|
||||
}
|
||||
|
||||
void TestQObjectHandler::testOldConnection()
|
||||
{
|
||||
QFETCH(bool, registerPost);
|
||||
QFETCH(bool, requestPost);
|
||||
QFETCH(QByteArray, slot);
|
||||
QFETCH(int, statusCode);
|
||||
QFETCH(QVariantMap, data);
|
||||
|
||||
QObjectHandler handler;
|
||||
DummyAPI api;
|
||||
|
||||
handler.registerMethod("test", &api, slot.constData(),
|
||||
registerPost ? QHttpSocket::POST : QHttpSocket::GET);
|
||||
handler.registerMethod("test", &api, slot.constData());
|
||||
|
||||
QSocketPair pair;
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
@@ -135,26 +87,11 @@ void TestQObjectHandler::testOldConnection()
|
||||
QSimpleHttpClient client(pair.client());
|
||||
QHttpSocket socket(pair.server(), &pair);
|
||||
|
||||
if (requestPost) {
|
||||
QByteArray buff = QJsonDocument(QJsonObject::fromVariantMap(data)).toJson();
|
||||
client.sendHeaders("POST", "test", QHttpSocket::QHttpHeaderMap{
|
||||
{"Content-Length", QByteArray::number(buff.length())},
|
||||
});
|
||||
client.sendData(buff);
|
||||
} else {
|
||||
client.sendHeaders("GET", "test");
|
||||
}
|
||||
|
||||
client.sendHeaders("GET", "test");
|
||||
QTRY_VERIFY(socket.isHeadersParsed());
|
||||
|
||||
handler.route(&socket, socket.path());
|
||||
QTRY_COMPARE(client.statusCode(), statusCode);
|
||||
|
||||
if (requestPost) {
|
||||
QVERIFY(client.headers().contains("Content-Length"));
|
||||
QTRY_COMPARE(client.data().length(), client.headers().value("Content-Length").toInt());
|
||||
QCOMPARE(QJsonDocument::fromJson(client.data()).object(), QJsonObject::fromVariantMap(data));
|
||||
}
|
||||
}
|
||||
|
||||
void TestQObjectHandler::testNewConnection()
|
||||
@@ -163,17 +100,17 @@ void TestQObjectHandler::testNewConnection()
|
||||
DummyAPI api;
|
||||
|
||||
// Connect to object slot
|
||||
handler.registerMethod("0", &api, &DummyAPI::noParameters);
|
||||
handler.registerMethod("1", &api, &DummyAPI::oneParameter);
|
||||
handler.registerMethod("2", &api, &DummyAPI::twoParameters);
|
||||
handler.registerMethod("0", &api, &DummyAPI::valid);
|
||||
|
||||
// Connect to functor
|
||||
handler.registerMethod("3", []() { return QVariantMap(); });
|
||||
handler.registerMethod("4", &api, []() { return QVariantMap(); });
|
||||
handler.registerMethod("5", &api, [](QHttpSocket*) { return QVariantMap(); });
|
||||
handler.registerMethod("6", &api, [](QHttpSocket*, QVariantMap d) { return d; });
|
||||
handler.registerMethod("1", [](QHttpSocket *socket) {
|
||||
socket->writeError(QHttpSocket::OK);
|
||||
});
|
||||
handler.registerMethod("2", &api, [](QHttpSocket *socket) {
|
||||
socket->writeError(QHttpSocket::OK);
|
||||
});
|
||||
|
||||
for (int i = 0; i < 7; ++i) {
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
QSocketPair pair;
|
||||
QTRY_VERIFY(pair.isConnected());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user