Skip to content

Commit 77a73f4

Browse files
committedNov 10, 2017
[tests] Brand new C++ test and new py test case for ogr proxy
1 parent e85897c commit 77a73f4

File tree

3 files changed

+158
-6
lines changed

3 files changed

+158
-6
lines changed
 

‎tests/src/providers/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ SET_TARGET_PROPERTIES(qgis_wcsprovidertest PROPERTIES
6666

6767
ADD_QGIS_TEST(gdalprovidertest testqgsgdalprovider.cpp)
6868

69+
ADD_QGIS_TEST(ogrprovidertest testqgsogrprovider.cpp)
70+
6971
ADD_QGIS_TEST(wmscapabilititestest
7072
testqgswmscapabilities.cpp)
7173
TARGET_LINK_LIBRARIES(qgis_wmscapabilititestest wmsprovider_a)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/***************************************************************************
2+
testqgsogrprovider.cpp - TestQgsOgrProvider
3+
4+
---------------------
5+
begin : 10.11.2017
6+
copyright : (C) 2017 by Alessandro Pasotti
7+
email : apasotti at boundlessgeo dot com
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
18+
#include "qgstest.h"
19+
20+
//qgis includes...
21+
#include <qgis.h>
22+
#include <qgssettings.h>
23+
#include <qgsapplication.h>
24+
#include <qgsproviderregistry.h>
25+
#include <qgsvectorlayer.h>
26+
#include <qgsnetworkaccessmanager.h>
27+
28+
#include <QObject>
29+
30+
#include <cpl_conv.h>
31+
32+
33+
/**
34+
* \ingroup UnitTests
35+
* This is a unit test for the ogr provider
36+
*/
37+
class TestQgsOgrProvider : public QObject
38+
{
39+
Q_OBJECT
40+
41+
private slots:
42+
void initTestCase();// will be called before the first testfunction is executed.
43+
void cleanupTestCase();// will be called after the last testfunction was executed.
44+
void init() {}// will be called before each testfunction is executed.
45+
void cleanup() {}// will be called after every testfunction.
46+
47+
void setupProxy();
48+
49+
private:
50+
QString mTestDataDir;
51+
QString mReport;
52+
signals:
53+
54+
public slots:
55+
};
56+
57+
58+
59+
//runs before all tests
60+
void TestQgsOgrProvider::initTestCase()
61+
{
62+
// init QGIS's paths - true means that all path will be inited from prefix
63+
QgsApplication::init();
64+
QgsApplication::initQgis();
65+
66+
mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
67+
mReport = QStringLiteral( "<h1>OGR Provider Tests</h1>\n" );
68+
}
69+
70+
//runs after all tests
71+
void TestQgsOgrProvider::cleanupTestCase()
72+
{
73+
QgsApplication::exitQgis();
74+
QString myReportFile = QDir::tempPath() + "/qgistest.html";
75+
QFile myFile( myReportFile );
76+
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
77+
{
78+
QTextStream myQTextStream( &myFile );
79+
myQTextStream << mReport;
80+
myFile.close();
81+
}
82+
}
83+
84+
void TestQgsOgrProvider::setupProxy()
85+
{
86+
87+
QgsSettings settings;
88+
{
89+
settings.setValue( QStringLiteral( "proxy/proxyEnabled" ), true );
90+
settings.setValue( QStringLiteral( "proxy/proxyPort" ), QStringLiteral( "1234" ) );
91+
settings.setValue( QStringLiteral( "proxy/proxyHost" ), QStringLiteral( "myproxyhostname.com" ) );
92+
settings.setValue( QStringLiteral( "proxy/proxyUser" ), QStringLiteral( "username" ) );
93+
settings.setValue( QStringLiteral( "proxy/proxyPassword" ), QStringLiteral( "password" ) );
94+
settings.setValue( QStringLiteral( "proxy/proxyExcludedUrls" ), QStringLiteral( "http://www.myhost.com|http://www.myotherhost.com" ) );
95+
QgsNetworkAccessManager::instance()->setupDefaultProxyAndCache();
96+
QgsVectorLayer vl( mTestDataDir + '/' + QStringLiteral( "lines.shp" ), QStringLiteral( "proxy_test" ), QLatin1Literal( "ogr" ) );
97+
QVERIFY( vl.isValid() );
98+
const char *proxyConfig = CPLGetConfigOption( "GDAL_HTTP_PROXY", NULL );
99+
QCOMPARE( proxyConfig, "myproxyhostname.com:1234" );
100+
const char *proxyCredentials = CPLGetConfigOption( "GDAL_HTTP_PROXYUSERPWD", NULL );
101+
QCOMPARE( proxyCredentials, "username:password" );
102+
}
103+
104+
{
105+
// Test partial config
106+
settings.setValue( QStringLiteral( "proxy/proxyEnabled" ), true );
107+
settings.remove( QStringLiteral( "proxy/proxyPort" ) );
108+
settings.setValue( QStringLiteral( "proxy/proxyHost" ), QStringLiteral( "myproxyhostname.com" ) );
109+
settings.setValue( QStringLiteral( "proxy/proxyUser" ), QStringLiteral( "username" ) );
110+
settings.remove( QStringLiteral( "proxy/proxyPassword" ) );
111+
QgsNetworkAccessManager::instance()->setupDefaultProxyAndCache();
112+
QgsVectorLayer vl( mTestDataDir + '/' + QStringLiteral( "lines.shp" ), QStringLiteral( "proxy_test" ), QLatin1Literal( "ogr" ) );
113+
QVERIFY( vl.isValid() );
114+
const char *proxyConfig = CPLGetConfigOption( "GDAL_HTTP_PROXY", NULL );
115+
QCOMPARE( proxyConfig, "myproxyhostname.com" );
116+
const char *proxyCredentials = CPLGetConfigOption( "GDAL_HTTP_PROXYUSERPWD", NULL );
117+
QCOMPARE( proxyCredentials, "username" );
118+
}
119+
120+
}
121+
122+
123+
QGSTEST_MAIN( TestQgsOgrProvider )
124+
#include "testqgsogrprovider.moc"

‎tests/src/python/test_provider_ogr.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
import sys
1818
import tempfile
1919

20-
from qgis.core import QgsVectorLayer, QgsVectorDataProvider, QgsWkbTypes, QgsFeature, QgsFeatureRequest
21-
from qgis.testing import (
22-
start_app,
23-
unittest
24-
)
25-
from utilities import unitTestDataPath
2620
from osgeo import gdal, ogr # NOQA
21+
from qgis.core import (QgsFeature, QgsFeatureRequest, QgsSettings,
22+
QgsVectorDataProvider, QgsVectorLayer, QgsWkbTypes, QgsNetworkAccessManager)
23+
from qgis.testing import start_app, unittest
24+
25+
from utilities import unitTestDataPath
2726

2827
start_app()
2928
TEST_DATA_DIR = unitTestDataPath()
@@ -292,6 +291,33 @@ def testTriangleTINPolyhedralSurface(self):
292291
Triangle => mapped to Polygon
293292
"""
294293

294+
def testSetupProxy(self):
295+
"""Test proxy setup"""
296+
settings = QgsSettings()
297+
settings.setValue("proxy/proxyEnabled", True)
298+
settings.setValue("proxy/proxyPort", '1234')
299+
settings.setValue("proxy/proxyHost", 'myproxyhostname.com')
300+
settings.setValue("proxy/proxyUser", 'username')
301+
settings.setValue("proxy/proxyPassword", 'password')
302+
settings.setValue("proxy/proxyExcludedUrls", "http://www.myhost.com|http://www.myotherhost.com")
303+
QgsNetworkAccessManager.instance().setupDefaultProxyAndCache()
304+
vl = QgsVectorLayer(TEST_DATA_DIR + '/' + 'lines.shp', 'proxy_test', 'ogr')
305+
self.assertTrue(vl.isValid())
306+
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXY"), "myproxyhostname.com:1234")
307+
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXYUSERPWD"), "username:password")
308+
309+
settings.setValue("proxy/proxyEnabled", True)
310+
settings.remove("proxy/proxyPort")
311+
settings.setValue("proxy/proxyHost", 'myproxyhostname.com')
312+
settings.setValue("proxy/proxyUser", 'username')
313+
settings.remove("proxy/proxyPassword")
314+
settings.setValue("proxy/proxyExcludedUrls", "http://www.myhost.com|http://www.myotherhost.com")
315+
QgsNetworkAccessManager.instance().setupDefaultProxyAndCache()
316+
vl = QgsVectorLayer(TEST_DATA_DIR + '/' + 'lines.shp', 'proxy_test', 'ogr')
317+
self.assertTrue(vl.isValid())
318+
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXY"), "myproxyhostname.com")
319+
self.assertEqual(gdal.GetConfigOption("GDAL_HTTP_PROXYUSERPWD"), "username")
320+
295321

296322
if __name__ == '__main__':
297323
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.