diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-11-11 00:04:31 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-11-11 00:04:31 (GMT) |
commit | 0184a2144b8299a9d9de6e616a74cea39e2f2e28 (patch) | |
tree | 74e7284f0a0c42b121610307472bc069ad0be99f | |
parent | 56bad222fb216ff5717eee0ad0afea14885b84da (diff) | |
download | Qt-0184a2144b8299a9d9de6e616a74cea39e2f2e28.zip Qt-0184a2144b8299a9d9de6e616a74cea39e2f2e28.tar.gz Qt-0184a2144b8299a9d9de6e616a74cea39e2f2e28.tar.bz2 |
Generalize TestHTTPServer
-rw-r--r-- | tests/auto/declarative/shared/testhttpserver.cpp (renamed from tests/auto/declarative/xmlhttprequest/testhttpserver.cpp) | 146 | ||||
-rw-r--r-- | tests/auto/declarative/shared/testhttpserver.h (renamed from tests/auto/declarative/xmlhttprequest/testhttpserver.h) | 12 | ||||
-rw-r--r-- | tests/auto/declarative/xmlhttprequest/xmlhttprequest.pro | 5 |
3 files changed, 160 insertions, 3 deletions
diff --git a/tests/auto/declarative/xmlhttprequest/testhttpserver.cpp b/tests/auto/declarative/shared/testhttpserver.cpp index 3b00685..c7b7f25 100644 --- a/tests/auto/declarative/xmlhttprequest/testhttpserver.cpp +++ b/tests/auto/declarative/shared/testhttpserver.cpp @@ -43,7 +43,50 @@ #include <QTcpSocket> #include <QDebug> #include <QFile> +#include <QTimer> +/*! +\internal +\class TestHTTPServer +\brief provides a very, very basic HTTP server for testing. + +Inside the test case, an instance of TestHTTPServer should be created, with the +appropriate port to listen on. The server will listen on the localhost interface. + +Directories to serve can then be added to server, which will be added as "roots". +Each root can be added as a Normal, Delay or Disconnect root. Requests for files +within a Normal root are returned immediately. Request for files within a Delay +root are delayed for 500ms, and then served. Requests for files within a Disconnect +directory cause the server to disconnect immediately. A request for a file that isn't +found in any root will return a 404 error. + +If you have the following directory structure: + +\code +disconnect/disconnectTest.qml +files/main.qml +files/Button.qml +files/content/WebView.qml +slowFiles/slowMain.qml +\endcode +it can be added like this: +\code +TestHTTPServer server(14445); +server.serveDirectory("disconnect", TestHTTPServer::Disconnect); +server.serveDirectory("files"); +server.serveDirectory("slowFiles", TestHTTPServer::Delay); +\endcode + +The following request urls will then result in the appropriate action: +\table +\header \o URL \o Action +\row \o http://localhost:14445/disconnectTest.qml \o Disconnection +\row \o http://localhost:14445/main.qml \o main.qml returned immediately +\row \o http://localhost:14445/Button.qml \o Button.qml returned immediately +\row \o http://localhost:14445/content/WebView.qml \o content/WebView.qml returned immediately +\row \o http://localhost:14445/slowMain.qml \o slowMain.qml returned after 500ms +\endtable +*/ TestHTTPServer::TestHTTPServer(quint16 port) : m_hasFailed(false) { @@ -57,6 +100,12 @@ bool TestHTTPServer::isValid() const return server.isListening(); } +bool TestHTTPServer::serveDirectory(const QString &dir, Mode mode) +{ + dirs.append(qMakePair(dir, mode)); + return true; +} + bool TestHTTPServer::wait(const QUrl &expect, const QUrl &reply, const QUrl &body) { m_hasFailed = false; @@ -108,13 +157,26 @@ void TestHTTPServer::newConnection() QTcpSocket *socket = server.nextPendingConnection(); if (!socket) return; + if (!dirs.isEmpty()) + dataCache.insert(socket, QByteArray()); + QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected())); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); } void TestHTTPServer::disconnected() { - sender()->deleteLater(); + QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender()); + if (!socket) return; + + dataCache.remove(socket); + for (int ii = 0; ii < toSend.count(); ++ii) { + if (toSend.at(ii).first == socket) { + toSend.removeAt(ii); + --ii; + } + } + socket->deleteLater(); } void TestHTTPServer::readyRead() @@ -124,6 +186,11 @@ void TestHTTPServer::readyRead() QByteArray ba = socket->readAll(); + if (!dirs.isEmpty()) { + serveGET(socket, ba); + return; + } + if (m_hasFailed || waitData.isEmpty()) { qWarning() << "TestHTTPServer: Unexpected data" << ba; return; @@ -152,3 +219,80 @@ void TestHTTPServer::readyRead() } } +bool TestHTTPServer::reply(QTcpSocket *socket, const QByteArray &fileName) +{ + for (int ii = 0; ii < dirs.count(); ++ii) { + QString dir = dirs.at(ii).first; + Mode mode = dirs.at(ii).second; + + QString dirFile = dir + QLatin1String("/") + QLatin1String(fileName); + + QFile file(dirFile); + if (file.open(QIODevice::ReadOnly)) { + + if (mode == Disconnect) + return true; + + QByteArray data = file.readAll(); + + QByteArray response = "HTTP/1.0 200 OK\r\nContent-type: text/html; charset=UTF-8\r\nContent-length: "; + response += QByteArray::number(data.count()); + response += "\r\n\r\n"; + response += data; + + if (mode == Delay) { + toSend.append(qMakePair(socket, response)); + QTimer::singleShot(500, this, SLOT(sendOne())); + return false; + } else { + socket->write(response); + return true; + } + } + } + + + QByteArray response = "HTTP/1.0 404 Not found\r\nContent-type: text/html; charset=UTF-8\r\n\r\n"; + socket->write(response); + + return true; +} + +void TestHTTPServer::sendOne() +{ + if (!toSend.isEmpty()) { + toSend.first().first->write(toSend.first().second); + toSend.first().first->close(); + toSend.removeFirst(); + } +} + +void TestHTTPServer::serveGET(QTcpSocket *socket, const QByteArray &data) +{ + if (!dataCache.contains(socket)) + return; + + QByteArray total = dataCache[socket] + data; + dataCache[socket] = total; + + if (total.contains("\n\r\n")) { + + bool close = true; + + if (total.startsWith("GET /")) { + + int space = total.indexOf(' ', 4); + if (space != -1) { + + QByteArray req = total.mid(5, space - 5); + close = reply(socket, req); + + } + } + dataCache.remove(socket); + + if (close) + socket->close(); + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/testhttpserver.h b/tests/auto/declarative/shared/testhttpserver.h index 709532e..62fe7b4 100644 --- a/tests/auto/declarative/xmlhttprequest/testhttpserver.h +++ b/tests/auto/declarative/shared/testhttpserver.h @@ -45,6 +45,7 @@ #include <QObject> #include <QTcpServer> #include <QUrl> +#include <QPair> class TestHTTPServer : public QObject { @@ -54,6 +55,9 @@ public: bool isValid() 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; @@ -61,8 +65,16 @@ private slots: void newConnection(); void disconnected(); void readyRead(); + void sendOne(); private: + void serveGET(QTcpSocket *, const QByteArray &); + bool reply(QTcpSocket *, const QByteArray &); + + QList<QPair<QString, Mode> > dirs; + QHash<QTcpSocket *, QByteArray> dataCache; + QList<QPair<QTcpSocket *, QByteArray> > toSend; + QByteArray waitData; QByteArray replyData; QByteArray bodyData; diff --git a/tests/auto/declarative/xmlhttprequest/xmlhttprequest.pro b/tests/auto/declarative/xmlhttprequest/xmlhttprequest.pro index 5d35a89..6af4e39 100644 --- a/tests/auto/declarative/xmlhttprequest/xmlhttprequest.pro +++ b/tests/auto/declarative/xmlhttprequest/xmlhttprequest.pro @@ -2,10 +2,11 @@ load(qttest_p4) contains(QT_CONFIG,declarative): QT += declarative webkit network macx:CONFIG -= app_bundle -HEADERS += testhttpserver.h +INCLUDEPATH += ../shared/ +HEADERS += ../shared/testhttpserver.h SOURCES += tst_xmlhttprequest.cpp \ - testhttpserver.cpp + ../shared/testhttpserver.cpp # Define SRCDIR equal to test's source directory |