diff options
author | Jüri Valdmann <juri.valdmann@qt.io> | 2019-09-24 17:46:19 +0200 |
---|---|---|
committer | Jüri Valdmann <juri.valdmann@qt.io> | 2019-09-25 15:56:35 +0200 |
commit | e31acc86e991d6f83bd56eebb7371914fc9ca5ac (patch) | |
tree | 349d3cde4ec0083b5fbd21245b6506478848bd03 /tests | |
parent | 3db2cf8b8e3cf98ed03ab7220e4006946b741973 (diff) |
Fix XMLHttpRequest status with custom schemes
Emulate a HTTP 200 OK response when QWebEngineUrlRequestJob::reply is called so
that an XMLHttpRequest to a custom scheme gets a proper 'status' property.
Note that calling QWebEngineUrlRequestJob::fail on the other hand emulates a
network error, meaning it triggers the 'error' event in XMLHttpRequest.
Fixes: QTBUG-78316
Change-Id: Ia7249a5b72533aa7e2ed4ef8f62d8b6b89820cc1
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp index 8b75067ee..921831700 100644 --- a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp +++ b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp @@ -40,6 +40,12 @@ #include <QtWebEngineWidgets/qwebengineview.h> #include <QtWebEngineWidgets/qwebenginedownloaditem.h> +#if QT_CONFIG(webengine_webchannel) +#include <QWebChannel> +#endif + +#include <map> + class tst_QWebEngineProfile : public QObject { Q_OBJECT @@ -58,6 +64,7 @@ private Q_SLOTS: void urlSchemeHandlerStreaming(); void urlSchemeHandlerRequestHeaders(); void urlSchemeHandlerInstallation(); + void urlSchemeHandlerXhrStatus(); void customUserAgent(); void httpAcceptLanguage(); void downloadItem(); @@ -568,6 +575,109 @@ void tst_QWebEngineProfile::urlSchemeHandlerInstallation() profile.removeUrlScheme("tst"); } +#if QT_CONFIG(webengine_webchannel) +class XhrStatusHost : public QObject +{ + Q_OBJECT +public: + std::map<QUrl, int> requests; + + bool isReady() + { + static const auto sig = QMetaMethod::fromSignal(&XhrStatusHost::load); + return isSignalConnected(sig); + } + +Q_SIGNALS: + void load(QUrl url); + +public Q_SLOTS: + void loadFinished(QUrl url, int status) + { + requests[url] = status; + } + +private: +}; + +class XhrStatusUrlSchemeHandler : public QWebEngineUrlSchemeHandler +{ +public: + void requestStarted(QWebEngineUrlRequestJob *job) + { + QString path = job->requestUrl().path(); + if (path == "/") { + QBuffer *buffer = new QBuffer(job); + buffer->open(QBuffer::ReadWrite); + buffer->write(QByteArrayLiteral(R"( +<html> + <body> + <script src="qwebchannel.js"></script> + <script> + new QWebChannel(qt.webChannelTransport, (channel) => { + const host = channel.objects.host; + host.load.connect((url) => { + const xhr = new XMLHttpRequest(); + xhr.onload = () => { host.loadFinished(url, xhr.status); }; + xhr.onerror = () => { host.loadFinished(url, -1); }; + xhr.open("GET", url, true); + xhr.send(); + }); + }); + </script> + </body> +</html> +)")); + buffer->seek(0); + job->reply("text/html", buffer); + } else if (path == "/qwebchannel.js") { + QFile *file = new QFile(":/qtwebchannel/qwebchannel.js", job); + file->open(QFile::ReadOnly); + job->reply("application/javascript", file); + } else if (path == "/ok") { + QBuffer *buffer = new QBuffer(job); + buffer->buffer() = QByteArrayLiteral("ok"); + job->reply("text/plain", buffer); + } else if (path == "/redirect") { + QUrl url = job->requestUrl(); + url.setPath("/ok"); + job->redirect(url); + } else if (path == "/fail") { + job->fail(QWebEngineUrlRequestJob::RequestFailed); + } else { + job->fail(QWebEngineUrlRequestJob::UrlNotFound); + } + } +}; +#endif + +void tst_QWebEngineProfile::urlSchemeHandlerXhrStatus() +{ +#if QT_CONFIG(webengine_webchannel) + XhrStatusUrlSchemeHandler handler; + XhrStatusHost host; + QWebEngineProfile profile; + QWebEnginePage page(&profile); + QWebChannel channel; + channel.registerObject("host", &host); + profile.installUrlSchemeHandler("aviancarrier", &handler); + page.setWebChannel(&channel); + page.load(QUrl("aviancarrier:/")); + QTRY_VERIFY(host.isReady()); + host.load(QUrl("aviancarrier:/ok")); + host.load(QUrl("aviancarrier:/redirect")); + host.load(QUrl("aviancarrier:/fail")); + host.load(QUrl("aviancarrier:/notfound")); + QTRY_COMPARE(host.requests.size(), 4u); + QCOMPARE(host.requests[QUrl("aviancarrier:/ok")], 200); + QCOMPARE(host.requests[QUrl("aviancarrier:/redirect")], 200); + QCOMPARE(host.requests[QUrl("aviancarrier:/fail")], -1); + QCOMPARE(host.requests[QUrl("aviancarrier:/notfound")], -1); +#else + QSKIP("No QtWebChannel"); +#endif +} + void tst_QWebEngineProfile::customUserAgent() { QString defaultUserAgent = QWebEngineProfile::defaultProfile()->httpUserAgent(); |