File indexing completed on 2024-05-19 04:01:09

0001 /*
0002     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #include "test-config.h"
0008 
0009 #include "servercontroller.h"
0010 
0011 #include <rest/restapi.h>
0012 #include <rest/restclient.h>
0013 
0014 #include <QNetworkReply>
0015 #include <QUrl>
0016 #include <QSignalSpy>
0017 
0018 using namespace KUserFeedback::Console;
0019 
0020 ServerController::ServerController()
0021 {
0022     m_process.setProgram(QStringLiteral(PHP_EXECUTABLE));
0023     m_process.setWorkingDirectory(QLatin1String(CURRENT_SOURCE_DIR "/../src/testserver"));
0024     m_process.setArguments({QStringLiteral("-S"), QStringLiteral("localhost:1984"), QStringLiteral("router.php")});
0025     m_process.setProcessChannelMode(QProcess::ForwardedChannels);
0026 }
0027 
0028 ServerController::~ServerController()
0029 {
0030     if (m_process.state() == QProcess::Running)
0031         stop();
0032 }
0033 
0034 bool ServerController::start()
0035 {
0036     m_process.start();
0037     if (!m_process.waitForStarted(1000)) {
0038         qWarning() << "Failed to launch:" << m_process.program() << m_process.arguments() << m_process.errorString();
0039         return false;
0040     }
0041 
0042     // wait for the server to become available, otherwise the HTTP access below fails
0043     m_process.waitForReadyRead(100);
0044 
0045     ServerInfo s;
0046     s.setUrl(url());
0047     RESTClient client;
0048     client.setServerInfo(s);
0049     auto reply = RESTApi::checkSchema(&client);
0050 
0051     QSignalSpy spy(reply, &QNetworkReply::finished);
0052     if (!spy.wait())
0053         return false;
0054 
0055     if (reply->error() != QNetworkReply::NoError)
0056         qWarning() << "Schema check failed:" << reply->errorString();
0057     return reply->error() == QNetworkReply::NoError;
0058 }
0059 
0060 void ServerController::stop()
0061 {
0062     m_process.terminate();
0063     m_process.waitForFinished(1000);
0064 }
0065 
0066 QUrl ServerController::url() const
0067 {
0068     return QUrl(QStringLiteral("http://localhost:1984"));
0069 }