aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/shared
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@theqtcompany.com>2015-09-25 10:30:59 +0200
committerUlf Hermann <ulf.hermann@theqtcompany.com>2015-10-14 08:03:38 +0000
commit35da68a15c12586415484def802839e67372eac1 (patch)
tree76639f2eba2999e519511d51a42c8693b4f27299 /tests/auto/shared
parent9e15fb156a0ef58584661a0599f1f85d7597e87c (diff)
Provide a threaded TestHTTPServer
This allows us to do blocking operations that interact with the test server in the main thread. The threaded server is used in tests that don't explicitly require asynchronous operation. Change-Id: Ibcb28e79a1114cb9cfb812e86aae0a1af71c569e Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Diffstat (limited to 'tests/auto/shared')
-rw-r--r--tests/auto/shared/testhttpserver.cpp79
-rw-r--r--tests/auto/shared/testhttpserver.h28
2 files changed, 102 insertions, 5 deletions
diff --git a/tests/auto/shared/testhttpserver.cpp b/tests/auto/shared/testhttpserver.cpp
index 6c466293b6..80ce10cacd 100644
--- a/tests/auto/shared/testhttpserver.cpp
+++ b/tests/auto/shared/testhttpserver.cpp
@@ -36,6 +36,7 @@
#include <QDebug>
#include <QFile>
#include <QTimer>
+#include <QTest>
/*!
\internal
@@ -80,6 +81,16 @@ The following request urls will then result in the appropriate action:
\row \li http://localhost:14445/slowMain.qml \li slowMain.qml returned after 500ms
\endtable
*/
+
+static QUrl localHostUrl(quint16 port)
+{
+ QUrl url;
+ url.setScheme(QStringLiteral("http"));
+ url.setHost(QStringLiteral("127.0.0.1"));
+ url.setPort(port);
+ return url;
+}
+
TestHTTPServer::TestHTTPServer()
: m_state(AwaitingHeader)
{
@@ -93,11 +104,12 @@ bool TestHTTPServer::listen()
QUrl TestHTTPServer::baseUrl() const
{
- QUrl url;
- url.setScheme(QStringLiteral("http"));
- url.setHost(QStringLiteral("127.0.0.1"));
- url.setPort(m_server.serverPort());
- return url;
+ return localHostUrl(m_server.serverPort());
+}
+
+quint16 TestHTTPServer::port() const
+{
+ return m_server.serverPort();
}
QUrl TestHTTPServer::url(const QString &documentPath) const
@@ -377,3 +389,60 @@ void TestHTTPServer::serveGET(QTcpSocket *socket, const QByteArray &data)
socket->disconnectFromHost();
}
}
+
+ThreadedTestHTTPServer::ThreadedTestHTTPServer(const QString &dir, TestHTTPServer::Mode mode) :
+ m_port(0)
+{
+ m_dirs[dir] = mode;
+ start();
+}
+
+ThreadedTestHTTPServer::ThreadedTestHTTPServer(const QHash<QString, TestHTTPServer::Mode> &dirs) :
+ m_dirs(dirs), m_port(0)
+{
+ start();
+}
+
+ThreadedTestHTTPServer::~ThreadedTestHTTPServer()
+{
+ quit();
+ wait();
+}
+
+QUrl ThreadedTestHTTPServer::baseUrl() const
+{
+ return localHostUrl(m_port);
+}
+
+QUrl ThreadedTestHTTPServer::url(const QString &documentPath) const
+{
+ return baseUrl().resolved(documentPath);
+}
+
+QString ThreadedTestHTTPServer::urlString(const QString &documentPath) const
+{
+ return url(documentPath).toString();
+}
+
+void ThreadedTestHTTPServer::run()
+{
+ TestHTTPServer server;
+ {
+ QMutexLocker locker(&m_mutex);
+ QVERIFY2(server.listen(), qPrintable(server.errorString()));
+ m_port = server.port();
+ for (QHash<QString, TestHTTPServer::Mode>::ConstIterator i = m_dirs.constBegin();
+ i != m_dirs.constEnd(); ++i) {
+ server.serveDirectory(i.key(), i.value());
+ }
+ m_condition.wakeAll();
+ }
+ exec();
+}
+
+void ThreadedTestHTTPServer::start()
+{
+ QMutexLocker locker(&m_mutex);
+ QThread::start();
+ m_condition.wait(&m_mutex);
+}
diff --git a/tests/auto/shared/testhttpserver.h b/tests/auto/shared/testhttpserver.h
index 0fc8e4a79c..bf826b247b 100644
--- a/tests/auto/shared/testhttpserver.h
+++ b/tests/auto/shared/testhttpserver.h
@@ -37,6 +37,9 @@
#include <QTcpServer>
#include <QUrl>
#include <QPair>
+#include <QThread>
+#include <QMutex>
+#include <QWaitCondition>
class TestHTTPServer : public QObject
{
@@ -45,6 +48,7 @@ public:
TestHTTPServer();
bool listen();
+ quint16 port() const;
QUrl baseUrl() const;
QUrl url(const QString &documentPath) const;
QString urlString(const QString &documentPath) const;
@@ -100,5 +104,29 @@ private:
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();
+
+private:
+ void start();
+
+ QHash<QString, TestHTTPServer::Mode> m_dirs;
+ quint16 m_port;
+ QMutex m_mutex;
+ QWaitCondition m_condition;
+};
+
#endif // TESTHTTPSERVER_H