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/auto') 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