summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorMichal Klocek <michal.klocek@qt.io>2020-04-30 15:23:16 +0200
committerMichal Klocek <michal.klocek@qt.io>2020-05-04 14:59:12 +0200
commit810eb3b20247ef3b162182f6a31f5e745615d3e3 (patch)
treeca78deb089e4dda0a0b46843b0a3f531c41d0e46 /tests/auto
parent6d86c3f97efe3ba8cb1eae83e0e458c0d7e817c8 (diff)
Add cookie testcase to proxy test
Task-number: QTBUG-58121 Change-Id: I9953eaec9d4dd971a7f7fb3ed9a156bccffa05d5 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/widgets/proxy/proxy_server.cpp18
-rw-r--r--tests/auto/widgets/proxy/proxy_server.h7
-rw-r--r--tests/auto/widgets/proxy/tst_proxy.cpp35
3 files changed, 55 insertions, 5 deletions
diff --git a/tests/auto/widgets/proxy/proxy_server.cpp b/tests/auto/widgets/proxy/proxy_server.cpp
index 55f014914..3bf915609 100644
--- a/tests/auto/widgets/proxy/proxy_server.cpp
+++ b/tests/auto/widgets/proxy/proxy_server.cpp
@@ -42,8 +42,16 @@ void ProxyServer::setCredentials(const QByteArray &user, const QByteArray passwo
m_auth.append(QChar(':'));
m_auth.append(password);
m_auth = m_auth.toBase64();
+ m_authenticate = true;
}
+void ProxyServer::setCookie(const QByteArray &cookie)
+{
+ m_cookie.append(QByteArrayLiteral("Cookie: "));
+ m_cookie.append(cookie);
+}
+
+
bool ProxyServer::isListening()
{
return m_server.isListening();
@@ -75,7 +83,7 @@ void ProxyServer::handleReadReady()
if (!m_data.endsWith("\r\n\r\n"))
return;
- if (!m_data.contains(QByteArrayLiteral("Proxy-Authorization: Basic"))) {
+ if (m_authenticate && !m_data.contains(QByteArrayLiteral("Proxy-Authorization: Basic"))) {
socket->write("HTTP/1.1 407 Proxy Authentication Required\nProxy-Authenticate: "
"Basic realm=\"Proxy requires authentication\"\r\n"
"content-length: 0\r\n"
@@ -83,8 +91,12 @@ void ProxyServer::handleReadReady()
return;
}
- if (m_data.contains(m_auth)) {
- emit success();
+ if (m_authenticate && m_data.contains(m_auth)) {
+ emit authenticationSuccess();
+ }
+
+ if (m_data.contains(m_cookie)) {
+ emit cookieMatch();
}
m_data.clear();
}
diff --git a/tests/auto/widgets/proxy/proxy_server.h b/tests/auto/widgets/proxy/proxy_server.h
index cb7c30600..7bc7b100b 100644
--- a/tests/auto/widgets/proxy/proxy_server.h
+++ b/tests/auto/widgets/proxy/proxy_server.h
@@ -39,6 +39,7 @@ class ProxyServer : public QObject
public:
explicit ProxyServer(QObject *parent = nullptr);
void setCredentials(const QByteArray &user, const QByteArray password);
+ void setCookie(const QByteArray &cookie);
bool isListening();
public slots:
@@ -49,11 +50,15 @@ private slots:
void handleReadReady();
signals:
- void success();
+ void authenticationSuccess();
+ void cookieMatch();
+
private:
QByteArray m_data;
QTcpServer m_server;
QByteArray m_auth;
+ QByteArray m_cookie;
+ bool m_authenticate = false;
};
#endif // PROXY_SERVER_H
diff --git a/tests/auto/widgets/proxy/tst_proxy.cpp b/tests/auto/widgets/proxy/tst_proxy.cpp
index 5f5dec016..c3e3c88a4 100644
--- a/tests/auto/widgets/proxy/tst_proxy.cpp
+++ b/tests/auto/widgets/proxy/tst_proxy.cpp
@@ -32,6 +32,17 @@
#include <QNetworkProxy>
#include <QWebEnginePage>
#include <QWebEngineView>
+#include <QWebEngineUrlRequestInterceptor>
+
+
+struct Interceptor : public QWebEngineUrlRequestInterceptor
+{
+ Interceptor(const QByteArray cookie):m_cookie(cookie){};
+ void interceptRequest(QWebEngineUrlRequestInfo &info) override {
+ info.setHttpHeader(QByteArray("Cookie"), m_cookie);
+ };
+ QByteArray m_cookie;
+};
class tst_Proxy : public QObject {
@@ -41,8 +52,10 @@ public:
private slots:
void proxyAuthentication();
+ void forwardCookie();
};
+
void tst_Proxy::proxyAuthentication()
{
QByteArray user(QByteArrayLiteral("test"));
@@ -59,11 +72,31 @@ void tst_Proxy::proxyAuthentication()
server.run();
QTRY_VERIFY2(server.isListening(), "Could not setup authentication server");
QWebEnginePage page;
- QSignalSpy successSpy(&server, &ProxyServer::success);
+ QSignalSpy successSpy(&server, &ProxyServer::authenticationSuccess);
page.load(QUrl("http://www.qt.io"));
QTRY_VERIFY2(successSpy.count() > 0, "Could not get authentication token");
}
+void tst_Proxy::forwardCookie()
+{
+ QNetworkProxy proxy;
+ proxy.setType(QNetworkProxy::HttpProxy);
+ proxy.setHostName("localhost");
+ proxy.setPort(5555);
+ QNetworkProxy::setApplicationProxy(proxy);
+ ProxyServer server;
+ QByteArray cookie("foo=bar; sessionToken=123");
+ server.setCookie(cookie);
+ server.run();
+ QTRY_VERIFY2(server.isListening(), "Could not setup proxy server");
+ Interceptor interceptor(cookie);
+ QWebEnginePage page;
+ page.setUrlRequestInterceptor(&interceptor);
+ QSignalSpy cookieSpy(&server, &ProxyServer::cookieMatch);
+ page.load(QUrl("http://www.qt.io"));
+ QTRY_VERIFY2(cookieSpy.count() > 0, "Could not get cookie");
+}
+
#include "tst_proxy.moc"
QTEST_MAIN(tst_Proxy)