Fix retrieving of the generic system proxy via environment variables

ignoreProxyFor() always returned true if the no_proxy was not set,
which resulted in the first token being an empty QByteArray, causing
the endsWith() check to always evaluate to true.

Add a unit test that is enabled for those platforms that use the generic
system proxy.

Change-Id: I6081ad5e0b8e2c3fee1568835907c32bde5b7772
Reviewed-by: Peter Hartmann <phartmann@blackberry.com>
This commit is contained in:
Thomas McGuire 2013-04-23 09:37:29 +02:00 committed by The Qt Project
parent 1ea1abeb91
commit e0cf3fcd8f
2 changed files with 46 additions and 1 deletions

View File

@ -55,7 +55,11 @@ QT_BEGIN_NAMESPACE
static bool ignoreProxyFor(const QNetworkProxyQuery &query)
{
const QList<QByteArray> noProxyTokens = qgetenv("no_proxy").split(',');
const QByteArray noProxy = qgetenv("no_proxy").trimmed();
if (noProxy.isEmpty())
return false;
const QList<QByteArray> noProxyTokens = noProxy.split(',');
foreach (const QByteArray &rawToken, noProxyTokens) {
QByteArray token = rawToken.trimmed();

View File

@ -87,6 +87,8 @@ private slots:
void inNetworkAccessManager_data();
void inNetworkAccessManager();
#endif
void genericSystemProxy();
void genericSystemProxy_data();
private:
QString formatProxyName(const QNetworkProxy & proxy) const;
@ -367,6 +369,45 @@ void tst_QNetworkProxyFactory::inNetworkAccessManager()
#endif //QT_NO_BEARERMANAGEMENT
Q_DECLARE_METATYPE(QNetworkProxy::ProxyType)
void tst_QNetworkProxyFactory::genericSystemProxy()
{
QFETCH(QByteArray, envVar);
QFETCH(QByteArray, url);
QFETCH(QNetworkProxy::ProxyType, proxyType);
QFETCH(QString, hostName);
QFETCH(int, port);
// The generic system proxy is only available on the following platforms
#if (!defined Q_OS_BLACKBERRY) && (!defined Q_OS_WIN) && ((!defined Q_OS_MAC) || defined Q_OS_IOS)
qputenv(envVar, url);
const QList<QNetworkProxy> systemProxy = QNetworkProxyFactory::systemProxyForQuery();
QCOMPARE(systemProxy.size(), 1);
QCOMPARE(systemProxy.first().type(), proxyType);
QCOMPARE(systemProxy.first().hostName(), hostName);
QCOMPARE(systemProxy.first().port(), static_cast<quint16>(port));
qunsetenv(envVar);
#else
QSKIP("Generic system proxy not available on this platform.");
#endif
}
void tst_QNetworkProxyFactory::genericSystemProxy_data()
{
QTest::addColumn<QByteArray>("envVar");
QTest::addColumn<QByteArray>("url");
QTest::addColumn<QNetworkProxy::ProxyType>("proxyType");
QTest::addColumn<QString>("hostName");
QTest::addColumn<int>("port");
QTest::newRow("no proxy") << QByteArray("http_proxy") << QByteArray() << QNetworkProxy::NoProxy
<< QString() << 0;
QTest::newRow("socks5") << QByteArray("http_proxy") << QByteArray("socks5://127.0.0.1:4242")
<< QNetworkProxy::Socks5Proxy << QString("127.0.0.1") << 4242;
QTest::newRow("http") << QByteArray("http_proxy") << QByteArray("http://example.com:666")
<< QNetworkProxy::HttpProxy << QString("example.com") << 666;
}
class QSPFQThread : public QThread
{