File indexing completed on 2024-04-21 15:32:09

0001 /** ===========================================================
0002  * @file
0003  *
0004  * This file is a part of KDE project
0005  * <a href="https://commits.kde.org/libmediawiki">libmediawiki</a>
0006  *
0007  * @date   2010-03-22
0008  * @brief  a MediaWiki C++ interface for KDE
0009  *
0010  * @author Copyright (C) 2010-2011 by Hormiere Guillaume
0011  *         <a href="mailto:hormiere dot guillaume at gmail dot com">hormiere dot guillaume at gmail dot com</a>
0012  *
0013  * This program is free software; you can redistribute it
0014  * and/or modify it under the terms of the GNU General
0015  * Public License as published by the Free Software Foundation;
0016  * either version 2, or (at your option)
0017  * any later version.
0018  *
0019  * This program is distributed in the hope that it will be useful,
0020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0022  * GNU General Public License for more details.
0023  *
0024  * ============================================================ */
0025 
0026 #ifndef SERVER_H
0027 #define SERVER_H
0028 
0029 #include <QList>
0030 #include <QMutex>
0031 #include <QObject>
0032 #include <QString>
0033 #include <QStringList>
0034 #include <QThread>
0035 #include <QTcpServer>
0036 #include <QTcpSocket>
0037 
0038 class FakeServer : QThread
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043 
0044     struct Request
0045     {
0046         Request(){}
0047         Request(const QString& t, const QString& a, const QString& v){type=t; agent=a; value=v;}
0048 
0049         QString type;
0050         QString agent;
0051         QString value;
0052 
0053         bool operator==(const FakeServer::Request &other) const {
0054             return this->type  == other.type  &&
0055                    this->agent == other.agent &&
0056                    this->value == other.value;
0057         }
0058     };
0059 
0060     FakeServer(QObject* const parent = nullptr);
0061     ~FakeServer() override;
0062 
0063     void startAndWait();
0064     void run() override;
0065 
0066     void setScenario( const QString& scenario, const QString& cookie = QStringLiteral("empty"));
0067     void addScenario( const QString& scenario, const QString& cookie = QStringLiteral("empty"));
0068     void addScenarioFromFile( const QString& fileName, const QString& cookie = QStringLiteral("empty"));
0069 
0070     bool isScenarioDone( int scenarioNumber ) const;
0071     bool isAllScenarioDone() const;
0072     QList<FakeServer::Request>& getRequest(){return m_request;} const
0073     FakeServer::Request takeLastRequest(){return m_request.takeLast();}
0074     FakeServer::Request takeFirstRequest(){return m_request.takeFirst();}
0075     void clearRequest(){return m_request.clear();}
0076 
0077 private Q_SLOTS:
0078 
0079     void newConnection();
0080     void dataAvailable();
0081     void started();
0082 
0083 private:
0084 
0085     QStringList                m_scenarios;
0086     QStringList                m_cookie;
0087     QList<FakeServer::Request> m_request;
0088     QTcpServer*                m_tcpServer;
0089     mutable QMutex             m_mutex;
0090     QTcpSocket*                m_clientSocket;
0091 };
0092 //! [0]
0093 
0094 #endif // SERVER_H