void TestQJsonRpcMessage::testInvalidDataResponseWithoutId()
{
    // invalid without id
    const char *invalid = "{\"jsonrpc\": \"2.0\", \"params\": []}";
    QJsonRpcMessage request(invalid);
    QJsonRpcMessage error    = request.createErrorResponse(QJsonRpc::NoError, QString());
    QJsonRpcMessage response = request.createResponse(QString());
    QCOMPARE(request.type(), QJsonRpcMessage::Invalid);
    QCOMPARE(response.type(), QJsonRpcMessage::Invalid);    
    QCOMPARE(error.id(), 0);
}
Esempio n. 2
0
QDebug operator<<(QDebug dbg, const QJsonRpcMessage &msg)
{
    dbg.nospace() << "QJsonRpcMessage(type=" << msg.type();
    if (msg.type() != QJsonRpcMessage::Notification) {
        dbg.nospace() << ", id=" << msg.id();
    }

    if (msg.type() == QJsonRpcMessage::Request ||
        msg.type() == QJsonRpcMessage::Notification) {
        dbg.nospace() << ", method=" << msg.method()
                      << ", params=" << msg.params();
    } else if (msg.type() == QJsonRpcMessage::Response) {
        dbg.nospace() << ", result=" << msg.result();
    } else if (msg.type() == QJsonRpcMessage::Error) {
        dbg.nospace() << ", code=" << msg.errorCode()
                      << ", message=" << msg.errorMessage()
                      << ", data=" << msg.errorData();
    }
    dbg.nospace() << ")";
    return dbg.space();
}
void TestQJsonRpcMessage::testWithVariantListArgs()
{
    const char *varListArgsFormat = "{ " \
            "\"id\": %1, " \
            "\"jsonrpc\": \"2.0\", " \
            "\"method\": \"service.variantListParameter\", " \
            "\"params\": [[ 1, 20, \"hello\", false ]] " \
            "}";

    QVariantList firstParameter;
    firstParameter << 1 << 20 << "hello" << false;
    QJsonRpcMessage requestFromQJsonRpc =
        QJsonRpcMessage::createRequest("service.variantListParameter", QVariant(firstParameter));

    // QJsonRpcMessage::createRequest is creating objects with an unique id,
    // and to allow a random test execution order - json data must have the same id
    int id = requestFromQJsonRpc.id();
    QByteArray varListArgs = QString(varListArgsFormat).arg(id).toLatin1();

    QJsonRpcMessage requestFromData(varListArgs);
    QCOMPARE(requestFromQJsonRpc, requestFromData);
}
Esempio n. 4
0
bool QJsonRpcMessage::operator==(const QJsonRpcMessage &message) const
{
    if (message.d == d)
        return true;

    if (message.type() == type()) {
        if (message.type() == QJsonRpcMessage::Error) {
            return (message.errorCode() == errorCode() &&
                    message.errorMessage() == errorMessage() &&
                    message.errorData() == errorData());
        } else {
            if (message.type() == QJsonRpcMessage::Notification) {
                return (message.method() == method() &&
                        message.params() == params());
            } else {
                return (message.id() == id() &&
                        message.method() == method() &&
                        message.params() == params());
            }
        }
    }

    return false;
}
void TestQJsonRpcMessage::testNotificationNoId()
{
    QJsonRpcMessage notification = QJsonRpcMessage::createNotification("testNotification");
    QCOMPARE(notification.id(), -1);
}
void TestQJsonRpcMessage::testResponseSameId()
{
    QJsonRpcMessage request = QJsonRpcMessage::createRequest("testMethod");
    QJsonRpcMessage response = request.createResponse("testResponse");
    QCOMPARE(response.id(), request.id());
}