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(SERVER_PORT), qPrintable(m_server->errorString()));
++ 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", "http://127.0.0.1:14445/testdocument.html");
++ 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
QTest::newRow("Correct content-type - no charset") << "send_data.1.expect" << "send_data.7.qml";
}
- QVERIFY2(server.listen(SERVER_PORT), qPrintable(server.errorString()));
+void tst_qqmlxmlhttprequest::send_options()
+{
+ QFETCH(QString, url_suffix);
+ QFETCH(QString, file_expected);
+ QFETCH(QString, file_qml);
+ QFETCH(QString, file_reply);
+
+ TestHTTPServer server;
- QString url = "http://127.0.0.1:14445";
++ 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()
{
QTRY_VERIFY(object->property("exceptionThrown").toBool() == true);
}
- QVERIFY2(server.listen(SERVER_PORT), qPrintable(server.errorString()));
+void tst_qqmlxmlhttprequest::getBinaryData()
+{
+ TestHTTPServer server;
- object->setProperty("url", "http://127.0.0.1:14445/gml_logo.png");
++ 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);