From a11bb985ab531fe570e6fb3d13c6e76a547b8178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= Date: Wed, 8 May 2019 10:40:05 +0200 Subject: Update docs and test for runJavaScript Fixes: QTBUG-69567 Change-Id: Icdf5a200f7be1eb7a98cce62848e3a641c49e804 Reviewed-by: Allan Sandfeld Jensen Reviewed-by: Leena Miettinen --- tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests') diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp index 8c3b4002c..098bae9de 100644 --- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp +++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp @@ -2806,6 +2806,18 @@ void tst_QWebEnginePage::runJavaScript() JavaScriptCallbackUndefined callbackUndefined; page.runJavaScript("undefined", QWebEngineCallback(callbackUndefined)); + JavaScriptCallback callbackDate(QVariant(42.0)); + page.runJavaScript("new Date(42000)", QWebEngineCallback(callbackDate)); + + JavaScriptCallback callbackBlob(QVariant(QByteArray(8, 0))); + page.runJavaScript("new ArrayBuffer(8)", QWebEngineCallback(callbackBlob)); + + JavaScriptCallbackUndefined callbackFunction; + page.runJavaScript("(function(){})", QWebEngineCallback(callbackFunction)); + + JavaScriptCallback callbackPromise(QVariant(QVariantMap{})); + page.runJavaScript("new Promise(function(){})", QWebEngineCallback(callbackPromise)); + QVERIFY(watcher.wait()); } -- cgit v1.2.3 From 1a8e93c95de92f6a00bdf3768c5315dd032513c0 Mon Sep 17 00:00:00 2001 From: Leander Beernaert Date: Mon, 20 May 2019 11:05:51 +0200 Subject: Referrer HTTP Header no longer ignored when set via RequestInterceptor Make sure the HTTP referer is properly placed on a request when it's set via the QWebEngineUrlRequestInterceptor. Added test case to catch future incidents. Fixes: QTBUG-60203 Change-Id: Ida2f713a7352c3199fc9f8e15b5d8350d50afdda Reviewed-by: Allan Sandfeld Jensen --- .../qwebengineurlrequestinterceptor.pro | 1 + .../tst_qwebengineurlrequestinterceptor.cpp | 43 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) (limited to 'tests') diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/qwebengineurlrequestinterceptor.pro b/tests/auto/core/qwebengineurlrequestinterceptor/qwebengineurlrequestinterceptor.pro index e99c7f493..9c239f1a7 100644 --- a/tests/auto/core/qwebengineurlrequestinterceptor/qwebengineurlrequestinterceptor.pro +++ b/tests/auto/core/qwebengineurlrequestinterceptor/qwebengineurlrequestinterceptor.pro @@ -1 +1,2 @@ include(../tests.pri) +include(../../shared/http.pri) diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp index 23bf88417..653a1e421 100644 --- a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp +++ b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp @@ -34,6 +34,9 @@ #include #include +#include +#include + class tst_QWebEngineUrlRequestInterceptor : public QObject { Q_OBJECT @@ -59,6 +62,7 @@ private Q_SLOTS: void requestInterceptorByResourceType_data(); void requestInterceptorByResourceType(); void firstPartyUrlHttp(); + void passRefererHeader(); }; tst_QWebEngineUrlRequestInterceptor::tst_QWebEngineUrlRequestInterceptor() @@ -97,6 +101,9 @@ struct RequestInfo { int resourceType; }; +static const QByteArray kHttpHeaderReferrerValue = QByteArrayLiteral("http://somereferrer.com/"); +static const QByteArray kHttpHeaderRefererName = QByteArrayLiteral("referer"); + class TestRequestInterceptor : public QWebEngineUrlRequestInterceptor { public: @@ -112,6 +119,9 @@ public: if (shouldIntercept && info.requestUrl().toString().endsWith(QLatin1String("__placeholder__"))) info.redirect(QUrl("qrc:///resources/content.html")); + // Set referrer header + info.setHttpHeader(kHttpHeaderRefererName, kHttpHeaderReferrerValue); + requestInfos.append(info); } @@ -487,5 +497,38 @@ void tst_QWebEngineUrlRequestInterceptor::firstPartyUrlHttp() QCOMPARE(info.firstPartyUrl, firstPartyUrl); } +void tst_QWebEngineUrlRequestInterceptor::passRefererHeader() +{ + // Create HTTP Server to parse the request. + HttpServer httpServer; + + if (!httpServer.start()) + QSKIP("Failed to start http server"); + + bool succeeded = false; + connect(&httpServer, &HttpServer::newRequest, [&succeeded](HttpReqRep *rr) { + const QByteArray headerValue = rr->requestHeader(kHttpHeaderRefererName); + QCOMPARE(headerValue, kHttpHeaderReferrerValue); + succeeded = headerValue == kHttpHeaderReferrerValue; + rr->setResponseStatus(200); + rr->sendResponse(); + }); + + QWebEngineProfile profile; + TestRequestInterceptor interceptor(true); + profile.setRequestInterceptor(&interceptor); + + QWebEnginePage page(&profile); + QSignalSpy spy(&page, SIGNAL(loadFinished(bool))); + QWebEngineHttpRequest httpRequest; + QUrl requestUrl = httpServer.url(); + httpRequest.setUrl(requestUrl); + page.load(httpRequest); + + QVERIFY(spy.wait()); + (void) httpServer.stop(); + QVERIFY(succeeded); +} + QTEST_MAIN(tst_QWebEngineUrlRequestInterceptor) #include "tst_qwebengineurlrequestinterceptor.moc" -- cgit v1.2.3 From e02bcb0855ebee0612cab0f3cd3f9fd494497336 Mon Sep 17 00:00:00 2001 From: Tamas Zakor Date: Fri, 3 May 2019 15:53:25 +0200 Subject: Add path validation for QWebEngineDownloadItem::setPath() Do not set path if it ends with separator or if it matches with an already existing directory name. Task-number: QTBUG-75566 Change-Id: I4b78b28afe034c7589633c569a4945a36b32008e Reviewed-by: Allan Sandfeld Jensen --- .../tst_qwebenginedownloaditem.cpp | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) (limited to 'tests') diff --git a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp index b30fc7258..9732de85c 100644 --- a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp +++ b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp @@ -70,6 +70,7 @@ private Q_SLOTS: void downloadFileNot2(); void downloadDeleted(); void downloadDeletedByProfile(); + void downloadPathValidation(); private: void saveLink(QPoint linkPos); @@ -844,5 +845,127 @@ void tst_QWebEngineDownloadItem::downloadDeletedByProfile() QTRY_COMPARE(downloadItem.isNull(), true); } +void tst_QWebEngineDownloadItem::downloadPathValidation() +{ + const QString fileName = "test.txt"; + QString downloadPath; + QString originalDownloadPath; + + QTemporaryDir tmpDir; + QVERIFY(tmpDir.isValid()); + + // Set up HTTP server + ScopedConnection sc1 = connect(m_server, &HttpServer::newRequest, [&](HttpReqRep *rr) { + if (rr->requestMethod() == "GET" && rr->requestPath() == ("/" + fileName)) { + rr->setResponseHeader(QByteArrayLiteral("content-type"), QByteArrayLiteral("application/octet-stream")); + rr->setResponseHeader(QByteArrayLiteral("content-disposition"), QByteArrayLiteral("attachment")); + rr->setResponseBody(QByteArrayLiteral("a")); + rr->sendResponse(); + } else { + rr->setResponseStatus(404); + rr->sendResponse(); + } + }); + + // Set up profile and download handler + QPointer downloadItem; + ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + downloadItem = item; + originalDownloadPath = item->path(); + + item->setPath(downloadPath); + // TODO: Do not cancel download from 5.13. This is for not messing up system download path. + // Use m_profile->setDownloadPath(tmpDir.path()) at initialization. + if (item->path() != downloadPath) + item->cancel(); + else + item->accept(); + + connect(item, &QWebEngineDownloadItem::stateChanged, [&, item](QWebEngineDownloadItem::DownloadState downloadState) { + if (downloadState == QWebEngineDownloadItem::DownloadInterrupted) { + item->cancel(); + } + }); + + connect(item, &QWebEngineDownloadItem::finished, [&, item]() { + QCOMPARE(item->isFinished(), true); + QCOMPARE(item->totalBytes(), item->receivedBytes()); + QVERIFY(item->receivedBytes() > 0); + QCOMPARE(item->page(), m_page); + }); + }); + + QString oldPath = QDir::currentPath(); + QDir::setCurrent(tmpDir.path()); + + // Set only the file name. + downloadItem.clear(); + originalDownloadPath = ""; + downloadPath = fileName; + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadItem); + QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCompleted); + QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::NoReason); + QCOMPARE(downloadItem->path(), fileName); + + // Set only the directory path. + downloadItem.clear(); + originalDownloadPath = ""; + downloadPath = tmpDir.path(); + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadItem); + QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled); + QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled); + QCOMPARE(downloadItem->path(), originalDownloadPath); + + // Set only the directory path with separator. + downloadItem.clear(); + originalDownloadPath = ""; + downloadPath = tmpDir.path() + QDir::separator(); + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadItem); + QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled); + QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled); + QCOMPARE(downloadItem->path(), originalDownloadPath); + + // Set only the directory with the current directory path without ending separator. + downloadItem.clear(); + originalDownloadPath = ""; + downloadPath = "."; + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadItem); + QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled); + QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled); + QCOMPARE(downloadItem->path(), originalDownloadPath); + + // Set only the directory with the current directory path with ending separator. + downloadItem.clear(); + originalDownloadPath = ""; + downloadPath = "./"; + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadItem); + QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled); + QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled); + QCOMPARE(downloadItem->path(), originalDownloadPath); + + + + downloadItem.clear(); + originalDownloadPath = ""; + downloadPath = "..."; + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadItem); + QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled); +#if !defined(Q_OS_WIN) + QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::FileFailed); + QCOMPARE(downloadItem->path(), downloadPath); +#else + // Windows interprets the "..." path as a valid path. It will be the current path. + QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled); + QCOMPARE(downloadItem->path(), originalDownloadPath); +#endif // !defined(Q_OS_WIN) + QDir::setCurrent(oldPath); +} + QTEST_MAIN(tst_QWebEngineDownloadItem) #include "tst_qwebenginedownloaditem.moc" -- cgit v1.2.3