diff options
Diffstat (limited to 'tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp')
-rw-r--r-- | tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp | 76 |
1 files changed, 30 insertions, 46 deletions
diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp index 4b5ecb615..33edf25f1 100644 --- a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp +++ b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp @@ -118,29 +118,42 @@ struct RequestInfo { static const QByteArray kHttpHeaderReferrerValue = QByteArrayLiteral("http://somereferrer.com/"); static const QByteArray kHttpHeaderRefererName = QByteArrayLiteral("referer"); +static const QUrl kRedirectUrl = QUrl("qrc:///resources/content.html"); class TestRequestInterceptor : public QWebEngineUrlRequestInterceptor { public: QList<RequestInfo> requestInfos; - bool shouldIntercept; + bool shouldRedirect = false; QMap<QUrl, QSet<QUrl>> requestInitiatorUrls; + QMap<QByteArray, QByteArray> headers; void interceptRequest(QWebEngineUrlRequestInfo &info) override { QCOMPARE(QThread::currentThread() == QCoreApplication::instance()->thread(), !property("deprecated").toBool()); + // Since 63 we also intercept some unrelated blob requests.. if (info.requestUrl().scheme() == QLatin1String("blob")) return; - info.block(info.requestMethod() != QByteArrayLiteral("GET")); - if (shouldIntercept && info.requestUrl().toString().endsWith(QLatin1String("__placeholder__"))) - info.redirect(QUrl("qrc:///resources/content.html")); - // Set referrer header - info.setHttpHeader(kHttpHeaderRefererName, kHttpHeaderReferrerValue); + bool block = info.requestMethod() != QByteArrayLiteral("GET"); + bool redirect = shouldRedirect && info.requestUrl() != kRedirectUrl; + + if (block) { + info.block(true); + } else if (redirect) { + info.redirect(kRedirectUrl); + } else { + // set additional headers if any required by test + for (auto it = headers.begin(); it != headers.end(); ++it) info.setHttpHeader(it.key(), it.value()); + } requestInitiatorUrls[info.requestUrl()].insert(info.initiator()); requestInfos.append(info); + + // MEMO avoid unintentionally changing request when it is not needed for test logic + // since api behavior depends on 'changed' state of the info object + Q_ASSERT(info.changed() == (block || redirect || !headers.empty())); } bool shouldSkipRequest(const RequestInfo &requestInfo) @@ -182,42 +195,9 @@ public: return false; } - TestRequestInterceptor(bool intercept) - : shouldIntercept(intercept) - { - } -}; - -class TestServer : public HttpServer -{ -public: - TestServer() - { - connect(this, &HttpServer::newRequest, this, &TestServer::onNewRequest); - } - -private: - void onNewRequest(HttpReqRep *rr) + TestRequestInterceptor(bool redirect) + : shouldRedirect(redirect) { - const QDir resourceDir(TESTS_SOURCE_DIR "qwebengineurlrequestinterceptor/resources"); - QString path = rr->requestPath(); - path.remove(0, 1); - - if (rr->requestMethod() != "GET" || !resourceDir.exists(path)) - { - rr->setResponseStatus(404); - rr->sendResponse(); - return; - } - - QFile file(resourceDir.filePath(path)); - file.open(QIODevice::ReadOnly); - QByteArray data = file.readAll(); - rr->setResponseBody(data); - QMimeDatabase db; - QMimeType mime = db.mimeTypeForFileNameAndData(file.fileName(), data); - rr->setResponseHeader(QByteArrayLiteral("content-type"), mime.name().toUtf8()); - rr->sendResponse(); } }; @@ -252,7 +232,7 @@ void tst_QWebEngineUrlRequestInterceptor::interceptRequest() QFETCH(InterceptorSetter, setter); QWebEngineProfile profile; profile.settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, false); - TestRequestInterceptor interceptor(/* intercept */ true); + TestRequestInterceptor interceptor(/* intercept */ false); (profile.*setter)(&interceptor); QWebEnginePage page(&profile); QSignalSpy loadSpy(&page, SIGNAL(loadFinished(bool))); @@ -272,6 +252,7 @@ void tst_QWebEngineUrlRequestInterceptor::interceptRequest() QVERIFY(!success.toBool()); loadSpy.clear(); + interceptor.shouldRedirect = true; page.load(QUrl("qrc:///resources/__placeholder__")); QTRY_COMPARE(loadSpy.count(), 1); success = loadSpy.takeFirst().takeFirst(); @@ -372,6 +353,8 @@ void tst_QWebEngineUrlRequestInterceptor::requestedUrl() QCOMPARE(page.requestedUrl(), QUrl("qrc:///resources/__placeholder__")); QCOMPARE(page.url(), QUrl("qrc:///resources/content.html")); + interceptor.shouldRedirect = false; + page.setUrl(QUrl("qrc:/non-existent.html")); QTRY_COMPARE(spy.count(), 2); QVERIFY(interceptor.requestInfos.count() >= 3); @@ -663,12 +646,12 @@ void tst_QWebEngineUrlRequestInterceptor::passRefererHeader() const QByteArray headerValue = rr->requestHeader(kHttpHeaderRefererName); QCOMPARE(headerValue, kHttpHeaderReferrerValue); succeeded = headerValue == kHttpHeaderReferrerValue; - rr->setResponseStatus(200); rr->sendResponse(); }); QWebEngineProfile profile; - TestRequestInterceptor interceptor(true); + TestRequestInterceptor interceptor(false); + interceptor.headers.insert(kHttpHeaderRefererName, kHttpHeaderReferrerValue); (profile.*setter)(&interceptor); QWebEnginePage page(&profile); @@ -762,7 +745,8 @@ void tst_QWebEngineUrlRequestInterceptor::jsServiceWorker() { QFETCH(InterceptorSetter, setter); - TestServer server; + HttpServer server; + server.setResourceDirs({ TESTS_SOURCE_DIR "qwebengineurlrequestinterceptor/resources" }); QVERIFY(server.start()); QWebEngineProfile profile(QStringLiteral("Test")); @@ -776,7 +760,7 @@ void tst_QWebEngineUrlRequestInterceptor::jsServiceWorker() QTRY_COMPARE(page->messages.count(), 1); QCOMPARE(page->levels.at(0), QWebEnginePage::InfoMessageLevel); - QUrl firstPartyUrl = QUrl(server.url().toString() + "sw.js"); + QUrl firstPartyUrl = QUrl(server.url().toString(QUrl::RemovePort)); QList<RequestInfo> infos; // Service Worker QTRY_VERIFY(interceptor.hasUrlRequestForType(QWebEngineUrlRequestInfo::ResourceTypeServiceWorker)); |