Skip to content

Commit 6fffee8

Browse files
author
Michal Klocek
committed
Adds proxy pac test
Task-number: QTBUG-71128 Task-number: QTBUG-69281 Task-number: QTBUG-71229 Change-Id: If6b839ce12efb3ccd69c270d99b10b3756014bb2 Reviewed-by: Kai Koehne <[email protected]>
1 parent 5be48d4 commit 6fffee8

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed

tests/auto/widgets/proxypac/proxy.pac

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function FindProxyForURL(url, host)
2+
{
3+
if (shExpMatch(host, "*.proxy1.com")) return "PROXY localhost:5551";
4+
if (shExpMatch(host, "*.proxy2.com")) return "PROXY localhost:5552";
5+
return "PROXY proxy.url:8080";
6+
}
7+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
include(../tests.pri)
2+
QT += webengine
3+
HEADERS += proxyserver.h
4+
SOURCES += proxyserver.cpp
5+
6+
# QTBUG-71229
7+
xgd_desktop.name=XDG_CURRENT_DESKTOP
8+
xgd_desktop.value=KDE
9+
QT_TOOL_ENV += xgd_desktop
10+
11+
kde_home.name=KDEHOME
12+
kde_home.value=$$OUT_PWD
13+
QT_TOOL_ENV += kde_home
14+
15+
PROXY_CONFIG= \
16+
"[Proxy Settings]" \
17+
"Proxy Config Script=$$PWD/proxy.pac" \
18+
"ProxyType=2"
19+
20+
mkpath($$OUT_PWD/share/config)
21+
KDE_FILE = $$OUT_PWD/share/config/kioslaverc
22+
23+
!build_pass {
24+
write_file($$KDE_FILE, PROXY_CONFIG)
25+
}
26+
27+
QMAKE_DISTCLEAN += $$KDE_FILE
28+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2018 The Qt Company Ltd.
4+
** Contact: https://www.qt.io/licensing/
5+
**
6+
** This file is part of the QtWebEngine module of the Qt Toolkit.
7+
**
8+
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9+
** Commercial License Usage
10+
** Licensees holding valid commercial Qt licenses may use this file in
11+
** accordance with the commercial license agreement provided with the
12+
** Software or, alternatively, in accordance with the terms contained in
13+
** a written agreement between you and The Qt Company. For licensing terms
14+
** and conditions see https://www.qt.io/terms-conditions. For further
15+
** information use the contact form at https://www.qt.io/contact-us.
16+
**
17+
** GNU General Public License Usage
18+
** Alternatively, this file may be used under the terms of the GNU
19+
** General Public License version 3 as published by the Free Software
20+
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21+
** included in the packaging of this file. Please review the following
22+
** information to ensure the GNU General Public License requirements will
23+
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24+
**
25+
** $QT_END_LICENSE$
26+
**
27+
****************************************************************************/
28+
29+
#include "proxyserver.h"
30+
#include <QDataStream>
31+
#include <QTcpSocket>
32+
#include <QDebug>
33+
34+
ProxyServer::ProxyServer(QObject *parent) : QObject(parent),
35+
m_port(5555)
36+
{
37+
connect(&m_server, &QTcpServer::newConnection, this, &ProxyServer::handleNewConnection);
38+
}
39+
40+
void ProxyServer::setPort(int port)
41+
{
42+
m_port = port;
43+
}
44+
45+
bool ProxyServer::isListening()
46+
{
47+
return m_server.isListening();
48+
}
49+
50+
void ProxyServer::run()
51+
{
52+
if (!m_server.listen(QHostAddress::LocalHost, m_port))
53+
qFatal("Could not start the test server");
54+
}
55+
56+
void ProxyServer::handleNewConnection()
57+
{
58+
// do one connection at the time
59+
Q_ASSERT(m_data.isEmpty());
60+
QTcpSocket *socket = m_server.nextPendingConnection();
61+
Q_ASSERT(socket);
62+
connect(socket, &QAbstractSocket::disconnected, socket, &QObject::deleteLater);
63+
connect(socket, &QAbstractSocket::readyRead, this, &ProxyServer::handleReadReady);
64+
}
65+
66+
void ProxyServer::handleReadReady()
67+
{
68+
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
69+
Q_ASSERT(socket);
70+
71+
m_data.append(socket->readAll());
72+
73+
//simply wait for whole request
74+
if (!m_data.endsWith("\r\n\r\n"))
75+
return;
76+
77+
// add fake proxy authetication
78+
socket->write("HTTP/1.1 407 Proxy Auth Required\nProxy-Authenticate: "
79+
"Basic realm=\"Proxy requires authentication\"\r\n\r\n");
80+
81+
m_data.clear();
82+
socket->disconnectFromHost();
83+
emit requestReceived();
84+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2018 The Qt Company Ltd.
4+
** Contact: https://www.qt.io/licensing/
5+
**
6+
** This file is part of the QtWebEngine module of the Qt Toolkit.
7+
**
8+
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9+
** Commercial License Usage
10+
** Licensees holding valid commercial Qt licenses may use this file in
11+
** accordance with the commercial license agreement provided with the
12+
** Software or, alternatively, in accordance with the terms contained in
13+
** a written agreement between you and The Qt Company. For licensing terms
14+
** and conditions see https://www.qt.io/terms-conditions. For further
15+
** information use the contact form at https://www.qt.io/contact-us.
16+
**
17+
** GNU General Public License Usage
18+
** Alternatively, this file may be used under the terms of the GNU
19+
** General Public License version 3 as published by the Free Software
20+
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21+
** included in the packaging of this file. Please review the following
22+
** information to ensure the GNU General Public License requirements will
23+
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24+
**
25+
** $QT_END_LICENSE$
26+
**
27+
****************************************************************************/
28+
29+
#ifndef PROXY_SERVER_H
30+
#define PROXY_SERVER_H
31+
32+
#include <QObject>
33+
#include <QTcpServer>
34+
35+
class ProxyServer : public QObject
36+
{
37+
Q_OBJECT
38+
39+
public:
40+
explicit ProxyServer(QObject *parent = nullptr);
41+
42+
void setPort(int port);
43+
bool isListening();
44+
45+
signals:
46+
void requestReceived();
47+
48+
public slots:
49+
void run();
50+
51+
private slots:
52+
void handleNewConnection();
53+
void handleReadReady();
54+
55+
private:
56+
int m_port;
57+
QByteArray m_data;
58+
QTcpServer m_server;
59+
60+
};
61+
62+
#endif // PROXY_SERVER_H
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/****************************************************************************
2+
**
3+
** Copyright (C) 2018 The Qt Company Ltd.
4+
** Contact: https://www.qt.io/licensing/
5+
**
6+
** This file is part of the QtWebEngine module of the Qt Toolkit.
7+
**
8+
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9+
** Commercial License Usage
10+
** Licensees holding valid commercial Qt licenses may use this file in
11+
** accordance with the commercial license agreement provided with the
12+
** Software or, alternatively, in accordance with the terms contained in
13+
** a written agreement between you and The Qt Company. For licensing terms
14+
** and conditions see https://www.qt.io/terms-conditions. For further
15+
** information use the contact form at https://www.qt.io/contact-us.
16+
**
17+
** GNU General Public License Usage
18+
** Alternatively, this file may be used under the terms of the GNU
19+
** General Public License version 3 as published by the Free Software
20+
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21+
** included in the packaging of this file. Please review the following
22+
** information to ensure the GNU General Public License requirements will
23+
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24+
**
25+
** $QT_END_LICENSE$
26+
**
27+
****************************************************************************/
28+
29+
#include "qtwebengineglobal.h"
30+
#include "proxyserver.h"
31+
#include <QTest>
32+
#include <QSignalSpy>
33+
#include <QWebEngineProfile>
34+
#include <QWebEnginePage>
35+
#include <QNetworkProxy>
36+
37+
38+
class tst_ProxyPac : public QObject {
39+
Q_OBJECT
40+
public:
41+
tst_ProxyPac(){}
42+
43+
private slots:
44+
void proxypac();
45+
};
46+
47+
void tst_ProxyPac::proxypac()
48+
{
49+
ProxyServer proxyServer1;
50+
proxyServer1.setPort(5551);
51+
proxyServer1.run();
52+
QSignalSpy proxySpy1(&proxyServer1, &ProxyServer::requestReceived);
53+
54+
ProxyServer proxyServer2;
55+
proxyServer2.setPort(5552);
56+
proxyServer2.run();
57+
QSignalSpy proxySpy2(&proxyServer2, &ProxyServer::requestReceived);
58+
59+
QTRY_VERIFY2(proxyServer1.isListening(), "Could not setup proxy server 1");
60+
QTRY_VERIFY2(proxyServer2.isListening(), "Could not setup proxy server 2");
61+
62+
QWebEngineProfile profile;
63+
QWebEnginePage page(&profile);
64+
page.load(QUrl("http://test.proxy1.com"));
65+
QTRY_COMPARE(proxySpy1.count() >= 1, true);
66+
QVERIFY(proxySpy2.count() == 0);
67+
page.load(QUrl("http://test.proxy2.com"));
68+
QTRY_COMPARE(proxySpy2.count() >= 1 , true);
69+
}
70+
71+
#include "tst_proxypac.moc"
72+
QTEST_MAIN(tst_ProxyPac)
73+

tests/auto/widgets/widgets.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ qtConfig(accessibility) {
2121
SUBDIRS += qwebengineaccessibility
2222
}
2323

24+
# QTBUG-71229
25+
linux:!boot2qt: SUBDIRS += proxypac
26+
2427
qtConfig(webengine-spellchecker):!cross_compile {
2528
!qtConfig(webengine-native-spellchecker) {
2629
SUBDIRS += qwebenginespellcheck

0 commit comments

Comments
 (0)