aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2021-04-14 15:14:33 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-04-14 20:26:16 +0000
commita0ebfd37f68ccc26a6fcb60947fa861c4df540fc (patch)
treed0b5b0fa163678960a769cd2e3840c9f657da714 /tests/auto
parent5e023f3a9a8647f905c7dc4f1c73420dfa03b554 (diff)
Check thisObject when calling various methods on URL
We should not crash when you try to call them on the wrong object. Rather, throw a type error. Change-Id: I1b146d9c77d838e013408988e02a65a623641f1f Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit f32436122f6ac16bfd6f23228b85a6f7c12502b6) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/qml/qjsengine/tst_qjsengine.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
index 85d61a3537..e892d2dfcf 100644
--- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp
+++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp
@@ -273,6 +273,7 @@ private slots:
void dataViewCtor();
void uiLanguage();
+ void urlObject();
public:
Q_INVOKABLE QJSValue throwingCppMethod1();
@@ -5270,6 +5271,69 @@ void tst_QJSEngine::uiLanguage()
}
}
+void tst_QJSEngine::urlObject()
+{
+ QJSEngine engine;
+
+ const QString href = QStringLiteral(
+ "http://uuu:ppp@example.com:777/foo/bar?search=stuff&other=where#hhh");
+ const QUrl url(href);
+
+ QJSManagedValue v(engine.evaluate(QStringLiteral("new URL('%1')").arg(href)), &engine);
+ QVERIFY(v.isObject());
+ QJSManagedValue proto(v.prototype());
+
+ auto check = [&](const QString &prop, const QString &expected) {
+ QCOMPARE(v.property(prop).toString(), expected);
+ QVERIFY(proto.property(prop).isUndefined());
+ QVERIFY(engine.hasError());
+ QCOMPARE(engine.catchError().toString(),
+ QStringLiteral("TypeError: Value of \"this\" must be of type URL"));
+ };
+
+ check(QStringLiteral("href"), url.toString());
+ check(QStringLiteral("origin"), QStringLiteral("http://example.com:777"));
+ check(QStringLiteral("protocol"), url.scheme());
+ check(QStringLiteral("username"), url.userName());
+ check(QStringLiteral("password"), url.password());
+ check(QStringLiteral("host"), url.host() + u':' + QString::number(url.port()));
+ check(QStringLiteral("hostname"), url.host());
+ check(QStringLiteral("port"), QString::number(url.port()));
+ check(QStringLiteral("pathname"), url.path());
+ check(QStringLiteral("search"), QStringLiteral("?search=stuff&other=where"));
+ check(QStringLiteral("hash"), u'#' + url.fragment());
+
+ QJSManagedValue s(v.property("searchParams"), &engine);
+ QVERIFY(s.isObject());
+
+ const QStringList searchParamsMethods = {
+ QStringLiteral("append"),
+ QStringLiteral("delete"),
+ QStringLiteral("get"),
+ QStringLiteral("getAll"),
+ QStringLiteral("has"),
+ QStringLiteral("set"),
+ QStringLiteral("sort"),
+ QStringLiteral("entries"),
+ QStringLiteral("forEach"),
+ QStringLiteral("keys"),
+ QStringLiteral("values"),
+ QStringLiteral("toString")
+ };
+
+ for (const QString &method : searchParamsMethods) {
+ QJSManagedValue get(s.property(method), &engine);
+
+ // Shoudn't crash.
+ // We get different error messages depending on parameters, though.
+ QJSValue undef = get.call({});
+ QVERIFY(undef.isUndefined());
+ QVERIFY(engine.hasError());
+ engine.catchError();
+ }
+
+}
+
QTEST_MAIN(tst_QJSEngine)
#include "tst_qjsengine.moc"