summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJüri Valdmann <juri.valdmann@qt.io>2019-04-10 12:30:04 +0200
committerJüri Valdmann <juri.valdmann@qt.io>2019-04-16 15:29:47 +0000
commit03b5eb203a18dcd97d6ddf5dd4dfc980eca8200e (patch)
tree12f35496c57b8e4090558090e1a24aea73c3c12a /tests
parent4f5380f413a8a5e89ee57f301a9d6010eac92ad4 (diff)
Fix resolving relative URLs with custom schemes
Pull in 3rdparty patch and add test. Fixes: QTBUG-74864 Change-Id: I5440f58ff55297c2a51f896d43f479404ff6ca2f Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/widgets/origins/tst_origins.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/auto/widgets/origins/tst_origins.cpp b/tests/auto/widgets/origins/tst_origins.cpp
index 59cbdae13..21afead1d 100644
--- a/tests/auto/widgets/origins/tst_origins.cpp
+++ b/tests/auto/widgets/origins/tst_origins.cpp
@@ -172,6 +172,7 @@ private Q_SLOTS:
void cleanupTestCase();
void jsUrlCanon();
+ void jsUrlRelative();
void jsUrlOrigin();
void subdirWithAccess();
void subdirWithoutAccess();
@@ -281,6 +282,54 @@ void tst_Origins::jsUrlCanon()
QVariant(QSL("hostportanduserinformationsyntax://a:b@foo/bar")));
}
+// Test relative URL resolution.
+void tst_Origins::jsUrlRelative()
+{
+ QVERIFY(load(QSL("about:blank")));
+
+ // Schemes with hosts, like http, work as expected.
+ QCOMPARE(eval(QSL("new URL('bar', 'http://foo').href")), QVariant(QSL("http://foo/bar")));
+ QCOMPARE(eval(QSL("new URL('baz', 'http://foo/bar').href")), QVariant(QSL("http://foo/baz")));
+ QCOMPARE(eval(QSL("new URL('baz', 'http://foo/bar/').href")), QVariant(QSL("http://foo/bar/baz")));
+ QCOMPARE(eval(QSL("new URL('/baz', 'http://foo/bar/').href")), QVariant(QSL("http://foo/baz")));
+ QCOMPARE(eval(QSL("new URL('./baz', 'http://foo/bar/').href")), QVariant(QSL("http://foo/bar/baz")));
+ QCOMPARE(eval(QSL("new URL('../baz', 'http://foo/bar/').href")), QVariant(QSL("http://foo/baz")));
+ QCOMPARE(eval(QSL("new URL('../../baz', 'http://foo/bar/').href")), QVariant(QSL("http://foo/baz")));
+ QCOMPARE(eval(QSL("new URL('//baz', 'http://foo/bar/').href")), QVariant(QSL("http://baz/")));
+
+ // In the case of schemes without hosts, relative URLs only work if the URL
+ // starts with a single slash -- and canonicalization does not guarantee
+ // this. The following cases all fail with TypeErrors.
+ QCOMPARE(eval(QSL("new URL('bar', 'tst:foo').href")), QVariant());
+ QCOMPARE(eval(QSL("new URL('baz', 'tst:foo/bar').href")), QVariant());
+ QCOMPARE(eval(QSL("new URL('bar', 'tst://foo').href")), QVariant());
+ QCOMPARE(eval(QSL("new URL('bar', 'tst:///foo').href")), QVariant());
+
+ // However, registered custom schemes have been patched to allow relative
+ // URLs even without an initial slash.
+ QCOMPARE(eval(QSL("new URL('bar', 'qrc:foo').href")), QVariant(QSL("qrc:bar")));
+ QCOMPARE(eval(QSL("new URL('baz', 'qrc:foo/bar').href")), QVariant(QSL("qrc:foo/baz")));
+ QCOMPARE(eval(QSL("new URL('bar', 'qrc://foo').href")), QVariant());
+ QCOMPARE(eval(QSL("new URL('bar', 'qrc:///foo').href")), QVariant());
+
+ // With a slash it works the same as http except 'foo' is part of the path and not the host.
+ QCOMPARE(eval(QSL("new URL('bar', 'qrc:/foo').href")), QVariant(QSL("qrc:/bar")));
+ QCOMPARE(eval(QSL("new URL('bar', 'qrc:/foo/').href")), QVariant(QSL("qrc:/foo/bar")));
+ QCOMPARE(eval(QSL("new URL('baz', 'qrc:/foo/bar').href")), QVariant(QSL("qrc:/foo/baz")));
+ QCOMPARE(eval(QSL("new URL('baz', 'qrc:/foo/bar/').href")), QVariant(QSL("qrc:/foo/bar/baz")));
+ QCOMPARE(eval(QSL("new URL('/baz', 'qrc:/foo/bar/').href")), QVariant(QSL("qrc:/baz")));
+ QCOMPARE(eval(QSL("new URL('./baz', 'qrc:/foo/bar/').href")), QVariant(QSL("qrc:/foo/bar/baz")));
+ QCOMPARE(eval(QSL("new URL('../baz', 'qrc:/foo/bar/').href")), QVariant(QSL("qrc:/foo/baz")));
+ QCOMPARE(eval(QSL("new URL('../../baz', 'qrc:/foo/bar/').href")), QVariant(QSL("qrc:/baz")));
+ QCOMPARE(eval(QSL("new URL('../../../baz', 'qrc:/foo/bar/').href")), QVariant(QSL("qrc:/baz")));
+
+ // If the relative URL begins with >= 2 slashes, then the scheme is treated
+ // not as a Syntax::Path scheme but as a Syntax::HostPortAndUserInformation
+ // scheme.
+ QCOMPARE(eval(QSL("new URL('//baz', 'qrc:/foo/bar/').href")), QVariant(QSL("qrc://baz/")));
+ QCOMPARE(eval(QSL("new URL('///baz', 'qrc:/foo/bar/').href")), QVariant(QSL("qrc://baz/")));
+}
+
// Test origin serialization in Blink, implemented by blink::KURL and
// blink::SecurityOrigin as opposed to GURL and url::Origin.
void tst_Origins::jsUrlOrigin()