diff options
Diffstat (limited to 'tests/auto/qml/qqmlxmlhttprequest')
12 files changed, 231 insertions, 18 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/data/qml_logo.png b/tests/auto/qml/qqmlxmlhttprequest/data/qml_logo.png Binary files differnew file mode 100644 index 0000000000..681aef8aa2 --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/qml_logo.png diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/receiveBinaryData.qml b/tests/auto/qml/qqmlxmlhttprequest/data/receiveBinaryData.qml new file mode 100644 index 0000000000..234d759284 --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/receiveBinaryData.qml @@ -0,0 +1,27 @@ +import QtQuick 2.0 + +QtObject { + property string url + property int readSize: 0 + + Component.onCompleted: { + + var request = new XMLHttpRequest(); + request.open("GET", url); + request.responseType = "arraybuffer"; + + request.onreadystatechange = function() { + if (request.readyState == XMLHttpRequest.DONE) { + var arrayBuffer = request.response; + if (arrayBuffer) { + var byteArray = new Uint8Array(arrayBuffer); + readSize = byteArray.byteLength; + } + } + } + + request.send(null); + + } +} + diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/receive_binary_data.expect b/tests/auto/qml/qqmlxmlhttprequest/data/receive_binary_data.expect new file mode 100644 index 0000000000..112ac4e207 --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/receive_binary_data.expect @@ -0,0 +1,7 @@ +GET /gml_logo.png HTTP/1.1 +Accept-Language: en-US,* +Content-Type: image/png +Connection: Keep-Alive +Accept-Encoding: gzip, deflate +User-Agent: Mozilla/5.0 +Host: {{ServerHostUrl}} diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/receive_binary_data.reply b/tests/auto/qml/qqmlxmlhttprequest/data/receive_binary_data.reply new file mode 100644 index 0000000000..44ba138213 --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/receive_binary_data.reply @@ -0,0 +1,3 @@ +HTTP/1.1 200 OK +Connection: close +Content-Type: image/png diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/send_data.10.expect b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.10.expect new file mode 100644 index 0000000000..8d83a00549 --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.10.expect @@ -0,0 +1,10 @@ +OPTIONS /testdocument.html HTTP/1.1 +Accept-Language: en-US,* +Content-Type: text/plain;charset=UTF-8 +Content-Length: 12 +Connection: Keep-Alive +Accept-Encoding: gzip, deflate +User-Agent: Mozilla/5.0 +Host: {{ServerHostUrl}} + +My Sent Data
\ No newline at end of file diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/send_data.2.reply b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.2.reply new file mode 100644 index 0000000000..b4f5ecd429 --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.2.reply @@ -0,0 +1,3 @@ +HTTP/1.1 200 OK +Allow: GET,HEAD,POST,OPTIONS,TRACE +Content-Length: 0 diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/send_data.8.expect b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.8.expect new file mode 100644 index 0000000000..e93c019dd5 --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.8.expect @@ -0,0 +1,7 @@ +OPTIONS / HTTP/1.1 +Content-Length: 0 +Connection: Keep-Alive +Accept-Encoding: gzip, deflate +Accept-Language: en-US,* +User-Agent: Mozilla/5.0 +Host: {{ServerHostUrl}} diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/send_data.8.qml b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.8.qml new file mode 100644 index 0000000000..bda5871a18 --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.8.qml @@ -0,0 +1,24 @@ +import QtQuick 2.0 + +QtObject { + property string url + + property bool dataOK: false + property bool headerOK: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + x.open("OPTIONS", url); + + // Test to the end + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.HEADERS_RECEIVED) { + headerOK = (x.getResponseHeader("Allow") == "GET,HEAD,POST,OPTIONS,TRACE"); + } else if (x.readyState == XMLHttpRequest.DONE) { + dataOK = (x.responseText == ""); + } + } + + x.send(); + } +} diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/send_data.9.expect b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.9.expect new file mode 100644 index 0000000000..da38968e9a --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.9.expect @@ -0,0 +1,7 @@ +OPTIONS /testdocument.html HTTP/1.1 +Content-Length: 0 +Connection: Keep-Alive +Accept-Encoding: gzip, deflate +Accept-Language: en-US,* +User-Agent: Mozilla/5.0 +Host: {{ServerHostUrl}} diff --git a/tests/auto/qml/qqmlxmlhttprequest/data/send_data.9.qml b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.9.qml new file mode 100644 index 0000000000..834916f96c --- /dev/null +++ b/tests/auto/qml/qqmlxmlhttprequest/data/send_data.9.qml @@ -0,0 +1,24 @@ +import QtQuick 2.0 + +QtObject { + property string url + + property bool dataOK: false + property bool headerOK: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + x.open("OPTIONS", url); + + // Test to the end + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.HEADERS_RECEIVED) { + headerOK = (x.getResponseHeader("Allow") == "GET,HEAD,POST,OPTIONS,TRACE"); + } else if (x.readyState == XMLHttpRequest.DONE) { + dataOK = (x.responseText == ""); + } + } + + x.send("My Sent Data"); + } +} diff --git a/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp b/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp index 90cf8f5ec1..c159dc8420 100644 --- a/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp +++ b/tests/auto/qml/qqmlxmlhttprequest/tst_qqmlxmlhttprequest.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** @@ -10,9 +10,9 @@ ** 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 Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser @@ -23,8 +23,8 @@ ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** -** In addition, as a special exception, Digia gives you certain additional -** rights. These rights are described in the Digia Qt LGPL Exception +** As a special exception, The Qt Company gives you certain additional +** rights. These rights are described in The Qt Company LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** $QT_END_LICENSE$ @@ -74,6 +74,8 @@ private slots: void send_ignoreData(); void send_withdata(); void send_withdata_data(); + void send_options(); + void send_options_data(); void abort(); void abort_unsent(); void abort_opened(); @@ -85,6 +87,7 @@ private slots: void getAllResponseHeaders_unsent(); void getAllResponseHeaders_sent(); void getAllResponseHeaders_args(); + void getBinaryData(); void status(); void status_data(); void statusText(); @@ -282,14 +285,55 @@ 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) { + QMutexLocker locker(&m_lock); + moveToThread(&m_thread); + m_thread.start(); + QMetaObject::invokeMethod(this, "start", Qt::QueuedConnection, Q_ARG(QUrl, expectUrl), Q_ARG(QUrl, replyUrl), Q_ARG(QUrl, bodyUrl)); + m_startupCondition.wait(&m_lock); + } + ~TestThreadedHTTPServer() { + m_server->deleteLater(); + m_thread.exit(); + m_thread.wait(); + } + + QUrl serverBaseUrl; + +private slots: + void start(const QUrl &expectUrl, const QUrl &replyUrl, const QUrl &bodyUrl) { + QMutexLocker locker(&m_lock); + m_server = new TestHTTPServer; + QVERIFY2(m_server->listen(), qPrintable(m_server->errorString())); + serverBaseUrl = m_server->baseUrl(); + QVERIFY(m_server->wait(expectUrl, replyUrl, bodyUrl)); + m_startupCondition.wakeAll(); + } + +private: + TestHTTPServer *m_server; + QThread m_thread; + QMutex m_lock; + QWaitCondition m_startupCondition; +}; + +// 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", server.serverBaseUrl.resolved(QStringLiteral("/testdocument.html")).toString()); + 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 @@ -554,6 +598,47 @@ void tst_qqmlxmlhttprequest::send_withdata_data() QTest::newRow("Correct content-type - no charset") << "send_data.1.expect" << "send_data.7.qml"; } +void tst_qqmlxmlhttprequest::send_options() +{ + QFETCH(QString, url_suffix); + QFETCH(QString, file_expected); + QFETCH(QString, file_qml); + QFETCH(QString, file_reply); + + TestHTTPServer server; + QVERIFY2(server.listen(), qPrintable(server.errorString())); + QVERIFY(server.wait(testFileUrl(file_expected), + testFileUrl(file_reply), + testFileUrl("testdocument.html"))); + + QQmlComponent component(&engine, testFileUrl(file_qml)); + QScopedPointer<QObject> object(component.beginCreate(engine.rootContext())); + QVERIFY(!object.isNull()); + QString url = server.baseUrl().toString(); + if (url_suffix != "/") + url.append("/"); + if (!url_suffix.isEmpty()) + url.append(url_suffix); + object->setProperty("url", url); + component.completeCreate(); + + QTRY_VERIFY(object->property("dataOK").toBool()); + QTRY_VERIFY(object->property("headerOK").toBool()); +} + +void tst_qqmlxmlhttprequest::send_options_data() +{ + QTest::addColumn<QString>("url_suffix"); + QTest::addColumn<QString>("file_expected"); + QTest::addColumn<QString>("file_qml"); + QTest::addColumn<QString>("file_reply"); + + QTest::newRow("OPTIONS (no data, no resource, no path)") << "" << "send_data.8.expect" << "send_data.8.qml" << "send_data.2.reply"; + QTest::newRow("OPTIONS (no data, no resource, path- \"/\")") << "/" << "send_data.8.expect" << "send_data.8.qml" << "send_data.2.reply"; + QTest::newRow("OPTIONS (no data, with resource)") << "testdocument.html" << "send_data.9.expect" << "send_data.8.qml" << "send_data.2.reply"; + QTest::newRow("OPTIONS (with data)") << "testdocument.html" << "send_data.10.expect" << "send_data.9.qml" << "send_data.2.reply"; +} + // Test abort() has no effect in unsent state void tst_qqmlxmlhttprequest::abort_unsent() { @@ -745,6 +830,24 @@ void tst_qqmlxmlhttprequest::getAllResponseHeaders_args() QTRY_VERIFY(object->property("exceptionThrown").toBool() == true); } +void tst_qqmlxmlhttprequest::getBinaryData() +{ + TestHTTPServer server; + QVERIFY2(server.listen(), qPrintable(server.errorString())); + QVERIFY(server.wait(testFileUrl("receive_binary_data.expect"), + testFileUrl("receive_binary_data.reply"), + testFileUrl("qml_logo.png"))); + + QQmlComponent component(&engine, testFileUrl("receiveBinaryData.qml")); + QScopedPointer<QObject> object(component.beginCreate(engine.rootContext())); + QVERIFY(!object.isNull()); + object->setProperty("url", server.urlString("/gml_logo.png")); + component.completeCreate(); + + QFileInfo fileInfo("data/qml_logo.png"); + QTRY_VERIFY(object->property("readSize").toInt() == fileInfo.size()); +} + void tst_qqmlxmlhttprequest::status() { QFETCH(QUrl, replyUrl); |