diff options
author | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-11-13 07:07:15 (GMT) |
---|---|---|
committer | Aaron Kennedy <aaron.kennedy@nokia.com> | 2009-11-13 07:07:15 (GMT) |
commit | 2ecf32919fd3747f537df14c938d2520d240a993 (patch) | |
tree | ce159e07e19f99dd93a2d6f5518509ebefc522ca /tests/auto/declarative/xmlhttprequest | |
parent | b143576a735b6ec67058d5c6b6cf369b1002756d (diff) | |
download | Qt-2ecf32919fd3747f537df14c938d2520d240a993.zip Qt-2ecf32919fd3747f537df14c938d2520d240a993.tar.gz Qt-2ecf32919fd3747f537df14c938d2520d240a993.tar.bz2 |
XMLHttpRequest tests
Diffstat (limited to 'tests/auto/declarative/xmlhttprequest')
15 files changed, 558 insertions, 1 deletions
diff --git a/tests/auto/declarative/xmlhttprequest/data/document.qml b/tests/auto/declarative/xmlhttprequest/data/document.qml index 7601a10..ce9e35f 100644 --- a/tests/auto/declarative/xmlhttprequest/data/document.qml +++ b/tests/auto/declarative/xmlhttprequest/data/document.qml @@ -25,6 +25,9 @@ Object { if (document.nodeValue != null) return; + if (document.parentNode != null) + return; + // ### Test other node properties // ### test encoding (what is a valid qt encoding?) xmlTest = true; diff --git a/tests/auto/declarative/xmlhttprequest/data/element.qml b/tests/auto/declarative/xmlhttprequest/data/element.qml index 79620bf..228db18 100644 --- a/tests/auto/declarative/xmlhttprequest/data/element.qml +++ b/tests/auto/declarative/xmlhttprequest/data/element.qml @@ -32,6 +32,15 @@ Object { if (e.childNodes[childTagNames.length + 1] != null) return; + // Check writing fails + e.childNodes[0] = null; + if (e.childNodes[0] == null) + return; + + e.childNodes[10] = 10; + if (e.childNodes[10] != null) + return; + if (e.firstChild.tagName != e.childNodes[0].tagName) return; @@ -70,6 +79,19 @@ Object { if (attrIdx2 != null) return; + // Check writing fails + e.attributes[0] = null; + if (e.attributes[0] == null) + return; + + e.attributes["attr"] = null; + if (e.attributes["attr"] == null) + return; + + e.attributes["attr3"] = 10; + if (e.attributes["attr3"] != null) + return; + // Check person and fruit sub elements if (person.parentNode.nodeName != "root") return; diff --git a/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_args.qml b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_args.qml new file mode 100644 index 0000000..c2cf898 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_args.qml @@ -0,0 +1,23 @@ +import Qt 4.6 + +Object { + property bool exceptionThrown: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + x.send(); + + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + try { + x.getAllResponseHeaders("Test-header"); + } catch (e) { + if (e.code == DOMException.SYNTAX_ERR) + exceptionThrown = true; + } + } + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_sent.qml b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_sent.qml new file mode 100644 index 0000000..9583f9d --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_sent.qml @@ -0,0 +1,20 @@ +import Qt 4.6 + +Object { + property bool test: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + x.send(); + + try { + x.getAllResponseHeaders(); + } catch (e) { + if (e.code == DOMException.INVALID_STATE_ERR) + test = true; + } + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_unsent.qml b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_unsent.qml new file mode 100644 index 0000000..fac5259 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getAllResponseHeaders_unsent.qml @@ -0,0 +1,16 @@ +import Qt 4.6 + +Object { + property bool test: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + try { + x.getAllResponseHeaders(); + } catch (e) { + if (e.code == DOMException.INVALID_STATE_ERR) + test = true; + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_args.qml b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_args.qml new file mode 100644 index 0000000..ca7aed8 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_args.qml @@ -0,0 +1,23 @@ +import Qt 4.6 + +Object { + property bool exceptionThrown: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + x.send(); + + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + try { + x.getResponseHeader(); + } catch (e) { + if (e.code == DOMException.SYNTAX_ERR) + exceptionThrown = true; + } + } + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_sent.qml b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_sent.qml new file mode 100644 index 0000000..148a19c --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_sent.qml @@ -0,0 +1,20 @@ +import Qt 4.6 + +Object { + property bool test: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + x.send(); + + try { + x.getResponseHeader("Test-header"); + } catch (e) { + if (e.code == DOMException.INVALID_STATE_ERR) + test = true; + } + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_unsent.qml b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_unsent.qml new file mode 100644 index 0000000..5abdf0a --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/getResponseHeader_unsent.qml @@ -0,0 +1,16 @@ +import Qt 4.6 + +Object { + property bool test: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + try { + x.getResponseHeader("Test-header"); + } catch (e) { + if (e.code == DOMException.INVALID_STATE_ERR) + test = true; + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/invalidMethodUsage.qml b/tests/auto/declarative/xmlhttprequest/data/invalidMethodUsage.qml new file mode 100644 index 0000000..893eb8b --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/invalidMethodUsage.qml @@ -0,0 +1,160 @@ +import Qt 4.6 + +Object { + property bool onreadystatechange: false + property bool readyState: false + property bool status: false + property bool statusText: false + property bool responseText: false + property bool responseXML: false + + property bool open: false + property bool setRequestHeader: false + property bool send: false + property bool abort: false + property bool getResponseHeader: false + property bool getAllResponseHeaders: false + + Component.onCompleted: { + var o = 10; + + try { + XMLHttpRequest.prototype.onreadystatechange + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + onreadystatechange = true; + } + try { + XMLHttpRequest.prototype.readyState + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + readyState = true; + } + try { + XMLHttpRequest.prototype.status + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + status = true; + } + try { + XMLHttpRequest.prototype.statusText + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + statusText = true; + } + try { + XMLHttpRequest.prototype.responseText + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + responseText = true; + } + try { + XMLHttpRequest.prototype.responseXML + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + responseXML = true; + } + + try { + XMLHttpRequest.prototype.open.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + open = true; + } + + try { + XMLHttpRequest.prototype.setRequestHeader.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + setRequestHeader = true; + } + + try { + XMLHttpRequest.prototype.send.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + send = true; + } + + try { + XMLHttpRequest.prototype.abort.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + abort = true; + } + + try { + XMLHttpRequest.prototype.getResponseHeader.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + getResponseHeader = true; + } + + try { + XMLHttpRequest.prototype.getAllResponseHeaders.call(o); + } catch (e) { + if (!(e instanceof ReferenceError)) + return; + + if (e.message != "Not an XMLHttpRequest object") + return; + + getAllResponseHeaders = true; + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/open_user.qml b/tests/auto/declarative/xmlhttprequest/data/open_user.qml new file mode 100644 index 0000000..cc90433 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/open_user.qml @@ -0,0 +1,53 @@ +import Qt 4.6 + +Object { + property string url + + property bool readyState: false + property bool openedState: false + + property bool status: false + property bool statusText: false + property bool responseText: false + property bool responseXML: false + + property bool dataOK: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + if (x.readyState == XMLHttpRequest.UNSENT) + readyState = true; + + x.open("GET", url, true, "username", "password"); + + if (x.readyState == XMLHttpRequest.OPENED) + openedState = true; + + try { + var a = x.status; + } catch (error) { + if (error.code == DOMException.INVALID_STATE_ERR) + status = true; + } + try { + var a = x.statusText; + } catch (error) { + if (error.code == DOMException.INVALID_STATE_ERR) + statusText = true; + } + responseText = (x.responseText == ""); + responseXML = (x.responseXML == null); + + // Test to the end + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + dataOK = (x.responseText == "QML Rocks!\n"); + } + } + + + x.send() + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/data/send_data.4.qml b/tests/auto/declarative/xmlhttprequest/data/send_data.4.qml index 375f2fa..4705007 100644 --- a/tests/auto/declarative/xmlhttprequest/data/send_data.4.qml +++ b/tests/auto/declarative/xmlhttprequest/data/send_data.4.qml @@ -21,4 +21,3 @@ Object { } } - diff --git a/tests/auto/declarative/xmlhttprequest/data/send_data.7.qml b/tests/auto/declarative/xmlhttprequest/data/send_data.7.qml new file mode 100644 index 0000000..3a2ba56 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/send_data.7.qml @@ -0,0 +1,23 @@ +import Qt 4.6 + +Object { + property string url + + property bool dataOK: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + x.open("POST", url); + x.setRequestHeader("Content-Type", "text/plain"); + + // Test to the end + x.onreadystatechange = function() { + if (x.readyState == XMLHttpRequest.DONE) { + dataOK = (x.responseText == "QML Rocks!\n"); + } + } + + x.send("My Sent Data"); + } +} + diff --git a/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_args.qml b/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_args.qml new file mode 100644 index 0000000..6824af2 --- /dev/null +++ b/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_args.qml @@ -0,0 +1,18 @@ +import Qt 4.6 + +Object { + property bool exceptionThrown: false + + Component.onCompleted: { + var x = new XMLHttpRequest; + + x.open("GET", "testdocument.html"); + + try { + x.setRequestHeader("Test-header"); + } catch (e) { + if (e.code == DOMException.SYNTAX_ERR) + exceptionThrown = true; + } + } +} diff --git a/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_illegalName.qml b/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_illegalName.qml index cf5ebcc..8029bc4 100644 --- a/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_illegalName.qml +++ b/tests/auto/declarative/xmlhttprequest/data/setRequestHeader_illegalName.qml @@ -22,6 +22,8 @@ Object { x.open("GET", url); + x.setRequestHeader(header, "Value"); + if (x.readyState == XMLHttpRequest.OPENED) openedState = true; diff --git a/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp b/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp index 100a11b..e74d1c5 100644 --- a/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp +++ b/tests/auto/declarative/xmlhttprequest/tst_xmlhttprequest.cpp @@ -71,6 +71,7 @@ private slots: void setRequestHeader_illegalName_data(); void setRequestHeader_illegalName(); void setRequestHeader_sent(); + void setRequestHeader_args(); void send_unsent(); void send_alreadySent(); void send_ignoreData(); @@ -79,7 +80,13 @@ private slots: void abort_unsent(); void abort_opened(); void getResponseHeader(); + void getResponseHeader_unsent(); + void getResponseHeader_sent(); + void getResponseHeader_args(); void getAllResponseHeaders(); + void getAllResponseHeaders_unsent(); + void getAllResponseHeaders_sent(); + void getAllResponseHeaders_args(); void status(); void statusText(); void responseText(); @@ -283,6 +290,34 @@ void tst_xmlhttprequest::open() delete object; } + + // User/pass + { + TestHTTPServer server(SERVER_PORT); + QVERIFY(server.isValid()); + QVERIFY(server.wait(TEST_FILE("open_network.expect"), + TEST_FILE("open_network.reply"), + TEST_FILE("testdocument.html"))); + + QmlComponent component(&engine, TEST_FILE("open_user.qml")); + QObject *object = component.beginCreate(engine.rootContext()); + QVERIFY(object != 0); + object->setProperty("url", "http://127.0.0.1:14445/testdocument.html"); + component.completeCreate(); + + QCOMPARE(object->property("readyState").toBool(), true); + QCOMPARE(object->property("openedState").toBool(), true); + QCOMPARE(object->property("status").toBool(), true); + QCOMPARE(object->property("statusText").toBool(), true); + QCOMPARE(object->property("responseText").toBool(), true); + QCOMPARE(object->property("responseXML").toBool(), true); + + TRY_WAIT(object->property("dataOK").toBool() == true); + + // ### Check that the username/password were sent to the server + + delete object; + } } // Test that calling XMLHttpRequest.open() with an invalid method raises an exception @@ -445,6 +480,18 @@ void tst_xmlhttprequest::setRequestHeader_sent() delete object; } +// Invalid arg count throws exception +void tst_xmlhttprequest::setRequestHeader_args() +{ + QmlComponent component(&engine, TEST_FILE("setRequestHeader_args.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("exceptionThrown").toBool(), true); + + delete object; +} + // Test that calling send() in UNSENT state throws an exception void tst_xmlhttprequest::send_unsent() { @@ -628,6 +675,25 @@ void tst_xmlhttprequest::send_withdata() delete object; } + + // Correct content-type - no charset + { + TestHTTPServer server(SERVER_PORT); + QVERIFY(server.isValid()); + QVERIFY(server.wait(TEST_FILE("send_data.1.expect"), + TEST_FILE("send_data.reply"), + TEST_FILE("testdocument.html"))); + + QmlComponent component(&engine, TEST_FILE("send_data.7.qml")); + QObject *object = component.beginCreate(engine.rootContext()); + QVERIFY(object != 0); + object->setProperty("url", "http://127.0.0.1:14445/testdocument.html"); + component.completeCreate(); + + TRY_WAIT(object->property("dataOK").toBool() == true); + + delete object; + } } // Test abort() has no effect in unsent state @@ -736,6 +802,42 @@ void tst_xmlhttprequest::getResponseHeader() delete object; } +// Test getResponseHeader throws an exception in an invalid state +void tst_xmlhttprequest::getResponseHeader_unsent() +{ + QmlComponent component(&engine, TEST_FILE("getResponseHeader_unsent.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test").toBool(), true); + + delete object; +} + +// Test getResponseHeader throws an exception in an invalid state +void tst_xmlhttprequest::getResponseHeader_sent() +{ + QmlComponent component(&engine, TEST_FILE("getResponseHeader_sent.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test").toBool(), true); + + delete object; +} + +// Invalid arg count throws exception +void tst_xmlhttprequest::getResponseHeader_args() +{ + QmlComponent component(&engine, TEST_FILE("getResponseHeader_args.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + TRY_WAIT(object->property("exceptionThrown").toBool() == true); + + delete object; +} + void tst_xmlhttprequest::getAllResponseHeaders() { QmlEngine engine; // Avoid cookie contamination @@ -768,6 +870,42 @@ void tst_xmlhttprequest::getAllResponseHeaders() delete object; } +// Test getAllResponseHeaders throws an exception in an invalid state +void tst_xmlhttprequest::getAllResponseHeaders_unsent() +{ + QmlComponent component(&engine, TEST_FILE("getAllResponseHeaders_unsent.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test").toBool(), true); + + delete object; +} + +// Test getAllResponseHeaders throws an exception in an invalid state +void tst_xmlhttprequest::getAllResponseHeaders_sent() +{ + QmlComponent component(&engine, TEST_FILE("getAllResponseHeaders_sent.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("test").toBool(), true); + + delete object; +} + +// Invalid arg count throws exception +void tst_xmlhttprequest::getAllResponseHeaders_args() +{ + QmlComponent component(&engine, TEST_FILE("getAllResponseHeaders_args.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + TRY_WAIT(object->property("exceptionThrown").toBool() == true); + + delete object; +} + void tst_xmlhttprequest::status() { { @@ -966,8 +1104,29 @@ void tst_xmlhttprequest::responseText() } } +// Test that calling hte XMLHttpRequest methods on a non-XMLHttpRequest object +// throws an exception void tst_xmlhttprequest::invalidMethodUsage() { + QmlComponent component(&engine, TEST_FILE("invalidMethodUsage.qml")); + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(object->property("onreadystatechange").toBool(), true); + QCOMPARE(object->property("readyState").toBool(), true); + QCOMPARE(object->property("status").toBool(), true); + QCOMPARE(object->property("statusText").toBool(), true); + QCOMPARE(object->property("responseText").toBool(), true); + QCOMPARE(object->property("responseXML").toBool(), true); + + QCOMPARE(object->property("open").toBool(), true); + QCOMPARE(object->property("setRequestHeader").toBool(), true); + QCOMPARE(object->property("send").toBool(), true); + QCOMPARE(object->property("abort").toBool(), true); + QCOMPARE(object->property("getResponseHeader").toBool(), true); + QCOMPARE(object->property("getAllResponseHeaders").toBool(), true); + + delete object; } void tst_xmlhttprequest::responseXML_invalid() |