diff options
| author | Mitch Curtis <mitch.curtis@qt.io> | 2021-08-06 12:27:35 +0200 |
|---|---|---|
| committer | Mitch Curtis <mitch.curtis@qt.io> | 2021-09-14 12:13:57 +0200 |
| commit | 5ae7066e84e45721ce58e9d16e3c4da72aa51692 (patch) | |
| tree | d0d710678c679477b0413744825b3c9ff0060569 /src/quicktestutils/qml/testhttpserver_p.h | |
| parent | b89322772234047849c4d44b592e1a555029bc7e (diff) | |
Consolidate test helpers into private libraries
Previously each test would include and build sources from the shared
folder. Now we make those sources a library, build it once, then have
each test link to it instead.
We also take the opportunity to move some helpers that qtquickcontrols2
had added into the quicktestutils library where it makes sense, and
for the helpers that don't make sense to be there, move them into
quickcontrolstestutils.
We add the libraries to src/ so that they are internal modules built as
part of Qt, rather than tests. That way we can use them in a standalone
test outside of qtdeclarative.
Task-number: QTBUG-95621
Change-Id: I0a2ab3976fdbff2e4414df7bdc0808f16453b80a
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit e310dadef779b28845b41fb091634cd001cda9de)
Diffstat (limited to 'src/quicktestutils/qml/testhttpserver_p.h')
| -rw-r--r-- | src/quicktestutils/qml/testhttpserver_p.h | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/src/quicktestutils/qml/testhttpserver_p.h b/src/quicktestutils/qml/testhttpserver_p.h new file mode 100644 index 0000000000..db99b348ca --- /dev/null +++ b/src/quicktestutils/qml/testhttpserver_p.h @@ -0,0 +1,143 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef TESTHTTPSERVER_P_H +#define TESTHTTPSERVER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QTcpServer> +#include <QUrl> +#include <QPair> +#include <QThread> +#include <QMutex> +#include <QWaitCondition> + +QT_BEGIN_NAMESPACE + +class TestHTTPServer : public QObject +{ + Q_OBJECT +public: + TestHTTPServer(); + + bool listen(); + quint16 port() const; + QUrl baseUrl() const; + QUrl url(const QString &documentPath) const; + QString urlString(const QString &documentPath) const; + QString errorString() const; + + enum Mode { Normal, Delay, Disconnect }; + bool serveDirectory(const QString &, Mode = Normal); + + bool wait(const QUrl &expect, const QUrl &reply, const QUrl &body); + bool hasFailed() const; + + void addAlias(const QString &filename, const QString &aliasName); + void addRedirect(const QString &filename, const QString &redirectName); + + void registerFileNameForContentSubstitution(const QString &fileName); + + // In Delay mode, each item needs one call to this function to be sent + void sendDelayedItem(); + +private slots: + void newConnection(); + void disconnected(); + void readyRead(); + void sendOne(); + +private: + enum State { + AwaitingHeader, + AwaitingData, + Failed + }; + + void serveGET(QTcpSocket *, const QByteArray &); + bool reply(QTcpSocket *, const QByteArray &); + + QList<QPair<QString, Mode> > m_directories; + QHash<QTcpSocket *, QByteArray> m_dataCache; + QList<QPair<QTcpSocket *, QByteArray> > m_toSend; + QSet<QString> m_contentSubstitutedFileNames; + + struct WaitData { + QList<QByteArray> headerExactMatches; + QList<QByteArray> headerPrefixes; + QByteArray body; + } m_waitData; + QByteArray m_replyData; + QByteArray m_bodyData; + QByteArray m_data; + State m_state; + + QHash<QString, QString> m_aliases; + QHash<QString, QString> m_redirects; + + QTcpServer m_server; +}; + +class ThreadedTestHTTPServer : public QThread +{ + Q_OBJECT +public: + ThreadedTestHTTPServer(const QString &dir, TestHTTPServer::Mode mode = TestHTTPServer::Normal); + ThreadedTestHTTPServer(const QHash<QString, TestHTTPServer::Mode> &dirs); + ~ThreadedTestHTTPServer(); + + QUrl baseUrl() const; + QUrl url(const QString &documentPath) const; + QString urlString(const QString &documentPath) const; + +protected: + void run() override; + +private: + void start(); + + QHash<QString, TestHTTPServer::Mode> m_dirs; + quint16 m_port; + QMutex m_mutex; + QWaitCondition m_condition; +}; + +QT_END_NAMESPACE + +#endif // TESTHTTPSERVER_P_H + |
