diff options
author | Tasuku Suzuki <stasuku@gmail.com> | 2014-11-30 01:40:42 +0900 |
---|---|---|
committer | Tasuku Suzuki <stasuku@gmail.com> | 2014-12-26 04:39:46 +0100 |
commit | f8beab1d7390095114e1d5f02097751dd1b07345 (patch) | |
tree | 87e668f21ac1f010f7a635c826b928df183c1814 /tests | |
parent | d641008d04475bfb1c1996e408b1408618845b6f (diff) |
support XMLHttpRequest sync mode
[ChangeLog][QtQml][XMLHttpRequest] Supported synchronous requests
Change-Id: Ia38fcf97f212a14657bb519240d8406368a72390
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml | 14 | ||||
-rw-r--r-- | tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp | 39 |
2 files changed, 42 insertions, 11 deletions
diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml b/tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml index 0f31c966fa..3c73141954 100644 --- a/tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml +++ b/tests/auto/qml/qqmlxmlhttprequest/data/open_sync.qml @@ -1,17 +1,15 @@ import QtQuick 2.0 QtObject { - property bool exceptionThrown: false + property url url + property string responseText Component.onCompleted: { var x = new XMLHttpRequest; - - try { - x.open("GET", "http://www.qt-project.org", false); - } catch (e) { - if (e.code == DOMException.NOT_SUPPORTED_ERR) - exceptionThrown = true; - } + x.open("GET", url, false); + x.setRequestHeader("Accept-Language", "en-US"); + x.send(); + responseText = x.responseText; } } diff --git a/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp b/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp index 441893a853..0d70e47b3d 100644 --- a/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp +++ b/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp @@ -37,6 +37,7 @@ #include <QDebug> #include <QScopedPointer> #include <QNetworkCookieJar> +#include <QThread> #include "testhttpserver.h" #include "../../shared/util.h" @@ -282,14 +283,46 @@ void tst_qqmlxmlhttprequest::open_invalid_method() QCOMPARE(object->property("exceptionThrown").toBool(), true); } -// Test that calling XMLHttpRequest.open() with sync raises an exception +class TestThreadedHTTPServer : public QObject +{ + Q_OBJECT +public: + TestThreadedHTTPServer(const QUrl &expectUrl, const QUrl &replyUrl, const QUrl &bodyUrl) + : m_server(Q_NULLPTR) { + moveToThread(&m_thread); + m_thread.start(); + QMetaObject::invokeMethod(this, "start", Qt::QueuedConnection, Q_ARG(QUrl, expectUrl), Q_ARG(QUrl, replyUrl), Q_ARG(QUrl, bodyUrl)); + } + ~TestThreadedHTTPServer() { + m_server->deleteLater(); + m_thread.exit(); + m_thread.wait(); + } + +private slots: + void start(const QUrl &expectUrl, const QUrl &replyUrl, const QUrl &bodyUrl) { + m_server = new TestHTTPServer; + QVERIFY2(m_server->listen(SERVER_PORT), qPrintable(m_server->errorString())); + QVERIFY(m_server->wait(expectUrl, replyUrl, bodyUrl)); + } + +private: + TestHTTPServer *m_server; + QThread m_thread; +}; + +// Test that calling XMLHttpRequest.open() with sync void tst_qqmlxmlhttprequest::open_sync() { + TestThreadedHTTPServer server(testFileUrl("open_network.expect"), testFileUrl("open_network.reply"), testFileUrl("testdocument.html")); + QQmlComponent component(&engine, testFileUrl("open_sync.qml")); - QScopedPointer<QObject> object(component.create()); + QScopedPointer<QObject> object(component.beginCreate(engine.rootContext())); QVERIFY(!object.isNull()); + object->setProperty("url", "http://127.0.0.1:14445/testdocument.html"); + component.completeCreate(); - QCOMPARE(object->property("exceptionThrown").toBool(), true); + QCOMPARE(object->property("responseText").toString(), QStringLiteral("QML Rocks!\n")); } // Calling with incorrect arg count raises an exception |