1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/*
* SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
*
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "testutils.h"
#include "job.h"
#include <QEventLoop>
#include <QFile>
#include <QJsonDocument>
#include <QProcess>
#include <QTemporaryFile>
#include <iostream>
bool execJob(KGAPI2::Job *job)
{
QEventLoop loop;
QObject::connect(job, &KGAPI2::Job::finished, &loop, &QEventLoop::quit);
return loop.exec() == 0;
}
FakeNetworkAccessManager::Scenario scenarioFromFile(const QString &requestFile, const QString &responseFile, bool needsAuth)
{
QFile request(requestFile);
VERIFY_RET(request.open(QIODevice::ReadOnly), {});
FakeNetworkAccessManager::Scenario scenario;
auto http = request.readLine().trimmed();
if (http.startsWith("PUT")) {
scenario.requestMethod = QNetworkAccessManager::PutOperation;
scenario.requestUrl = QUrl(QString::fromLatin1(http.constData() + 4));
} else if (http.startsWith("POST")) {
scenario.requestMethod = QNetworkAccessManager::PostOperation;
scenario.requestUrl = QUrl(QString::fromLatin1(http.constData() + 5));
} else if (http.startsWith("GET")) {
scenario.requestMethod = QNetworkAccessManager::GetOperation;
scenario.requestUrl = QUrl(QString::fromLatin1(http.constData() + 4));
} else if (http.startsWith("DELETE")) {
scenario.requestMethod = QNetworkAccessManager::DeleteOperation;
scenario.requestUrl = QUrl(QString::fromLatin1(http.constData() + 7));
} else if (http.startsWith("PATCH")) {
scenario.requestMethod = QNetworkAccessManager::CustomOperation;
scenario.requestUrl = QUrl(QString::fromLatin1(http.constData() + 6));
} else {
FAIL_RET("Invalid request method in test data", {});
}
auto line = request.readLine().trimmed();
while (!line.isEmpty()) {
const int idx = line.indexOf(':');
scenario.requestHeaders.push_back(qMakePair(line.left(idx), line.mid(idx + 1).trimmed()));
line = request.readLine().trimmed();
}
scenario.requestData = request.readAll();
QFile response(responseFile);
VERIFY_RET(response.open(QIODevice::ReadOnly), {});
http = response.readLine();
scenario.responseCode = http.mid(9, 3).toInt();
line = response.readLine().trimmed();
while (!line.isEmpty()) {
const int idx = line.indexOf(':');
scenario.responseHeaders.push_back(qMakePair(line.left(idx), line.mid(idx + 1).trimmed()));
line = response.readLine().trimmed();
}
scenario.responseData = response.readAll();
scenario.needsAuth = needsAuth;
return scenario;
}
QByteArray reformatXML(const QByteArray &xml)
{
#ifdef Q_OS_UNIX
QProcess lint;
lint.start(QStringLiteral("xmllint"), {QStringLiteral("--format"), QStringLiteral("-")});
lint.waitForStarted();
if (lint.state() != QProcess::Running) { // missing xmllint?
return xml;
}
if (!xml.startsWith("<?xml")) {
lint.write("<?xml version=\"1.0\" ?>");
}
lint.write(xml);
lint.closeWriteChannel();
lint.waitForFinished();
const auto err = lint.readAllStandardError();
if (!err.isEmpty()) {
std::cerr << err.constData() << std::endl;
FAIL_RET("Malformed XML!", xml);
}
return lint.readAllStandardOutput();
#else
return xml;
#endif
}
QByteArray reformatJSON(const QByteArray &json)
{
return QJsonDocument::fromJson(json).toJson(QJsonDocument::Indented);
}
QByteArray diffData(const QByteArray &actual, const QByteArray &expected)
{
#ifdef Q_OS_UNIX
QTemporaryFile tmpActual(QStringLiteral("%1-actual.XXXXXX").arg(QCoreApplication::applicationName()));
tmpActual.open();
tmpActual.write(actual);
tmpActual.close();
QTemporaryFile tmpExpected(QStringLiteral("%1-expected.XXXXXX").arg(QCoreApplication::applicationName()));
tmpExpected.open();
tmpExpected.write(expected);
tmpExpected.close();
QProcess diff;
diff.start(QStringLiteral("diff"), {QStringLiteral("-a"), QStringLiteral("-u"), tmpActual.fileName(), tmpExpected.fileName()});
diff.waitForStarted();
if (diff.state() != QProcess::Running) {
return "======== ACTUAL ========\n\n" + actual + "\n\n"
"======= EXPECTED =======\n\n" + expected + "\n\n"
"========================";
}
diff.waitForFinished();
return diff.readAll();
#else
return "======== ACTUAL ========\n\n" + actual + "\n\n"
"======= EXPECTED =======\n\n" + expected + "\n\n"
"========================";
#endif
}
|